blob: ea869c9ab4e12e97af002ec5a1e41bdbf88d2a1d [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "CPUSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <istream>
James Feist6714a252018-09-10 15:26:18 -070031#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070034#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <vector>
James Feist6714a252018-09-10 15:26:18 -070036
James Feistd8705872019-02-08 13:26:09 -080037CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
38 sdbusplus::asio::object_server& objectServer,
39 std::shared_ptr<sdbusplus::asio::connection>& conn,
40 boost::asio::io_service& io, const std::string& sensorName,
41 std::vector<thresholds::Threshold>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080042 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070043 bool show, double dtsOffset) :
James Feist930fcde2019-05-28 12:58:43 -070044 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080045 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
46 minReading),
James Feist71d31b22019-01-02 16:57:54 -080047 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050048 waitTimer(io), path(path),
49 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
50 dtsOffset(dtsOffset), show(show), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070051{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080052 nameTcontrol = labelTcontrol;
53 nameTcontrol += " CPU" + std::to_string(cpuId);
54
55 if (show)
James Feist6714a252018-09-10 15:26:18 -070056 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020057 if (auto fileParts = thresholds::splitFileName(path))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020059 auto [type, nr, item] = *fileParts;
60 std::string interfacePath;
61 if (type.compare("power") == 0)
62 {
63 interfacePath = "/xyz/openbmc_project/sensors/power/" + name;
64 }
65 else
66 {
67 interfacePath =
68 "/xyz/openbmc_project/sensors/temperature/" + name;
69 }
James Feist078f2322019-03-08 11:09:05 -080070
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +020071 sensorInterface = objectServer.add_interface(
72 interfacePath, "xyz.openbmc_project.Sensor.Value");
73 if (thresholds::hasWarningInterface(thresholds))
74 {
75 thresholdInterfaceWarning = objectServer.add_interface(
76 interfacePath,
77 "xyz.openbmc_project.Sensor.Threshold.Warning");
78 }
79 if (thresholds::hasCriticalInterface(thresholds))
80 {
81 thresholdInterfaceCritical = objectServer.add_interface(
82 interfacePath,
83 "xyz.openbmc_project.Sensor.Threshold.Critical");
84 }
85 association = objectServer.add_interface(interfacePath,
86 association::interface);
87
88 setInitialProperties(conn);
89 }
James Feist6714a252018-09-10 15:26:18 -070090 }
James Feist71d31b22019-01-02 16:57:54 -080091 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 setupRead();
James Feist6714a252018-09-10 15:26:18 -070093}
94
95CPUSensor::~CPUSensor()
96{
97 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 inputDev.close();
99 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800100 if (show)
101 {
102 objServer.remove_interface(thresholdInterfaceWarning);
103 objServer.remove_interface(thresholdInterfaceCritical);
104 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800105 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800106 }
James Feist6714a252018-09-10 15:26:18 -0700107}
108
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700110{
111 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800113 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700115}
116
James Feistd8705872019-02-08 13:26:09 -0800117void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700118{
119 if (err == boost::system::errc::bad_file_descriptor)
120 {
121 return; // we're being destroyed
122 }
James Feist1169eb42018-10-31 10:08:47 -0700123 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700125 if (!err)
126 {
127 std::string response;
128 try
129 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700130 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800131 double nvalue = std::stod(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700132 responseStream.clear();
133 nvalue /= CPUSensor::sensorScaleFactor;
Josh Lehan833661a2020-03-04 17:35:41 -0800134
135 if (show)
James Feist6714a252018-09-10 15:26:18 -0700136 {
Josh Lehan833661a2020-03-04 17:35:41 -0800137 updateValue(nvalue);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800138 }
Josh Lehan833661a2020-03-04 17:35:41 -0800139 else
140 {
141 value = nvalue;
142 }
143
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800144 double gTcontrol = gCpuSensors[nameTcontrol]
145 ? gCpuSensors[nameTcontrol]->value
146 : std::numeric_limits<double>::quiet_NaN();
147 if (gTcontrol != privTcontrol)
148 {
149 privTcontrol = gTcontrol;
150
151 if (!thresholds.empty())
152 {
153 std::vector<thresholds::Threshold> newThresholds;
154 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700155 CPUSensor::sensorScaleFactor,
156 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800157 {
158 if (!std::equal(thresholds.begin(), thresholds.end(),
159 newThresholds.begin(),
160 newThresholds.end()))
161 {
162 thresholds = newThresholds;
163 if (show)
164 {
165 thresholds::updateThresholds(this);
166 }
167 }
168 }
169 else
170 {
171 std::cerr << "Failure to update thresholds for " << name
172 << "\n";
173 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800174 }
175 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700177 }
James Feistd8705872019-02-08 13:26:09 -0800178 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700179 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700180 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700181 }
182 }
183 else
184 {
James Feist1169eb42018-10-31 10:08:47 -0700185 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700187 }
188
James Feistdc6c55f2018-10-31 12:53:20 -0700189 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700190 {
191 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800192 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700193 {
James Feistdc6c55f2018-10-31 12:53:20 -0700194 // only print once
195 if (errCount == warnAfterErrorCount)
196 {
197 std::cerr << "Failure to read sensor " << name << " at " << path
198 << "\n";
199 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800200 if (show)
201 {
202 updateValue(0);
203 }
204 else
205 {
206 value = 0;
207 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700208 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700209 }
210 else
211 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700212 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800213 if (show)
214 {
215 updateValue(std::numeric_limits<double>::quiet_NaN());
216 }
217 else
218 {
219 value = std::numeric_limits<double>::quiet_NaN();
220 }
James Feist6714a252018-09-10 15:26:18 -0700221 }
222 }
223
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700224 responseStream.clear();
225 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700226 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700227 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700228 {
229 return; // we're no longer valid
230 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700231 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700232 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800233 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700234 if (ec == boost::asio::error::operation_aborted)
235 {
236 return; // we're being canceled
237 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700238 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700239 });
240}
241
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700242void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700243{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800244 if (show)
245 {
246 thresholds::checkThresholds(this);
247 }
James Feist6714a252018-09-10 15:26:18 -0700248}