blob: 7b5b661a61b963efd800dfd39aba0498a5fbfd5b [file] [log] [blame]
Glenn Kasten92183232013-06-18 09:39:15 -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
17#define LOG_TAG "ThreadCpuUsage"
18//#define LOG_NDEBUG 0
19
20#include <errno.h>
21#include <stdlib.h>
22#include <time.h>
23
Glenn Kasten92183232013-06-18 09:39:15 -070024#include <utils/Log.h>
25
26#include <cpustats/ThreadCpuUsage.h>
27
28namespace android {
29
30bool ThreadCpuUsage::setEnabled(bool isEnabled)
31{
32 bool wasEnabled = mIsEnabled;
33 // only do something if there is a change
34 if (isEnabled != wasEnabled) {
35 ALOGV("setEnabled(%d)", isEnabled);
36 int rc;
37 // enabling
38 if (isEnabled) {
39 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &mPreviousTs);
40 if (rc) {
41 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
42 isEnabled = false;
43 } else {
44 mWasEverEnabled = true;
45 // record wall clock time at first enable
46 if (!mMonotonicKnown) {
47 rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
48 if (rc) {
49 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
50 } else {
51 mMonotonicKnown = true;
52 }
53 }
54 }
55 // disabling
56 } else {
57 struct timespec ts;
58 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
59 if (rc) {
60 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
61 } else {
62 long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
63 (ts.tv_nsec - mPreviousTs.tv_nsec);
64 mAccumulator += delta;
65#if 0
66 mPreviousTs = ts;
67#endif
68 }
69 }
70 mIsEnabled = isEnabled;
71 }
72 return wasEnabled;
73}
74
75bool ThreadCpuUsage::sampleAndEnable(double& ns)
76{
Glenn Kasten92183232013-06-18 09:39:15 -070077 bool wasEverEnabled = mWasEverEnabled;
78 if (enable()) {
79 // already enabled, so add a new sample relative to previous
80 return sample(ns);
81 } else if (wasEverEnabled) {
82 // was disabled, but add sample for accumulated time while enabled
83 ns = (double) mAccumulator;
84 mAccumulator = 0;
85 ALOGV("sampleAndEnable %.0f", ns);
86 return true;
87 } else {
88 // first time called
89 ns = 0.0;
90 ALOGV("sampleAndEnable false");
91 return false;
92 }
93}
94
95bool ThreadCpuUsage::sample(double &ns)
96{
97 if (mWasEverEnabled) {
98 if (mIsEnabled) {
99 struct timespec ts;
100 int rc;
101 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
102 if (rc) {
103 ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno);
104 ns = 0.0;
105 return false;
106 } else {
107 long long delta = (ts.tv_sec - mPreviousTs.tv_sec) * 1000000000LL +
108 (ts.tv_nsec - mPreviousTs.tv_nsec);
109 mAccumulator += delta;
110 mPreviousTs = ts;
111 }
112 } else {
113 mWasEverEnabled = false;
114 }
115 ns = (double) mAccumulator;
116 ALOGV("sample %.0f", ns);
117 mAccumulator = 0;
118 return true;
119 } else {
120 ALOGW("Can't add sample because measurements have never been enabled");
121 ns = 0.0;
122 return false;
123 }
124}
125
126long long ThreadCpuUsage::elapsed() const
127{
128 long long elapsed;
129 if (mMonotonicKnown) {
130 struct timespec ts;
131 int rc;
132 rc = clock_gettime(CLOCK_MONOTONIC, &ts);
133 if (rc) {
134 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
135 elapsed = 0;
136 } else {
137 // mMonotonicTs is updated only at first enable and resetStatistics
138 elapsed = (ts.tv_sec - mMonotonicTs.tv_sec) * 1000000000LL +
139 (ts.tv_nsec - mMonotonicTs.tv_nsec);
140 }
141 } else {
142 ALOGW("Can't compute elapsed time because measurements have never been enabled");
143 elapsed = 0;
144 }
145 ALOGV("elapsed %lld", elapsed);
146 return elapsed;
147}
148
149void ThreadCpuUsage::resetElapsed()
150{
151 ALOGV("resetElapsed");
152 if (mMonotonicKnown) {
153 int rc;
154 rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs);
155 if (rc) {
156 ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno);
157 mMonotonicKnown = false;
158 }
159 }
160}
161
162/*static*/
163int ThreadCpuUsage::sScalingFds[ThreadCpuUsage::MAX_CPU];
164pthread_once_t ThreadCpuUsage::sOnceControl = PTHREAD_ONCE_INIT;
165int ThreadCpuUsage::sKernelMax;
166pthread_mutex_t ThreadCpuUsage::sMutex = PTHREAD_MUTEX_INITIALIZER;
167
168/*static*/
169void ThreadCpuUsage::init()
170{
171 // read the number of CPUs
172 sKernelMax = 1;
173 int fd = open("/sys/devices/system/cpu/kernel_max", O_RDONLY);
174 if (fd >= 0) {
175#define KERNEL_MAX_SIZE 12
176 char kernelMax[KERNEL_MAX_SIZE];
177 ssize_t actual = read(fd, kernelMax, sizeof(kernelMax));
178 if (actual >= 2 && kernelMax[actual-1] == '\n') {
179 sKernelMax = atoi(kernelMax);
180 if (sKernelMax >= MAX_CPU - 1) {
181 ALOGW("kernel_max %d but MAX_CPU %d", sKernelMax, MAX_CPU);
182 sKernelMax = MAX_CPU;
183 } else if (sKernelMax < 0) {
184 ALOGW("kernel_max invalid %d", sKernelMax);
185 sKernelMax = 1;
186 } else {
187 ++sKernelMax;
188 ALOGV("number of CPUs %d", sKernelMax);
189 }
190 } else {
191 ALOGW("Can't read number of CPUs");
192 }
193 (void) close(fd);
194 } else {
195 ALOGW("Can't open number of CPUs");
196 }
197 int i;
198 for (i = 0; i < MAX_CPU; ++i) {
199 sScalingFds[i] = -1;
200 }
201}
202
203uint32_t ThreadCpuUsage::getCpukHz(int cpuNum)
204{
205 if (cpuNum < 0 || cpuNum >= MAX_CPU) {
206 ALOGW("getCpukHz called with invalid CPU %d", cpuNum);
207 return 0;
208 }
209 // double-checked locking idiom is not broken for atomic values such as fd
210 int fd = sScalingFds[cpuNum];
211 if (fd < 0) {
212 // some kernels can't open a scaling file until hot plug complete
213 pthread_mutex_lock(&sMutex);
214 fd = sScalingFds[cpuNum];
215 if (fd < 0) {
216#define FREQ_SIZE 64
217 char freq_path[FREQ_SIZE];
218#define FREQ_DIGIT 27
Mark Salyzynfc3afda2014-05-30 13:30:18 -0700219 static_assert(MAX_CPU <= 10, "MAX_CPU too large");
Glenn Kasten92183232013-06-18 09:39:15 -0700220#define FREQ_PATH "/sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq"
221 strlcpy(freq_path, FREQ_PATH, sizeof(freq_path));
222 freq_path[FREQ_DIGIT] = cpuNum + '0';
223 fd = open(freq_path, O_RDONLY | O_CLOEXEC);
224 // keep this fd until process exit or exec
225 sScalingFds[cpuNum] = fd;
226 }
227 pthread_mutex_unlock(&sMutex);
228 if (fd < 0) {
229 ALOGW("getCpukHz can't open CPU %d", cpuNum);
230 return 0;
231 }
232 }
233#define KHZ_SIZE 12
234 char kHz[KHZ_SIZE]; // kHz base 10
235 ssize_t actual = pread(fd, kHz, sizeof(kHz), (off_t) 0);
236 uint32_t ret;
237 if (actual >= 2 && kHz[actual-1] == '\n') {
238 ret = atoi(kHz);
239 } else {
240 ret = 0;
241 }
242 if (ret != mCurrentkHz[cpuNum]) {
243 if (ret > 0) {
244 ALOGV("CPU %d frequency %u kHz", cpuNum, ret);
245 } else {
246 ALOGW("Can't read CPU %d frequency", cpuNum);
247 }
248 mCurrentkHz[cpuNum] = ret;
249 }
250 return ret;
251}
252
253} // namespace android