blob: c28b2303266ac2aede5f3d33dea6196f549e2c3a [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,
Ed Tanous8a57ec02020-10-09 12:46:52 -070040 std::vector<thresholds::Threshold>&& thresholds,
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) :
Ed Tanous8a57ec02020-10-09 12:46:52 -070043 Sensor(boost::replace_all_copy(sensorName, " ", "_"), std::move(thresholds),
44 sensorConfiguration, objectType, 0, 0, conn, PowerState::on),
James Feistc22b8422020-07-07 14:40:39 -070045 objServer(objectServer), inputDev(io), waitTimer(io), path(path),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050046 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +020047 dtsOffset(dtsOffset), show(show), pollTime(CPUSensor::sensorPollMs),
48 minMaxReadCounter(0)
James Feist6714a252018-09-10 15:26:18 -070049{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080050 nameTcontrol = labelTcontrol;
51 nameTcontrol += " CPU" + std::to_string(cpuId);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080052 if (show)
James Feist6714a252018-09-10 15:26:18 -070053 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +020054 if (auto fileParts = splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +020056 auto& [type, nr, item] = *fileParts;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020057 std::string interfacePath;
58 if (type.compare("power") == 0)
59 {
60 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030061 minValue = 0;
62 maxValue = 511;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020063 }
64 else
65 {
66 interfacePath =
67 "/xyz/openbmc_project/sensors/temperature/" + name;
Andrei Kartashev6736d4b2020-12-18 18:59:21 +030068 minValue = -128;
69 maxValue = 127;
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020070 }
James Feist078f2322019-03-08 11:09:05 -080071
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020072 sensorInterface = objectServer.add_interface(
73 interfacePath, "xyz.openbmc_project.Sensor.Value");
74 if (thresholds::hasWarningInterface(thresholds))
75 {
76 thresholdInterfaceWarning = objectServer.add_interface(
77 interfacePath,
78 "xyz.openbmc_project.Sensor.Threshold.Warning");
79 }
80 if (thresholds::hasCriticalInterface(thresholds))
81 {
82 thresholdInterfaceCritical = objectServer.add_interface(
83 interfacePath,
84 "xyz.openbmc_project.Sensor.Threshold.Critical");
85 }
86 association = objectServer.add_interface(interfacePath,
87 association::interface);
88
89 setInitialProperties(conn);
90 }
James Feist6714a252018-09-10 15:26:18 -070091 }
James Feist961bf092020-07-01 16:38:12 -070092
93 // call setup always as not all sensors call setInitialProperties
James Feist71d31b22019-01-02 16:57:54 -080094 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 setupRead();
James Feist6714a252018-09-10 15:26:18 -070096}
97
98CPUSensor::~CPUSensor()
99{
100 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 inputDev.close();
102 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800103 if (show)
104 {
105 objServer.remove_interface(thresholdInterfaceWarning);
106 objServer.remove_interface(thresholdInterfaceCritical);
107 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800108 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800109 }
James Feist6714a252018-09-10 15:26:18 -0700110}
111
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700113{
James Feistc22b8422020-07-07 14:40:39 -0700114 if (readingStateGood())
115 {
116 inputDev.close();
117 int fd = open(path.c_str(), O_RDONLY);
118 if (fd >= 0)
119 {
120 inputDev.assign(fd);
121
122 boost::asio::async_read_until(
123 inputDev, readBuf, '\n',
124 [&](const boost::system::error_code& ec,
125 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
126 }
127 else
128 {
129 std::cerr << name << " unable to open fd!\n";
130 pollTime = sensorFailedPollTimeMs;
131 }
132 }
133 else
134 {
135 pollTime = sensorFailedPollTimeMs;
136 markAvailable(false);
137 }
138 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
139 waitTimer.async_wait([&](const boost::system::error_code& ec) {
140 if (ec == boost::asio::error::operation_aborted)
141 {
142 return; // we're being canceled
143 }
144 setupRead();
145 });
James Feist6714a252018-09-10 15:26:18 -0700146}
147
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200148void CPUSensor::updateMinMaxValues(void)
149{
150 const boost::container::flat_map<
151 std::string,
152 std::vector<std::tuple<const char*, std::reference_wrapper<double>,
153 const char*>>>
154 map = {
155 {
156 "cap",
157 {
158 std::make_tuple("cap_max", std::ref(maxValue), "MaxValue"),
159 std::make_tuple("cap_min", std::ref(minValue), "MinValue"),
160 },
161 },
162 };
163
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200164 if (auto fileParts = splitFileName(path))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200165 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200166 auto& [fileType, fileNr, fileItem] = *fileParts;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200167 const auto mapIt = map.find(fileItem);
168 if (mapIt != map.cend())
169 {
170 for (const auto& vectorItem : mapIt->second)
171 {
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200172 auto& [suffix, oldValue, dbusName] = vectorItem;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200173 auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200174 if (auto newVal =
175 readFile(attrPath, CPUSensor::sensorScaleFactor))
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200176 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200177 updateProperty(sensorInterface, oldValue, *newVal,
178 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200179 }
180 else
181 {
182 if (isPowerOn())
183 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200184 updateProperty(sensorInterface, oldValue, 0, dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200185 }
186 else
187 {
Zbigniew Kurzynski4f45e422020-06-09 12:42:15 +0200188 updateProperty(sensorInterface, oldValue,
189 std::numeric_limits<double>::quiet_NaN(),
190 dbusName);
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200191 }
192 }
193 }
194 }
195 }
196}
197
James Feistd8705872019-02-08 13:26:09 -0800198void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700199{
James Feist838529b2020-09-02 16:57:05 -0700200
James Feist6714a252018-09-10 15:26:18 -0700201 if (err == boost::system::errc::bad_file_descriptor)
202 {
203 return; // we're being destroyed
204 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700205 if (err == boost::system::errc::operation_canceled)
James Feist838529b2020-09-02 16:57:05 -0700206 {
207 if (readingStateGood())
208 {
209 if (!loggedInterfaceDown)
210 {
211 std::cerr << name << " interface down!\n";
212 loggedInterfaceDown = true;
213 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700214 pollTime = CPUSensor::sensorPollMs * 10u;
James Feist838529b2020-09-02 16:57:05 -0700215 markFunctional(false);
216 }
217 return;
218 }
219 loggedInterfaceDown = false;
James Feistc22b8422020-07-07 14:40:39 -0700220 pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700221 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700222 if (!err)
223 {
224 std::string response;
225 try
226 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700227 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700228 rawValue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700229 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700230 double nvalue = rawValue / CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800231
232 if (show)
James Feist6714a252018-09-10 15:26:18 -0700233 {
Josh Lehan833661a2020-03-04 17:35:41 -0800234 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800235 }
Josh Lehan833661a2020-03-04 17:35:41 -0800236 else
237 {
238 value = nvalue;
239 }
Zbigniew Kurzynski98be9842020-09-07 18:49:15 +0200240 if (minMaxReadCounter++ % 8 == 0)
241 {
242 updateMinMaxValues();
243 }
Josh Lehan833661a2020-03-04 17:35:41 -0800244
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800245 double gTcontrol = gCpuSensors[nameTcontrol]
246 ? gCpuSensors[nameTcontrol]->value
247 : std::numeric_limits<double>::quiet_NaN();
248 if (gTcontrol != privTcontrol)
249 {
250 privTcontrol = gTcontrol;
251
252 if (!thresholds.empty())
253 {
254 std::vector<thresholds::Threshold> newThresholds;
255 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700256 CPUSensor::sensorScaleFactor,
257 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800258 {
259 if (!std::equal(thresholds.begin(), thresholds.end(),
260 newThresholds.begin(),
261 newThresholds.end()))
262 {
263 thresholds = newThresholds;
264 if (show)
265 {
266 thresholds::updateThresholds(this);
267 }
268 }
269 }
270 else
271 {
272 std::cerr << "Failure to update thresholds for " << name
273 << "\n";
274 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800275 }
276 }
James Feist6714a252018-09-10 15:26:18 -0700277 }
James Feistd8705872019-02-08 13:26:09 -0800278 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700279 {
James Feist961bf092020-07-01 16:38:12 -0700280 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700281 }
282 }
283 else
284 {
James Feist1169eb42018-10-31 10:08:47 -0700285 pollTime = sensorFailedPollTimeMs;
James Feist961bf092020-07-01 16:38:12 -0700286 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700287 }
288
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700289 responseStream.clear();
James Feist6714a252018-09-10 15:26:18 -0700290}
291
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700292void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700293{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800294 if (show)
295 {
296 thresholds::checkThresholds(this);
297 }
James Feist6714a252018-09-10 15:26:18 -0700298}