Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 17 | #define LOG_TAG "ThreadCpuUsage" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 22 | #include <time.h> |
| 23 | |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 24 | #include <utils/Debug.h> |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <cpustats/ThreadCpuUsage.h> |
| 28 | |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 31 | bool ThreadCpuUsage::setEnabled(bool isEnabled) |
| 32 | { |
| 33 | bool wasEnabled = mIsEnabled; |
| 34 | // only do something if there is a change |
| 35 | if (isEnabled != wasEnabled) { |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 36 | ALOGV("setEnabled(%d)", isEnabled); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 37 | int rc; |
| 38 | // enabling |
| 39 | if (isEnabled) { |
| 40 | rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &mPreviousTs); |
| 41 | if (rc) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 42 | ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 43 | 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 50 | ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 51 | } 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 61 | ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 62 | } 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 Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 76 | bool ThreadCpuUsage::sampleAndEnable(double& ns) |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 77 | { |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 78 | bool ret; |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 79 | bool wasEverEnabled = mWasEverEnabled; |
| 80 | if (enable()) { |
| 81 | // already enabled, so add a new sample relative to previous |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 82 | return sample(ns); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 83 | } else if (wasEverEnabled) { |
| 84 | // was disabled, but add sample for accumulated time while enabled |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 85 | ns = (double) mAccumulator; |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 86 | mAccumulator = 0; |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 87 | 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 Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 97 | bool ThreadCpuUsage::sample(double &ns) |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 98 | { |
| 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 105 | ALOGE("clock_gettime(CLOCK_THREAD_CPUTIME_ID) errno=%d", errno); |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 106 | ns = 0.0; |
| 107 | return false; |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 108 | } 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 Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 117 | ns = (double) mAccumulator; |
| 118 | ALOGV("sample %.0f", ns); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 119 | mAccumulator = 0; |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 120 | return true; |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 121 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 122 | ALOGW("Can't add sample because measurements have never been enabled"); |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 123 | ns = 0.0; |
| 124 | return false; |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | long 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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 136 | ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 137 | 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 144 | ALOGW("Can't compute elapsed time because measurements have never been enabled"); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 145 | elapsed = 0; |
| 146 | } |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 147 | ALOGV("elapsed %lld", elapsed); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 148 | return elapsed; |
| 149 | } |
| 150 | |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 151 | void ThreadCpuUsage::resetElapsed() |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 152 | { |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 153 | ALOGV("resetElapsed"); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 154 | if (mMonotonicKnown) { |
| 155 | int rc; |
| 156 | rc = clock_gettime(CLOCK_MONOTONIC, &mMonotonicTs); |
| 157 | if (rc) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 158 | ALOGE("clock_gettime(CLOCK_MONOTONIC) errno=%d", errno); |
Glenn Kasten | 131515b | 2011-07-08 14:36:46 -0700 | [diff] [blame] | 159 | mMonotonicKnown = false; |
| 160 | } |
| 161 | } |
| 162 | } |
Glenn Kasten | f57e2bc | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 163 | |
| 164 | /*static*/ |
| 165 | int ThreadCpuUsage::sScalingFds[ThreadCpuUsage::MAX_CPU]; |
| 166 | pthread_once_t ThreadCpuUsage::sOnceControl = PTHREAD_ONCE_INIT; |
| 167 | int ThreadCpuUsage::sKernelMax; |
| 168 | |
| 169 | /*static*/ |
| 170 | void 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 | |
| 221 | uint32_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 |