Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 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; |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 19 | import android.os.StrictMode; |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 20 | import android.system.OsConstants; |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 21 | import android.util.Slog; |
| 22 | |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 23 | import libcore.io.Libcore; |
| 24 | |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 25 | import java.io.BufferedReader; |
| 26 | import java.io.FileReader; |
| 27 | import java.io.IOException; |
| 28 | import java.util.Arrays; |
| 29 | |
| 30 | /** |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 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: |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 33 | * |
| 34 | * freq time |
| 35 | * |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 36 | * where time is measured in jiffies. |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class KernelCpuSpeedReader { |
| 39 | private static final String TAG = "KernelCpuSpeedReader"; |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 41 | private final String mProcFile; |
| 42 | private final long[] mLastSpeedTimes; |
| 43 | private final long[] mDeltaSpeedTimes; |
| 44 | |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 45 | // How long a CPU jiffy is in milliseconds. |
| 46 | private final long mJiffyMillis; |
| 47 | |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 48 | /** |
| 49 | * @param cpuNumber The cpu (cpu0, cpu1, etc) whose state to read. |
| 50 | */ |
| 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]; |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 56 | long jiffyHz = Libcore.os.sysconf(OsConstants._SC_CLK_TCK); |
| 57 | mJiffyMillis = 1000/jiffyHz; |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 58 | } |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 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 | */ |
| 65 | public long[] readDelta() { |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 66 | StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads(); |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 67 | try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) { |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 68 | TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' '); |
| 69 | String line; |
| 70 | int speedIndex = 0; |
Adam Lesinski | 8057133 | 2015-09-18 14:56:34 -0700 | [diff] [blame] | 71 | while (speedIndex < mLastSpeedTimes.length && (line = reader.readLine()) != null) { |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 72 | splitter.setString(line); |
| 73 | Long.parseLong(splitter.next()); |
| 74 | |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 75 | long time = Long.parseLong(splitter.next()) * mJiffyMillis; |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 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 | } |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 83 | mLastSpeedTimes[speedIndex] = time; |
| 84 | speedIndex++; |
| 85 | } |
| 86 | } catch (IOException e) { |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 87 | Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage()); |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 88 | Arrays.fill(mDeltaSpeedTimes, 0); |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 89 | } finally { |
| 90 | StrictMode.setThreadPolicy(policy); |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 91 | } |
| 92 | return mDeltaSpeedTimes; |
| 93 | } |
| 94 | } |