Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / tools / profiler / core / ProfilerCPUFreq-linux-android.cpp
blobc7efbbd75fb3606c5232f6baa9ad41886b1cc67d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "ProfilerCPUFreq.h"
6 #include "nsThreadUtils.h"
7 #include <fcntl.h>
8 #include <unistd.h>
10 ProfilerCPUFreq::ProfilerCPUFreq() {
11 if (!mCPUCounters.resize(mozilla::GetNumberOfProcessors())) {
12 NS_WARNING("failing to resize the mCPUCounters vector");
13 return;
16 for (unsigned cpuId = 0; cpuId < mCPUCounters.length(); ++cpuId) {
17 const size_t buf_sz = 64;
18 char buf[buf_sz];
19 int rv = sprintf(
20 buf, "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq", cpuId);
21 if (NS_WARN_IF(rv < 0)) {
22 continue;
25 int fd = open(buf, O_RDONLY);
26 if (NS_WARN_IF(!fd)) {
27 continue;
30 mCPUCounters[cpuId].fd = fd;
34 ProfilerCPUFreq::~ProfilerCPUFreq() {
35 for (CPUCounterInfo& CPUCounter : mCPUCounters) {
36 int fd = CPUCounter.fd;
37 if (NS_WARN_IF(!fd)) {
38 continue;
40 close(fd);
41 CPUCounter.fd = 0;
45 uint32_t ProfilerCPUFreq::GetCPUSpeedMHz(unsigned cpuId) {
46 MOZ_ASSERT(cpuId < mCPUCounters.length());
47 int fd = mCPUCounters[cpuId].fd;
48 if (NS_WARN_IF(!fd)) {
49 return 0;
52 long rv = lseek(fd, 0, SEEK_SET);
53 if (NS_WARN_IF(rv < 0)) {
54 return 0;
57 const size_t buf_sz = 64;
58 char buf[buf_sz];
59 rv = read(fd, buf, buf_sz);
60 if (NS_WARN_IF(rv < 0)) {
61 return 0;
64 int cpufreq = 0;
65 rv = sscanf(buf, "%u", &cpufreq);
66 if (NS_WARN_IF(rv != 1)) {
67 return 0;
70 // Convert kHz to MHz, rounding to the nearst 10Mhz, to ignore tiny
71 // variations that are likely due to rounding errors.
72 return uint32_t(cpufreq / 10000) * 10;