blob: 73a9f01538c2384ef205d015b25366484f503824 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10303#include "Thresholds.hpp"
4#include "Utils.hpp"
5
Ed Tanous18b61862025-01-30 10:56:28 -08006#include <boost/asio/io_context.hpp>
7#include <boost/asio/posix/stream_descriptor.hpp>
8#include <boost/asio/steady_timer.hpp>
James Feist8086aba2020-08-25 16:00:59 -07009#include <boost/asio/streambuf.hpp>
James Feist58295ad2019-05-30 15:01:41 -070010#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070011#include <gpiod.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080012#include <sdbusplus/asio/connection.hpp>
James Feist38fb5982020-05-28 10:09:54 -070013#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070014#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070015
Ed Tanous18b61862025-01-30 10:56:28 -080016#include <cstddef>
17#include <cstdint>
18#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070019#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070020#include <string>
Ed Tanous18b61862025-01-30 10:56:28 -080021#include <system_error>
Patrick Venturefd6ba732019-10-31 14:27:39 -070022#include <variant>
23#include <vector>
James Feist6714a252018-09-10 15:26:18 -070024
Thu Nguyen255da6b2022-07-29 10:05:52 +070025class IntelCPUSensor :
26 public Sensor,
27 public std::enable_shared_from_this<IntelCPUSensor>
James Feist6714a252018-09-10 15:26:18 -070028{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070029 public:
Thu Nguyen255da6b2022-07-29 10:05:52 +070030 IntelCPUSensor(const std::string& path, const std::string& objectType,
31 sdbusplus::asio::object_server& objectServer,
32 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080033 boost::asio::io_context& io, const std::string& sensorName,
Thu Nguyen255da6b2022-07-29 10:05:52 +070034 std::vector<thresholds::Threshold>&& thresholds,
35 const std::string& configuration, int cpuId, bool show,
36 double dtsOffset);
37 ~IntelCPUSensor() override;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070038 static constexpr unsigned int sensorScaleFactor = 1000;
39 static constexpr unsigned int sensorPollMs = 1000;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080040 static constexpr size_t warnAfterErrorCount = 10;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080041 static constexpr const char* labelTcontrol = "Tcontrol";
Ed Tanous201a1012024-04-03 18:07:28 -070042 void setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070043
James Feist6714a252018-09-10 15:26:18 -070044 private:
James Feistd8705872019-02-08 13:26:09 -080045 sdbusplus::asio::object_server& objServer;
Ed Tanous77518b22021-11-05 13:02:23 -070046 boost::asio::streambuf readBuf;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047 boost::asio::posix::stream_descriptor inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070048 boost::asio::steady_timer waitTimer;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080049 std::string nameTcontrol;
James Feist930fcde2019-05-28 12:58:43 -070050 std::string path;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080051 double privTcontrol;
Vijay Khemka86dea2b2019-06-06 11:14:37 -070052 double dtsOffset;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080053 bool show;
James Feistc22b8422020-07-07 14:40:39 -070054 size_t pollTime;
James Feist838529b2020-09-02 16:57:05 -070055 bool loggedInterfaceDown = false;
Ed Tanousb429f312022-06-27 16:09:53 -070056 uint8_t minMaxReadCounter{0};
Ed Tanous2049bd22022-07-09 07:20:26 -070057 int fd{};
James Feistd8705872019-02-08 13:26:09 -080058 void handleResponse(const boost::system::error_code& err);
Ed Tanous201a1012024-04-03 18:07:28 -070059 void checkThresholds() override;
60 void updateMinMaxValues();
61 void restartRead();
James Feist6714a252018-09-10 15:26:18 -070062};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080063
Thu Nguyen255da6b2022-07-29 10:05:52 +070064extern boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080065 gCpuSensors;
James Feist58295ad2019-05-30 15:01:41 -070066
Thu Nguyen255da6b2022-07-29 10:05:52 +070067// this is added to intelcpusensor.hpp to avoid having every sensor have to link
James Feist58295ad2019-05-30 15:01:41 -070068// against libgpiod, if another sensor needs it we may move it to utils
Zev Weissafd15042022-07-18 12:28:40 -070069inline bool cpuIsPresent(const SensorBaseConfigMap& gpioConfig)
James Feist58295ad2019-05-30 15:01:41 -070070{
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070071 static boost::container::flat_map<std::string, bool> cpuPresence;
James Feist58295ad2019-05-30 15:01:41 -070072
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070073 auto findName = gpioConfig.find("Name");
74 if (findName == gpioConfig.end())
75 {
76 return false;
77 }
Patrick Williams2aaf7172024-08-16 15:20:40 -040078 std::string gpioName =
79 std::visit(VariantToStringVisitor(), findName->second);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070080
81 auto findIndex = cpuPresence.find(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070082 if (findIndex != cpuPresence.end())
83 {
84 return findIndex->second;
85 }
86
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070087 bool activeHigh = true;
88 auto findPolarity = gpioConfig.find("Polarity");
89 if (findPolarity != gpioConfig.end())
James Feist58295ad2019-05-30 15:01:41 -070090 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070091 if (std::string("Low") ==
92 std::visit(VariantToStringVisitor(), findPolarity->second))
James Feist58295ad2019-05-30 15:01:41 -070093 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070094 activeHigh = false;
James Feist58295ad2019-05-30 15:01:41 -070095 }
96 }
97
Jae Hyun Yooffa07e22019-07-17 10:47:18 -070098 auto line = gpiod::find_line(gpioName);
James Feist58295ad2019-05-30 15:01:41 -070099 if (!line)
100 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700101 std::cerr << "Error requesting gpio: " << gpioName << "\n";
102 return false;
James Feist58295ad2019-05-30 15:01:41 -0700103 }
104
Ed Tanous74cffa82022-01-25 13:00:28 -0800105 bool resp = false;
James Feist58295ad2019-05-30 15:01:41 -0700106 try
107 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700108 line.request({"cpusensor", gpiod::line_request::DIRECTION_INPUT,
109 activeHigh ? 0 : gpiod::line_request::FLAG_ACTIVE_LOW});
Ed Tanous2049bd22022-07-09 07:20:26 -0700110 resp = (line.get_value() != 0);
James Feist58295ad2019-05-30 15:01:41 -0700111 }
Patrick Williams26601e82021-10-06 12:43:25 -0500112 catch (const std::system_error&)
James Feist58295ad2019-05-30 15:01:41 -0700113 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700114 std::cerr << "Error reading gpio: " << gpioName << "\n";
115 return false;
James Feist58295ad2019-05-30 15:01:41 -0700116 }
117
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700118 cpuPresence[gpioName] = resp;
119
James Feist58295ad2019-05-30 15:01:41 -0700120 return resp;
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700121}