KernelUidCpuFreqTimeReader.java revision 9b735c5c1a4575d9f0ea9f3229ad8bf9401caee0
1/*
2 * Copyright (C) 2017 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
17package com.android.internal.os;
18
19import android.annotation.Nullable;
20import android.util.Slog;
21import android.util.SparseArray;
22
23import com.android.internal.annotations.VisibleForTesting;
24
25import java.io.BufferedReader;
26import java.io.FileReader;
27import java.io.IOException;
28
29/**
30 * Reads /proc/uid_time_in_state which has the format:
31 *
32 * uid: [freq1] [freq2] [freq3] ...
33 * [uid1]: [time in freq1] [time in freq2] [time in freq3] ...
34 * [uid2]: [time in freq1] [time in freq2] [time in freq3] ...
35 * ...
36 *
37 * This provides the times a UID's processes spent executing at each different cpu frequency.
38 * The file contains a monotonically increasing count of time for a single boot. This class
39 * maintains the previous results of a call to {@link #readDelta} in order to provide a proper
40 * delta.
41 */
42public class KernelUidCpuFreqTimeReader {
43    private static final String TAG = "KernelUidCpuFreqTimeReader";
44    private static final String UID_TIMES_PROC_FILE = "/proc/uid_time_in_state";
45
46    public interface Callback {
47        void onCpuFreqs(long[] cpuFreqs);
48        void onUidCpuFreqTime(int uid, long[] cpuFreqTimeMs);
49    }
50
51    private long[] mCpuFreqs;
52    private int mCpuFreqsCount;
53
54    private SparseArray<long[]> mLastUidCpuFreqTimeMs = new SparseArray<>();
55
56    public void readDelta(@Nullable Callback callback) {
57        try (BufferedReader reader = new BufferedReader(new FileReader(UID_TIMES_PROC_FILE))) {
58            readDelta(reader, callback);
59        } catch (IOException e) {
60            Slog.e(TAG, "Failed to read " + UID_TIMES_PROC_FILE + ": " + e);
61        }
62    }
63
64    @VisibleForTesting
65    public void readDelta(BufferedReader reader, @Nullable Callback callback) throws IOException {
66        String line = reader.readLine();
67        if (line == null) {
68            return;
69        }
70        readCpuFreqs(line, callback);
71        while ((line = reader.readLine()) != null) {
72            final int index = line.indexOf(' ');
73            final int uid = Integer.parseInt(line.substring(0, index - 1), 10);
74            readTimesForUid(uid, line.substring(index + 1, line.length()), callback);
75        }
76    }
77
78    private void readTimesForUid(int uid, String line, Callback callback) {
79        long[] uidTimeMs = mLastUidCpuFreqTimeMs.get(uid);
80        if (uidTimeMs == null) {
81            uidTimeMs = new long[mCpuFreqsCount];
82            mLastUidCpuFreqTimeMs.put(uid, uidTimeMs);
83        }
84        final String[] timesStr = line.split(" ");
85        final int size = timesStr.length;
86        if (size != uidTimeMs.length) {
87            Slog.e(TAG, "No. of readings don't match cpu freqs, readings: " + size
88                    + " cpuFreqsCount: " + uidTimeMs.length);
89            return;
90        }
91        final long[] deltaUidTimeMs = new long[size];
92        for (int i = 0; i < size; ++i) {
93            // Times read will be in units of 10ms
94            final long totalTimeMs = Long.parseLong(timesStr[i], 10) * 10;
95            deltaUidTimeMs[i] = totalTimeMs - uidTimeMs[i];
96            uidTimeMs[i] = totalTimeMs;
97        }
98        if (callback != null) {
99            callback.onUidCpuFreqTime(uid, deltaUidTimeMs);
100        }
101    }
102
103    private void readCpuFreqs(String line, Callback callback) {
104        if (mCpuFreqs == null) {
105            final String[] freqStr = line.split(" ");
106            // First item would be "uid:" which needs to be ignored
107            mCpuFreqsCount = freqStr.length - 1;
108            mCpuFreqs = new long[mCpuFreqsCount];
109            for (int i = 0; i < mCpuFreqsCount; ++i) {
110                mCpuFreqs[i] = Long.parseLong(freqStr[i + 1], 10);
111            }
112        }
113        if (callback != null) {
114            callback.onCpuFreqs(mCpuFreqs);
115        }
116    }
117}
118