blob: 5aedf4b3cab3cd23546519aa82d423034fe30a7c [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>
27#include <limits>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30#include <string>
31
James Feistd8705872019-02-08 13:26:09 -080032CPUSensor::CPUSensor(const std::string& path, const std::string& objectType,
33 sdbusplus::asio::object_server& objectServer,
34 std::shared_ptr<sdbusplus::asio::connection>& conn,
35 boost::asio::io_service& io, const std::string& sensorName,
36 std::vector<thresholds::Threshold>&& _thresholds,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080037 const std::string& sensorConfiguration, int cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -070038 bool show, double dtsOffset) :
James Feist930fcde2019-05-28 12:58:43 -070039 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feistce3fca42018-11-21 12:58:24 -080040 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
41 minReading),
James Feist71d31b22019-01-02 16:57:54 -080042 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
Vijay Khemka86dea2b2019-06-06 11:14:37 -070043 path(path), waitTimer(io), show(show), dtsOffset(dtsOffset),
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044 privTcontrol(std::numeric_limits<double>::quiet_NaN()), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070045{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080046 nameTcontrol = labelTcontrol;
47 nameTcontrol += " CPU" + std::to_string(cpuId);
48
49 if (show)
James Feist6714a252018-09-10 15:26:18 -070050 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 sensorInterface = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070052 "/xyz/openbmc_project/sensors/temperature/" + name,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 "xyz.openbmc_project.Sensor.Value");
54 if (thresholds::hasWarningInterface(thresholds))
55 {
56 thresholdInterfaceWarning = objectServer.add_interface(
57 "/xyz/openbmc_project/sensors/temperature/" + name,
58 "xyz.openbmc_project.Sensor.Threshold.Warning");
59 }
60 if (thresholds::hasCriticalInterface(thresholds))
61 {
62 thresholdInterfaceCritical = objectServer.add_interface(
63 "/xyz/openbmc_project/sensors/temperature/" + name,
64 "xyz.openbmc_project.Sensor.Threshold.Critical");
65 }
James Feist078f2322019-03-08 11:09:05 -080066 association = objectServer.add_interface(
67 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070068 association::interface);
James Feist078f2322019-03-08 11:09:05 -080069
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080070 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070071 }
James Feist71d31b22019-01-02 16:57:54 -080072 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073 setupRead();
James Feist6714a252018-09-10 15:26:18 -070074}
75
76CPUSensor::~CPUSensor()
77{
78 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079 inputDev.close();
80 waitTimer.cancel();
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080081 if (show)
82 {
83 objServer.remove_interface(thresholdInterfaceWarning);
84 objServer.remove_interface(thresholdInterfaceCritical);
85 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080086 objServer.remove_interface(association);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080087 }
James Feist6714a252018-09-10 15:26:18 -070088}
89
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070091{
92 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080094 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070096}
97
James Feistd8705872019-02-08 13:26:09 -080098void CPUSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070099{
100 if (err == boost::system::errc::bad_file_descriptor)
101 {
102 return; // we're being destroyed
103 }
James Feist1169eb42018-10-31 10:08:47 -0700104 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -0700106 if (!err)
107 {
108 std::string response;
109 try
110 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 std::getline(responseStream, response);
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800112 double nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113 responseStream.clear();
114 nvalue /= CPUSensor::sensorScaleFactor;
James Feist6714a252018-09-10 15:26:18 -0700115 if (nvalue != value)
116 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800117 if (show)
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800118 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800119 updateValue(nvalue);
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800120 }
121 else
122 {
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800123 value = nvalue;
124 }
125 }
126 double gTcontrol = gCpuSensors[nameTcontrol]
127 ? gCpuSensors[nameTcontrol]->value
128 : std::numeric_limits<double>::quiet_NaN();
129 if (gTcontrol != privTcontrol)
130 {
131 privTcontrol = gTcontrol;
132
133 if (!thresholds.empty())
134 {
135 std::vector<thresholds::Threshold> newThresholds;
136 if (parseThresholdsFromAttr(newThresholds, path,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700137 CPUSensor::sensorScaleFactor,
138 dtsOffset))
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800139 {
140 if (!std::equal(thresholds.begin(), thresholds.end(),
141 newThresholds.begin(),
142 newThresholds.end()))
143 {
144 thresholds = newThresholds;
145 if (show)
146 {
147 thresholds::updateThresholds(this);
148 }
149 }
150 }
151 else
152 {
153 std::cerr << "Failure to update thresholds for " << name
154 << "\n";
155 }
Jae Hyun Yoo95b8a2d2019-02-25 20:15:09 -0800156 }
157 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700159 }
James Feistd8705872019-02-08 13:26:09 -0800160 catch (const std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700161 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700162 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700163 }
164 }
165 else
166 {
James Feist1169eb42018-10-31 10:08:47 -0700167 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700168 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700169 }
170
James Feistdc6c55f2018-10-31 12:53:20 -0700171 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700172 {
173 // only an error if power is on
James Feist71d31b22019-01-02 16:57:54 -0800174 if (isPowerOn())
James Feist6714a252018-09-10 15:26:18 -0700175 {
James Feistdc6c55f2018-10-31 12:53:20 -0700176 // only print once
177 if (errCount == warnAfterErrorCount)
178 {
179 std::cerr << "Failure to read sensor " << name << " at " << path
180 << "\n";
181 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800182 if (show)
183 {
184 updateValue(0);
185 }
186 else
187 {
188 value = 0;
189 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700191 }
192 else
193 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 errCount = 0; // check power again in 10 cycles
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800195 if (show)
196 {
197 updateValue(std::numeric_limits<double>::quiet_NaN());
198 }
199 else
200 {
201 value = std::numeric_limits<double>::quiet_NaN();
202 }
James Feist6714a252018-09-10 15:26:18 -0700203 }
204 }
205
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700206 responseStream.clear();
207 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700208 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700209 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700210 {
211 return; // we're no longer valid
212 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700213 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700214 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800215 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700216 if (ec == boost::asio::error::operation_aborted)
217 {
218 return; // we're being canceled
219 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700220 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700221 });
222}
223
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700224void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700225{
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800226 if (show)
227 {
228 thresholds::checkThresholds(this);
229 }
James Feist6714a252018-09-10 15:26:18 -0700230}