tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AndroidHeterogeneousCpuInfo.cpp (2799B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      6 
      7 #include "Hal.h"
      8 #include "HalLog.h"
      9 #include "HalTypes.h"
     10 
     11 #include "mozilla/CheckedInt.h"
     12 #include "prsystem.h"
     13 #include <fstream>
     14 
     15 namespace mozilla::hal_impl {
     16 
     17 using HeterogeneousCpuInfo = hal::HeterogeneousCpuInfo;
     18 
     19 mozilla::Maybe<HeterogeneousCpuInfo> CreateHeterogeneousCpuInfo() {
     20  CheckedInt<size_t> cpuCount = PR_GetNumberOfProcessors();
     21  if (!cpuCount.isValid()) {
     22    HAL_ERR("HeterogeneousCpuInfo: Failed to query processor count");
     23    return Nothing();
     24  }
     25 
     26  if (cpuCount.value() > HeterogeneousCpuInfo::MAX_CPUS) {
     27    HAL_ERR("HeterogeneousCpuInfo: Too many cpus");
     28    return Nothing();
     29  }
     30 
     31  std::vector<std::pair<int, int>> cpu_freqs;
     32  cpu_freqs.reserve(cpuCount.value());
     33  for (size_t i = 0; i < cpuCount.value(); i++) {
     34    std::stringstream freq_filename;
     35    // On all devices tested, even with isolated content processes we do have
     36    // permission to read this file. If, however, this function stops working
     37    // one day, then this may be a good place to start investigating.
     38    freq_filename << "/sys/devices/system/cpu/cpu" << i
     39                  << "/cpufreq/cpuinfo_max_freq";
     40    std::ifstream ifs(freq_filename.str());
     41    if (!ifs) {
     42      HAL_ERR("HeterogeneousCpuInfo: Failed to open file %s",
     43              freq_filename.str().c_str());
     44      return Nothing();
     45    }
     46    int freq;
     47    if (!(ifs >> freq)) {
     48      HAL_ERR("HeterogeneousCpuInfo: Failed to parse file %s",
     49              freq_filename.str().c_str());
     50      return Nothing();
     51    }
     52    cpu_freqs.push_back(std::make_pair(i, freq));
     53  }
     54 
     55  std::sort(cpu_freqs.begin(), cpu_freqs.end(),
     56            [](auto lhs, auto rhs) { return lhs.second < rhs.second; });
     57 
     58  HeterogeneousCpuInfo info;
     59  info.mTotalNumCpus = cpuCount.value();
     60 
     61  int lowest_freq = cpu_freqs.front().second;
     62  int highest_freq = cpu_freqs.back().second;
     63  for (const auto& [cpu, freq] : cpu_freqs) {
     64    if (freq == highest_freq) {
     65      info.mBigCpus[cpu] = true;
     66    } else if (freq == lowest_freq) {
     67      info.mLittleCpus[cpu] = true;
     68    } else {
     69      info.mMediumCpus[cpu] = true;
     70    }
     71  }
     72 
     73  HAL_LOG("HeterogeneousCpuInfo: little: %zu, med: %zu, big: %zu",
     74          info.mLittleCpus.Count(), info.mMediumCpus.Count(),
     75          info.mBigCpus.Count());
     76 
     77  return Some(info);
     78 }
     79 
     80 const Maybe<HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
     81  static const Maybe<HeterogeneousCpuInfo> cpuInfo =
     82      CreateHeterogeneousCpuInfo();
     83  return cpuInfo;
     84 }
     85 
     86 }  // namespace mozilla::hal_impl