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 | |
Tobias Thierer | 6217e37 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 18 | import android.system.Os; |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 19 | import android.text.TextUtils; |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 20 | import android.os.StrictMode; |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 21 | import android.system.OsConstants; |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 22 | import android.util.Slog; |
| 23 | |
| 24 | import java.io.BufferedReader; |
| 25 | import java.io.FileReader; |
| 26 | import java.io.IOException; |
| 27 | import java.util.Arrays; |
| 28 | |
| 29 | /** |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 30 | * Reads CPU time of a specific core spent at various frequencies and provides a delta from the |
| 31 | * 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] | 32 | * |
| 33 | * freq time |
| 34 | * |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 35 | * where time is measured in jiffies. |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 36 | */ |
| 37 | public class KernelCpuSpeedReader { |
| 38 | private static final String TAG = "KernelCpuSpeedReader"; |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 40 | private final String mProcFile; |
| 41 | private final long[] mLastSpeedTimes; |
| 42 | private final long[] mDeltaSpeedTimes; |
| 43 | |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 44 | // How long a CPU jiffy is in milliseconds. |
| 45 | private final long mJiffyMillis; |
| 46 | |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 47 | /** |
| 48 | * @param cpuNumber The cpu (cpu0, cpu1, etc) whose state to read. |
| 49 | */ |
| 50 | public KernelCpuSpeedReader(int cpuNumber, int numSpeedSteps) { |
| 51 | mProcFile = String.format("/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", |
| 52 | cpuNumber); |
| 53 | mLastSpeedTimes = new long[numSpeedSteps]; |
| 54 | mDeltaSpeedTimes = new long[numSpeedSteps]; |
Tobias Thierer | 6217e37 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 55 | long jiffyHz = Os.sysconf(OsConstants._SC_CLK_TCK); |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 56 | mJiffyMillis = 1000/jiffyHz; |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 57 | } |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * The returned array is modified in subsequent calls to {@link #readDelta}. |
| 61 | * @return The time (in milliseconds) spent at different cpu speeds since the last call to |
| 62 | * {@link #readDelta}. |
| 63 | */ |
| 64 | public long[] readDelta() { |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 65 | StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads(); |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 66 | try (BufferedReader reader = new BufferedReader(new FileReader(mProcFile))) { |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 67 | TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(' '); |
| 68 | String line; |
| 69 | int speedIndex = 0; |
Adam Lesinski | 8057133 | 2015-09-18 14:56:34 -0700 | [diff] [blame] | 70 | while (speedIndex < mLastSpeedTimes.length && (line = reader.readLine()) != null) { |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 71 | splitter.setString(line); |
| 72 | Long.parseLong(splitter.next()); |
| 73 | |
Thierry Strudel | b786aaa | 2016-01-21 16:31:21 -0800 | [diff] [blame] | 74 | long time = Long.parseLong(splitter.next()) * mJiffyMillis; |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 75 | if (time < mLastSpeedTimes[speedIndex]) { |
| 76 | // The stats reset when the cpu hotplugged. That means that the time |
| 77 | // we read is offset from 0, so the time is the delta. |
| 78 | mDeltaSpeedTimes[speedIndex] = time; |
| 79 | } else { |
| 80 | mDeltaSpeedTimes[speedIndex] = time - mLastSpeedTimes[speedIndex]; |
| 81 | } |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 82 | mLastSpeedTimes[speedIndex] = time; |
| 83 | speedIndex++; |
| 84 | } |
| 85 | } catch (IOException e) { |
Adam Lesinski | 6832f39 | 2015-09-05 18:05:40 -0700 | [diff] [blame] | 86 | Slog.e(TAG, "Failed to read cpu-freq: " + e.getMessage()); |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 87 | Arrays.fill(mDeltaSpeedTimes, 0); |
Joe LaPenna | d1d80c2 | 2016-04-27 09:25:15 -0700 | [diff] [blame] | 88 | } finally { |
| 89 | StrictMode.setThreadPolicy(policy); |
Adam Lesinski | 06af1fa | 2015-05-05 17:35:35 -0700 | [diff] [blame] | 90 | } |
| 91 | return mDeltaSpeedTimes; |
| 92 | } |
| 93 | } |