William A. Kennington III | 29a8ed1 | 2018-11-06 15:05:11 -0800 | [diff] [blame] | 1 | #include <cassert> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 2 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include <powercap.hpp> |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 4 | #include <regex> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 5 | |
| 6 | namespace open_power |
| 7 | { |
| 8 | namespace occ |
| 9 | { |
| 10 | namespace powercap |
| 11 | { |
| 12 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 13 | constexpr auto PCAP_PATH = "/xyz/openbmc_project/control/host0/power_cap"; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 14 | constexpr auto PCAP_INTERFACE = "xyz.openbmc_project.Control.Power.Cap"; |
| 15 | |
| 16 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 17 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 18 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 19 | |
| 20 | constexpr auto POWER_CAP_PROP = "PowerCap"; |
| 21 | constexpr auto POWER_CAP_ENABLE_PROP = "PowerCapEnable"; |
| 22 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 23 | using namespace phosphor::logging; |
Matt Spinler | 00a6478 | 2019-07-24 14:29:24 -0500 | [diff] [blame] | 24 | namespace fs = std::experimental::filesystem; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 25 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 26 | std::string PowerCap::getService(std::string path, std::string interface) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 27 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 28 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 29 | MAPPER_INTERFACE, "GetObject"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 30 | |
| 31 | mapper.append(path, std::vector<std::string>({interface})); |
| 32 | auto mapperResponseMsg = bus.call(mapper); |
| 33 | |
| 34 | if (mapperResponseMsg.is_method_error()) |
| 35 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 36 | log<level::ERR>("Error in mapper call", entry("PATH=%s", path.c_str()), |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 37 | entry("INTERFACE=%s", interface.c_str())); |
| 38 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 39 | throw std::runtime_error("Error in mapper call"); |
| 40 | } |
| 41 | |
| 42 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 43 | mapperResponseMsg.read(mapperResponse); |
| 44 | if (mapperResponse.empty()) |
| 45 | { |
| 46 | log<level::ERR>("Error reading mapper response", |
| 47 | entry("PATH=%s", path.c_str()), |
| 48 | entry("INTERFACE=%s", interface.c_str())); |
| 49 | // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently |
| 50 | throw std::runtime_error("Error reading mapper response"); |
| 51 | } |
| 52 | |
| 53 | return mapperResponse.begin()->first; |
| 54 | } |
| 55 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 56 | uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled) |
| 57 | { |
| 58 | if (!pcapEnabled) |
| 59 | { |
| 60 | // Pcap disabled, return 0 to indicate disabled |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | // If pcap is not disabled then just return the pcap with the derating |
| 65 | // factor applied. |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 66 | return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100); |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 69 | uint32_t PowerCap::getPcap() |
| 70 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 71 | auto settingService = getService(PCAP_PATH, PCAP_INTERFACE); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 72 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 73 | auto method = |
| 74 | this->bus.new_method_call(settingService.c_str(), PCAP_PATH, |
| 75 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 76 | |
| 77 | method.append(PCAP_INTERFACE, POWER_CAP_PROP); |
| 78 | auto reply = this->bus.call(method); |
| 79 | |
| 80 | if (reply.is_method_error()) |
| 81 | { |
| 82 | log<level::ERR>("Error in getPcap prop"); |
| 83 | return 0; |
| 84 | } |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 85 | std::variant<uint32_t> pcap; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 86 | reply.read(pcap); |
| 87 | |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 88 | return std::get<uint32_t>(pcap); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool PowerCap::getPcapEnabled() |
| 92 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 93 | auto settingService = getService(PCAP_PATH, PCAP_INTERFACE); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 94 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 95 | auto method = |
| 96 | this->bus.new_method_call(settingService.c_str(), PCAP_PATH, |
| 97 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 98 | |
| 99 | method.append(PCAP_INTERFACE, POWER_CAP_ENABLE_PROP); |
| 100 | auto reply = this->bus.call(method); |
| 101 | |
| 102 | if (reply.is_method_error()) |
| 103 | { |
| 104 | log<level::ERR>("Error in getPcapEnabled prop"); |
| 105 | return 0; |
| 106 | } |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 107 | std::variant<bool> pcapEnabled; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 108 | reply.read(pcapEnabled); |
| 109 | |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 110 | return std::get<bool>(pcapEnabled); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 111 | } |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 112 | |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 113 | std::string PowerCap::getPcapFilename(const fs::path& path) |
| 114 | { |
| 115 | std::regex expr{"power\\d+_cap_user$"}; |
| 116 | for (auto& file : fs::directory_iterator(path)) |
| 117 | { |
| 118 | if (std::regex_search(file.path().string(), expr)) |
| 119 | { |
| 120 | return file.path().filename(); |
| 121 | } |
| 122 | } |
| 123 | return std::string{}; |
| 124 | } |
| 125 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 126 | void PowerCap::writeOcc(uint32_t pcapValue) |
| 127 | { |
| 128 | // Create path out to master occ hwmon entry |
| 129 | std::unique_ptr<fs::path> fileName = |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 130 | std::make_unique<fs::path>(OCC_HWMON_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 131 | *fileName /= occMasterName; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 132 | *fileName /= "/hwmon/"; |
| 133 | |
| 134 | // Need to get the hwmonXX directory name, there better only be 1 dir |
| 135 | assert(std::distance(fs::directory_iterator(*fileName), |
| 136 | fs::directory_iterator{}) == 1); |
| 137 | // Now set our path to this full path, including this hwmonXX directory |
| 138 | fileName = std::make_unique<fs::path>(*fs::directory_iterator(*fileName)); |
| 139 | // Append on the hwmon string where we write the user power cap |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 140 | |
Matt Spinler | eaaf3b2 | 2019-07-16 10:29:27 -0500 | [diff] [blame] | 141 | auto baseName = getPcapFilename(*fileName); |
| 142 | if (baseName.empty()) |
| 143 | { |
| 144 | log<level::ERR>("Could not find a power cap file to write to", |
| 145 | entry("PATH=%s", *fileName->c_str())); |
| 146 | return; |
| 147 | } |
| 148 | *fileName /= baseName; |
| 149 | |
| 150 | uint64_t microWatts = pcapValue * 1000000ull; |
| 151 | |
| 152 | auto pcapString{std::to_string(microWatts)}; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 153 | |
| 154 | log<level::INFO>("Writing pcap value to hwmon", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 155 | entry("PCAP_PATH=%s", fileName->c_str()), |
| 156 | entry("PCAP_VALUE=%s", pcapString.c_str())); |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 157 | // Open the hwmon file and write the power cap |
| 158 | std::ofstream file(*fileName, std::ios::out); |
| 159 | file << pcapString; |
| 160 | file.close(); |
| 161 | return; |
| 162 | } |
| 163 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 164 | void PowerCap::pcapChanged(sdbusplus::message::message& msg) |
| 165 | { |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 166 | if (!occStatus.occActive()) |
| 167 | { |
| 168 | // Nothing to do |
| 169 | return; |
| 170 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 171 | |
| 172 | uint32_t pcap = 0; |
| 173 | bool pcapEnabled = false; |
| 174 | |
| 175 | std::string msgSensor; |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 176 | std::map<std::string, std::variant<uint32_t, bool>> msgData; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 177 | msg.read(msgSensor, msgData); |
| 178 | |
| 179 | // Retrieve which property changed via the msg and read the other one |
| 180 | auto valPropMap = msgData.find(POWER_CAP_PROP); |
| 181 | if (valPropMap != msgData.end()) |
| 182 | { |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 183 | pcap = std::get<uint32_t>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 184 | pcapEnabled = getPcapEnabled(); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | valPropMap = msgData.find(POWER_CAP_ENABLE_PROP); |
| 189 | if (valPropMap != msgData.end()) |
| 190 | { |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 191 | pcapEnabled = std::get<bool>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 192 | pcap = getPcap(); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | log<level::INFO>("Unknown power cap property changed"); |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 201 | log<level::INFO>("Power Cap Property Change", entry("PCAP=%u", pcap), |
| 202 | entry("PCAP_ENABLED=%u", pcapEnabled)); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 203 | |
| 204 | // Determine desired action to write to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 205 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 206 | auto occInput = getOccInput(pcap, pcapEnabled); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 207 | |
| 208 | // Write action to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 209 | writeOcc(occInput); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 210 | |
| 211 | return; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 214 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 215 | |
| 216 | } // namespace occ |
| 217 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 218 | } // namespace open_power |