blob: 27c9c38883f686fb8947aee11628f8f7ff66dedc [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>
26#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
James Feist6714a252018-09-10 15:26:18 -070028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
James Feist6714a252018-09-10 15:26:18 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
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,
40 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) :
James Feist930fcde2019-05-28 12:58:43 -070043 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080044 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
45 minReading),
James Feist71d31b22019-01-02 16:57:54 -080046 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050047 waitTimer(io), path(path),
48 privTcontrol(std::numeric_limits<double>::quiet_NaN()),
49 dtsOffset(dtsOffset), show(show), errCount(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);
53
54 if (show)
James Feist6714a252018-09-10 15:26:18 -070055 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056 sensorInterface = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070057 "/xyz/openbmc_project/sensors/temperature/" + name,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058 "xyz.openbmc_project.Sensor.Value");
59 if (thresholds::hasWarningInterface(thresholds))
60 {
61 thresholdInterfaceWarning = objectServer.add_interface(
62 "/xyz/openbmc_project/sensors/temperature/" + name,
63 "xyz.openbmc_project.Sensor.Threshold.Warning");
64 }
65 if (thresholds::hasCriticalInterface(thresholds))
66 {
67 thresholdInterfaceCritical = objectServer.add_interface(
68 "/xyz/openbmc_project/sensors/temperature/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Critical");
70 }
James Feist078f2322019-03-08 11:09:05 -080071 association = objectServer.add_interface(
72 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070073 association::interface);
James Feist078f2322019-03-08 11:09:05 -080074
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080075 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070076 }
James Feist71d31b22019-01-02 16:57:54 -080077 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070078 setupRead();
James Feist6714a252018-09-10 15:26:18 -070079}
80
81CPUSensor::~CPUSensor()
82{
83 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 inputDev.close();
85 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080086 if (show)
87 {
88 objServer.remove_interface(thresholdInterfaceWarning);
89 objServer.remove_interface(thresholdInterfaceCritical);
90 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080091 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080092 }
James Feist6714a252018-09-10 15:26:18 -070093}
94
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070096{
97 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080099 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700101}
102
James Feistd8705872019-02-08 13:26:09 -0800103void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700104{
105 if (err == boost::system::errc::bad_file_descriptor)
106 {
107 return; // we're being destroyed
108 }
James Feist1169eb42018-10-31 10:08:47 -0700109 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700111 if (!err)
112 {
113 std::string response;
114 try
115 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116 std::getline(responseStream, response);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800117 double nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118 responseStream.clear();
119 nvalue /= CPUSensor::sensorScaleFactor;
James Feist6714a252018-09-10 15:26:18 -0700120 if (nvalue != value)
121 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800122 if (show)
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800123 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800124 updateValue(nvalue);
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800125 }
126 else
127 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800128 value = nvalue;
129 }
130 }
131 double gTcontrol = gCpuSensors[nameTcontrol]
132 ? gCpuSensors[nameTcontrol]->value
133 : std::numeric_limits<double>::quiet_NaN();
134 if (gTcontrol != privTcontrol)
135 {
136 privTcontrol = gTcontrol;
137
138 if (!thresholds.empty())
139 {
140 std::vector<thresholds::Threshold> newThresholds;
141 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700142 CPUSensor::sensorScaleFactor,
143 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800144 {
145 if (!std::equal(thresholds.begin(), thresholds.end(),
146 newThresholds.begin(),
147 newThresholds.end()))
148 {
149 thresholds = newThresholds;
150 if (show)
151 {
152 thresholds::updateThresholds(this);
153 }
154 }
155 }
156 else
157 {
158 std::cerr << "Failure to update thresholds for " << name
159 << "\n";
160 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800161 }
162 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700163 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700164 }
James Feistd8705872019-02-08 13:26:09 -0800165 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700166 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700168 }
169 }
170 else
171 {
James Feist1169eb42018-10-31 10:08:47 -0700172 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700174 }
175
James Feistdc6c55f2018-10-31 12:53:20 -0700176 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700177 {
178 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800179 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700180 {
James Feistdc6c55f2018-10-31 12:53:20 -0700181 // only print once
182 if (errCount == warnAfterErrorCount)
183 {
184 std::cerr << "Failure to read sensor " << name << " at " << path
185 << "\n";
186 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800187 if (show)
188 {
189 updateValue(0);
190 }
191 else
192 {
193 value = 0;
194 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700196 }
197 else
198 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700199 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800200 if (show)
201 {
202 updateValue(std::numeric_limits<double>::quiet_NaN());
203 }
204 else
205 {
206 value = std::numeric_limits<double>::quiet_NaN();
207 }
James Feist6714a252018-09-10 15:26:18 -0700208 }
209 }
210
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700211 responseStream.clear();
212 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700213 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700214 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700215 {
216 return; // we're no longer valid
217 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700218 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700219 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800220 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700221 if (ec == boost::asio::error::operation_aborted)
222 {
223 return; // we're being canceled
224 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700225 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700226 });
227}
228
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700229void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700230{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800231 if (show)
232 {
233 thresholds::checkThresholds(this);
234 }
James Feist6714a252018-09-10 15:26:18 -0700235}