blob: e2d4730563462e19dc3d366594804e442665b2c2 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070020
21#include <CPUSensor.hpp>
22#include <Utils.hpp>
23#include <VariantVisitors.hpp>
24#include <boost/algorithm/string/predicate.hpp>
25#include <boost/algorithm/string/replace.hpp>
26#include <boost/container/flat_set.hpp>
27#include <boost/date_time/posix_time/posix_time.hpp>
28#include <boost/process/child.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
James Feistf87dc4c2018-12-05 14:39:51 -080034// clang-format off
35// this needs to be included last or we'll have build issues
36#include <linux/peci-ioctl.h>
37// clang-format on
38
James Feist6714a252018-09-10 15:26:18 -070039static constexpr bool DEBUG = false;
40
41enum State
42{
43 OFF, // host powered down
44 ON, // host powered on
45 READY // host powered on and mem test passed - fully ready
46};
47
48struct CPUConfig
49{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070050 CPUConfig(const uint64_t& bus, const uint64_t& addr,
51 const std::string& name, const State& state) :
52 bus(bus),
53 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070054 {
55 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070056 int bus;
James Feist6714a252018-09-10 15:26:18 -070057 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070058 std::string name;
James Feist6714a252018-09-10 15:26:18 -070059 State state;
60
61 bool operator<(const CPUConfig& rhs) const
62 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070063 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070064 }
65};
66
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070067static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070069
James Feistcf3bce62019-01-08 10:07:19 -080070namespace fs = std::filesystem;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070071namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070073 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070075 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
76
Jae Hyun Yood64262b2018-11-01 13:31:16 -070077void detectCpuAsync(
78 boost::asio::deadline_timer& pingTimer,
79 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
80 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080081 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070082 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080083 cpuSensors,
84 boost::container::flat_set<CPUConfig>& cpuConfigs,
85 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070086
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -070087bool createSensors(
James Feist6714a252018-09-10 15:26:18 -070088 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080089 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist6714a252018-09-10 15:26:18 -070090 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080091 cpuSensors,
92 boost::container::flat_set<CPUConfig>& cpuConfigs,
93 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070094{
95 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080096 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -070097 {
98 if (cpu.state != State::OFF)
99 {
100 available = true;
101 break;
102 }
103 }
104 if (!available)
105 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700106 return false;
James Feist6714a252018-09-10 15:26:18 -0700107 }
108
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800109 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700110 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800111 return false;
James Feist6714a252018-09-10 15:26:18 -0700112 }
113
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700114 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
116 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
117 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700118 {
119 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700120 return true;
James Feist6714a252018-09-10 15:26:18 -0700121 }
122
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700123 boost::container::flat_set<std::string> scannedDirectories;
124 boost::container::flat_set<std::string> createdSensors;
125
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800126 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700127 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700128 const std::string& pathStr = hwmonNamePath.string();
129 auto hwmonDirectory = hwmonNamePath.parent_path();
130
131 auto ret = scannedDirectories.insert(hwmonDirectory.string());
132 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700133 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700134 continue; // already searched this path
135 }
136
137 fs::path::iterator it = hwmonNamePath.begin();
138 std::advance(it, 6); // pick the 6th part for a PECI client device name
139 std::string deviceName = *it;
140 auto findHyphen = deviceName.find("-");
141 if (findHyphen == std::string::npos)
142 {
143 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700144 continue;
145 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700146 std::string busStr = deviceName.substr(0, findHyphen);
147 std::string addrStr = deviceName.substr(findHyphen + 1);
148
149 size_t bus = 0;
150 size_t addr = 0;
151 try
152 {
153 bus = std::stoi(busStr);
154 addr = std::stoi(addrStr, 0, 16);
155 }
156 catch (std::invalid_argument)
157 {
158 continue;
159 }
160
161 std::ifstream nameFile(hwmonNamePath);
162 if (!nameFile.good())
163 {
164 std::cerr << "Failure reading " << hwmonNamePath << "\n";
165 continue;
166 }
167 std::string hwmonName;
168 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700169 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800170 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700171 {
172 // shouldn't have an empty name file
173 continue;
174 }
James Feist6714a252018-09-10 15:26:18 -0700175 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700176 {
177 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
178 << "\n";
179 }
James Feist6714a252018-09-10 15:26:18 -0700180
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700181 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700182 const SensorData* sensorData = nullptr;
183 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700184 const std::pair<std::string, boost::container::flat_map<
185 std::string, BasicVariantType>>*
186 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700187
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700188 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800189 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700190 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700191 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700192 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700193 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 auto sensorBase = sensorData->find(sensorType);
196 if (sensorBase != sensorData->end())
197 {
198 baseConfiguration = &(*sensorBase);
199 break;
200 }
201 }
202 if (baseConfiguration == nullptr)
203 {
204 std::cerr << "error finding base configuration for" << hwmonName
205 << "\n";
206 continue;
207 }
208 auto configurationBus = baseConfiguration->second.find("Bus");
209 auto configurationAddress =
210 baseConfiguration->second.find("Address");
211
212 if (configurationBus == baseConfiguration->second.end() ||
213 configurationAddress == baseConfiguration->second.end())
214 {
215 std::cerr << "error finding bus or address in configuration";
216 continue;
217 }
218
219 if (sdbusplus::message::variant_ns::get<uint64_t>(
220 configurationBus->second) != bus ||
221 sdbusplus::message::variant_ns::get<uint64_t>(
222 configurationAddress->second) != addr)
223 {
224 continue;
225 }
226
227 interfacePath = &(sensor.first.str);
228 break;
229 }
230 if (interfacePath == nullptr)
231 {
232 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700233 continue;
234 }
235
236 auto findCpuId = baseConfiguration->second.find("CpuID");
237 if (findCpuId == baseConfiguration->second.end())
238 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700239 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700240 continue;
241 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 int cpuId =
243 variant_ns::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700244
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700246 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700248 {
249 std::cerr << "No temperature sensors in system\n";
250 continue;
251 }
252
253 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800254 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700255 {
256 auto inputPathStr = inputPath.string();
257 auto labelPath =
258 boost::replace_all_copy(inputPathStr, "input", "label");
259 std::ifstream labelFile(labelPath);
260 if (!labelFile.good())
261 {
262 std::cerr << "Failure reading " << labelPath << "\n";
263 continue;
264 }
265 std::string label;
266 std::getline(labelFile, label);
267 labelFile.close();
268 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700269
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800270 auto findSensor = cpuSensors.find(sensorName);
271 if (findSensor != cpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700272 {
273 if (DEBUG)
274 {
275 std::cout << "Skipped: " << inputPath << ": " << sensorName
276 << " is already created\n";
277 }
278 continue;
279 }
280
James Feist6714a252018-09-10 15:26:18 -0700281 std::vector<thresholds::Threshold> sensorThresholds;
282 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700283 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700284 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800285 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700286 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700287 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
288 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700289 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700290 std::cerr << "error populating thresholds for "
291 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700292 }
293 }
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800294 cpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 inputPathStr, sensorType, objectServer, dbusConnection, io,
296 sensorName, std::move(sensorThresholds), *interfacePath);
297 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700298 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700299 {
James Feist6714a252018-09-10 15:26:18 -0700300 std::cout << "Mapped: " << inputPath << " to " << sensorName
301 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700302 }
James Feist6714a252018-09-10 15:26:18 -0700303 }
304 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700305
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700306 if (createdSensors.size())
307 {
308 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
309 << " created\n";
310 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700311
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700312 return true;
James Feist6714a252018-09-10 15:26:18 -0700313}
314
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700315void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700316{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700317 std::ostringstream hex;
318 hex << std::hex << config.addr;
319 const std::string& addrHexStr = hex.str();
320 std::string busStr = std::to_string(config.bus);
321
322 std::string parameters = "peci-client 0x" + addrHexStr;
323 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
324
James Feistcf3bce62019-01-08 10:07:19 -0800325 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700326 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800327 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700328 {
James Feistcf3bce62019-01-08 10:07:19 -0800329 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700330 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 continue;
James Feist6714a252018-09-10 15:26:18 -0700332 }
333
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700334 const std::string& directoryName = path.path().filename();
335 if (boost::starts_with(directoryName, busStr) &&
336 boost::ends_with(directoryName, addrHexStr))
337 {
338 if (DEBUG)
339 {
340 std::cout << parameters << " on bus " << busStr
341 << " is already exported\n";
342 }
343 return;
344 }
James Feist6714a252018-09-10 15:26:18 -0700345 }
346
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700347 std::ofstream deviceFile(device);
348 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700349 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700350 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700351 return;
352 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700353 deviceFile << parameters;
354 deviceFile.close();
355
356 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700357}
358
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800359void detectCpu(
360 boost::asio::deadline_timer& pingTimer,
361 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
362 sdbusplus::asio::object_server& objectServer,
363 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
364 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
365 cpuSensors,
366 boost::container::flat_set<CPUConfig>& cpuConfigs,
367 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700368{
James Feist6714a252018-09-10 15:26:18 -0700369 size_t rescanDelaySeconds = 0;
370 bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700371
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800372 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700373 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700374 std::string peciDevPath = peciDev + std::to_string(config.bus);
375 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
376 if (file < 0)
377 {
378 std::cerr << "unable to open " << peciDevPath << "\n";
379 std::exit(EXIT_FAILURE);
380 }
381
James Feist6714a252018-09-10 15:26:18 -0700382 State state;
383 struct peci_ping_msg msg;
384 msg.addr = config.addr;
385 if (!ioctl(file, PECI_IOC_PING, &msg))
386 {
387 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700388 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700389 {
390 struct peci_rd_pkg_cfg_msg msg;
391 msg.addr = config.addr;
392 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
393 msg.param = rank;
394 msg.rx_len = 4;
395 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
396 {
397 if (msg.pkg_config[0] || msg.pkg_config[1] ||
398 msg.pkg_config[2])
399 {
400 dimmReady = true;
401 break;
402 }
403 }
404 else
405 {
406 break;
407 }
408 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700409
James Feist6714a252018-09-10 15:26:18 -0700410 if (dimmReady)
411 {
412 state = State::READY;
413 }
414 else
415 {
416 state = State::ON;
417 }
418 }
419 else
420 {
421 state = State::OFF;
422 }
423
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700424 close(file);
425
James Feist6714a252018-09-10 15:26:18 -0700426 if (config.state != state)
427 {
428 if (config.state == State::OFF)
429 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700430 std::cout << config.name << " is detected\n";
431 exportDevice(config);
James Feist6714a252018-09-10 15:26:18 -0700432 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700433 if (state == State::READY)
434 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700435 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700436 << " is/are detected\n";
437 }
James Feist6714a252018-09-10 15:26:18 -0700438 config.state = state;
439 }
440
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700441 if (config.state != State::OFF)
442 {
443 if (config.state == State::ON)
444 {
445 rescanDelaySeconds = 1;
446 }
447 else
448 {
449 rescanDelaySeconds = 5;
450 }
451 }
452
453 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700454 {
455 keepPinging = true;
456 }
457
458 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700459 {
460 std::cout << config.name << ", state: " << config.state << "\n";
461 }
James Feist6714a252018-09-10 15:26:18 -0700462 }
463
James Feist6714a252018-09-10 15:26:18 -0700464 if (rescanDelaySeconds)
465 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700466 creationTimer.expires_from_now(
467 boost::posix_time::seconds(rescanDelaySeconds));
468 creationTimer.async_wait([&](const boost::system::error_code& ec) {
469 if (ec == boost::asio::error::operation_aborted)
470 {
471 return; // we're being canceled
472 }
473
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800474 if (!createSensors(io, objectServer, dbusConnection, cpuSensors,
475 cpuConfigs, sensorConfigs))
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700476 {
477 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800478 dbusConnection, cpuSensors, cpuConfigs,
479 sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700480 }
481 });
James Feist6714a252018-09-10 15:26:18 -0700482 }
483
484 if (keepPinging)
485 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800486 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
487 dbusConnection, cpuSensors, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700488 }
489}
490
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700491void detectCpuAsync(
492 boost::asio::deadline_timer& pingTimer,
493 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
494 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800495 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700496 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800497 cpuSensors,
498 boost::container::flat_set<CPUConfig>& cpuConfigs,
499 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700500{
501 pingTimer.expires_from_now(boost::posix_time::seconds(1));
502 pingTimer.async_wait([&](const boost::system::error_code& ec) {
503 if (ec == boost::asio::error::operation_aborted)
504 {
505 return; // we're being canceled
506 }
507
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800508 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
509 cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700510 });
511}
512
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700513bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800514 boost::container::flat_set<CPUConfig>& cpuConfigs,
515 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700516{
James Feist6714a252018-09-10 15:26:18 -0700517 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800518 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700519 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700520 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700521 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700522 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800523 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700524 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700525 return false;
James Feist6714a252018-09-10 15:26:18 -0700526 }
527 useCache = true;
528 }
529
530 // check PECI client addresses and DT overlay names from CPU configuration
531 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700532 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700533 {
534 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800535 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700536 {
537 for (const std::pair<
538 std::string,
539 boost::container::flat_map<std::string, BasicVariantType>>&
540 config : sensor.second)
541 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700542 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700543 {
544 continue;
545 }
546
James Feist6714a252018-09-10 15:26:18 -0700547 auto findName = config.second.find("Name");
548 if (findName == config.second.end())
549 {
550 continue;
551 }
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700552 std::string nameRaw = variant_ns::visit(
James Feist6714a252018-09-10 15:26:18 -0700553 VariantToStringVisitor(), findName->second);
554 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700555 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700556
557 auto findBus = config.second.find("Bus");
558 if (findBus == config.second.end())
559 {
560 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
561 continue;
562 }
563 uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(),
564 findBus->second);
565
566 auto findAddress = config.second.find("Address");
567 if (findAddress == config.second.end())
568 {
569 std::cerr << "Can't find 'Address' setting in " << name
570 << "\n";
571 continue;
572 }
573 uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(),
574 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700575
576 if (DEBUG)
577 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700578 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700579 std::cout << "addr: " << addr << "\n";
580 std::cout << "name: " << name << "\n";
581 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700582 }
583
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800584 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700585 }
586 }
587 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700588
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800589 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700590 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800591 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700592 << " parsed\n";
593 return true;
594 }
595
596 return false;
James Feist6714a252018-09-10 15:26:18 -0700597}
598
599int main(int argc, char** argv)
600{
601 boost::asio::io_service io;
602 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800603 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700604
605 systemBus->request_name("xyz.openbmc_project.CPUSensor");
606 sdbusplus::asio::object_server objectServer(systemBus);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800607 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
608 cpuSensors;
James Feist6714a252018-09-10 15:26:18 -0700609 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
610 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700611 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700612 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800613 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700614
615 filterTimer.expires_from_now(boost::posix_time::seconds(1));
616 filterTimer.async_wait([&](const boost::system::error_code& ec) {
617 if (ec == boost::asio::error::operation_aborted)
618 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700619 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700620 }
621
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800622 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700623 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800624 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
625 systemBus, cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700626 }
627 });
628
James Feist6714a252018-09-10 15:26:18 -0700629 std::function<void(sdbusplus::message::message&)> eventHandler =
630 [&](sdbusplus::message::message& message) {
631 if (message.is_method_error())
632 {
633 std::cerr << "callback method error\n";
634 return;
635 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700636
637 if (DEBUG)
638 {
639 std::cout << message.get_path() << " is changed\n";
640 }
641
James Feist6714a252018-09-10 15:26:18 -0700642 // this implicitly cancels the timer
643 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700644 filterTimer.async_wait([&](const boost::system::error_code& ec) {
645 if (ec == boost::asio::error::operation_aborted)
646 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700647 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700648 }
649
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800650 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
James Feist6714a252018-09-10 15:26:18 -0700651 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700652 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800653 systemBus, cpuSensors, cpuConfigs,
654 sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700655 }
656 });
657 };
658
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700659 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700660 {
661 auto match = std::make_unique<sdbusplus::bus::match::match>(
662 static_cast<sdbusplus::bus::bus&>(*systemBus),
663 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700664 std::string(inventoryPath) + "',arg0namespace='" +
665 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700666 eventHandler);
667 matches.emplace_back(std::move(match));
668 }
669
670 io.run();
671}