blob: 4c5eb8b279eec4d331560be9a3d950f3dbd19653 [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 }
James Feist078f2322019-03-08 11:09:05 -080064 association = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/temperature/" + name,
66 "org.openbmc.Associations");
67
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080068 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070069 }
James Feist71d31b22019-01-02 16:57:54 -080070 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 setupRead();
James Feist6714a252018-09-10 15:26:18 -070072}
73
74CPUSensor::~CPUSensor()
75{
76 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 inputDev.close();
78 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080079 if (show)
80 {
81 objServer.remove_interface(thresholdInterfaceWarning);
82 objServer.remove_interface(thresholdInterfaceCritical);
83 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080084 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080085 }
James Feist6714a252018-09-10 15:26:18 -070086}
87
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070089{
90 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080092 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070094}
95
James Feistd8705872019-02-08 13:26:09 -080096void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070097{
98 if (err == boost::system::errc::bad_file_descriptor)
99 {
100 return; // we're being destroyed
101 }
James Feist1169eb42018-10-31 10:08:47 -0700102 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700104 if (!err)
105 {
106 std::string response;
107 try
108 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109 std::getline(responseStream, response);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800110 double nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 responseStream.clear();
112 nvalue /= CPUSensor::sensorScaleFactor;
James Feist6714a252018-09-10 15:26:18 -0700113 if (nvalue != value)
114 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800115 if (show)
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800116 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800117 updateValue(nvalue);
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800118 }
119 else
120 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800121 value = nvalue;
122 }
123 }
124 double gTcontrol = gCpuSensors[nameTcontrol]
125 ? gCpuSensors[nameTcontrol]->value
126 : std::numeric_limits<double>::quiet_NaN();
127 if (gTcontrol != privTcontrol)
128 {
129 privTcontrol = gTcontrol;
130
131 if (!thresholds.empty())
132 {
133 std::vector<thresholds::Threshold> newThresholds;
134 if (parseThresholdsFromAttr(newThresholds, path,
135 CPUSensor::sensorScaleFactor))
136 {
137 if (!std::equal(thresholds.begin(), thresholds.end(),
138 newThresholds.begin(),
139 newThresholds.end()))
140 {
141 thresholds = newThresholds;
142 if (show)
143 {
144 thresholds::updateThresholds(this);
145 }
146 }
147 }
148 else
149 {
150 std::cerr << "Failure to update thresholds for " << name
151 << "\n";
152 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800153 }
154 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700155 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700156 }
James Feistd8705872019-02-08 13:26:09 -0800157 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700158 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700159 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 }
162 else
163 {
James Feist1169eb42018-10-31 10:08:47 -0700164 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700165 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700166 }
167
James Feistdc6c55f2018-10-31 12:53:20 -0700168 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700169 {
170 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800171 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700172 {
James Feistdc6c55f2018-10-31 12:53:20 -0700173 // only print once
174 if (errCount == warnAfterErrorCount)
175 {
176 std::cerr << "Failure to read sensor " << name << " at " << path
177 << "\n";
178 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800179 if (show)
180 {
181 updateValue(0);
182 }
183 else
184 {
185 value = 0;
186 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700188 }
189 else
190 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700191 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800192 if (show)
193 {
194 updateValue(std::numeric_limits<double>::quiet_NaN());
195 }
196 else
197 {
198 value = std::numeric_limits<double>::quiet_NaN();
199 }
James Feist6714a252018-09-10 15:26:18 -0700200 }
201 }
202
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700203 responseStream.clear();
204 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700205 int fd = open(path.c_str(), O_RDONLY);
206 if (fd <= 0)
207 {
208 return; // we're no longer valid
209 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700210 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700211 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800212 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700213 if (ec == boost::asio::error::operation_aborted)
214 {
215 return; // we're being canceled
216 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700217 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700218 });
219}
220
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700221void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700222{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800223 if (show)
224 {
225 thresholds::checkThresholds(this);
226 }
James Feist6714a252018-09-10 15:26:18 -0700227}