OSDN Git Service

pull absolute value for cpu time per frequency.
authorChenjie Yu <cjyu@google.com>
Thu, 11 Jan 2018 00:02:57 +0000 (16:02 -0800)
committerChenjie Yu <cjyu@google.com>
Thu, 11 Jan 2018 17:27:28 +0000 (09:27 -0800)
Test: cts test
Change-Id: I519616905ed8ec6afdaa7e1a0743e279009aa0e5

cmds/statsd/src/atoms.proto
core/java/com/android/internal/os/KernelCpuSpeedReader.java
services/core/java/com/android/server/stats/StatsCompanionService.java

index 7f77ef7..400dc4c 100644 (file)
@@ -1109,14 +1109,10 @@ message IsolatedUidChanged {
 
 /**
  * Pulls Cpu time per frequency.
- * Note: this should be pulled for gauge metric only, without condition.
- * The puller keeps internal state of last values. It should not be pulled by
- * different metrics.
- * The pulled data is delta of cpu time from last pull, calculated as
- * following:
- * if current time is larger than last value, take delta between the two.
- * if current time is smaller than last value, there must be a cpu
- * hotplug event, and the current time is taken as delta.
+ * Pulls the time the cpu spend on the frequency index. Frequency index
+ * starts from highest to lowest. The value should be monotonically
+ * increasing since boot. However, if there is a cpu
+ * hotplug event, the value would be reset as well.
  */
 message CpuTimePerFreq {
     optional uint32 cluster = 1;
index 4c0370c..98fea01 100644 (file)
@@ -38,6 +38,7 @@ public class KernelCpuSpeedReader {
     private static final String TAG = "KernelCpuSpeedReader";
 
     private final String mProcFile;
+    private final int mNumSpeedSteps;
     private final long[] mLastSpeedTimesMs;
     private final long[] mDeltaSpeedTimesMs;
 
@@ -50,6 +51,7 @@ public class KernelCpuSpeedReader {
     public KernelCpuSpeedReader(int cpuNumber, int numSpeedSteps) {
         mProcFile = String.format("/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state",
                 cpuNumber);
+        mNumSpeedSteps = numSpeedSteps;
         mLastSpeedTimesMs = new long[numSpeedSteps];
         mDeltaSpeedTimesMs = new long[numSpeedSteps];
         long jiffyHz = Os.sysconf(OsConstants._SC_CLK_TCK);
@@ -90,4 +92,31 @@ public class KernelCpuSpeedReader {
         }
         return mDeltaSpeedTimesMs;
     }
+
+    /**
+     * @return The time (in milliseconds) spent at different cpu speeds. The values should be
+     * monotonically increasing, unless the cpu was hotplugged.
+     */
+    public long[] readAbsolute() {
+        StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads();
+        long[] speedTimeMs = new long[mNumSpeedSteps];
+        try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) {
+            TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' ');
+            String line;
+            int speedIndex = 0;
+            while (speedIndex < mNumSpeedSteps && (line = reader.readLine()) != null) {
+                splitter.setString(line);
+                splitter.next();
+                long time = Long.parseLong(splitter.next()) * mJiffyMillis;
+                speedTimeMs[speedIndex] = time;
+                speedIndex++;
+            }
+        } catch (IOException e) {
+            Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage());
+            Arrays.fill(speedTimeMs, 0);
+        } finally {
+            StrictMode.setThreadPolicy(policy);
+        }
+        return speedTimeMs;
+    }
 }
index b31f4b3..7cd12d1 100644 (file)
@@ -555,7 +555,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
             case StatsLog.CPU_TIME_PER_FREQ: {
                 List<StatsLogEventWrapper> ret = new ArrayList();
                 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
-                    long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readDelta();
+                    long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute();
                     if (clusterTimeMs != null) {
                         for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) {
                             StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);