Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 1 | #include "occ_status.hpp" |
| 2 | |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/lg2.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> |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 7 | #include <filesystem> |
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 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 22 | namespace fs = std::filesystem; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 23 | |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 24 | using CapLimits = |
| 25 | sdbusplus::xyz::openbmc_project::Control::Power::server::CapLimits; |
| 26 | |
| 27 | // Print the current values |
| 28 | void OccPersistCapData::print() |
| 29 | { |
| 30 | if (capData.initialized) |
| 31 | { |
| 32 | lg2::info( |
| 33 | "OccPersistCapData: Soft Min: {SOFT}, Hard Min: {HARD}, Max: {MAX}", |
| 34 | "SOFT", capData.softMin, "HARD", capData.hardMin, "MAX", |
| 35 | capData.max); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Saves the power cap data in the filesystem. |
| 40 | void OccPersistCapData::save() |
| 41 | { |
| 42 | std::filesystem::path opath = |
| 43 | std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerCapFilename; |
| 44 | |
| 45 | if (!std::filesystem::exists(opath.parent_path())) |
| 46 | { |
| 47 | std::filesystem::create_directory(opath.parent_path()); |
| 48 | } |
| 49 | |
| 50 | try |
| 51 | { |
| 52 | std::ofstream stream{opath.c_str(), std::ios_base::binary}; |
| 53 | stream.write((char*)&capData, sizeof(capData)); |
| 54 | } |
| 55 | catch (const std::exception& e) |
| 56 | { |
| 57 | auto error = errno; |
| 58 | lg2::error( |
| 59 | "OccPersistCapData::save: failed to read {PATH}, errno={ERRNO}, err={ERR}", |
| 60 | "PATH", opath, "ERRNO", error, "ERR", e); |
| 61 | capData.initialized = false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Loads the power cap data from the filesystem |
| 66 | void OccPersistCapData::load() |
| 67 | { |
| 68 | std::filesystem::path ipath = |
| 69 | std::filesystem::path{OCC_CONTROL_PERSIST_PATH} / powerCapFilename; |
| 70 | |
| 71 | if (!std::filesystem::exists(ipath)) |
| 72 | { |
| 73 | capData.initialized = false; |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | try |
| 78 | { |
| 79 | PowerCapData newCapData; |
| 80 | std::ifstream stream{ipath.c_str(), std::ios_base::binary}; |
| 81 | stream.read((char*)&newCapData, sizeof(newCapData)); |
| 82 | if (newCapData.version != PCAPDATA_FILE_VERSION) |
| 83 | { |
| 84 | lg2::warning( |
| 85 | "OccPersistCapData::load() file version was {VER} (expected {EXP})", |
| 86 | "VER", newCapData.version, "EXP", PCAPDATA_FILE_VERSION); |
| 87 | } |
| 88 | memcpy(&capData, &newCapData, sizeof(capData)); |
| 89 | } |
| 90 | catch (const std::exception& e) |
| 91 | { |
| 92 | auto error = errno; |
| 93 | lg2::error( |
| 94 | "OccPersistCapData::load: failed to read {PATH}, errno={ERRNO}, err={ERR}", |
| 95 | "PATH", ipath, "ERRNO", error, "ERR", e); |
| 96 | capData.initialized = false; |
| 97 | } |
| 98 | } |
| 99 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 100 | void PowerCap::updatePcapBounds() |
| 101 | { |
| 102 | // Build the hwmon string to write the power cap bounds |
| 103 | fs::path minName = getPcapFilename(std::regex{"power\\d+_cap_min$"}); |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 104 | fs::path softMinName = |
| 105 | getPcapFilename(std::regex{"power\\d+_cap_min_soft$"}); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 106 | fs::path maxName = getPcapFilename(std::regex{"power\\d+_cap_max$"}); |
| 107 | |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 108 | // Read the current limits from persistent data |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 109 | uint32_t capSoftMin, capHardMin, capMax; |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 110 | persistedData.getCapLimits(capSoftMin, capHardMin, capMax); |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 111 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 112 | // Read the power cap bounds from sysfs files (from OCC) |
| 113 | uint64_t cap; |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 114 | bool parmsChanged = false; |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 115 | std::ifstream softMinFile(softMinName, std::ios::in); |
| 116 | if (softMinFile) |
| 117 | { |
| 118 | softMinFile >> cap; |
| 119 | softMinFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 120 | // Convert to input/AC Power in Watts (round up) |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 121 | capSoftMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9); |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 122 | parmsChanged = true; |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 126 | lg2::error( |
| 127 | "updatePcapBounds: unable to find pcap_min_soft file: {FILE} (errno={ERR})", |
| 128 | "FILE", pcapBasePathname, "ERR", errno); |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 131 | std::ifstream minFile(minName, std::ios::in); |
| 132 | if (minFile) |
| 133 | { |
| 134 | minFile >> cap; |
| 135 | minFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 136 | // Convert to input/AC Power in Watts (round up) |
Chris Cain | 8676e03 | 2022-03-24 15:06:23 -0500 | [diff] [blame] | 137 | capHardMin = ((cap / (PS_DERATING_FACTOR / 100.0) / 1000000) + 0.9); |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 138 | parmsChanged = true; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 142 | lg2::error( |
| 143 | "updatePcapBounds: unable to find cap_min file: {FILE} (errno={ERR})", |
| 144 | "FILE", pcapBasePathname, "ERR", errno); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 145 | } |
Chris Cain | 613dc90 | 2022-04-08 09:56:22 -0500 | [diff] [blame] | 146 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 147 | std::ifstream maxFile(maxName, std::ios::in); |
| 148 | if (maxFile) |
| 149 | { |
| 150 | maxFile >> cap; |
| 151 | maxFile.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 152 | // Convert to input/AC Power in Watts (truncate remainder) |
Chris Cain | 8676e03 | 2022-03-24 15:06:23 -0500 | [diff] [blame] | 153 | capMax = cap / (PS_DERATING_FACTOR / 100.0) / 1000000; |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 154 | parmsChanged = true; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 158 | lg2::error( |
| 159 | "updatePcapBounds: unable to find cap_max file: {FILE} (errno={ERR})", |
| 160 | "FILE", pcapBasePathname, "ERR", errno); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 161 | } |
| 162 | |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 163 | if (parmsChanged) |
| 164 | { |
| 165 | // Save the power cap bounds to dbus |
| 166 | updateDbusPcapLimits(capSoftMin, capHardMin, capMax); |
| 167 | persistedData.updateCapLimits(capSoftMin, capHardMin, capMax); |
| 168 | } |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 169 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 170 | // Validate user power cap (if enabled) is within the bounds |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 171 | const uint32_t dbusUserCap = getPcap(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 172 | const bool pcapEnabled = getPcapEnabled(); |
| 173 | if (pcapEnabled && (dbusUserCap != 0)) |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 174 | { |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 175 | const uint32_t hwmonUserCap = readUserCapHwmon(); |
| 176 | if ((dbusUserCap >= capSoftMin) && (dbusUserCap <= capMax)) |
| 177 | { |
| 178 | // Validate dbus and hwmon user caps match |
| 179 | if ((hwmonUserCap != 0) && (dbusUserCap != hwmonUserCap)) |
| 180 | { |
| 181 | // User power cap is enabled, but does not match dbus |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 182 | lg2::error( |
| 183 | "updatePcapBounds: user powercap mismatch (hwmon:{HCAP}W, bdus:{DCAP}W) - using dbus", |
| 184 | "HCAP", hwmonUserCap, "DCAP", dbusUserCap); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 185 | auto occInput = getOccInput(dbusUserCap, pcapEnabled); |
| 186 | writeOcc(occInput); |
| 187 | } |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | // User power cap is outside of current bounds |
| 192 | uint32_t newCap = capMax; |
| 193 | if (dbusUserCap < capSoftMin) |
| 194 | { |
| 195 | newCap = capSoftMin; |
| 196 | } |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 197 | lg2::error( |
| 198 | "updatePcapBounds: user powercap {CAP}W is outside bounds " |
| 199 | "(soft min:{SMIN}, min:{MIN}, max:{MAX})", |
| 200 | "CAP", dbusUserCap, "SMIN", capSoftMin, "MIN", capHardMin, |
| 201 | "MAX", capMax); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 202 | try |
| 203 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 204 | lg2::info( |
| 205 | "updatePcapBounds: Updating user powercap from {OLD} to {NEW}W", |
| 206 | "OLD", hwmonUserCap, "NEW", newCap); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 207 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, |
| 208 | newCap); |
| 209 | auto occInput = getOccInput(newCap, pcapEnabled); |
| 210 | writeOcc(occInput); |
| 211 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 212 | catch (const sdbusplus::exception_t& e) |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 213 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 214 | lg2::error( |
| 215 | "updatePcapBounds: Failed to update user powercap due to {ERR}", |
| 216 | "ERR", e.what()); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 217 | } |
| 218 | } |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 219 | } |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 220 | } |
| 221 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 222 | // Get value of power cap to send to the OCC (output/DC power) |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 223 | uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled) |
| 224 | { |
| 225 | if (!pcapEnabled) |
| 226 | { |
| 227 | // Pcap disabled, return 0 to indicate disabled |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | // If pcap is not disabled then just return the pcap with the derating |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 232 | // factor applied (output/DC power). |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 233 | return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100); |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 236 | uint32_t PowerCap::getPcap() |
| 237 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 238 | utils::PropertyValue pcap{}; |
| 239 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 240 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 241 | pcap = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP); |
| 242 | |
| 243 | return std::get<uint32_t>(pcap); |
| 244 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 245 | catch (const sdbusplus::exception_t& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 246 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 247 | lg2::error("Failed to get PowerCap property: path={PATH}: {ERR}", |
| 248 | "PATH", PCAP_PATH, "ERR", e.what()); |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 249 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 250 | return 0; |
| 251 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | bool PowerCap::getPcapEnabled() |
| 255 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 256 | utils::PropertyValue pcapEnabled{}; |
| 257 | try |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 258 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 259 | pcapEnabled = utils::getProperty(PCAP_PATH, PCAP_INTERFACE, |
| 260 | POWER_CAP_ENABLE_PROP); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 261 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 262 | return std::get<bool>(pcapEnabled); |
| 263 | } |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 264 | catch (const sdbusplus::exception_t& e) |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 265 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 266 | lg2::error("Failed to get PowerCapEnable property: path={PATH}: {ERR}", |
| 267 | "PATH", PCAP_PATH, "ERR", e.what()); |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 268 | |
| 269 | return false; |
| 270 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 271 | } |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 272 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 273 | fs::path PowerCap::getPcapFilename(const std::regex& expr) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 274 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 275 | if (pcapBasePathname.empty()) |
| 276 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 277 | pcapBasePathname = occStatus.getHwmonPath(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 278 | } |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 279 | |
| 280 | if (fs::exists(pcapBasePathname)) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 281 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 282 | // Search for pcap file based on the supplied expr |
| 283 | for (auto& file : fs::directory_iterator(pcapBasePathname)) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 284 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 285 | if (std::regex_search(file.path().string(), expr)) |
| 286 | { |
| 287 | // Found match |
| 288 | return file; |
| 289 | } |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 290 | } |
| 291 | } |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 292 | else |
| 293 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 294 | lg2::error("Power Cap base filename not found: {FILE}", "FILE", |
| 295 | pcapBasePathname); |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 298 | // return empty path |
| 299 | return fs::path{}; |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 302 | // Write the user power cap to sysfs (output/DC power) |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 303 | // This will trigger the driver to send the cap to the OCC |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 304 | void PowerCap::writeOcc(uint32_t pcapValue) |
| 305 | { |
Chris Cain | d4c19a0 | 2022-04-22 15:46:13 -0500 | [diff] [blame] | 306 | if (!occStatus.occActive()) |
| 307 | { |
| 308 | // OCC not running, skip update |
| 309 | return; |
| 310 | } |
| 311 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 312 | // Build the hwmon string to write the user power cap |
| 313 | fs::path fileName = getPcapFilename(std::regex{"power\\d+_cap_user$"}); |
| 314 | if (fileName.empty()) |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 315 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 316 | lg2::error("Could not find a power cap file to write to: {FILE})", |
| 317 | "FILE", pcapBasePathname); |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 318 | return; |
| 319 | } |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 320 | |
| 321 | uint64_t microWatts = pcapValue * 1000000ull; |
| 322 | |
| 323 | auto pcapString{std::to_string(microWatts)}; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 324 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 325 | // Open the hwmon file and write the power cap |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 326 | std::ofstream file(fileName, std::ios::out); |
| 327 | if (file) |
| 328 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 329 | lg2::info("Writing {CAP}uW to {FILE}", "CAP", pcapString, "FILE", |
| 330 | fileName); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 331 | file << pcapString; |
| 332 | file.close(); |
| 333 | } |
| 334 | else |
| 335 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 336 | lg2::error("Failed writing {CAP}uW to {FILE} (errno={ERR})", "CAP", |
| 337 | microWatts, "FILE", fileName, "ERR", errno); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 338 | } |
| 339 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 343 | // Read the current user power cap from sysfs as input/AC power |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 344 | uint32_t PowerCap::readUserCapHwmon() |
| 345 | { |
| 346 | uint32_t userCap = 0; |
| 347 | |
| 348 | // Get the sysfs filename for the user power cap |
| 349 | fs::path userCapName = getPcapFilename(std::regex{"power\\d+_cap_user$"}); |
| 350 | if (userCapName.empty()) |
| 351 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 352 | lg2::error( |
| 353 | "readUserCapHwmon: Could not find a power cap file to read: {FILE})", |
| 354 | "FILE", pcapBasePathname); |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | // Open the sysfs file and read the power cap |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 359 | std::ifstream file(userCapName, std::ios::in); |
| 360 | if (file) |
| 361 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 362 | uint64_t cap; |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 363 | file >> cap; |
| 364 | file.close(); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 365 | // Convert to input/AC Power in Watts |
| 366 | userCap = (cap / (PS_DERATING_FACTOR / 100.0) / 1000000); |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 367 | } |
| 368 | else |
| 369 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 370 | lg2::error("readUserCapHwmon: Failed reading {FILE} (errno={ERR})", |
| 371 | "FILE", userCapName, "ERR", errno); |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | return userCap; |
| 375 | } |
| 376 | |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 377 | void PowerCap::pcapChanged(sdbusplus::message_t& msg) |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 378 | { |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 379 | if (!occStatus.occActive()) |
| 380 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 381 | // Nothing to do |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 382 | return; |
| 383 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 384 | |
| 385 | uint32_t pcap = 0; |
| 386 | bool pcapEnabled = false; |
| 387 | |
| 388 | std::string msgSensor; |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 389 | std::map<std::string, std::variant<uint32_t, bool>> msgData; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 390 | msg.read(msgSensor, msgData); |
| 391 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 392 | bool changeFound = false; |
| 393 | for (const auto& [prop, value] : msgData) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 394 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 395 | if (prop == POWER_CAP_PROP) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 396 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 397 | pcap = std::get<uint32_t>(value); |
| 398 | pcapEnabled = getPcapEnabled(); |
| 399 | changeFound = true; |
| 400 | } |
| 401 | else if (prop == POWER_CAP_ENABLE_PROP) |
| 402 | { |
| 403 | pcapEnabled = std::get<bool>(value); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 404 | pcap = getPcap(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 405 | changeFound = true; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 409 | // Ignore other properties |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 410 | lg2::debug( |
| 411 | "pcapChanged: Unknown power cap property changed {PROP} to {VAL}", |
| 412 | "PROP", prop, "VAL", std::get<uint32_t>(value)); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 416 | // Validate the cap is within supported range |
| 417 | uint32_t capSoftMin, capHardMin, capMax; |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 418 | persistedData.getCapLimits(capSoftMin, capHardMin, capMax); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 419 | if (((pcap > 0) && (pcap < capSoftMin)) || ((pcap == 0) && (pcapEnabled))) |
| 420 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 421 | lg2::error( |
| 422 | "pcapChanged: Power cap of {CAP}W is lower than allowed (soft min:{SMIN}, min:{MIN}) - using soft min", |
| 423 | "CAP", pcap, "SMIN", capSoftMin, "MIN", capHardMin); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 424 | pcap = capSoftMin; |
| 425 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap); |
| 426 | } |
| 427 | else if (pcap > capMax) |
| 428 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 429 | lg2::error( |
| 430 | "pcapChanged: Power cap of {CAP}W is higher than allowed (max:{MAX}) - using max", |
| 431 | "CAP", pcap, "MAX", capMax); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 432 | pcap = capMax; |
| 433 | utils::setProperty(PCAP_PATH, PCAP_INTERFACE, POWER_CAP_PROP, pcap); |
| 434 | } |
| 435 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 436 | if (changeFound) |
| 437 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 438 | lg2::info( |
| 439 | "Power Cap Property Change (cap={CAP}W (input), enabled={ENABLE})", |
| 440 | "CAP", pcap, "ENABLE", pcapEnabled); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 441 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 442 | // Determine desired action to write to occ |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 443 | auto occInput = getOccInput(pcap, pcapEnabled); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 444 | // Write action to occ |
| 445 | writeOcc(occInput); |
| 446 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 447 | |
| 448 | return; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 451 | // Update the Power Cap bounds on DBus |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 452 | void PowerCap::updateDbusPcapLimits(uint32_t softMin, uint32_t hardMin, |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 453 | uint32_t max) |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 454 | { |
Chris Cain | d6ced7c | 2022-10-17 17:11:07 -0500 | [diff] [blame^] | 455 | CapLimits::minSoftPowerCapValue(softMin); |
| 456 | CapLimits::minPowerCapValue(hardMin); |
| 457 | CapLimits::maxPowerCapValue(max); |
Chris Cain | 81c8343 | 2022-06-27 08:21:52 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 460 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 461 | |
| 462 | } // namespace occ |
| 463 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 464 | } // namespace open_power |