blob: 939bbc2119cdecaa65aa170ff999c851de9aa5e5 [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
17#include <unistd.h>
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <CPUSensor.hpp>
20#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
James Feist6714a252018-09-10 15:26:18 -070028#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <istream>
James Feist6714a252018-09-10 15:26:18 -070030#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070033#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <vector>
James Feist6714a252018-09-10 15:26:18 -070035
James Feistd8705872019-02-08 13:26:09 -080036CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
37 sdbusplus::asio::object_server& objectServer,
38 std::shared_ptr<sdbusplus::asio::connection>& conn,
39 boost::asio::io_service& io, const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080040 std::vector<thresholds::Threshold>&& thresholdsIn,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070042 bool show, double dtsOffset) :
Jeff Lin7b7a9de2021-02-22 11:16:27 +080043 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
44 std::move(thresholdsIn), sensorConfiguration, objectType, 0, 0, conn,
45 PowerState::on),
James Feistc22b8422020-07-07 14:40:39 -070046 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020048 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs),
49 minMaxReadCounter(0)
James Feist6714a252018-09-10 15:26:18 -070050{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 nameTcontrol = labelTcontrol;
52 nameTcontrol += " CPU" + std::to_string(cpuId);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 if (show)
James Feist6714a252018-09-10 15:26:18 -070054 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020055 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020057 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020058 std::string interfacePath;
59 if (type.compare("power") == 0)
60 {
61 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030062 minValue = 0;
63 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020064 }
65 else
66 {
67 interfacePath =
68 "/xyz/openbmc_project/sensors/temperature/" + name;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030069 minValue = -128;
70 maxValue = 127;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020071 }
James Feist078f2322019-03-08 11:09:05 -080072
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020073 sensorInterface = objectServer.add_interface(
74 interfacePath, "xyz.openbmc_project.Sensor.Value");
75 if (thresholds::hasWarningInterface(thresholds))
76 {
77 thresholdInterfaceWarning = objectServer.add_interface(
78 interfacePath,
79 "xyz.openbmc_project.Sensor.Threshold.Warning");
80 }
81 if (thresholds::hasCriticalInterface(thresholds))
82 {
83 thresholdInterfaceCritical = objectServer.add_interface(
84 interfacePath,
85 "xyz.openbmc_project.Sensor.Threshold.Critical");
86 }
87 association = objectServer.add_interface(interfacePath,
88 association::interface);
89
90 setInitialProperties(conn);
91 }
James Feist6714a252018-09-10 15:26:18 -070092 }
James Feist961bf092020-07-01 16:38:12 -070093
94 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080095 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096 setupRead();
James Feist6714a252018-09-10 15:26:18 -070097}
98
99CPUSensor::~CPUSensor()
100{
101 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 inputDev.close();
103 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800104 if (show)
105 {
106 objServer.remove_interface(thresholdInterfaceWarning);
107 objServer.remove_interface(thresholdInterfaceCritical);
108 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800109 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800110 }
James Feist6714a252018-09-10 15:26:18 -0700111}
112
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700114{
James Feistc22b8422020-07-07 14:40:39 -0700115 if (readingStateGood())
116 {
117 inputDev.close();
118 int fd = open(path.c_str(), O_RDONLY);
119 if (fd >= 0)
120 {
121 inputDev.assign(fd);
122
123 boost::asio::async_read_until(
124 inputDev, readBuf, '\n',
125 [&](const boost::system::error_code& ec,
126 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
127 }
128 else
129 {
130 std::cerr << name << " unable to open fd!\n";
131 pollTime = sensorFailedPollTimeMs;
132 }
133 }
134 else
135 {
136 pollTime = sensorFailedPollTimeMs;
137 markAvailable(false);
138 }
139 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
140 waitTimer.async_wait([&](const boost::system::error_code& ec) {
141 if (ec == boost::asio::error::operation_aborted)
142 {
143 return; // we're being canceled
144 }
145 setupRead();
146 });
James Feist6714a252018-09-10 15:26:18 -0700147}
148
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200149void CPUSensor::updateMinMaxValues(void)
150{
151 const boost::container::flat_map<
152 std::string,
153 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
154 const char*>>>
155 map = {
156 {
157 "cap",
158 {
159 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
160 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
161 },
162 },
163 };
164
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200165 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200166 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200167 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200168 const auto mapIt = map.find(fileItem);
169 if (mapIt != map.cend())
170 {
171 for (const auto& vectorItem : mapIt->second)
172 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200173 auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200174 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200175 if (auto newVal =
176 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200177 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200178 updateProperty(sensorInterface, oldValue, *newVal,
179 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200180 }
181 else
182 {
183 if (isPowerOn())
184 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200185 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200186 }
187 else
188 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200189 updateProperty(sensorInterface, oldValue,
190 std::numeric_limits<double>::quiet_NaN(),
191 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200192 }
193 }
194 }
195 }
196 }
197}
198
James Feistd8705872019-02-08 13:26:09 -0800199void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700200{
James Feist838529b2020-09-02 16:57:05 -0700201
James Feist6714a252018-09-10 15:26:18 -0700202 if (err == boost::system::errc::bad_file_descriptor)
203 {
204 return; // we're being destroyed
205 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700206 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700207 {
208 if (readingStateGood())
209 {
210 if (!loggedInterfaceDown)
211 {
212 std::cerr << name << " interface down!\n";
213 loggedInterfaceDown = true;
214 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700215 pollTime = CPUSensor::sensorPollMs * 10u;
James Feist838529b2020-09-02 16:57:05 -0700216 markFunctional(false);
217 }
218 return;
219 }
220 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700221 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700222 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700223 if (!err)
224 {
225 std::string response;
226 try
227 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700228 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700229 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700230 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700231 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800232
233 if (show)
James Feist6714a252018-09-10 15:26:18 -0700234 {
Josh Lehan833661a2020-03-04 17:35:41 -0800235 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800236 }
Josh Lehan833661a2020-03-04 17:35:41 -0800237 else
238 {
239 value = nvalue;
240 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200241 if (minMaxReadCounter++ % 8 == 0)
242 {
243 updateMinMaxValues();
244 }
Josh Lehan833661a2020-03-04 17:35:41 -0800245
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800246 double gTcontrol = gCpuSensors[nameTcontrol]
247 ? gCpuSensors[nameTcontrol]->value
248 : std::numeric_limits<double>::quiet_NaN();
249 if (gTcontrol != privTcontrol)
250 {
251 privTcontrol = gTcontrol;
252
253 if (!thresholds.empty())
254 {
255 std::vector<thresholds::Threshold> newThresholds;
256 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700257 CPUSensor::sensorScaleFactor,
258 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800259 {
260 if (!std::equal(thresholds.begin(), thresholds.end(),
261 newThresholds.begin(),
262 newThresholds.end()))
263 {
264 thresholds = newThresholds;
265 if (show)
266 {
267 thresholds::updateThresholds(this);
268 }
269 }
270 }
271 else
272 {
273 std::cerr << "Failure to update thresholds for " << name
274 << "\n";
275 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800276 }
277 }
James Feist6714a252018-09-10 15:26:18 -0700278 }
James Feistd8705872019-02-08 13:26:09 -0800279 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700280 {
James Feist961bf092020-07-01 16:38:12 -0700281 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700282 }
283 }
284 else
285 {
James Feist1169eb42018-10-31 10:08:47 -0700286 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700287 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700288 }
289
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700290 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700291}
292
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700293void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700294{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800295 if (show)
296 {
297 thresholds::checkThresholds(this);
298 }
James Feist6714a252018-09-10 15:26:18 -0700299}