1921832327619f7852b16f73a19504702c5a28a31Glenn Kasten/* 2921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * Copyright (C) 2011 The Android Open Source Project 3921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * 4921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License"); 5921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * you may not use this file except in compliance with the License. 6921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * You may obtain a copy of the License at 7921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * 8921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * http://www.apache.org/licenses/LICENSE-2.0 9921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * 10921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * Unless required by applicable law or agreed to in writing, software 11921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS, 12921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * See the License for the specific language governing permissions and 14921832327619f7852b16f73a19504702c5a28a31Glenn Kasten * limitations under the License. 15921832327619f7852b16f73a19504702c5a28a31Glenn Kasten */ 16921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 17921832327619f7852b16f73a19504702c5a28a31Glenn Kasten#ifndef _THREAD_CPU_USAGE_H 18921832327619f7852b16f73a19504702c5a28a31Glenn Kasten#define _THREAD_CPU_USAGE_H 19921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 20921832327619f7852b16f73a19504702c5a28a31Glenn Kasten#include <fcntl.h> 21921832327619f7852b16f73a19504702c5a28a31Glenn Kasten#include <pthread.h> 22921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 23921832327619f7852b16f73a19504702c5a28a31Glenn Kastennamespace android { 24921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 25921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// Track CPU usage for the current thread. 26921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// Units are in per-thread CPU ns, as reported by 27921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// clock_gettime(CLOCK_THREAD_CPUTIME_ID). Simple usage: for cyclic 28921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// threads where you want to measure the execution time of the whole 29921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// cycle, just call sampleAndEnable() at the start of each cycle. 30921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// For acyclic threads, or for cyclic threads where you want to measure/track 31921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// only part of each cycle, call enable(), disable(), and/or setEnabled() 32921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// to demarcate the region(s) of interest, and then call sample() periodically. 33921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// This class is not thread-safe for concurrent calls from multiple threads; 34921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// the methods of this class may only be called by the current thread 35921832327619f7852b16f73a19504702c5a28a31Glenn Kasten// which constructed the object. 36921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 37921832327619f7852b16f73a19504702c5a28a31Glenn Kastenclass ThreadCpuUsage 38921832327619f7852b16f73a19504702c5a28a31Glenn Kasten{ 39921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 40921832327619f7852b16f73a19504702c5a28a31Glenn Kastenpublic: 41921832327619f7852b16f73a19504702c5a28a31Glenn Kasten ThreadCpuUsage() : 42921832327619f7852b16f73a19504702c5a28a31Glenn Kasten mIsEnabled(false), 43921832327619f7852b16f73a19504702c5a28a31Glenn Kasten mWasEverEnabled(false), 44921832327619f7852b16f73a19504702c5a28a31Glenn Kasten mAccumulator(0), 45921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // mPreviousTs 46921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // mMonotonicTs 47921832327619f7852b16f73a19504702c5a28a31Glenn Kasten mMonotonicKnown(false) 48921832327619f7852b16f73a19504702c5a28a31Glenn Kasten { 49921832327619f7852b16f73a19504702c5a28a31Glenn Kasten (void) pthread_once(&sOnceControl, &init); 50921832327619f7852b16f73a19504702c5a28a31Glenn Kasten for (int i = 0; i < sKernelMax; ++i) { 51921832327619f7852b16f73a19504702c5a28a31Glenn Kasten mCurrentkHz[i] = (uint32_t) ~0; // unknown 52921832327619f7852b16f73a19504702c5a28a31Glenn Kasten } 53921832327619f7852b16f73a19504702c5a28a31Glenn Kasten } 54921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 55921832327619f7852b16f73a19504702c5a28a31Glenn Kasten ~ThreadCpuUsage() { } 56921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 57921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Return whether currently tracking CPU usage by current thread 58921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool isEnabled() const { return mIsEnabled; } 59921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 60921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Enable tracking of CPU usage by current thread; 61921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // any CPU used from this point forward will be tracked. 62921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Returns the previous enabled status. 63921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool enable() { return setEnabled(true); } 64921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 65921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Disable tracking of CPU usage by current thread; 66921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // any CPU used from this point forward will be ignored. 67921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Returns the previous enabled status. 68921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool disable() { return setEnabled(false); } 69921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 70921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Set the enabled status and return the previous enabled status. 71921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // This method is intended to be used for safe nested enable/disabling. 72921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool setEnabled(bool isEnabled); 73921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 74921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Add a sample point, and also enable tracking if needed. 75921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // If tracking has never been enabled, then this call enables tracking but 76921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // does _not_ add a sample -- it is not possible to add a sample the 77921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // first time because there is no previous point to subtract from. 78921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Otherwise, if tracking is enabled, 79921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // then adds a sample for tracked CPU ns since the previous 80921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // sample, or since the first call to sampleAndEnable(), enable(), or 81921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // setEnabled(true). If there was a previous sample but tracking is 82921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // now disabled, then adds a sample for the tracked CPU ns accumulated 83921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // up until the most recent disable(), resets this accumulator, and then 84921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // enables tracking. Calling this method rather than enable() followed 85921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // by sample() avoids a race condition for the first sample. 86921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Returns true if the sample 'ns' is valid, or false if invalid. 87921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Note that 'ns' is an output parameter passed by reference. 88921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // The caller does not need to initialize this variable. 89921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // The units are CPU nanoseconds consumed by current thread. 90921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool sampleAndEnable(double& ns); 91921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 92921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Add a sample point, but do not 93921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // change the tracking enabled status. If tracking has either never been 94921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // enabled, or has never been enabled since the last sample, then log a warning 95921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // and don't add sample. Otherwise, adds a sample for tracked CPU ns since 96921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // the previous sample or since the first call to sampleAndEnable(), 97921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // enable(), or setEnabled(true) if no previous sample. 98921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Returns true if the sample is valid, or false if invalid. 99921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Note that 'ns' is an output parameter passed by reference. 100921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // The caller does not need to initialize this variable. 101921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // The units are CPU nanoseconds consumed by current thread. 102921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool sample(double& ns); 103921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 104921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Return the elapsed delta wall clock ns since initial enable or reset, 105921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // as reported by clock_gettime(CLOCK_MONOTONIC). 106921832327619f7852b16f73a19504702c5a28a31Glenn Kasten long long elapsed() const; 107921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 108921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Reset elapsed wall clock. Has no effect on tracking or accumulator. 109921832327619f7852b16f73a19504702c5a28a31Glenn Kasten void resetElapsed(); 110921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 111921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Return current clock frequency for specified CPU, in kHz. 112921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // You can get your CPU number using sched_getcpu(2). Note that, unless CPU affinity 113921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // has been configured appropriately, the CPU number can change. 114921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // Also note that, unless the CPU governor has been configured appropriately, 115921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // the CPU frequency can change. And even if the CPU frequency is locked down 116921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // to a particular value, that the frequency might still be adjusted 117921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // to prevent thermal overload. Therefore you should poll for your thread's 118921832327619f7852b16f73a19504702c5a28a31Glenn Kasten // current CPU number and clock frequency periodically. 119921832327619f7852b16f73a19504702c5a28a31Glenn Kasten uint32_t getCpukHz(int cpuNum); 120921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 121921832327619f7852b16f73a19504702c5a28a31Glenn Kastenprivate: 122921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool mIsEnabled; // whether tracking is currently enabled 123921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool mWasEverEnabled; // whether tracking was ever enabled 124921832327619f7852b16f73a19504702c5a28a31Glenn Kasten long long mAccumulator; // accumulated thread CPU time since last sample, in ns 125921832327619f7852b16f73a19504702c5a28a31Glenn Kasten struct timespec mPreviousTs; // most recent thread CPU time, valid only if mIsEnabled is true 126921832327619f7852b16f73a19504702c5a28a31Glenn Kasten struct timespec mMonotonicTs; // most recent monotonic time 127921832327619f7852b16f73a19504702c5a28a31Glenn Kasten bool mMonotonicKnown; // whether mMonotonicTs has been set 128921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 129921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static const int MAX_CPU = 8; 130921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static int sScalingFds[MAX_CPU];// file descriptor per CPU for reading scaling_cur_freq 131921832327619f7852b16f73a19504702c5a28a31Glenn Kasten uint32_t mCurrentkHz[MAX_CPU]; // current CPU frequency in kHz, not static to avoid a race 132921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static pthread_once_t sOnceControl; 133921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static int sKernelMax; // like MAX_CPU, but determined at runtime == cpu/kernel_max + 1 134921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static void init(); // called once at first ThreadCpuUsage construction 135921832327619f7852b16f73a19504702c5a28a31Glenn Kasten static pthread_mutex_t sMutex; // protects sScalingFds[] after initialization 136921832327619f7852b16f73a19504702c5a28a31Glenn Kasten}; 137921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 138921832327619f7852b16f73a19504702c5a28a31Glenn Kasten} // namespace android 139921832327619f7852b16f73a19504702c5a28a31Glenn Kasten 140921832327619f7852b16f73a19504702c5a28a31Glenn Kasten#endif // _THREAD_CPU_USAGE_H 141