OSDN Git Service

kbdsensor: fix system_server high load issue android-x86-7.1-r2
authorChih-Wei Huang <cwhuang@linux.org.tw>
Wed, 11 Apr 2018 07:19:34 +0000 (15:19 +0800)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Wed, 11 Apr 2018 07:19:34 +0000 (15:19 +0800)
After the change 1f9a8a2e, if the sensor is invalid, the polling method
will return immediately without any way to throttle the number of
executions of the main sensor thread loop. That causes CPU high load.

Indeed the HAL should not set the pointer of struct hw_device_t if the
initialization fails. Then the SensorService won't enable and poll the
sensor device.

Fixes: 1f9a8a2e (kbdsensor: update HAL version to 1_3)
Tested-by: Michael Goffioul <michael.goffioul@gmail.com>
kbdsensor.cpp

index 79864f1..8f8e1b7 100644 (file)
@@ -43,16 +43,14 @@ struct KbdSensorKeys {
 const int ID_ACCELERATION = (SENSORS_HANDLE_BASE + 0);
 
 template <typename T> struct SensorFd : T {
-       SensorFd(const struct hw_module_t *module, struct hw_device_t **device);
+       SensorFd(const struct hw_module_t *module);
 };
 
-template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module, struct hw_device_t **device)
+template <typename T> SensorFd<T>::SensorFd(const struct hw_module_t *module)
 {
        this->common.tag     = HARDWARE_DEVICE_TAG;
        this->common.version = SENSORS_DEVICE_API_VERSION_1_3;
        this->common.module  = const_cast<struct hw_module_t *>(module);
-       *device              = &this->common;
-       ALOGD("%s: module=%p dev=%p", __FUNCTION__, module, *device);
 }
 
 struct SensorPollContext : SensorFd<sensors_poll_device_1> {
@@ -87,7 +85,7 @@ struct SensorPollContext : SensorFd<sensors_poll_device_1> {
 };
 
 SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw_device_t **device)
-      : SensorFd<sensors_poll_device_1>(module, device), enabled(false), rotation(ROT_0), ktype(KeysType)
+      : SensorFd<sensors_poll_device_1>(module), enabled(false), rotation(ROT_0), ktype(KeysType)
 {
        common.close = poll_close;
        activate     = poll_activate;
@@ -140,8 +138,13 @@ SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw
                        close(fd);
                        fd = -1;
                }
-               ALOGI_IF(fd >= 0, "Open %s ok, fd=%d", name, fd);
                closedir(dir);
+               if (fd < 0) {
+                       ALOGW("could not find any kbdsensor device");
+                       return;
+               }
+               *device = &common;
+               ALOGI("Open %s ok, fd=%d", name, fd);
        }
 
        pfd.events = POLLIN;
@@ -166,7 +169,7 @@ SensorPollContext::SensorPollContext(const struct hw_module_t *module, struct hw
        orients[ROT_270].acceleration.y = 0.0;
        orients[ROT_270].acceleration.z = -sin_angle;
 
-       ALOGD("%s: dev=%p fd=%d", __FUNCTION__, this, fd);
+       ALOGD("%s: module=%p dev=%p fd=%d", __FUNCTION__, module, this, fd);
 }
 
 SensorPollContext::~SensorPollContext()