1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_POWER_MONITOR_CPU_FREQUENCY_UTILS_H_ 6 #define BASE_POWER_MONITOR_CPU_FREQUENCY_UTILS_H_ 7 8 #include <optional> 9 10 #include "base/base_export.h" 11 #include "base/time/time.h" 12 #include "base/values.h" 13 14 namespace base { 15 16 struct BASE_EXPORT CpuFrequencyInfo { 17 // These fields map to fields in the win32 PROCESSOR_POWER_INFORMATION 18 // struct. They are currently only implemented on Windows since that's 19 // the platform being investigated, but they could be replaced with 20 // something more generic/cross-platform as needed. 21 22 // The maximum frequency or the frequency limit of a CPU. 23 unsigned long max_mhz; 24 unsigned long mhz_limit; 25 26 enum class CoreType { 27 kPerformance, 28 kBalanced, 29 kEfficiency, 30 }; 31 32 // A best effort guess at whether the associated CPU core is a performance 33 // core, an efficiency core, or something in between (balanced). 34 CoreType type; 35 36 // The number of CPU cores that are in the C0 state (active). 37 size_t num_active_cpus; 38 }; 39 40 struct BASE_EXPORT CpuThroughputEstimationResult { 41 // The estimated CPU frequency of the current core, in Hz. 42 double estimated_frequency; 43 // True if the current core is different after the estimation loop than 44 // before. 45 bool migrated; 46 47 // The wall time and thread time of the CPU estimation task's assembly loop. 48 base::TimeDelta wall_time; 49 base::TimeDelta thread_time; 50 }; 51 52 // Returns the estimated CPU frequency of the current core by executing a tight 53 // loop of predictable assembly instructions. The estimated frequency should be 54 // proportional to and about the same magnitude as the real CPU frequency, 55 // although it is possible for the code to be migrated/descheduled during the 56 // execution of this function. The measurement should be long enough to avoid 57 // Turbo Boost effect (~3ms) and be low enough to stay within the operating 58 // system scheduler quantum (~100ms). 59 // The return value is the estimated CPU frequency, in Hz. 60 BASE_EXPORT double EstimateCpuFrequency(); 61 BASE_EXPORT std::optional<CpuThroughputEstimationResult> 62 EstimateCpuThroughput(); 63 64 // Populates and returns a `CpuFrequencyInfo` struct with information from the 65 // current CPU core. 66 BASE_EXPORT CpuFrequencyInfo GetCpuFrequencyInfo(); 67 68 #if BUILDFLAG(IS_WIN) 69 BASE_EXPORT void GenerateCpuInfoForTracingMetadata(base::Value::Dict* metadata); 70 #endif 71 } // namespace base 72 73 #endif // BASE_POWER_MONITOR_CPU_FREQUENCY_UTILS_H_ 74