Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 1 | #include <fmt/core.h> |
| 2 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 4 | #include <powercap.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 5 | |
| 6 | #include <cassert> |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 7 | #include <regex> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 8 | |
| 9 | namespace open_power |
| 10 | { |
| 11 | namespace occ |
| 12 | { |
| 13 | namespace powercap |
| 14 | { |
| 15 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 16 | constexpr auto PCAP_PATH = "/xyz/openbmc_project/control/host0/power_cap"; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 17 | constexpr auto PCAP_INTERFACE = "xyz.openbmc_project.Control.Power.Cap"; |
| 18 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 19 | constexpr auto POWER_CAP_PROP = "PowerCap"; |
| 20 | constexpr auto POWER_CAP_ENABLE_PROP = "PowerCapEnable"; |
| 21 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 22 | using namespace phosphor::logging; |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 25 | uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled) |
| 26 | { |
| 27 | if (!pcapEnabled) |
| 28 | { |
| 29 | // Pcap disabled, return 0 to indicate disabled |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | // If pcap is not disabled then just return the pcap with the derating |
| 34 | // factor applied. |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 35 | return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100); |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 38 | uint32_t PowerCap::getPcap() |
| 39 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 40 | utils::PropertyValue pcap{}; |
| 41 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 42 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 43 | pcap = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP); |
| 44 | |
| 45 | return std::get<uint32_t>(pcap); |
| 46 | } |
Patrick Williams | 2561362 | 2021-09-02 09:29:54 -0500 | [diff] [blame] | 47 | catch (const sdbusplus::exception::exception& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 48 | { |
| 49 | log<level::ERR>("Failed to get PowerCap property", |
| 50 | entry("ERROR=%s", e.what()), |
| 51 | entry("PATH=%s", PCAP_PATH)); |
| 52 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 53 | return 0; |
| 54 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool PowerCap::getPcapEnabled() |
| 58 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 59 | utils::PropertyValue pcapEnabled{}; |
| 60 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 61 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 62 | pcapEnabled = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, |
| 63 | POWER_CAP_ENABLE_PROP); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 64 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 65 | return std::get<bool>(pcapEnabled); |
| 66 | } |
Patrick Williams | 2561362 | 2021-09-02 09:29:54 -0500 | [diff] [blame] | 67 | catch (const sdbusplus::exception::exception& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 68 | { |
| 69 | log<level::ERR>("Failed to get PowerCapEnable property", |
| 70 | entry("ERROR=%s", e.what()), |
| 71 | entry("PATH=%s", PCAP_PATH)); |
| 72 | |
| 73 | return false; |
| 74 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 75 | } |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 76 | |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 77 | std::string PowerCap::getPcapFilename(const fs::path& path) |
| 78 | { |
| 79 | std::regex expr{"power\\d+_cap_user$"}; |
| 80 | for (auto& file : fs::directory_iterator(path)) |
| 81 | { |
| 82 | if (std::regex_search(file.path().string(), expr)) |
| 83 | { |
| 84 | return file.path().filename(); |
| 85 | } |
| 86 | } |
| 87 | return std::string{}; |
| 88 | } |
| 89 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 90 | void PowerCap::writeOcc(uint32_t pcapValue) |
| 91 | { |
| 92 | // Create path out to master occ hwmon entry |
| 93 | std::unique_ptr<fs::path> fileName = |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 94 | std::make_unique<fs::path>(OCC_HWMON_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 95 | *fileName /= occMasterName; |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 96 | *fileName /= "hwmon"; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 97 | |
| 98 | // Need to get the hwmonXX directory name, there better only be 1 dir |
| 99 | assert(std::distance(fs::directory_iterator(*fileName), |
| 100 | fs::directory_iterator{}) == 1); |
| 101 | // Now set our path to this full path, including this hwmonXX directory |
| 102 | fileName = std::make_unique<fs::path>(*fs::directory_iterator(*fileName)); |
| 103 | // Append on the hwmon string where we write the user power cap |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 104 | |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 105 | auto baseName = getPcapFilename(*fileName); |
| 106 | if (baseName.empty()) |
| 107 | { |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 108 | log<level::ERR>( |
| 109 | fmt::format("Could not find a power cap file to write to: {})", |
| 110 | fileName->c_str()) |
| 111 | .c_str()); |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | *fileName /= baseName; |
| 115 | |
| 116 | uint64_t microWatts = pcapValue * 1000000ull; |
| 117 | |
| 118 | auto pcapString{std::to_string(microWatts)}; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 119 | |
| 120 | log<level::INFO>("Writing pcap value to hwmon", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 121 | entry("PCAP_PATH=%s", fileName->c_str()), |
| 122 | entry("PCAP_VALUE=%s", pcapString.c_str())); |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 123 | // Open the hwmon file and write the power cap |
| 124 | std::ofstream file(*fileName, std::ios::out); |
| 125 | file << pcapString; |
| 126 | file.close(); |
| 127 | return; |
| 128 | } |
| 129 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 130 | void PowerCap::pcapChanged(sdbusplus::message::message& msg) |
| 131 | { |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 132 | if (!occStatus.occActive()) |
| 133 | { |
| 134 | // Nothing to do |
| 135 | return; |
| 136 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 137 | |
| 138 | uint32_t pcap = 0; |
| 139 | bool pcapEnabled = false; |
| 140 | |
| 141 | std::string msgSensor; |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 142 | std::map<std::string, std::variant<uint32_t, bool>> msgData; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 143 | msg.read(msgSensor, msgData); |
| 144 | |
| 145 | // Retrieve which property changed via the msg and read the other one |
| 146 | auto valPropMap = msgData.find(POWER_CAP_PROP); |
| 147 | if (valPropMap != msgData.end()) |
| 148 | { |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 149 | pcap = std::get<uint32_t>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 150 | pcapEnabled = getPcapEnabled(); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | valPropMap = msgData.find(POWER_CAP_ENABLE_PROP); |
| 155 | if (valPropMap != msgData.end()) |
| 156 | { |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 157 | pcapEnabled = std::get<bool>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 158 | pcap = getPcap(); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | log<level::INFO>("Unknown power cap property changed"); |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | |
Chris Cain | b6535e8 | 2022-01-21 08:24:13 -0600 | [diff] [blame] | 167 | log<level::INFO>( |
| 168 | fmt::format("Power Cap Property Change (cap={}W (input), enabled={})", |
| 169 | pcap, pcapEnabled ? 'y' : 'n') |
| 170 | .c_str()); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 171 | |
| 172 | // Determine desired action to write to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 173 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 174 | auto occInput = getOccInput(pcap, pcapEnabled); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 175 | |
| 176 | // Write action to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 177 | writeOcc(occInput); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 178 | |
| 179 | return; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 182 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 183 | |
| 184 | } // namespace occ |
| 185 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 186 | } // namespace open_power |