blob: a6dc27590d8422bbec91712c2a51f65216ad3c72 [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
19#include <CPUSensor.hpp>
20#include <Utils.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
24#include <iostream>
25#include <limits>
26#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <string>
29
James Feistd8705872019-02-08 13:26:09 -080030CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
31 sdbusplus::asio::object_server& objectServer,
32 std::shared_ptr<sdbusplus::asio::connection>& conn,
33 boost::asio::io_service& io, const std::string& sensorName,
34 std::vector<thresholds::Threshold>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080035 const std::string& sensorConfiguration, int cpuId,
36 bool show) :
James Feistdc6c55f2018-10-31 12:53:20 -070037 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
James Feistce3fca42018-11-21 12:58:24 -080038 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
39 minReading),
James Feist71d31b22019-01-02 16:57:54 -080040 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 waitTimer(io), show(show),
42 privTcontrol(std::numeric_limits<double>::quiet_NaN()), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070043{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044 nameTcontrol = labelTcontrol;
45 nameTcontrol += " CPU" + std::to_string(cpuId);
46
47 if (show)
James Feist6714a252018-09-10 15:26:18 -070048 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080049 sensorInterface = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070050 "/xyz/openbmc_project/sensors/temperature/" + name,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 "xyz.openbmc_project.Sensor.Value");
52 if (thresholds::hasWarningInterface(thresholds))
53 {
54 thresholdInterfaceWarning = objectServer.add_interface(
55 "/xyz/openbmc_project/sensors/temperature/" + name,
56 "xyz.openbmc_project.Sensor.Threshold.Warning");
57 }
58 if (thresholds::hasCriticalInterface(thresholds))
59 {
60 thresholdInterfaceCritical = objectServer.add_interface(
61 "/xyz/openbmc_project/sensors/temperature/" + name,
62 "xyz.openbmc_project.Sensor.Threshold.Critical");
63 }
64 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070065 }
James Feist71d31b22019-01-02 16:57:54 -080066 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067 setupRead();
James Feist6714a252018-09-10 15:26:18 -070068}
69
70CPUSensor::~CPUSensor()
71{
72 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 inputDev.close();
74 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080075 if (show)
76 {
77 objServer.remove_interface(thresholdInterfaceWarning);
78 objServer.remove_interface(thresholdInterfaceCritical);
79 objServer.remove_interface(sensorInterface);
80 }
James Feist6714a252018-09-10 15:26:18 -070081}
82
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070084{
85 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080087 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070089}
90
James Feistd8705872019-02-08 13:26:09 -080091void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070092{
93 if (err == boost::system::errc::bad_file_descriptor)
94 {
95 return; // we're being destroyed
96 }
James Feist1169eb42018-10-31 10:08:47 -070097 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -070099 if (!err)
100 {
101 std::string response;
102 try
103 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 std::getline(responseStream, response);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800105 double nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 responseStream.clear();
107 nvalue /= CPUSensor::sensorScaleFactor;
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +0530108 if (overridenState)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530109 {
110 nvalue = overriddenValue;
111 }
James Feist6714a252018-09-10 15:26:18 -0700112 if (nvalue != value)
113 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800114 if (show)
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800115 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800116 updateValue(nvalue);
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800117 }
118 else
119 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800120 value = nvalue;
121 }
122 }
123 double gTcontrol = gCpuSensors[nameTcontrol]
124 ? gCpuSensors[nameTcontrol]->value
125 : std::numeric_limits<double>::quiet_NaN();
126 if (gTcontrol != privTcontrol)
127 {
128 privTcontrol = gTcontrol;
129
130 if (!thresholds.empty())
131 {
132 std::vector<thresholds::Threshold> newThresholds;
133 if (parseThresholdsFromAttr(newThresholds, path,
134 CPUSensor::sensorScaleFactor))
135 {
136 if (!std::equal(thresholds.begin(), thresholds.end(),
137 newThresholds.begin(),
138 newThresholds.end()))
139 {
140 thresholds = newThresholds;
141 if (show)
142 {
143 thresholds::updateThresholds(this);
144 }
145 }
146 }
147 else
148 {
149 std::cerr << "Failure to update thresholds for " << name
150 << "\n";
151 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800152 }
153 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700154 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700155 }
James Feistd8705872019-02-08 13:26:09 -0800156 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700157 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700159 }
160 }
161 else
162 {
James Feist1169eb42018-10-31 10:08:47 -0700163 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700164 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700165 }
166
James Feistdc6c55f2018-10-31 12:53:20 -0700167 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700168 {
169 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800170 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700171 {
James Feistdc6c55f2018-10-31 12:53:20 -0700172 // only print once
173 if (errCount == warnAfterErrorCount)
174 {
175 std::cerr << "Failure to read sensor " << name << " at " << path
176 << "\n";
177 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800178 if (show)
179 {
180 updateValue(0);
181 }
182 else
183 {
184 value = 0;
185 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700187 }
188 else
189 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800191 if (show)
192 {
193 updateValue(std::numeric_limits<double>::quiet_NaN());
194 }
195 else
196 {
197 value = std::numeric_limits<double>::quiet_NaN();
198 }
James Feist6714a252018-09-10 15:26:18 -0700199 }
200 }
201
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700202 responseStream.clear();
203 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700204 int fd = open(path.c_str(), O_RDONLY);
205 if (fd <= 0)
206 {
207 return; // we're no longer valid
208 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700209 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700210 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800211 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700212 if (ec == boost::asio::error::operation_aborted)
213 {
214 return; // we're being canceled
215 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700216 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700217 });
218}
219
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700220void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700221{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800222 if (show)
223 {
224 thresholds::checkThresholds(this);
225 }
James Feist6714a252018-09-10 15:26:18 -0700226}