summaryrefslogtreecommitdiffstats
path: root/libs
diff options
context:
space:
mode:
authorGlenn Kasten <gkasten@google.com>2012-05-15 11:37:41 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2012-05-15 11:37:41 -0700
commit8d057994a0061f56614ff020a2af1d59c894a7d0 (patch)
treed1b99f117796fa66b7fb76f8d3fbf53f63203e62 /libs
parent15e23859ba8dc3d64eb384b72521a834c86648dd (diff)
parentbd1132249479e52ddd128171da24327358e2191c (diff)
downloadframeworks_native-8d057994a0061f56614ff020a2af1d59c894a7d0.zip
frameworks_native-8d057994a0061f56614ff020a2af1d59c894a7d0.tar.gz
frameworks_native-8d057994a0061f56614ff020a2af1d59c894a7d0.tar.bz2
am bd113224: Merge "Fix log spam about CPU frequency on one device" into jb-dev
* commit 'bd1132249479e52ddd128171da24327358e2191c': Fix log spam about CPU frequency on one device
Diffstat (limited to 'libs')
-rw-r--r--libs/cpustats/ThreadCpuUsage.cpp41
1 files changed, 22 insertions, 19 deletions
diff --git a/libs/cpustats/ThreadCpuUsage.cpp b/libs/cpustats/ThreadCpuUsage.cpp
index 99b4c83..637402a 100644
--- a/libs/cpustats/ThreadCpuUsage.cpp
+++ b/libs/cpustats/ThreadCpuUsage.cpp
@@ -165,6 +165,7 @@ void ThreadCpuUsage::resetElapsed()
int ThreadCpuUsage::sScalingFds[ThreadCpuUsage::MAX_CPU];
pthread_once_t ThreadCpuUsage::sOnceControl = PTHREAD_ONCE_INIT;
int ThreadCpuUsage::sKernelMax;
+pthread_mutex_t ThreadCpuUsage::sMutex = PTHREAD_MUTEX_INITIALIZER;
/*static*/
void ThreadCpuUsage::init()
@@ -195,27 +196,10 @@ void ThreadCpuUsage::init()
} else {
ALOGW("Can't open number of CPUs");
}
-
- // open fd to each frequency per CPU
-#define FREQ_SIZE 64
- char freq_path[FREQ_SIZE];
-#define FREQ_DIGIT 27
- COMPILE_TIME_ASSERT_FUNCTION_SCOPE(MAX_CPU <= 10);
- strlcpy(freq_path, "/sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq", sizeof(freq_path));
int i;
for (i = 0; i < MAX_CPU; ++i) {
sScalingFds[i] = -1;
}
- for (i = 0; i < sKernelMax; ++i) {
- freq_path[FREQ_DIGIT] = i + '0';
- fd = open(freq_path, O_RDONLY);
- if (fd >= 0) {
- // keep this fd until process exit
- sScalingFds[i] = fd;
- } else {
- ALOGW("Can't open CPU %d", i);
- }
- }
}
uint32_t ThreadCpuUsage::getCpukHz(int cpuNum)
@@ -224,10 +208,29 @@ uint32_t ThreadCpuUsage::getCpukHz(int cpuNum)
ALOGW("getCpukHz called with invalid CPU %d", cpuNum);
return 0;
}
+ // double-checked locking idiom is not broken for atomic values such as fd
int fd = sScalingFds[cpuNum];
if (fd < 0) {
- ALOGW("getCpukHz called for unopened CPU %d", cpuNum);
- return 0;
+ // some kernels can't open a scaling file until hot plug complete
+ pthread_mutex_lock(&sMutex);
+ fd = sScalingFds[cpuNum];
+ if (fd < 0) {
+#define FREQ_SIZE 64
+ char freq_path[FREQ_SIZE];
+#define FREQ_DIGIT 27
+ COMPILE_TIME_ASSERT_FUNCTION_SCOPE(MAX_CPU <= 10);
+#define FREQ_PATH "/sys/devices/system/cpu/cpu?/cpufreq/scaling_cur_freq"
+ strlcpy(freq_path, FREQ_PATH, sizeof(freq_path));
+ freq_path[FREQ_DIGIT] = cpuNum + '0';
+ fd = open(freq_path, O_RDONLY | O_CLOEXEC);
+ // keep this fd until process exit or exec
+ sScalingFds[cpuNum] = fd;
+ }
+ pthread_mutex_unlock(&sMutex);
+ if (fd < 0) {
+ ALOGW("getCpukHz can't open CPU %d", cpuNum);
+ return 0;
+ }
}
#define KHZ_SIZE 12
char kHz[KHZ_SIZE]; // kHz base 10