diff options
Diffstat (limited to 'core/java/com/android/internal/os/KernelCpuSpeedReader.java')
-rw-r--r-- | core/java/com/android/internal/os/KernelCpuSpeedReader.java | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/core/java/com/android/internal/os/KernelCpuSpeedReader.java b/core/java/com/android/internal/os/KernelCpuSpeedReader.java index c30df28..5b776ac 100644 --- a/core/java/com/android/internal/os/KernelCpuSpeedReader.java +++ b/core/java/com/android/internal/os/KernelCpuSpeedReader.java @@ -24,8 +24,8 @@ import java.io.IOException; import java.util.Arrays; /** - * Reads CPU time spent at various frequencies and provides a delta from the last call to - * {@link #readDelta}. Each line in the proc file has the format: + * Reads CPU time of a specific core spent at various frequencies and provides a delta from the + * last call to {@link #readDelta}. Each line in the proc file has the format: * * freq time * @@ -33,12 +33,20 @@ import java.util.Arrays; */ public class KernelCpuSpeedReader { private static final String TAG = "KernelCpuSpeedReader"; - private static final String sProcFile = - "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"; - private static final int MAX_SPEEDS = 60; - private long[] mLastSpeedTimes = new long[MAX_SPEEDS]; - private long[] mDeltaSpeedTimes = new long[MAX_SPEEDS]; + private final String mProcFile; + private final long[] mLastSpeedTimes; + private final long[] mDeltaSpeedTimes; + + /** + * @param cpuNumber The cpu (cpu0, cpu1, etc) whose state to read. + */ + public KernelCpuSpeedReader(int cpuNumber, int numSpeedSteps) { + mProcFile = String.format("/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", + cpuNumber); + mLastSpeedTimes = new long[numSpeedSteps]; + mDeltaSpeedTimes = new long[numSpeedSteps]; + } /** * The returned array is modified in subsequent calls to {@link #readDelta}. @@ -46,22 +54,28 @@ public class KernelCpuSpeedReader { * {@link #readDelta}. */ public long[] readDelta() { - try (BufferedReader reader = new BufferedReader(new FileReader(sProcFile))) { + try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) { TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' '); String line; int speedIndex = 0; - while ((line = reader.readLine()) != null) { + while (speedIndex < mLastSpeedTimes.length && (line = reader.readLine()) != null) { splitter.setString(line); Long.parseLong(splitter.next()); // The proc file reports time in 1/100 sec, so convert to milliseconds. long time = Long.parseLong(splitter.next()) * 10; - mDeltaSpeedTimes[speedIndex] = time - mLastSpeedTimes[speedIndex]; + if (time < mLastSpeedTimes[speedIndex]) { + // The stats reset when the cpu hotplugged. That means that the time + // we read is offset from 0, so the time is the delta. + mDeltaSpeedTimes[speedIndex] = time; + } else { + mDeltaSpeedTimes[speedIndex] = time - mLastSpeedTimes[speedIndex]; + } mLastSpeedTimes[speedIndex] = time; speedIndex++; } } catch (IOException e) { - Slog.e(TAG, "Failed to read cpu-freq", e); + Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage()); Arrays.fill(mDeltaSpeedTimes, 0); } return mDeltaSpeedTimes; |