blob: 99b4c836394c3200a3f081a29b203c81d008c2b0 [file] [log] [blame]
Glenn Kasten131515b2011-07-08 14:36:46 -07001/*
2 * Copyright (C) 2011 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
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080017#define LOG_TAG "ThreadCpuUsage"
18//#define LOG_NDEBUG 0
19
Glenn Kasten131515b2011-07-08 14:36:46 -070020#include <errno.h>
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080021#include <stdlib.h>
Glenn Kasten131515b2011-07-08 14:36:46 -070022#include <time.h>
23
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080024#include <utils/Debug.h>
Glenn Kasten131515b2011-07-08 14:36:46 -070025#include <utils/Log.h>
26
27#include <cpustats/ThreadCpuUsage.h>
28
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080029namespace android {
30
Glenn Kasten131515b2011-07-08 14:36:46 -070031bool ThreadCpuUsage::setEnabled(bool isEnabled)
32{
33 bool wasEnabled = mIsEnabled;
34 // only do something if there is a change
35 if (isEnabled != wasEnabled) {
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080036 ALOGV("setEnabled(%d)", isEnabled);
Glenn Kasten131515b2011-07-08 14:36:46 -070037 int rc;
38 // enabling
39 if (isEnabled) {
40 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &mPreviousTs);
41 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +000042 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
Glenn Kasten131515b2011-07-08 14:36:46 -070043 isEnabled = false;
44 } else {
45 mWasEverEnabled = true;
46 // record wall clock time at first enable
47 if (!mMonotonicKnown) {
48 rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
49 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +000050 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
Glenn Kasten131515b2011-07-08 14:36:46 -070051 } else {
52 mMonotonicKnown = true;
53 }
54 }
55 }
56 // disabling
57 } else {
58 struct timespec ts;
59 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
60 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +000061 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
Glenn Kasten131515b2011-07-08 14:36:46 -070062 } else {
63 long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
64 (ts.tv_nsec - mPreviousTs.tv_nsec);
65 mAccumulator += delta;
66#if 0
67 mPreviousTs = ts;
68#endif
69 }
70 }
71 mIsEnabled = isEnabled;
72 }
73 return wasEnabled;
74}
75
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080076bool ThreadCpuUsage::sampleAndEnable(double& ns)
Glenn Kasten131515b2011-07-08 14:36:46 -070077{
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080078 bool ret;
Glenn Kasten131515b2011-07-08 14:36:46 -070079 bool wasEverEnabled = mWasEverEnabled;
80 if (enable()) {
81 // already enabled, so add a new sample relative to previous
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080082 return sample(ns);
Glenn Kasten131515b2011-07-08 14:36:46 -070083 } else if (wasEverEnabled) {
84 // was disabled, but add sample for accumulated time while enabled
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080085 ns = (double) mAccumulator;
Glenn Kasten131515b2011-07-08 14:36:46 -070086 mAccumulator = 0;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080087 ALOGV("sampleAndEnable %.0f", ns);
88 return true;
89 } else {
90 // first time called
91 ns = 0.0;
92 ALOGV("sampleAndEnable false");
93 return false;
Glenn Kasten131515b2011-07-08 14:36:46 -070094 }
95}
96
Glenn Kastenf57e2bc2012-03-06 11:27:10 -080097bool ThreadCpuUsage::sample(double &ns)
Glenn Kasten131515b2011-07-08 14:36:46 -070098{
99 if (mWasEverEnabled) {
100 if (mIsEnabled) {
101 struct timespec ts;
102 int rc;
103 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
104 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +0000105 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800106 ns = 0.0;
107 return false;
Glenn Kasten131515b2011-07-08 14:36:46 -0700108 } else {
109 long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
110 (ts.tv_nsec - mPreviousTs.tv_nsec);
111 mAccumulator += delta;
112 mPreviousTs = ts;
113 }
114 } else {
115 mWasEverEnabled = false;
116 }
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800117 ns = (double) mAccumulator;
118 ALOGV("sample %.0f", ns);
Glenn Kasten131515b2011-07-08 14:36:46 -0700119 mAccumulator = 0;
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800120 return true;
Glenn Kasten131515b2011-07-08 14:36:46 -0700121 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000122 ALOGW("Can't add sample because measurements have never been enabled");
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800123 ns = 0.0;
124 return false;
Glenn Kasten131515b2011-07-08 14:36:46 -0700125 }
126}
127
128long long ThreadCpuUsage::elapsed() const
129{
130 long long elapsed;
131 if (mMonotonicKnown) {
132 struct timespec ts;
133 int rc;
134 rc = clock_gettime(CLOCK_MONOTONIC, &ts);
135 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +0000136 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
Glenn Kasten131515b2011-07-08 14:36:46 -0700137 elapsed = 0;
138 } else {
139 // mMonotonicTs is updated only at first enable and resetStatistics
140 elapsed = (ts.tv_sec - mMonotonicTs.tv_sec) * 1000000000LL +
141 (ts.tv_nsec - mMonotonicTs.tv_nsec);
142 }
143 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000144 ALOGW("Can't compute elapsed time because measurements have never been enabled");
Glenn Kasten131515b2011-07-08 14:36:46 -0700145 elapsed = 0;
146 }
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800147 ALOGV("elapsed %lld", elapsed);
Glenn Kasten131515b2011-07-08 14:36:46 -0700148 return elapsed;
149}
150
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800151void ThreadCpuUsage::resetElapsed()
Glenn Kasten131515b2011-07-08 14:36:46 -0700152{
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800153 ALOGV("resetElapsed");
Glenn Kasten131515b2011-07-08 14:36:46 -0700154 if (mMonotonicKnown) {
155 int rc;
156 rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
157 if (rc) {
Steve Block3762c312012-01-06 19:20:56 +0000158 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
Glenn Kasten131515b2011-07-08 14:36:46 -0700159 mMonotonicKnown = false;
160 }
161 }
162}
Glenn Kastenf57e2bc2012-03-06 11:27:10 -0800163
164/*static*/
165int ThreadCpuUsage::sScalingFds[ThreadCpuUsage::MAX_CPU];
166pthread_once_t ThreadCpuUsage::sOnceControl = PTHREAD_ONCE_INIT;
167int ThreadCpuUsage::sKernelMax;
168
169/*static*/
170void ThreadCpuUsage::init()
171{
172 // read the number of CPUs
173 sKernelMax = 1;
174 int fd = open("/sys/devices/system/cpu/kernel_max", O_RDONLY);
175 if (fd >= 0) {
176#define KERNEL_MAX_SIZE 12
177 char kernelMax[KERNEL_MAX_SIZE];
178 ssize_t actual = read(fd, kernelMax, sizeof(kernelMax));
179 if (actual >= 2 && kernelMax[actual-1] == '\n') {
180 sKernelMax = atoi(kernelMax);
181 if (sKernelMax >= MAX_CPU - 1) {
182 ALOGW("kernel_max %d but MAX_CPU %d", sKernelMax, MAX_CPU);
183 sKernelMax = MAX_CPU;
184 } else if (sKernelMax < 0) {
185 ALOGW("kernel_max invalid %d", sKernelMax);
186 sKernelMax = 1;
187 } else {
188 ++sKernelMax;
189 ALOGV("number of CPUs %d", sKernelMax);
190 }
191 } else {
192 ALOGW("Can't read number of CPUs");
193 }
194 (void) close(fd);
195 } else {
196 ALOGW("Can't open number of CPUs");
197 }
198
199 // open fd to each frequency per CPU
200#define FREQ_SIZE 64
201 char freq_path[FREQ_SIZE];
202#define FREQ_DIGIT 27
203 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(MAX_CPU <= 10);
204 strlcpy(freq_path, "/sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq", sizeof(freq_path));
205 int i;
206 for (i = 0; i < MAX_CPU; ++i) {
207 sScalingFds[i] = -1;
208 }
209 for (i = 0; i < sKernelMax; ++i) {
210 freq_path[FREQ_DIGIT] = i + '0';
211 fd = open(freq_path, O_RDONLY);
212 if (fd >= 0) {
213 // keep this fd until process exit
214 sScalingFds[i] = fd;
215 } else {
216 ALOGW("Can't open CPU %d", i);
217 }
218 }
219}
220
221uint32_t ThreadCpuUsage::getCpukHz(int cpuNum)
222{
223 if (cpuNum < 0 || cpuNum >= MAX_CPU) {
224 ALOGW("getCpukHz called with invalid CPU %d", cpuNum);
225 return 0;
226 }
227 int fd = sScalingFds[cpuNum];
228 if (fd < 0) {
229 ALOGW("getCpukHz called for unopened CPU %d", cpuNum);
230 return 0;
231 }
232#define KHZ_SIZE 12
233 char kHz[KHZ_SIZE]; // kHz base 10
234 ssize_t actual = pread(fd, kHz, sizeof(kHz), (off_t) 0);
235 uint32_t ret;
236 if (actual >= 2 && kHz[actual-1] == '\n') {
237 ret = atoi(kHz);
238 } else {
239 ret = 0;
240 }
241 if (ret != mCurrentkHz[cpuNum]) {
242 if (ret > 0) {
243 ALOGV("CPU %d frequency %u kHz", cpuNum, ret);
244 } else {
245 ALOGW("Can't read CPU %d frequency", cpuNum);
246 }
247 mCurrentkHz[cpuNum] = ret;
248 }
249 return ret;
250}
251
252} // namespace android