Merge "remove compass from default wearable_core_hardware.xml" into cw-e-dev
am: 86d5272

* commit '86d5272389ec8077765684f2b89e99c623833850':
  remove compass from default wearable_core_hardware.xml

Change-Id: Idc888dee9bbf8521285a131577952bd4d48dc9c7
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp
index 9a5a81e..55282cf 100644
--- a/cmds/atrace/atrace.cpp
+++ b/cmds/atrace/atrace.cpp
@@ -93,6 +93,7 @@
     { "sched",      "CPU Scheduling",   0, {
         { REQ,      "/sys/kernel/debug/tracing/events/sched/sched_switch/enable" },
         { REQ,      "/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable" },
+        { OPT,      "/sys/kernel/debug/tracing/events/sched/sched_blocked_reason/enable" },
     } },
     { "irq",        "IRQ Events",   0, {
         { REQ,      "/sys/kernel/debug/tracing/events/irq/enable" },
diff --git a/cmds/dumpstate/dumpstate.c b/cmds/dumpstate/dumpstate.c
index b88b605..a2e4f4b 100644
--- a/cmds/dumpstate/dumpstate.c
+++ b/cmds/dumpstate/dumpstate.c
@@ -262,6 +262,8 @@
 
 /* End copy from system/core/logd/LogBuffer.cpp */
 
+static const unsigned long logcat_min_timeout = 40000; /* ms */
+
 /* dumps the current system state to stdout */
 static void dumpstate() {
     unsigned long timeout;
@@ -334,18 +336,18 @@
     // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
     // calculate timeout
     timeout = logcat_timeout("main") + logcat_timeout("system") + logcat_timeout("crash");
-    if (timeout < 20000) {
-        timeout = 20000;
+    if (timeout < logcat_min_timeout) {
+        timeout = logcat_min_timeout;
     }
     run_command("SYSTEM LOG", timeout / 1000, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
     timeout = logcat_timeout("events");
-    if (timeout < 20000) {
-        timeout = 20000;
+    if (timeout < logcat_min_timeout) {
+        timeout = logcat_min_timeout;
     }
     run_command("EVENT LOG", timeout / 1000, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
     timeout = logcat_timeout("radio");
-    if (timeout < 20000) {
-        timeout = 20000;
+    if (timeout < logcat_min_timeout) {
+        timeout = logcat_min_timeout;
     }
     run_command("RADIO LOG", timeout / 1000, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
 
diff --git a/include/gui/SensorManager.h b/include/gui/SensorManager.h
index f039caf..0cff46c 100644
--- a/include/gui/SensorManager.h
+++ b/include/gui/SensorManager.h
@@ -22,11 +22,9 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#include <binder/BinderService.h>
 #include <binder/IBinder.h>
 #include <binder/IPCThreadState.h>
 #include <binder/IServiceManager.h>
-#include <binder/PermissionCache.h>
 
 #include <utils/Errors.h>
 #include <utils/RefBase.h>
@@ -56,8 +54,7 @@
     static SensorManager& getInstanceForPackage(const String16& packageName);
     ~SensorManager();
 
-    ssize_t getSensorList(Sensor const* const** list);
-    ssize_t getAvailableSensorList(Sensor const* const** list);
+    ssize_t getSensorList(Sensor const* const** list) const;
     Sensor const* getDefaultSensor(int type);
     sp<SensorEventQueue> createEventQueue(String8 packageName = String8(""), int mode = 0);
     bool isDataInjectionEnabled();
@@ -67,27 +64,18 @@
     void sensorManagerDied();
 
     SensorManager(const String16& opPackageName);
-    status_t assertStateLocked();
-    void updateAvailableSensorList();
+    status_t assertStateLocked() const;
 
 private:
     static Mutex sLock;
     static std::map<String16, SensorManager*> sPackageInstances;
 
-    Mutex mLock;
-    sp<ISensorServer> mSensorServer;
-
-    // for Java API
-    Sensor const** mSensorList;
-
-    // for NDK API
-    Sensor const** mAvailableSensorList;
-    ssize_t mNumAvailableSensor;
-
-    Vector<Sensor> mSensors;
-    sp<IBinder::DeathRecipient> mDeathObserver;
+    mutable Mutex mLock;
+    mutable sp<ISensorServer> mSensorServer;
+    mutable Sensor const** mSensorList;
+    mutable Vector<Sensor> mSensors;
+    mutable sp<IBinder::DeathRecipient> mDeathObserver;
     const String16 mOpPackageName;
-    bool mBodyPermission;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/libs/gui/SensorManager.cpp b/libs/gui/SensorManager.cpp
index 4277032..33608b5 100644
--- a/libs/gui/SensorManager.cpp
+++ b/libs/gui/SensorManager.cpp
@@ -89,8 +89,7 @@
 }
 
 SensorManager::SensorManager(const String16& opPackageName)
-    : mSensorList(NULL), mAvailableSensorList(NULL), mNumAvailableSensor(0),
-      mOpPackageName(opPackageName), mBodyPermission(false)
+    : mSensorList(0), mOpPackageName(opPackageName)
 {
     // okay we're not locked here, but it's not needed during construction
     assertStateLocked();
@@ -99,9 +98,6 @@
 SensorManager::~SensorManager()
 {
     free(mSensorList);
-    if (mAvailableSensorList) {
-        free(mAvailableSensorList);
-    }
 }
 
 void SensorManager::sensorManagerDied()
@@ -110,14 +106,10 @@
     mSensorServer.clear();
     free(mSensorList);
     mSensorList = NULL;
-    if (mAvailableSensorList) {
-        free(mAvailableSensorList);
-        mAvailableSensorList = NULL;
-    }
     mSensors.clear();
 }
 
-status_t SensorManager::assertStateLocked() {
+status_t SensorManager::assertStateLocked() const {
     bool initSensorManager = false;
     if (mSensorServer == NULL) {
         initSensorManager = true;
@@ -167,14 +159,13 @@
         for (size_t i=0 ; i<count ; i++) {
             mSensorList[i] = mSensors.array() + i;
         }
-
-        updateAvailableSensorList();
     }
 
     return NO_ERROR;
 }
 
-ssize_t SensorManager::getSensorList(Sensor const* const** list) {
+ssize_t SensorManager::getSensorList(Sensor const* const** list) const
+{
     Mutex::Autolock _l(mLock);
     status_t err = assertStateLocked();
     if (err < 0) {
@@ -184,76 +175,10 @@
     return static_cast<ssize_t>(mSensors.size());
 }
 
-void SensorManager::updateAvailableSensorList() {
-    const int uid = static_cast<int>(IPCThreadState::self()->getCallingUid());
-    const int pid = static_cast<int>(IPCThreadState::self()->getCallingPid());
-    const String16 BODY_SENSOR_PERMISSION("android.permission.BODY_SENSORS");
-    const String8 BODY_SENSOR_PERMISSION8("android.permission.BODY_SENSORS");
-
-    bool bodySensorPermission = false;
-
-    sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
-    if (binder != NULL) {
-        bodySensorPermission = interface_cast<IPermissionController>(binder)->
-                checkPermission(BODY_SENSOR_PERMISSION, pid, uid);
-    }
-
-    // only update if app got BODY_SENSORS permission after last call or the sensor list has not
-    // been populated.
-    //
-    // it is not possible for the reverse transition, as the app will be killed when permission is
-    // revoked.
-    if ( (bodySensorPermission && !mBodyPermission) || mAvailableSensorList == NULL) {
-
-        // allocate only when necessary
-        if (mAvailableSensorList == NULL) {
-            // allocate a list big enough to fit all sensors (including those requires permission
-            // that the app do not have;
-            mAvailableSensorList =
-                    static_cast<Sensor const**>(malloc(mSensors.size() * sizeof(Sensor*)));
-
-            // first populate all sensors that do not need body sensor permission
-            ssize_t& n = mNumAvailableSensor;
-            for (size_t i = 0; i < mSensors.size() ; i++) {
-                if (mSensors[i].getRequiredPermission() != BODY_SENSOR_PERMISSION8) {
-                    mAvailableSensorList[n++] = mSensors.array() + i;
-                }
-            }
-        }
-
-        if (bodySensorPermission) {
-            // if the app just got the sensor permission back, fill the sensor at the end of list
-            ssize_t& n = mNumAvailableSensor;
-            for (size_t i = 0; i < mSensors.size() ; i++) {
-                if (mSensors[i].getRequiredPermission() == BODY_SENSOR_PERMISSION8) {
-                    mAvailableSensorList[n++] = mSensors.array() + i;
-                }
-            }
-        }
-
-        mBodyPermission = bodySensorPermission;
-    }
-}
-
-ssize_t SensorManager::getAvailableSensorList(Sensor const* const** list) {
-    Mutex::Autolock _l(mLock);
-    status_t err = assertStateLocked();
-    if (err < 0) {
-        return static_cast<ssize_t>(err);
-    }
-
-    updateAvailableSensorList();
-
-    *list = mAvailableSensorList;
-    return mNumAvailableSensor;
-}
-
-Sensor const* SensorManager::getDefaultSensor(int type) {
+Sensor const* SensorManager::getDefaultSensor(int type)
+{
     Mutex::Autolock _l(mLock);
     if (assertStateLocked() == NO_ERROR) {
-
-        updateAvailableSensorList();
-
         bool wakeUpSensor = false;
         // For the following sensor types, return a wake-up sensor. These types are by default
         // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
@@ -267,9 +192,9 @@
         // in the future it will make sense to let the SensorService make
         // that decision.
         for (size_t i=0 ; i<mSensors.size() ; i++) {
-            if (mAvailableSensorList[i]->getType() == type &&
-                mAvailableSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
-                return mAvailableSensorList[i];
+            if (mSensorList[i]->getType() == type &&
+                mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
+                return mSensorList[i];
             }
         }
     }
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
old mode 100755
new mode 100644
index 00d875d..2f64558
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -723,7 +723,14 @@
     Vector<Sensor> accessibleSensorList;
     for (size_t i = 0; i < initialSensorList.size(); i++) {
         Sensor sensor = initialSensorList[i];
-        accessibleSensorList.add(sensor);
+        if (canAccessSensor(sensor, "getSensorList", opPackageName)) {
+            accessibleSensorList.add(sensor);
+        } else {
+            ALOGI("Skipped sensor %s because it requires permission %s and app op %d",
+                  sensor.getName().string(),
+                  sensor.getRequiredPermission().string(),
+                  sensor.getRequiredAppOp());
+        }
     }
     return accessibleSensorList;
 }
@@ -1619,26 +1626,21 @@
                 reAllocateCacheLocked(scratch, count);
                 return status_t(NO_ERROR);
             }
-            if (count >= mMaxCacheSize) {
-                //count is larger than MaxCacheSize,
-                //so copy the end MaxCacheSize data in scratch buffer to cache
-                countFlushCompleteEventsLocked(mEventCache, mCacheSize);
-                countFlushCompleteEventsLocked(scratch, count - mMaxCacheSize);
-                memcpy(&mEventCache[0], &scratch[count-mMaxCacheSize],
-                    mMaxCacheSize * sizeof(sensors_event_t));
-                mCacheSize = mMaxCacheSize;
-            } else {
-                //count is smaller than MaxCacheSize, so numEventsDropped data in cache
-                //should be dropped, then copy the scratch to the end of cache
-                int numEventsDropped = count + mCacheSize - mMaxCacheSize;
-                countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
-                memmove(mEventCache, &mEventCache[numEventsDropped],
+            // Some events need to be dropped.
+            int remaningCacheSize = mMaxCacheSize - mCacheSize;
+            if (remaningCacheSize != 0) {
+                memcpy(&mEventCache[mCacheSize], scratch,
+                                                remaningCacheSize * sizeof(sensors_event_t));
+            }
+            int numEventsDropped = count - remaningCacheSize;
+            countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
+            // Drop the first "numEventsDropped" in the cache.
+            memmove(mEventCache, &mEventCache[numEventsDropped],
                     (mCacheSize - numEventsDropped) * sizeof(sensors_event_t));
 
-                memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch,
-                    count * sizeof(sensors_event_t));
-                mCacheSize = mMaxCacheSize;
-            }
+            // Copy the remainingEvents in scratch buffer to the end of cache.
+            memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize,
+                                            numEventsDropped * sizeof(sensors_event_t));
         }
         return status_t(NO_ERROR);
     }
@@ -1657,7 +1659,6 @@
                                     reinterpret_cast<ASensorEvent const*>(scratch), count);
     if (size < 0) {
         // Write error, copy events to local cache.
-        ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno));
         if (index_wake_up_event >= 0) {
             // If there was a wake_up sensor_event, reset the flag.
             scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
@@ -1673,21 +1674,8 @@
             mEventCache = new sensors_event_t[mMaxCacheSize];
             mCacheSize = 0;
         }
-        //here mCacheSize must be 0 because if (mCacheSize != 0) has been executed above
-        if (count <= mMaxCacheSize) {
-            //count is no more than free size of cache,
-            //copy the scratch to the end of cache
-            memcpy(&mEventCache[0], scratch, count * sizeof(sensors_event_t));
-            mCacheSize = count;
-        } else {
-            //count is larger than free size of cache,
-            //so just copy the free size of scratch to the end of cache
-            ALOGI("count > MaxCache count=%d mMaxCacheSize=%d", count, mMaxCacheSize);
-            countFlushCompleteEventsLocked(scratch, count - mMaxCacheSize);
-            memcpy(&mEventCache[0], &scratch[count - mMaxCacheSize],
-                mMaxCacheSize * sizeof(sensors_event_t));
-            mCacheSize = mMaxCacheSize;
-        }
+        memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
+        mCacheSize += count;
 
         // Add this file descriptor to the looper to get a callback when this fd is available for
         // writing.
@@ -1740,7 +1728,6 @@
             }
             ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
             if (size < 0) {
-                ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno));
                 if (wakeUpSensor) --mWakeLockRefCount;
                 return;
             }
@@ -1776,7 +1763,6 @@
                           reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
                           numEventsToWrite);
         if (size < 0) {
-            ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno));
             if (index_wake_up_event >= 0) {
                 // If there was a wake_up sensor_event, reset the flag.
                 mEventCache[index_wake_up_event + numEventsSent].flags  &=
diff --git a/services/surfaceflinger/Android.mk b/services/surfaceflinger/Android.mk
index 1eb2361..1901ef9 100644
--- a/services/surfaceflinger/Android.mk
+++ b/services/surfaceflinger/Android.mk
@@ -122,6 +122,10 @@
 LOCAL_CFLAGS := -DLOG_TAG=\"SurfaceFlinger\"
 LOCAL_CPPFLAGS := -std=c++11
 
+ifneq ($(ENABLE_CPUSETS),)
+    LOCAL_CFLAGS += -DENABLE_CPUSETS
+endif
+
 LOCAL_SRC_FILES := \
     main_surfaceflinger.cpp
 
diff --git a/services/surfaceflinger/main_surfaceflinger.cpp b/services/surfaceflinger/main_surfaceflinger.cpp
index a74bc4c..6fa8b53 100644
--- a/services/surfaceflinger/main_surfaceflinger.cpp
+++ b/services/surfaceflinger/main_surfaceflinger.cpp
@@ -41,6 +41,13 @@
 
     set_sched_policy(0, SP_FOREGROUND);
 
+#ifdef ENABLE_CPUSETS
+    // Put most SurfaceFlinger threads in the system-background cpuset
+    // Keeps us from unnecessarily using big cores
+    // Do this after the binder thread pool init
+    set_cpuset_policy(0, SP_SYSTEM);
+#endif
+
     // initialize before clients can connect
     flinger->init();