diff options
author | Ben Fennema <fennema@google.com> | 2017-09-06 11:39:46 -0700 |
---|---|---|
committer | Ben Fennema <fennema@google.com> | 2017-09-14 16:08:58 -0700 |
commit | fd9172bb9cc8b250152bbf98f888d14ba20a7a4f (patch) | |
tree | b20ab66acad5e3026698431dba0060f30a09f805 | |
parent | 596363edf151b423ef0e51126733ecba36794be7 (diff) | |
download | contexthub-fd9172bb9cc8b250152bbf98f888d14ba20a7a4f.tar.gz |
sensorhal: protect mWakeEventCount with lock and handle mRing full
Bug: 62513931
Test: build/enable prox, trigger prox, verify wakelock not stuck on
Change-Id: I29d734d404d982b9c6bca445a91c7f9bc7a65f40
Signed-off-by: Ben Fennema <fennema@google.com>
-rw-r--r-- | sensorhal/hubconnection.cpp | 106 | ||||
-rw-r--r-- | sensorhal/hubconnection.h | 7 | ||||
-rw-r--r-- | sensorhal/sensors.cpp | 23 |
3 files changed, 72 insertions, 64 deletions
diff --git a/sensorhal/hubconnection.cpp b/sensorhal/hubconnection.cpp index 251dcb9e..ba87c152 100644 --- a/sensorhal/hubconnection.cpp +++ b/sensorhal/hubconnection.cpp @@ -114,6 +114,21 @@ static bool isActivitySensor(int sensorIndex) { && sensorIndex <= COMMS_SENSOR_ACTIVITY_LAST; } +static bool isWakeEvent(int32_t sensor) +{ + switch (sensor) { + case COMMS_SENSOR_DOUBLE_TOUCH: + case COMMS_SENSOR_DOUBLE_TWIST: + case COMMS_SENSOR_GESTURE: + case COMMS_SENSOR_PROXIMITY: + case COMMS_SENSOR_SIGNIFICANT_MOTION: + case COMMS_SENSOR_TILT: + return true; + default: + return false; + } +} + HubConnection::HubConnection() : Thread(false /* canCallJava */), mRing(10 *1024), @@ -143,6 +158,7 @@ HubConnection::HubConnection() mWakelockHeld = false; mWakeEventCount = 0; + mWriteFailures = 0; initNanohubLock(); @@ -616,31 +632,20 @@ sensors_event_t *HubConnection::initEv(sensors_event_t *ev, uint64_t timestamp, return ev; } -ssize_t HubConnection::getWakeEventCount() +ssize_t HubConnection::decrementIfWakeEventLocked(int32_t sensor) { - return mWakeEventCount; -} - -ssize_t HubConnection::decrementWakeEventCount() -{ - return --mWakeEventCount; -} - -bool HubConnection::isWakeEvent(int32_t sensor) -{ - switch (sensor) { - case COMMS_SENSOR_PROXIMITY: - case COMMS_SENSOR_SIGNIFICANT_MOTION: - case COMMS_SENSOR_TILT: - case COMMS_SENSOR_DOUBLE_TWIST: - case COMMS_SENSOR_GESTURE: - return true; - default: - return false; + if (isWakeEvent(sensor)) { + if (mWakeEventCount > 0) + mWakeEventCount--; + else + ALOGW("%s: sensor=%d, unexpected count=%d, no-op", + __FUNCTION__, sensor, mWakeEventCount); } + + return mWakeEventCount; } -void HubConnection::protectIfWakeEvent(int32_t sensor) +void HubConnection::protectIfWakeEventLocked(int32_t sensor) { if (isWakeEvent(sensor)) { if (mWakelockHeld == false) { @@ -653,6 +658,8 @@ void HubConnection::protectIfWakeEvent(int32_t sensor) void HubConnection::releaseWakeLockIfAppropriate() { + Mutex::Autolock autoLock(mLock); + if (mWakelockHeld && (mWakeEventCount == 0)) { mWakelockHeld = false; release_wake_lock(WAKELOCK_NAME); @@ -728,11 +735,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } uint8_t HubConnection::magAccuracyUpdate(sensors_vec_t *sv) @@ -830,11 +834,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t sensor, struct ThreeAxisSample *sample, bool highAccuracy) @@ -1021,11 +1022,8 @@ void HubConnection::processSample(uint64_t timestamp, uint32_t type, uint32_t se break; } - if (cnt > 0) { - // If event is a wake event, protect it with a wakelock - protectIfWakeEvent(sensor); + if (cnt > 0) write(nev, cnt); - } } void HubConnection::discardInotifyEvent() { @@ -1659,10 +1657,6 @@ bool HubConnection::threadLoop() { return false; } -ssize_t HubConnection::read(sensors_event_t *ev, size_t size) { - return mRing.read(ev, size); -} - void HubConnection::setActivityCallback(ActivityEventHandler *eventHandler) { Mutex::Autolock autoLock(mLock); @@ -1912,8 +1906,44 @@ void HubConnection::initNanohubLock() { } } +ssize_t HubConnection::read(sensors_event_t *ev, size_t size) { + ssize_t n = mRing.read(ev, size); + + Mutex::Autolock autoLock(mLock); + + // We log the first failure in write, so only log 2+ errors + if (mWriteFailures > 1) { + ALOGW("%s: mRing.write failed %d times", + __FUNCTION__, mWriteFailures); + mWriteFailures = 0; + } + + for (ssize_t i = 0; i < n; i++) + decrementIfWakeEventLocked(ev[i].sensor); + + return n; +} + + ssize_t HubConnection::write(const sensors_event_t *ev, size_t n) { - return mRing.write(ev, n); + ssize_t ret = 0; + + Mutex::Autolock autoLock(mLock); + + for (size_t i=0; i<n; i++) { + if (mRing.write(&ev[i], 1) == 1) { + ret++; + // If event is a wake event, protect it with a wakelock + protectIfWakeEventLocked(ev[i].sensor); + } else { + if (mWriteFailures++ == 0) + ALOGW("%s: mRing.write failed @ %zu/%zu", + __FUNCTION__, i, n); + break; + } + } + + return ret; } #ifdef USB_MAG_BIAS_REPORTING_ENABLED diff --git a/sensorhal/hubconnection.h b/sensorhal/hubconnection.h index 4dbd64e0..388660cf 100644 --- a/sensorhal/hubconnection.h +++ b/sensorhal/hubconnection.h @@ -79,10 +79,7 @@ struct HubConnection : public Thread { void setOperationParameter(const additional_info_event_t &info); - bool isWakeEvent(int32_t sensor); void releaseWakeLockIfAppropriate(); - ssize_t getWakeEventCount(); - ssize_t decrementWakeEventCount(); //TODO: factor out event ring buffer functionality into a separate class ssize_t read(sensors_event_t *ev, size_t size); @@ -111,7 +108,8 @@ private: bool mWakelockHeld; int32_t mWakeEventCount; - void protectIfWakeEvent(int32_t sensor); + void protectIfWakeEventLocked(int32_t sensor); + ssize_t decrementIfWakeEventLocked(int32_t sensor); static inline uint64_t period_ns_to_frequency_q10(nsecs_t period_ns) { return 1024000000000ULL / period_ns; @@ -244,6 +242,7 @@ private: Mutex mLock; RingBuffer mRing; + int32_t mWriteFailures; ActivityEventHandler *mActivityEventHandler; diff --git a/sensorhal/sensors.cpp b/sensorhal/sensors.cpp index 0ddb263b..234f7da3 100644 --- a/sensorhal/sensors.cpp +++ b/sensorhal/sensors.cpp @@ -105,28 +105,7 @@ int SensorContext::poll(sensors_event_t *data, int count) { // Release wakelock if held and no more events in ring buffer mHubConnection->releaseWakeLockIfAppropriate(); - ssize_t n = mHubConnection->read(data, count); - - if (n < 0) { - return -1; - } - - // If we have wake events in the queue, determine how many we're sending - // up this round and decrement that count now so that when we get called back, - // we'll have an accurate count of how many wake events are STILL in the HAL queue - // to be able to determine whether we can release our wakelock if held. - if (mHubConnection->getWakeEventCount() != 0) { - for (ssize_t i = 0; i < n; i++) { - if (mHubConnection->isWakeEvent(data[i].sensor)) { - ssize_t count = mHubConnection->decrementWakeEventCount(); - if (count == 0) { - break; - } - } - } - } - - return n; + return mHubConnection->read(data, count); } int SensorContext::batch( |