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 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 30 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 31 | static constexpr double maxReading = 127; |
| 32 | static constexpr double minReading = -128; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 34 | CPUSensor::CPUSensor(const std::string& path, const std::string& objectType, |
| 35 | sdbusplus::asio::object_server& objectServer, |
| 36 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 37 | boost::asio::io_service& io, const std::string& sensorName, |
| 38 | std::vector<thresholds::Threshold>&& _thresholds, |
| 39 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 40 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), path, |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 41 | std::move(_thresholds), sensorConfiguration, objectType, maxReading, |
| 42 | minReading), |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 43 | objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 44 | waitTimer(io), errCount(0) |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 45 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 47 | sensorInterface = objectServer.add_interface( |
| 48 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 49 | "xyz.openbmc_project.Sensor.Value"); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 50 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 51 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 52 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 54 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 55 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 56 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 58 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 60 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 61 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 62 | setInitialProperties(conn); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 63 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 64 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | CPUSensor::~CPUSensor() |
| 68 | { |
| 69 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 70 | inputDev.close(); |
| 71 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 72 | objServer.remove_interface(thresholdInterfaceWarning); |
| 73 | objServer.remove_interface(thresholdInterfaceCritical); |
| 74 | objServer.remove_interface(sensorInterface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 77 | void CPUSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 78 | { |
| 79 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 80 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 81 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 85 | void CPUSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | { |
| 87 | if (err == boost::system::errc::bad_file_descriptor) |
| 88 | { |
| 89 | return; // we're being destroyed |
| 90 | } |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 91 | size_t pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 92 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | if (!err) |
| 94 | { |
| 95 | std::string response; |
| 96 | try |
| 97 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 98 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 99 | float nvalue = std::stof(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | responseStream.clear(); |
| 101 | nvalue /= CPUSensor::sensorScaleFactor; |
Richard Marian Thomaiyar | b2eb29a | 2018-12-08 18:26:58 +0530 | [diff] [blame] | 102 | if (overridenState) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 103 | { |
| 104 | nvalue = overriddenValue; |
| 105 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | if (nvalue != value) |
| 107 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | } |
Jae Hyun Yoo | 95b8a2d | 2019-02-25 20:15:09 -0800 | [diff] [blame^] | 110 | if (!thresholds.empty()) |
| 111 | { |
| 112 | std::vector<thresholds::Threshold> newThresholds; |
| 113 | if (parseThresholdsFromAttr(newThresholds, path, |
| 114 | CPUSensor::sensorScaleFactor)) |
| 115 | { |
| 116 | if (!std::equal(thresholds.begin(), thresholds.end(), |
| 117 | newThresholds.begin(), newThresholds.end())) |
| 118 | { |
| 119 | thresholds = newThresholds; |
| 120 | thresholds::updateThresholds(this); |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | std::cerr << "Failure to update thresholds for " << name |
| 126 | << "\n"; |
| 127 | } |
| 128 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 129 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 131 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 133 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | else |
| 137 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 138 | pollTime = sensorFailedPollTimeMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 139 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 140 | } |
| 141 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 142 | if (errCount >= warnAfterErrorCount) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 143 | { |
| 144 | // only an error if power is on |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 145 | if (isPowerOn()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 146 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 147 | // only print once |
| 148 | if (errCount == warnAfterErrorCount) |
| 149 | { |
| 150 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 151 | << "\n"; |
| 152 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 153 | updateValue(0); |
| 154 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | } |
| 156 | else |
| 157 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 158 | errCount = 0; // check power again in 10 cycles |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 159 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 163 | responseStream.clear(); |
| 164 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 165 | int fd = open(path.c_str(), O_RDONLY); |
| 166 | if (fd <= 0) |
| 167 | { |
| 168 | return; // we're no longer valid |
| 169 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 170 | inputDev.assign(fd); |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 171 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 172 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | if (ec == boost::asio::error::operation_aborted) |
| 174 | { |
| 175 | return; // we're being canceled |
| 176 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 177 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 178 | }); |
| 179 | } |
| 180 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 181 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 182 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 183 | thresholds::checkThresholds(this); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | } |