Merge "Refactor SF. Move all GL operations in their own class."
diff --git a/cmds/atrace/atrace.cpp b/cmds/atrace/atrace.cpp
index 6d6f77e..b500a6b 100644
--- a/cmds/atrace/atrace.cpp
+++ b/cmds/atrace/atrace.cpp
@@ -103,6 +103,9 @@
         { REQ,      "/sys/kernel/debug/tracing/events/block/block_rq_issue/enable" },
         { REQ,      "/sys/kernel/debug/tracing/events/block/block_rq_complete/enable" },
     } },
+    { "mmc",        "eMMC commands",    0, {
+        { REQ,      "/sys/kernel/debug/tracing/events/mmc/enable" },
+    } },
     { "load",       "CPU Load",         0, {
         { REQ,      "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable" },
     } },
diff --git a/cmds/dumpstate/dumpstate.c b/cmds/dumpstate/dumpstate.c
index ed362d7..37d4646 100644
--- a/cmds/dumpstate/dumpstate.c
+++ b/cmds/dumpstate/dumpstate.c
@@ -282,8 +282,10 @@
     printf("========================================================\n");
 
     run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "--checkin", NULL);
+    run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
     run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
-    run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "--checkin", NULL);
+    run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c",
+            "--include-committed", NULL);
     run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "--c", NULL);
 
     printf("========================================================\n");
diff --git a/include/android/sensor.h b/include/android/sensor.h
index f163f18..32c5c0a 100644
--- a/include/android/sensor.h
+++ b/include/android/sensor.h
@@ -114,14 +114,20 @@
     int32_t reserved0;
     int64_t timestamp;
     union {
-        float           data[16];
-        ASensorVector   vector;
-        ASensorVector   acceleration;
-        ASensorVector   magnetic;
-        float           temperature;
-        float           distance;
-        float           light;
-        float           pressure;
+        union {
+            float           data[16];
+            ASensorVector   vector;
+            ASensorVector   acceleration;
+            ASensorVector   magnetic;
+            float           temperature;
+            float           distance;
+            float           light;
+            float           pressure;
+        };
+        union {
+            uint64_t        data[8];
+            uint64_t        step_counter;
+        } u64;
     };
     int32_t reserved1[4];
 } ASensorEvent;
diff --git a/include/binder/IPCThreadState.h b/include/binder/IPCThreadState.h
index 3378d97..ad0daee 100644
--- a/include/binder/IPCThreadState.h
+++ b/include/binder/IPCThreadState.h
@@ -51,6 +51,8 @@
             int64_t             clearCallingIdentity();
             void                restoreCallingIdentity(int64_t token);
             
+            int                 setupPolling(int* fd);
+            status_t            handlePolledCommands();
             void                flushCommands();
 
             void                joinThreadPool(bool isMain = true);
@@ -96,7 +98,9 @@
                                                      uint32_t code,
                                                      const Parcel& data,
                                                      status_t* statusBuffer);
+            status_t            getAndExecuteCommand();
             status_t            executeCommand(int32_t command);
+            void                processPendingDerefs();
             
             void                clearCaller();
             
diff --git a/include/cpustats/CentralTendencyStatistics.h b/include/cpustats/CentralTendencyStatistics.h
deleted file mode 100644
index 21b6981..0000000
--- a/include/cpustats/CentralTendencyStatistics.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _CENTRAL_TENDENCY_STATISTICS_H
-#define _CENTRAL_TENDENCY_STATISTICS_H
-
-#include <math.h>
-
-// Not multithread safe
-class CentralTendencyStatistics {
-
-public:
-
-    CentralTendencyStatistics() :
-            mMean(NAN), mMedian(NAN), mMinimum(INFINITY), mMaximum(-INFINITY), mN(0), mM2(0),
-            mVariance(NAN), mVarianceKnownForN(0), mStddev(NAN), mStddevKnownForN(0) { }
-
-    ~CentralTendencyStatistics() { }
-
-    // add x to the set of samples
-    void sample(double x);
-
-    // return the arithmetic mean of all samples so far
-    double mean() const { return mMean; }
-
-    // return the minimum of all samples so far
-    double minimum() const { return mMinimum; }
-
-    // return the maximum of all samples so far
-    double maximum() const { return mMaximum; }
-
-    // return the variance of all samples so far
-    double variance() const;
-
-    // return the standard deviation of all samples so far
-    double stddev() const;
-
-    // return the number of samples added so far
-    unsigned n() const { return mN; }
-
-    // reset the set of samples to be empty
-    void reset();
-
-private:
-    double mMean;
-    double mMedian;
-    double mMinimum;
-    double mMaximum;
-    unsigned mN;    // number of samples so far
-    double mM2;
-
-    // cached variance, and n at time of caching
-    mutable double mVariance;
-    mutable unsigned mVarianceKnownForN;
-
-    // cached standard deviation, and n at time of caching
-    mutable double mStddev;
-    mutable unsigned mStddevKnownForN;
-
-};
-
-#endif // _CENTRAL_TENDENCY_STATISTICS_H
diff --git a/include/cpustats/README.txt b/include/cpustats/README.txt
deleted file mode 100644
index 14439f0..0000000
--- a/include/cpustats/README.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-This is a static library of CPU usage statistics, originally written
-for audio but most are not actually specific to audio.
-
-Requirements to be here:
- * should be related to CPU usage statistics
- * should be portable to host; avoid Android OS dependencies without a conditional
diff --git a/include/cpustats/ThreadCpuUsage.h b/include/cpustats/ThreadCpuUsage.h
deleted file mode 100644
index 9756844..0000000
--- a/include/cpustats/ThreadCpuUsage.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _THREAD_CPU_USAGE_H
-#define _THREAD_CPU_USAGE_H
-
-#include <fcntl.h>
-#include <pthread.h>
-
-namespace android {
-
-// Track CPU usage for the current thread.
-// Units are in per-thread CPU ns, as reported by
-// clock_gettime(CLOCK_THREAD_CPUTIME_ID).  Simple usage: for cyclic
-// threads where you want to measure the execution time of the whole
-// cycle, just call sampleAndEnable() at the start of each cycle.
-// For acyclic threads, or for cyclic threads where you want to measure/track
-// only part of each cycle, call enable(), disable(), and/or setEnabled()
-// to demarcate the region(s) of interest, and then call sample() periodically.
-// This class is not thread-safe for concurrent calls from multiple threads;
-// the methods of this class may only be called by the current thread
-// which constructed the object.
-
-class ThreadCpuUsage
-{
-
-public:
-    ThreadCpuUsage() :
-        mIsEnabled(false),
-        mWasEverEnabled(false),
-        mAccumulator(0),
-        // mPreviousTs
-        // mMonotonicTs
-        mMonotonicKnown(false)
-        {
-            (void) pthread_once(&sOnceControl, &init);
-            for (int i = 0; i < sKernelMax; ++i) {
-                mCurrentkHz[i] = (uint32_t) ~0;   // unknown
-            }
-        }
-
-    ~ThreadCpuUsage() { }
-
-    // Return whether currently tracking CPU usage by current thread
-    bool isEnabled() const  { return mIsEnabled; }
-
-    // Enable tracking of CPU usage by current thread;
-    // any CPU used from this point forward will be tracked.
-    // Returns the previous enabled status.
-    bool enable()       { return setEnabled(true); }
-
-    // Disable tracking of CPU usage by current thread;
-    // any CPU used from this point forward will be ignored.
-    // Returns the previous enabled status.
-    bool disable()      { return setEnabled(false); }
-
-    // Set the enabled status and return the previous enabled status.
-    // This method is intended to be used for safe nested enable/disabling.
-    bool setEnabled(bool isEnabled);
-
-    // Add a sample point, and also enable tracking if needed.
-    // If tracking has never been enabled, then this call enables tracking but
-    // does _not_ add a sample -- it is not possible to add a sample the
-    // first time because there is no previous point to subtract from.
-    // Otherwise, if tracking is enabled,
-    // then adds a sample for tracked CPU ns since the previous
-    // sample, or since the first call to sampleAndEnable(), enable(), or
-    // setEnabled(true).  If there was a previous sample but tracking is
-    // now disabled, then adds a sample for the tracked CPU ns accumulated
-    // up until the most recent disable(), resets this accumulator, and then
-    // enables tracking.  Calling this method rather than enable() followed
-    // by sample() avoids a race condition for the first sample.
-    // Returns true if the sample 'ns' is valid, or false if invalid.
-    // Note that 'ns' is an output parameter passed by reference.
-    // The caller does not need to initialize this variable.
-    // The units are CPU nanoseconds consumed by current thread.
-    bool sampleAndEnable(double& ns);
-
-    // Add a sample point, but do not
-    // change the tracking enabled status.  If tracking has either never been
-    // enabled, or has never been enabled since the last sample, then log a warning
-    // and don't add sample.  Otherwise, adds a sample for tracked CPU ns since
-    // the previous sample or since the first call to sampleAndEnable(),
-    // enable(), or setEnabled(true) if no previous sample.
-    // Returns true if the sample is valid, or false if invalid.
-    // Note that 'ns' is an output parameter passed by reference.
-    // The caller does not need to initialize this variable.
-    // The units are CPU nanoseconds consumed by current thread.
-    bool sample(double& ns);
-
-    // Return the elapsed delta wall clock ns since initial enable or reset,
-    // as reported by clock_gettime(CLOCK_MONOTONIC).
-    long long elapsed() const;
-
-    // Reset elapsed wall clock.  Has no effect on tracking or accumulator.
-    void resetElapsed();
-
-    // Return current clock frequency for specified CPU, in kHz.
-    // You can get your CPU number using sched_getcpu(2).  Note that, unless CPU affinity
-    // has been configured appropriately, the CPU number can change.
-    // Also note that, unless the CPU governor has been configured appropriately,
-    // the CPU frequency can change.  And even if the CPU frequency is locked down
-    // to a particular value, that the frequency might still be adjusted
-    // to prevent thermal overload.  Therefore you should poll for your thread's
-    // current CPU number and clock frequency periodically.
-    uint32_t getCpukHz(int cpuNum);
-
-private:
-    bool mIsEnabled;                // whether tracking is currently enabled
-    bool mWasEverEnabled;           // whether tracking was ever enabled
-    long long mAccumulator;         // accumulated thread CPU time since last sample, in ns
-    struct timespec mPreviousTs;    // most recent thread CPU time, valid only if mIsEnabled is true
-    struct timespec mMonotonicTs;   // most recent monotonic time
-    bool mMonotonicKnown;           // whether mMonotonicTs has been set
-
-    static const int MAX_CPU = 8;
-    static int sScalingFds[MAX_CPU];// file descriptor per CPU for reading scaling_cur_freq
-    uint32_t mCurrentkHz[MAX_CPU];  // current CPU frequency in kHz, not static to avoid a race
-    static pthread_once_t sOnceControl;
-    static int sKernelMax;          // like MAX_CPU, but determined at runtime == cpu/kernel_max + 1
-    static void init();             // called once at first ThreadCpuUsage construction
-    static pthread_mutex_t sMutex;  // protects sScalingFds[] after initialization
-};
-
-}   // namespace android
-
-#endif //  _THREAD_CPU_USAGE_H
diff --git a/libs/binder/IPCThreadState.cpp b/libs/binder/IPCThreadState.cpp
index 28b74ba..5a38b95 100644
--- a/libs/binder/IPCThreadState.cpp
+++ b/libs/binder/IPCThreadState.cpp
@@ -418,6 +418,60 @@
     talkWithDriver(false);
 }
 
+status_t IPCThreadState::getAndExecuteCommand()
+{
+    status_t result;
+    int32_t cmd;
+
+    result = talkWithDriver();
+    if (result >= NO_ERROR) {
+        size_t IN = mIn.dataAvail();
+        if (IN < sizeof(int32_t)) return result;
+        cmd = mIn.readInt32();
+        IF_LOG_COMMANDS() {
+            alog << "Processing top-level Command: "
+                 << getReturnString(cmd) << endl;
+        }
+
+        result = executeCommand(cmd);
+
+        // After executing the command, ensure that the thread is returned to the
+        // foreground cgroup before rejoining the pool.  The driver takes care of
+        // restoring the priority, but doesn't do anything with cgroups so we
+        // need to take care of that here in userspace.  Note that we do make
+        // sure to go in the foreground after executing a transaction, but
+        // there are other callbacks into user code that could have changed
+        // our group so we want to make absolutely sure it is put back.
+        set_sched_policy(mMyThreadId, SP_FOREGROUND);
+    }
+
+    return result;
+}
+
+// When we've cleared the incoming command queue, process any pending derefs
+void IPCThreadState::processPendingDerefs()
+{
+    if (mIn.dataPosition() >= mIn.dataSize()) {
+        size_t numPending = mPendingWeakDerefs.size();
+        if (numPending > 0) {
+            for (size_t i = 0; i < numPending; i++) {
+                RefBase::weakref_type* refs = mPendingWeakDerefs[i];
+                refs->decWeak(mProcess.get());
+            }
+            mPendingWeakDerefs.clear();
+        }
+
+        numPending = mPendingStrongDerefs.size();
+        if (numPending > 0) {
+            for (size_t i = 0; i < numPending; i++) {
+                BBinder* obj = mPendingStrongDerefs[i];
+                obj->decStrong(mProcess.get());
+            }
+            mPendingStrongDerefs.clear();
+        }
+    }
+}
+
 void IPCThreadState::joinThreadPool(bool isMain)
 {
     LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid());
@@ -431,57 +485,16 @@
         
     status_t result;
     do {
-        int32_t cmd;
-        
-        // When we've cleared the incoming command queue, process any pending derefs
-        if (mIn.dataPosition() >= mIn.dataSize()) {
-            size_t numPending = mPendingWeakDerefs.size();
-            if (numPending > 0) {
-                for (size_t i = 0; i < numPending; i++) {
-                    RefBase::weakref_type* refs = mPendingWeakDerefs[i];
-                    refs->decWeak(mProcess.get());
-                }
-                mPendingWeakDerefs.clear();
-            }
-
-            numPending = mPendingStrongDerefs.size();
-            if (numPending > 0) {
-                for (size_t i = 0; i < numPending; i++) {
-                    BBinder* obj = mPendingStrongDerefs[i];
-                    obj->decStrong(mProcess.get());
-                }
-                mPendingStrongDerefs.clear();
-            }
-        }
-
+        processPendingDerefs();
         // now get the next command to be processed, waiting if necessary
-        result = talkWithDriver();
-        if (result >= NO_ERROR) {
-            size_t IN = mIn.dataAvail();
-            if (IN < sizeof(int32_t)) continue;
-            cmd = mIn.readInt32();
-            IF_LOG_COMMANDS() {
-                alog << "Processing top-level Command: "
-                    << getReturnString(cmd) << endl;
-            }
+        result = getAndExecuteCommand();
 
-
-            result = executeCommand(cmd);
-        } else if (result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
-            ALOGE("talkWithDriver(fd=%d) returned unexpected error %d, aborting",
+        if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) {
+            ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting",
                   mProcess->mDriverFD, result);
             abort();
         }
         
-        // After executing the command, ensure that the thread is returned to the
-        // foreground cgroup before rejoining the pool.  The driver takes care of
-        // restoring the priority, but doesn't do anything with cgroups so we
-        // need to take care of that here in userspace.  Note that we do make
-        // sure to go in the foreground after executing a transaction, but
-        // there are other callbacks into user code that could have changed
-        // our group so we want to make absolutely sure it is put back.
-        set_sched_policy(mMyThreadId, SP_FOREGROUND);
-
         // Let this thread exit the thread pool if it is no longer
         // needed and it is not the main process thread.
         if(result == TIMED_OUT && !isMain) {
@@ -496,6 +509,30 @@
     talkWithDriver(false);
 }
 
+int IPCThreadState::setupPolling(int* fd)
+{
+    if (mProcess->mDriverFD <= 0) {
+        return -EBADF;
+    }
+
+    mOut.writeInt32(BC_ENTER_LOOPER);
+    *fd = mProcess->mDriverFD;
+    return 0;
+}
+
+status_t IPCThreadState::handlePolledCommands()
+{
+    status_t result;
+
+    do {
+        result = getAndExecuteCommand();
+    } while (mIn.dataPosition() < mIn.dataSize());
+
+    processPendingDerefs();
+    flushCommands();
+    return result;
+}
+
 void IPCThreadState::stopProcess(bool immediate)
 {
     //ALOGI("**** STOPPING PROCESS");
@@ -826,7 +863,7 @@
     IF_LOG_COMMANDS() {
         alog << "Our err: " << (void*)err << ", write consumed: "
             << bwr.write_consumed << " (of " << mOut.dataSize()
-			<< "), read consumed: " << bwr.read_consumed << endl;
+                        << "), read consumed: " << bwr.read_consumed << endl;
     }
 
     if (err >= NO_ERROR) {
@@ -1104,16 +1141,16 @@
 
 void IPCThreadState::threadDestructor(void *st)
 {
-	IPCThreadState* const self = static_cast<IPCThreadState*>(st);
-	if (self) {
-		self->flushCommands();
+        IPCThreadState* const self = static_cast<IPCThreadState*>(st);
+        if (self) {
+                self->flushCommands();
 #if defined(HAVE_ANDROID_OS)
         if (self->mProcess->mDriverFD > 0) {
             ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0);
         }
 #endif
-		delete self;
-	}
+                delete self;
+        }
 }
 
 
diff --git a/libs/cpustats/Android.mk b/libs/cpustats/Android.mk
deleted file mode 100644
index b506353..0000000
--- a/libs/cpustats/Android.mk
+++ /dev/null
@@ -1,11 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES :=     \
-        CentralTendencyStatistics.cpp \
-        ThreadCpuUsage.cpp
-
-LOCAL_MODULE := libcpustats
-
-include $(BUILD_STATIC_LIBRARY)
diff --git a/libs/cpustats/CentralTendencyStatistics.cpp b/libs/cpustats/CentralTendencyStatistics.cpp
deleted file mode 100644
index 42ab62b..0000000
--- a/libs/cpustats/CentralTendencyStatistics.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdlib.h>
-
-#include <cpustats/CentralTendencyStatistics.h>
-
-void CentralTendencyStatistics::sample(double x)
-{
-    // update min and max
-    if (x < mMinimum)
-        mMinimum = x;
-    if (x > mMaximum)
-        mMaximum = x;
-    // Knuth
-    if (mN == 0) {
-        mMean = 0;
-    }
-    ++mN;
-    double delta = x - mMean;
-    mMean += delta / mN;
-    mM2 += delta * (x - mMean);
-}
-
-void CentralTendencyStatistics::reset()
-{
-    mMean = NAN;
-    mMedian = NAN;
-    mMinimum = INFINITY;
-    mMaximum = -INFINITY;
-    mN = 0;
-    mM2 = 0;
-    mVariance = NAN;
-    mVarianceKnownForN = 0;
-    mStddev = NAN;
-    mStddevKnownForN = 0;
-}
-
-double CentralTendencyStatistics::variance() const
-{
-    double variance;
-    if (mVarianceKnownForN != mN) {
-        if (mN > 1) {
-            // double variance_n = M2/n;
-            variance = mM2 / (mN - 1);
-        } else {
-            variance = NAN;
-        }
-        mVariance = variance;
-        mVarianceKnownForN = mN;
-    } else {
-        variance = mVariance;
-    }
-    return variance;
-}
-
-double CentralTendencyStatistics::stddev() const
-{
-    double stddev;
-    if (mStddevKnownForN != mN) {
-        stddev = sqrt(variance());
-        mStddev = stddev;
-        mStddevKnownForN = mN;
-    } else {
-        stddev = mStddev;
-    }
-    return stddev;
-}
diff --git a/libs/cpustats/ThreadCpuUsage.cpp b/libs/cpustats/ThreadCpuUsage.cpp
deleted file mode 100644
index 637402a..0000000
--- a/libs/cpustats/ThreadCpuUsage.cpp
+++ /dev/null
@@ -1,255 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "ThreadCpuUsage"
-//#define LOG_NDEBUG 0
-
-#include <errno.h>
-#include <stdlib.h>
-#include <time.h>
-
-#include <utils/Debug.h>
-#include <utils/Log.h>
-
-#include <cpustats/ThreadCpuUsage.h>
-
-namespace android {
-
-bool ThreadCpuUsage::setEnabled(bool isEnabled)
-{
-    bool wasEnabled = mIsEnabled;
-    // only do something if there is a change
-    if (isEnabled != wasEnabled) {
-        ALOGV("setEnabled(%d)", isEnabled);
-        int rc;
-        // enabling
-        if (isEnabled) {
-            rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &mPreviousTs);
-            if (rc) {
-                ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
-                isEnabled = false;
-            } else {
-                mWasEverEnabled = true;
-                // record wall clock time at first enable
-                if (!mMonotonicKnown) {
-                    rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
-                    if (rc) {
-                        ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
-                    } else {
-                        mMonotonicKnown = true;
-                    }
-                }
-            }
-        // disabling
-        } else {
-            struct timespec ts;
-            rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
-            if (rc) {
-                ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
-            } else {
-                long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
-                        (ts.tv_nsec - mPreviousTs.tv_nsec);
-                mAccumulator += delta;
-#if 0
-                mPreviousTs = ts;
-#endif
-            }
-        }
-        mIsEnabled = isEnabled;
-    }
-    return wasEnabled;
-}
-
-bool ThreadCpuUsage::sampleAndEnable(double& ns)
-{
-    bool ret;
-    bool wasEverEnabled = mWasEverEnabled;
-    if (enable()) {
-        // already enabled, so add a new sample relative to previous
-        return sample(ns);
-    } else if (wasEverEnabled) {
-        // was disabled, but add sample for accumulated time while enabled
-        ns = (double) mAccumulator;
-        mAccumulator = 0;
-        ALOGV("sampleAndEnable %.0f", ns);
-        return true;
-    } else {
-        // first time called
-        ns = 0.0;
-        ALOGV("sampleAndEnable false");
-        return false;
-    }
-}
-
-bool ThreadCpuUsage::sample(double &ns)
-{
-    if (mWasEverEnabled) {
-        if (mIsEnabled) {
-            struct timespec ts;
-            int rc;
-            rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
-            if (rc) {
-                ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
-                ns = 0.0;
-                return false;
-            } else {
-                long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
-                        (ts.tv_nsec - mPreviousTs.tv_nsec);
-                mAccumulator += delta;
-                mPreviousTs = ts;
-            }
-        } else {
-            mWasEverEnabled = false;
-        }
-        ns = (double) mAccumulator;
-        ALOGV("sample %.0f", ns);
-        mAccumulator = 0;
-        return true;
-    } else {
-        ALOGW("Can't add sample because measurements have never been enabled");
-        ns = 0.0;
-        return false;
-    }
-}
-
-long long ThreadCpuUsage::elapsed() const
-{
-    long long elapsed;
-    if (mMonotonicKnown) {
-        struct timespec ts;
-        int rc;
-        rc = clock_gettime(CLOCK_MONOTONIC, &ts);
-        if (rc) {
-            ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
-            elapsed = 0;
-        } else {
-            // mMonotonicTs is updated only at first enable and resetStatistics
-            elapsed = (ts.tv_sec - mMonotonicTs.tv_sec) * 1000000000LL +
-                    (ts.tv_nsec - mMonotonicTs.tv_nsec);
-        }
-    } else {
-        ALOGW("Can't compute elapsed time because measurements have never been enabled");
-        elapsed = 0;
-    }
-    ALOGV("elapsed %lld", elapsed);
-    return elapsed;
-}
-
-void ThreadCpuUsage::resetElapsed()
-{
-    ALOGV("resetElapsed");
-    if (mMonotonicKnown) {
-        int rc;
-        rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
-        if (rc) {
-            ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
-            mMonotonicKnown = false;
-        }
-    }
-}
-
-/*static*/
-int ThreadCpuUsage::sScalingFds[ThreadCpuUsage::MAX_CPU];
-pthread_once_t ThreadCpuUsage::sOnceControl = PTHREAD_ONCE_INIT;
-int ThreadCpuUsage::sKernelMax;
-pthread_mutex_t ThreadCpuUsage::sMutex = PTHREAD_MUTEX_INITIALIZER;
-
-/*static*/
-void ThreadCpuUsage::init()
-{
-    // read the number of CPUs
-    sKernelMax = 1;
-    int fd = open("/sys/devices/system/cpu/kernel_max", O_RDONLY);
-    if (fd >= 0) {
-#define KERNEL_MAX_SIZE 12
-        char kernelMax[KERNEL_MAX_SIZE];
-        ssize_t actual = read(fd, kernelMax, sizeof(kernelMax));
-        if (actual >= 2 && kernelMax[actual-1] == '\n') {
-            sKernelMax = atoi(kernelMax);
-            if (sKernelMax >= MAX_CPU - 1) {
-                ALOGW("kernel_max %d but MAX_CPU %d", sKernelMax, MAX_CPU);
-                sKernelMax = MAX_CPU;
-            } else if (sKernelMax < 0) {
-                ALOGW("kernel_max invalid %d", sKernelMax);
-                sKernelMax = 1;
-            } else {
-                ++sKernelMax;
-                ALOGV("number of CPUs %d", sKernelMax);
-            }
-        } else {
-            ALOGW("Can't read number of CPUs");
-        }
-        (void) close(fd);
-    } else {
-        ALOGW("Can't open number of CPUs");
-    }
-    int i;
-    for (i = 0; i < MAX_CPU; ++i) {
-        sScalingFds[i] = -1;
-    }
-}
-
-uint32_t ThreadCpuUsage::getCpukHz(int cpuNum)
-{
-    if (cpuNum < 0 || cpuNum >= MAX_CPU) {
-        ALOGW("getCpukHz called with invalid CPU %d", cpuNum);
-        return 0;
-    }
-    // double-checked locking idiom is not broken for atomic values such as fd
-    int fd = sScalingFds[cpuNum];
-    if (fd < 0) {
-        // some kernels can't open a scaling file until hot plug complete
-        pthread_mutex_lock(&sMutex);
-        fd = sScalingFds[cpuNum];
-        if (fd < 0) {
-#define FREQ_SIZE 64
-            char freq_path[FREQ_SIZE];
-#define FREQ_DIGIT 27
-            COMPILE_TIME_ASSERT_FUNCTION_SCOPE(MAX_CPU <= 10);
-#define FREQ_PATH "/sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq"
-            strlcpy(freq_path, FREQ_PATH, sizeof(freq_path));
-            freq_path[FREQ_DIGIT] = cpuNum + '0';
-            fd = open(freq_path, O_RDONLY | O_CLOEXEC);
-            // keep this fd until process exit or exec
-            sScalingFds[cpuNum] = fd;
-        }
-        pthread_mutex_unlock(&sMutex);
-        if (fd < 0) {
-            ALOGW("getCpukHz can't open CPU %d", cpuNum);
-            return 0;
-        }
-    }
-#define KHZ_SIZE 12
-    char kHz[KHZ_SIZE];   // kHz base 10
-    ssize_t actual = pread(fd, kHz, sizeof(kHz), (off_t) 0);
-    uint32_t ret;
-    if (actual >= 2 && kHz[actual-1] == '\n') {
-        ret = atoi(kHz);
-    } else {
-        ret = 0;
-    }
-    if (ret != mCurrentkHz[cpuNum]) {
-        if (ret > 0) {
-            ALOGV("CPU %d frequency %u kHz", cpuNum, ret);
-        } else {
-            ALOGW("Can't read CPU %d frequency", cpuNum);
-        }
-        mCurrentkHz[cpuNum] = ret;
-    }
-    return ret;
-}
-
-}   // namespace android
diff --git a/services/sensorservice/SensorDevice.cpp b/services/sensorservice/SensorDevice.cpp
index 16dabe8..18a1523 100644
--- a/services/sensorservice/SensorDevice.cpp
+++ b/services/sensorservice/SensorDevice.cpp
@@ -65,30 +65,26 @@
     }
 }
 
-void SensorDevice::dump(String8& result, char* buffer, size_t SIZE)
+void SensorDevice::dump(String8& result)
 {
     if (!mSensorModule) return;
     sensor_t const* list;
     ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
 
-    snprintf(buffer, SIZE, "%d h/w sensors:\n", int(count));
-    result.append(buffer);
+    result.appendFormat("%d h/w sensors:\n", int(count));
 
     Mutex::Autolock _l(mLock);
     for (size_t i=0 ; i<size_t(count) ; i++) {
         const Info& info = mActivationCount.valueFor(list[i].handle);
-        snprintf(buffer, SIZE, "handle=0x%08x, active-count=%d, rates(ms)={ ",
+        result.appendFormat("handle=0x%08x, active-count=%d, rates(ms)={ ",
                 list[i].handle,
                 info.rates.size());
-        result.append(buffer);
         for (size_t j=0 ; j<info.rates.size() ; j++) {
-            snprintf(buffer, SIZE, "%4.1f%s",
+            result.appendFormat("%4.1f%s",
                     info.rates.valueAt(j) / 1e6f,
                     j<info.rates.size()-1 ? ", " : "");
-            result.append(buffer);
         }
-        snprintf(buffer, SIZE, " }, selected=%4.1f ms\n",  info.delay / 1e6f);
-        result.append(buffer);
+        result.appendFormat(" }, selected=%4.1f ms\n",  info.delay / 1e6f);
     }
 }
 
diff --git a/services/sensorservice/SensorDevice.h b/services/sensorservice/SensorDevice.h
index c0b357d..ca67ce2 100644
--- a/services/sensorservice/SensorDevice.h
+++ b/services/sensorservice/SensorDevice.h
@@ -57,7 +57,7 @@
     status_t activate(void* ident, int handle, int enabled);
     status_t setDelay(void* ident, int handle, int64_t ns);
     status_t resetStateWithoutActuatingHardware(void *ident, int handle);
-    void dump(String8& result, char* buffer, size_t SIZE);
+    void dump(String8& result);
 };
 
 // ---------------------------------------------------------------------------
diff --git a/services/sensorservice/SensorFusion.cpp b/services/sensorservice/SensorFusion.cpp
index a0a17da..4014477 100644
--- a/services/sensorservice/SensorFusion.cpp
+++ b/services/sensorservice/SensorFusion.cpp
@@ -136,9 +136,9 @@
     return mAcc.getMinDelay();
 }
 
-void SensorFusion::dump(String8& result, char* buffer, size_t SIZE) {
+void SensorFusion::dump(String8& result) {
     const Fusion& fusion(mFusion);
-    snprintf(buffer, SIZE, "9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, "
+    result.appendFormat("9-axis fusion %s (%d clients), gyro-rate=%7.2fHz, "
             "q=< %g, %g, %g, %g > (%g), "
             "b=< %g, %g, %g >\n",
             mEnabled ? "enabled" : "disabled",
@@ -152,7 +152,6 @@
             fusion.getBias().x,
             fusion.getBias().y,
             fusion.getBias().z);
-    result.append(buffer);
 }
 
 // ---------------------------------------------------------------------------
diff --git a/services/sensorservice/SensorFusion.h b/services/sensorservice/SensorFusion.h
index 3c2244e..432adbc 100644
--- a/services/sensorservice/SensorFusion.h
+++ b/services/sensorservice/SensorFusion.h
@@ -68,7 +68,7 @@
     float getPowerUsage() const;
     int32_t getMinDelay() const;
 
-    void dump(String8& result, char* buffer, size_t SIZE);
+    void dump(String8& result);
 };
 
 
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
index 4718447..99993ba 100644
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -187,47 +187,77 @@
 
 status_t SensorService::dump(int fd, const Vector<String16>& args)
 {
-    const size_t SIZE = 1024;
-    char buffer[SIZE];
     String8 result;
     if (!PermissionCache::checkCallingPermission(sDump)) {
-        snprintf(buffer, SIZE, "Permission Denial: "
+        result.appendFormat("Permission Denial: "
                 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
                 IPCThreadState::self()->getCallingPid(),
                 IPCThreadState::self()->getCallingUid());
-        result.append(buffer);
     } else {
         Mutex::Autolock _l(mLock);
-        snprintf(buffer, SIZE, "Sensor List:\n");
-        result.append(buffer);
+        result.append("Sensor List:\n");
         for (size_t i=0 ; i<mSensorList.size() ; i++) {
             const Sensor& s(mSensorList[i]);
             const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
-            snprintf(buffer, SIZE,
-                    "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
-                    "last=<%5.1f,%5.1f,%5.1f>\n",
+            result.appendFormat(
+                    "%-48s| %-32s | 0x%08x | ",
                     s.getName().string(),
                     s.getVendor().string(),
-                    s.getHandle(),
-                    s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
-                    e.data[0], e.data[1], e.data[2]);
-            result.append(buffer);
-        }
-        SensorFusion::getInstance().dump(result, buffer, SIZE);
-        SensorDevice::getInstance().dump(result, buffer, SIZE);
+                    s.getHandle());
 
-        snprintf(buffer, SIZE, "%d active connections\n",
-                mActiveConnections.size());
-        result.append(buffer);
-        snprintf(buffer, SIZE, "Active sensors:\n");
-        result.append(buffer);
+            if (s.getMinDelay() > 0) {
+                result.appendFormat(
+                    "maxRate=%7.2fHz | ", 1e6f / s.getMinDelay());
+            } else {
+                result.append(s.getMinDelay() == 0
+                        ? "on-demand         | "
+                        : "one-shot          | ");
+            }
+
+            switch (s.getType()) {
+                case SENSOR_TYPE_ROTATION_VECTOR:
+                case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
+                    result.appendFormat(
+                            "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
+                            e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
+                    break;
+                case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
+                case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                    result.appendFormat(
+                            "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
+                            e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
+                    break;
+                case SENSOR_TYPE_GAME_ROTATION_VECTOR:
+                    result.appendFormat(
+                            "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
+                            e.data[0], e.data[1], e.data[2], e.data[3]);
+                    break;
+                case SENSOR_TYPE_SIGNIFICANT_MOTION:
+                case SENSOR_TYPE_STEP_DETECTOR:
+                    result.appendFormat( "last=<%f>\n", e.data[0]);
+                    break;
+                case SENSOR_TYPE_STEP_COUNTER:
+                    result.appendFormat( "last=<%llu>\n", e.u64.step_counter);
+                    break;
+                default:
+                    // default to 3 values
+                    result.appendFormat(
+                            "last=<%5.1f,%5.1f,%5.1f>\n",
+                            e.data[0], e.data[1], e.data[2]);
+                    break;
+            }
+        }
+        SensorFusion::getInstance().dump(result);
+        SensorDevice::getInstance().dump(result);
+
+        result.appendFormat("%d active connections\n", mActiveConnections.size());
+        result.append("Active sensors:\n");
         for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
             int handle = mActiveSensors.keyAt(i);
-            snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
+            result.appendFormat("%s (handle=0x%08x, connections=%d)\n",
                     getSensorName(handle).string(),
                     handle,
                     mActiveSensors.valueAt(i)->getNumConnections());
-            result.append(buffer);
         }
     }
     write(fd, result.string(), result.size());
@@ -240,7 +270,8 @@
     status_t err = NO_ERROR;
     for (int i=0 ; i<count ; i++) {
         int handle = buffer[i].sensor;
-        if (getSensorType(handle) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
+        int type = buffer[i].type;
+        if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
             if (connection->hasSensor(handle)) {
                 sensor = mSensorMap.valueFor(handle);
                 err = sensor ?sensor->resetStateWithoutActuatingHardware(connection.get(), handle)
@@ -279,7 +310,7 @@
         // Todo(): add a flag to the sensors definitions to indicate
         // the sensors which can wake up the AP
         for (int i = 0; i < count; i++) {
-            if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
+            if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
                  acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
                  wakeLockAcquired = true;
                  break;
@@ -331,7 +362,7 @@
         // handle backward compatibility for RotationVector sensor
         if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
             for (int i = 0; i < count; i++) {
-                if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_ROTATION_VECTOR) {
+                if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
                     // All the 4 components of the quaternion should be available
                     // No heading accuracy. Set it to -1
                     buffer[i].data[4] = -1;
@@ -419,18 +450,6 @@
     return result;
 }
 
-int SensorService::getSensorType(int handle) const {
-    size_t count = mUserSensorList.size();
-    for (size_t i=0 ; i<count ; i++) {
-        const Sensor& sensor(mUserSensorList[i]);
-        if (sensor.getHandle() == handle) {
-            return sensor.getType();
-        }
-    }
-    return -1;
-}
-
-
 Vector<Sensor> SensorService::getSensorList()
 {
     char value[PROPERTY_VALUE_MAX];
diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h
index 67489cc..56b0a3e 100644
--- a/services/sensorservice/SensorService.h
+++ b/services/sensorservice/SensorService.h
@@ -111,7 +111,6 @@
     DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
 
     String8 getSensorName(int handle) const;
-    int getSensorType(int handle) const;
     void recordLastValue(sensors_event_t const * buffer, size_t count);
     static void sortEventBuffer(sensors_event_t* buffer, size_t count);
     Sensor registerSensor(SensorInterface* sensor);
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 5d138d1..c6feee1 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -74,6 +74,12 @@
 
 #define DISPLAY_COUNT       1
 
+/*
+ * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
+ * black pixels.
+ */
+#define DEBUG_SCREENSHOTS   false
+
 EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
 
 namespace android {
@@ -2714,6 +2720,7 @@
                             if (buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr) == NO_ERROR) {
                                 glReadPixels(0, 0, buffer->stride, reqHeight,
                                         GL_RGBA, GL_UNSIGNED_BYTE, vaddr);
+                                checkScreenshot(buf, vaddr, hw, minLayerZ, maxLayerZ);
                                 buf->unlock();
                             }
                         }
@@ -2748,6 +2755,35 @@
     return result;
 }
 
+void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
+        const sp<const DisplayDevice>& hw,
+        uint32_t minLayerZ, uint32_t maxLayerZ) {
+    if (DEBUG_SCREENSHOTS) {
+        for (ssize_t y=0 ; y<buf->height ; y++) {
+            uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
+            for (ssize_t x=0 ; x<buf->width ; x++) {
+                if (p[x] != 0xFF000000) return;
+            }
+        }
+        ALOGE("*** we just took a black screenshot ***\n"
+                "requested minz=%d, maxz=%d, layerStack=%d",
+                minLayerZ, maxLayerZ, hw->getLayerStack());
+        const LayerVector& layers( mDrawingState.layersSortedByZ );
+        const size_t count = layers.size();
+        for (size_t i=0 ; i<count ; ++i) {
+            const sp<Layer>& layer(layers[i]);
+            const Layer::State& state(layer->getDrawingState());
+            const bool visible = (state.layerStack == hw->getLayerStack())
+                                && (state.z >= minLayerZ && state.z <= maxLayerZ)
+                                && (layer->isVisible());
+            ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
+                    visible ? '+' : '-',
+                            i, layer->getName().string(), state.layerStack, state.z,
+                            layer->isVisible(), state.flags, state.alpha);
+        }
+    }
+}
+
 // ---------------------------------------------------------------------------
 
 SurfaceFlinger::LayerVector::LayerVector() {
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index c0815f8..21d523b 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -388,6 +388,9 @@
         String8& result) const;
     bool startDdmConnection();
     static void appendSfConfigString(String8& result);
+    void checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
+            const sp<const DisplayDevice>& hw,
+            uint32_t minLayerZ, uint32_t maxLayerZ);
 
     /* ------------------------------------------------------------------------
      * Attributes