Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ec2066e authored by Aravind Akella's avatar Aravind Akella Committed by Waleed Kadous
Browse files

SensorService fixes.

   i) Initialize sensor_event.flags to zero or wake_up flag before sending dropped flush_complete_events. This will avoid unnecessary acks. 
Currently it is not set to anything which may result in garbage values for flags field.
   ii) Change the dump format of SensorEventConnection.
   iii) Add const to method signatures wherever applicable.

Bug id: https://b2.corp.google.com/issues/17498626

Change-Id: Ia736ffb01c366540aab73cdcf596ce852c052593
parent 2ab32d69
Loading
Loading
Loading
Loading
+30 −25
Original line number Original line Diff line number Diff line
@@ -1002,32 +1002,27 @@ bool SensorService::SensorEventConnection::needsWakeLock() {


void SensorService::SensorEventConnection::dump(String8& result) {
void SensorService::SensorEventConnection::dump(String8& result) {
    Mutex::Autolock _l(mConnectionLock);
    Mutex::Autolock _l(mConnectionLock);
    result.appendFormat("\t %d WakeLockRefCount \n", mWakeLockRefCount);
    result.appendFormat("\t WakeLockRefCount %d | uid %d | cache size %d | max cache size %d\n",
            mWakeLockRefCount, mUid, mCacheSize, mMaxCacheSize);
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d | uid %d|"
        result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
                            "cache size: %d max cache size %d\n",
                            mService->getSensorName(mSensorInfo.keyAt(i)).string(),
                            mService->getSensorName(mSensorInfo.keyAt(i)).string(),
                            mSensorInfo.keyAt(i),
                            mSensorInfo.keyAt(i),
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                            flushInfo.mFirstFlushPending ? "First flush pending" :
                                                           "active",
                                                           "active",
                            flushInfo.mPendingFlushEventsToSend,
                            flushInfo.mPendingFlushEventsToSend);
                            mUid,
    }
                            mCacheSize,
                            mMaxCacheSize);
#if DEBUG_CONNECTIONS
#if DEBUG_CONNECTIONS
    result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
    result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
            " total_acks_needed %d | total_acks_recvd %d\n",
            " total_acks_needed %d | total_acks_recvd %d\n",
            mEventsReceived,
            mEventsReceived,
            mEventsSent,
            mEventsSent,
            mEventsSentFromCache,
            mEventsSentFromCache,
                                        mEventsReceived - (mEventsSentFromCache +
            mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
                                                           mEventsSent + mCacheSize),
            mTotalAcksNeeded,
            mTotalAcksNeeded,
            mTotalAcksReceived);
            mTotalAcksReceived);
#endif
#endif

    }
}
}


bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
@@ -1082,7 +1077,7 @@ void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
}
}


status_t SensorService::SensorEventConnection::sendEvents(
status_t SensorService::SensorEventConnection::sendEvents(
        sensors_event_t* buffer, size_t numEvents,
        sensors_event_t const* buffer, size_t numEvents,
        sensors_event_t* scratch,
        sensors_event_t* scratch,
        SensorEventConnection const * const * mapFlushEventsToConnections) {
        SensorEventConnection const * const * mapFlushEventsToConnections) {
    // filter out events not for this connection
    // filter out events not for this connection
@@ -1207,7 +1202,9 @@ status_t SensorService::SensorEventConnection::sendEvents(
        if (index_wake_up_event >= 0) {
        if (index_wake_up_event >= 0) {
            // If there was a wake_up sensor_event, reset the flag.
            // If there was a wake_up sensor_event, reset the flag.
            scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
            scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
            if (mWakeLockRefCount > 0) {
                --mWakeLockRefCount;
                --mWakeLockRefCount;
            }
#if DEBUG_CONNECTIONS
#if DEBUG_CONNECTIONS
            --mTotalAcksNeeded;
            --mTotalAcksNeeded;
#endif
#endif
@@ -1256,14 +1253,18 @@ void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t


void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
    ASensorEvent flushCompleteEvent;
    ASensorEvent flushCompleteEvent;
    memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
    flushCompleteEvent.sensor = 0;
    // Loop through all the sensors for this connection and check if there are any pending
    // Loop through all the sensors for this connection and check if there are any pending
    // flush complete events to be sent.
    // flush complete events to be sent.
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
    for (size_t i = 0; i < mSensorInfo.size(); ++i) {
        FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
        FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
        while (flushInfo.mPendingFlushEventsToSend > 0) {
        while (flushInfo.mPendingFlushEventsToSend > 0) {
            flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i);
            const int sensor_handle = mSensorInfo.keyAt(i);
            flushCompleteEvent.meta_data.sensor = sensor_handle;
            if (mService->getSensorFromHandle(sensor_handle).isWakeUpSensor()) {
               flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
            }
            ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
            ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
            if (size < 0) {
            if (size < 0) {
                return;
                return;
@@ -1303,7 +1304,9 @@ void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() {
                // If there was a wake_up sensor_event, reset the flag.
                // If there was a wake_up sensor_event, reset the flag.
                mEventCache[index_wake_up_event + numEventsSent].flags  &=
                mEventCache[index_wake_up_event + numEventsSent].flags  &=
                        ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
                        ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
                if (mWakeLockRefCount > 0) {
                    --mWakeLockRefCount;
                    --mWakeLockRefCount;
                }
#if DEBUG_CONNECTIONS
#if DEBUG_CONNECTIONS
                --mTotalAcksNeeded;
                --mTotalAcksNeeded;
#endif
#endif
@@ -1328,7 +1331,7 @@ void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() {
}
}


void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
                sensors_event_t* scratch, const int numEventsDropped) {
                sensors_event_t const* scratch, const int numEventsDropped) {
    ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
    ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
    // Count flushComplete events in the events that are about to the dropped. These will be sent
    // Count flushComplete events in the events that are about to the dropped. These will be sent
    // separately before the next batch of events.
    // separately before the next batch of events.
@@ -1394,7 +1397,9 @@ int SensorService::SensorEventConnection::handleEvent(int fd, int events, void*


        {
        {
           Mutex::Autolock _l(mConnectionLock);
           Mutex::Autolock _l(mConnectionLock);
           if (mWakeLockRefCount > 0) {
               --mWakeLockRefCount;
               --mWakeLockRefCount;
           }
#if DEBUG_CONNECTIONS
#if DEBUG_CONNECTIONS
           ++mTotalAcksReceived;
           ++mTotalAcksReceived;
#endif
#endif
+3 −3
Original line number Original line Diff line number Diff line
@@ -86,7 +86,7 @@ class SensorService :
        // Count the number of flush complete events which are about to be dropped in the buffer.
        // Count the number of flush complete events which are about to be dropped in the buffer.
        // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
        // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
        // sent separately before the next batch of events.
        // sent separately before the next batch of events.
        void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
        void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped);


        // Check if there are any wake up events in the buffer. If yes, return the index of the
        // Check if there are any wake up events in the buffer. If yes, return the index of the
        // first wake_up sensor event in the buffer else return -1. This wake_up sensor event will
        // first wake_up sensor event in the buffer else return -1. This wake_up sensor event will
@@ -148,7 +148,7 @@ class SensorService :
    public:
    public:
        SensorEventConnection(const sp<SensorService>& service, uid_t uid);
        SensorEventConnection(const sp<SensorService>& service, uid_t uid);


        status_t sendEvents(sensors_event_t* buffer, size_t count,
        status_t sendEvents(sensors_event_t const* buffer, size_t count,
                sensors_event_t* scratch,
                sensors_event_t* scratch,
                SensorEventConnection const * const * mapFlushEventsToConnections = NULL);
                SensorEventConnection const * const * mapFlushEventsToConnections = NULL);
        bool hasSensor(int32_t handle) const;
        bool hasSensor(int32_t handle) const;
@@ -190,7 +190,7 @@ class SensorService :
    bool isVirtualSensor(int handle) const;
    bool isVirtualSensor(int handle) const;
    Sensor getSensorFromHandle(int handle) const;
    Sensor getSensorFromHandle(int handle) const;
    bool isWakeUpSensor(int type) const;
    bool isWakeUpSensor(int type) const;
    void recordLastValueLocked(const sensors_event_t* buffer, size_t count);
    void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
    Sensor registerSensor(SensorInterface* sensor);
    Sensor registerSensor(SensorInterface* sensor);
    Sensor registerVirtualSensor(SensorInterface* sensor);
    Sensor registerVirtualSensor(SensorInterface* sensor);