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