James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 30 | CPUSensor::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 Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 35 | const std::string& sensorConfiguration, int cpuId, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 36 | bool show, double dtsOffset) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 37 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 38 | std::move(_thresholds), sensorConfiguration, objectType, maxReading, |
| 39 | minReading), |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 40 | objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)), |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 41 | path(path), waitTimer(io), show(show), dtsOffset(dtsOffset), |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 42 | privTcontrol(std::numeric_limits<double>::quiet_NaN()), errCount(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 44 | nameTcontrol = labelTcontrol; |
| 45 | nameTcontrol += " CPU" + std::to_string(cpuId); |
| 46 | |
| 47 | if (show) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 48 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 49 | sensorInterface = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | "/xyz/openbmc_project/sensors/temperature/" + name, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 51 | "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 Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 64 | association = objectServer.add_interface( |
| 65 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 66 | "org.openbmc.Associations"); |
| 67 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 68 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 70 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 71 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | CPUSensor::~CPUSensor() |
| 75 | { |
| 76 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 77 | inputDev.close(); |
| 78 | waitTimer.cancel(); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 79 | if (show) |
| 80 | { |
| 81 | objServer.remove_interface(thresholdInterfaceWarning); |
| 82 | objServer.remove_interface(thresholdInterfaceCritical); |
| 83 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 84 | objServer.remove_interface(association); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 85 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | void CPUSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 89 | { |
| 90 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 91 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 92 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 93 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 96 | void CPUSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | { |
| 98 | if (err == boost::system::errc::bad_file_descriptor) |
| 99 | { |
| 100 | return; // we're being destroyed |
| 101 | } |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 102 | size_t pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 103 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | if (!err) |
| 105 | { |
| 106 | std::string response; |
| 107 | try |
| 108 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 109 | std::getline(responseStream, response); |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 110 | double nvalue = std::stof(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | responseStream.clear(); |
| 112 | nvalue /= CPUSensor::sensorScaleFactor; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | if (nvalue != value) |
| 114 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 115 | if (show) |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 116 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 117 | updateValue(nvalue); |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 121 | 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, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 135 | CPUSensor::sensorScaleFactor, |
| 136 | dtsOffset)) |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 137 | { |
| 138 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 139 | newThresholds.begin(), |
| 140 | newThresholds.end())) |
| 141 | { |
| 142 | thresholds = newThresholds; |
| 143 | if (show) |
| 144 | { |
| 145 | thresholds::updateThresholds(this); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | std::cerr << "Failure to update thresholds for " << name |
| 152 | << "\n"; |
| 153 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 156 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 158 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 160 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 165 | pollTime = sensorFailedPollTimeMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 166 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 167 | } |
| 168 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 169 | if (errCount >= warnAfterErrorCount) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 170 | { |
| 171 | // only an error if power is on |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 172 | if (isPowerOn()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 174 | // only print once |
| 175 | if (errCount == warnAfterErrorCount) |
| 176 | { |
| 177 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 178 | << "\n"; |
| 179 | } |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 180 | if (show) |
| 181 | { |
| 182 | updateValue(0); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | value = 0; |
| 187 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 188 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 189 | } |
| 190 | else |
| 191 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 192 | errCount = 0; // check power again in 10 cycles |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 193 | if (show) |
| 194 | { |
| 195 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | value = std::numeric_limits<double>::quiet_NaN(); |
| 200 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 204 | responseStream.clear(); |
| 205 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 206 | int fd = open(path.c_str(), O_RDONLY); |
| 207 | if (fd <= 0) |
| 208 | { |
| 209 | return; // we're no longer valid |
| 210 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 211 | inputDev.assign(fd); |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 212 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 213 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 214 | if (ec == boost::asio::error::operation_aborted) |
| 215 | { |
| 216 | return; // we're being canceled |
| 217 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 218 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 219 | }); |
| 220 | } |
| 221 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 222 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 223 | { |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 224 | if (show) |
| 225 | { |
| 226 | thresholds::checkThresholds(this); |
| 227 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 228 | } |