• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.os;
17 
18 import android.text.TextUtils;
19 import android.os.StrictMode;
20 import android.system.OsConstants;
21 import android.util.Slog;
22 
23 import libcore.io.Libcore;
24 
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.Arrays;
29 
30 /**
31  * Reads CPU time of a specific core spent at various frequencies and provides a delta from the
32  * last call to {@link #readDelta}. Each line in the proc file has the format:
33  *
34  * freq time
35  *
36  * where time is measured in jiffies.
37  */
38 public class KernelCpuSpeedReader {
39     private static final String TAG = "KernelCpuSpeedReader";
40 
41     private final String mProcFile;
42     private final long[] mLastSpeedTimes;
43     private final long[] mDeltaSpeedTimes;
44 
45     // How long a CPU jiffy is in milliseconds.
46     private final long mJiffyMillis;
47 
48     /**
49      * @param cpuNumber The cpu (cpu0, cpu1, etc) whose state to read.
50      */
KernelCpuSpeedReader(int cpuNumber, int numSpeedSteps)51     public KernelCpuSpeedReader(int cpuNumber, int numSpeedSteps) {
52         mProcFile = String.format("/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state",
53                 cpuNumber);
54         mLastSpeedTimes = new long[numSpeedSteps];
55         mDeltaSpeedTimes = new long[numSpeedSteps];
56         long jiffyHz = Libcore.os.sysconf(OsConstants._SC_CLK_TCK);
57         mJiffyMillis = 1000/jiffyHz;
58     }
59 
60     /**
61      * The returned array is modified in subsequent calls to {@link #readDelta}.
62      * @return The time (in milliseconds) spent at different cpu speeds since the last call to
63      * {@link #readDelta}.
64      */
readDelta()65     public long[] readDelta() {
66         StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads();
67         try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) {
68             TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' ');
69             String line;
70             int speedIndex = 0;
71             while (speedIndex < mLastSpeedTimes.length && (line = reader.readLine()) != null) {
72                 splitter.setString(line);
73                 Long.parseLong(splitter.next());
74 
75                 long time = Long.parseLong(splitter.next()) * mJiffyMillis;
76                 if (time < mLastSpeedTimes[speedIndex]) {
77                     // The stats reset when the cpu hotplugged. That means that the time
78                     // we read is offset from 0, so the time is the delta.
79                     mDeltaSpeedTimes[speedIndex] = time;
80                 } else {
81                     mDeltaSpeedTimes[speedIndex] = time - mLastSpeedTimes[speedIndex];
82                 }
83                 mLastSpeedTimes[speedIndex] = time;
84                 speedIndex++;
85             }
86         } catch (IOException e) {
87             Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage());
88             Arrays.fill(mDeltaSpeedTimes, 0);
89         } finally {
90             StrictMode.setThreadPolicy(policy);
91         }
92         return mDeltaSpeedTimes;
93     }
94 }
95