| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreqprovider.h" |
| 5 | |
| 6 | #include "common/fileutils.h" |
| 7 | #include "common/stringutils.h" |
| 8 | #include "core/info/icpuinfo.h" |
| 9 | #include "core/sysfsdatasource.h" |
| 10 | #include "cpufreq.h" |
| 11 | #include "cpufreqmodeprovider.h" |
| 12 | #include "handlers/epphandler.h" |
| 13 | #include <algorithm> |
| 14 | #include <filesystem> |
| 15 | #include <utility> |
| 16 | |
| 17 | std::vector<std::unique_ptr<IControl>> |
| 18 | CPUFreqProvider::provideCPUControls(ICPUInfo const &cpuInfo, ISWInfo const &) const |
| 19 | { |
| 20 | if (!Utils::File::isDirectoryPathValid("/sys/devices/system/cpu/cpufreq")) |
| 21 | return {}; |
| 22 | |
| 23 | auto &executionUnits = cpuInfo.executionUnits(); |
| 24 | if (executionUnits.empty()) |
| 25 | return {}; |
| 26 | |
| 27 | auto governors = availableGovernors(cpuInfo); |
| 28 | if (governors.empty()) |
| 29 | return {}; |
| 30 | |
| 31 | auto governor = defaultGovernor(cpuInfo, governors); |
| 32 | auto scalingGovernorDataSources = createScalingGovernorDataSources(cpuInfo); |
| 33 | |
| 34 | if (scalingGovernorDataSources.empty()) |
| 35 | return {}; |
| 36 | |
| 37 | std::vector<std::unique_ptr<IControl>> controls; |
| 38 | controls.emplace_back(std::make_unique<CPUFreq>( |
| 39 | std::move(governors), governor, std::move(scalingGovernorDataSources), |
| 40 | createEPPHandler(cpuInfo))); |
| 41 | |
| 42 | return controls; |
| 43 | } |
| 44 | |
| 45 | std::vector<std::string> |
| 46 | CPUFreqProvider::availableGovernors(ICPUInfo const &cpuInfo) const |
| 47 | { |
| 48 | std::string availableGovernorsPath{"cpufreq/scaling_available_governors"}; |
| 49 | |
| 50 | // get available governors from the first execution unit |
| 51 | auto unitAvailableGovernorsPath = cpuInfo.executionUnits().front().sysPath / |
| 52 | availableGovernorsPath; |
| 53 | |
| 54 | if (!Utils::File::isSysFSEntryValid(unitAvailableGovernorsPath)) |
| 55 | return {}; |
| 56 | |
| 57 | auto lines = Utils::File::readFileLines(unitAvailableGovernorsPath); |
| 58 | auto governors = Utils::String::split(lines.front()); |
| 59 | |
| 60 | // remove not implemented userspace option (see #175) |
| 61 | std::erase(governors, "userspace"); |
| 62 | |
| 63 | return governors; |
| 64 | } |
| 65 | |
| 66 | std::string CPUFreqProvider::defaultGovernor( |
| 67 | ICPUInfo const &cpuInfo, std::vector<std::string> const &governors) const |
| 68 | { |
| 69 | std::string scalingDriverPath{"cpufreq/scaling_driver"}; |
| 70 | std::string fallbackGovernor = governors.front(); |
| 71 | |
| 72 | // get scaling driver from the first execution unit |
| 73 | auto unitScalingDriverPath = cpuInfo.executionUnits().front().sysPath / |
| 74 | scalingDriverPath; |
| 75 | if (!Utils::File::isSysFSEntryValid(unitScalingDriverPath)) |
| 76 | return fallbackGovernor; |
| 77 | |
| 78 | auto lines = Utils::File::readFileLines(unitScalingDriverPath); |
| 79 | if (lines.empty()) |
| 80 | return fallbackGovernor; |
| 81 | |
| 82 | std::string governor("ondemand"); |
| 83 | |
| 84 | auto driver = lines.front(); |
| 85 | if (driver == "intel_pstate") |
| 86 | governor = "powersave"; |
| 87 | |
| 88 | // clamp governor into available governors |
| 89 | auto iter = std::find_if(governors.cbegin(), governors.cend(), |
| 90 | [&](auto const &availableGovernor) { |
| 91 | return governor == availableGovernor; |
| 92 | }); |
| 93 | if (iter == governors.cend()) |
| 94 | return fallbackGovernor; |
| 95 | |
| 96 | return governor; |
| 97 | } |
| 98 | |
| 99 | std::vector<std::unique_ptr<IDataSource<std::string>>> |
| 100 | CPUFreqProvider::createScalingGovernorDataSources(ICPUInfo const &cpuInfo) const |
| 101 | { |
| 102 | std::vector<std::unique_ptr<IDataSource<std::string>>> scalingGovernorDataSources; |
| 103 | |
| 104 | std::string scalingGovernorPath{"cpufreq/scaling_governor"}; |
| 105 | for (auto const &executionUnit : cpuInfo.executionUnits()) { |
| 106 | |
| 107 | auto unitScalingGovernorPath = executionUnit.sysPath / scalingGovernorPath; |
| 108 | if (!Utils::File::isSysFSEntryValid(unitScalingGovernorPath)) |
| 109 | continue; |
| 110 | |
| 111 | scalingGovernorDataSources.emplace_back( |
| 112 | std::make_unique<SysFSDataSource<std::string>>(unitScalingGovernorPath)); |
| 113 | } |
| 114 | |
| 115 | return scalingGovernorDataSources; |
| 116 | } |
| 117 | |
| 118 | std::unique_ptr<IEPPHandler> |
| 119 | CPUFreqProvider::createEPPHandler(ICPUInfo const &cpuInfo) const |
| 120 | { |
| 121 | auto avaiableEPPHintsDataSource = createAvailableHintsDataSource(cpuInfo); |
| 122 | if (!avaiableEPPHintsDataSource) |
| 123 | return {}; |
| 124 | |
| 125 | auto eppHintDataSources = createHintDataSources(cpuInfo); |
| 126 | if (eppHintDataSources.empty()) |
| 127 | return {}; |
| 128 | |
| 129 | return std::make_unique<EPPHandler>(std::move(avaiableEPPHintsDataSource), |
| 130 | std::move(eppHintDataSources)); |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<IDataSource<std::string>> |
| 134 | CPUFreqProvider::createAvailableHintsDataSource(ICPUInfo const &cpuInfo) const |
| 135 | { |
| 136 | std::string availableGovernorsPath{ |
| 137 | "cpufreq/energy_performance_available_preferences"}; |
| 138 | |
| 139 | // available hints will be read from the first execution unit |
| 140 | auto availableHintsPath = cpuInfo.executionUnits().front().sysPath / |
| 141 | availableGovernorsPath; |
| 142 | |
| 143 | if (!Utils::File::isSysFSEntryValid(availableHintsPath)) |
| 144 | return {}; |
| 145 | |
| 146 | return std::make_unique<SysFSDataSource<std::string>>(availableHintsPath); |
| 147 | } |
| 148 | |
| 149 | std::vector<std::unique_ptr<IDataSource<std::string>>> |
| 150 | CPUFreqProvider::createHintDataSources(ICPUInfo const &cpuInfo) const |
| 151 | { |
| 152 | std::vector<std::unique_ptr<IDataSource<std::string>>> hintDataSources; |
| 153 | |
| 154 | std::string hintPath{"cpufreq/energy_performance_preference"}; |
| 155 | for (auto const &executionUnit : cpuInfo.executionUnits()) { |
| 156 | |
| 157 | auto unitHintPath = executionUnit.sysPath / hintPath; |
| 158 | if (!Utils::File::isSysFSEntryValid(unitHintPath)) |
| 159 | continue; |
| 160 | |
| 161 | hintDataSources.emplace_back( |
| 162 | std::make_unique<SysFSDataSource<std::string>>(unitHintPath)); |
| 163 | } |
| 164 | |
| 165 | return hintDataSources; |
| 166 | } |
| 167 | |
| 168 | bool const CPUFreqProvider::registered_ = |
| 169 | CPUFreqModeProvider::registerProvider(std::make_unique<CPUFreqProvider>()); |