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