Skip to content

Commit

Permalink
power_manager: Add nicer API to get and write frequency by index
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejKafka committed Jul 23, 2021
1 parent c2675e5 commit 45e391b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/cpufreq_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ class CpufreqPolicy
"Could not set frequency for `cpufreq` policy '" + name + "'");
}

/** Get the n-th lowest frequency available on this `cpufreq` policy. */
[[nodiscard]] CpuFrequencyHz get_freq(size_t index) const
{
if (!available_frequencies) {
throw runtime_error(
"Cannot get frequency by index for `cpufreq` policy '" + name +
"', because the list of available frequencies is not available (some drivers do not "
"provide it, most notably the `intel_pstate` driver used on Intel processors)");
}
if (index >= available_frequencies->size()) {
throw runtime_error("Tried to get an out-of-bounds frequency #'" +
std::to_string(index) + "' for `cpufreq` policy '" + name +
"' (there are only '" +
std::to_string(available_frequencies->size()) +
"' defined frequencies)");
}
return available_frequencies->at(index);
}

void write_frequency_i(size_t index)
{
if (!active) return;
write_frequency(get_freq(index));
}

private: ///////////////////////////////////////////////////////////////////////////////////////////
static string freq_to_str(CpuFrequencyHz freq)
{
Expand Down

0 comments on commit 45e391b

Please sign in to comment.