Debian Codegraph @

src/core/components/sensors/amd/junctiontemp.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2021 Juan Palacios <[email protected]>
3
4 #include "junctiontemp.h"
5
6 #include "../gpusensorprovider.h"
7 #include "../graphitemprofilepart.h"
8 #include "../graphitemxmlparser.h"
9 #include "../sensor.h"
10 #include "common/fileutils.h"
11 #include "common/stringutils.h"
12 #include "core/info/igpuinfo.h"
13 #include "core/info/vendor.h"
14 #include "core/iprofilepart.h"
15 #include "core/iprofilepartxmlparser.h"
16 #include "core/profilepartprovider.h"
17 #include "core/profilepartxmlparserprovider.h"
18 #include "core/sysfsdatasource.h"
19 #include <filesystem>
20 #include <memory>
21 #include <optional>
22 #include <spdlog/spdlog.h>
23 #include <string>
24 #include <units.h>
25 #include <utility>
26 #include <vector>
27
28 namespace AMD::JunctionTemp {
29
30 class Provider final : public IGPUSensorProvider::IProvider
31 {
32 public:
33 std::vector<std::unique_ptr<ISensor>>
34 provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override
35 {
36 if (gpuInfo.vendor() != Vendor::AMD)
37 return {};
38
39 auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon");
40 if (!path)
41 return {};
42
43 auto tempInput = path.value() / "temp2_input";
44 if (!Utils::File::isSysFSEntryValid(tempInput))
45 return {};
46
47 int value;
48 auto data = Utils::File::readFileLines(tempInput);
49 if (!Utils::String::toNumber<int>(value, data.front())) {
50 SPDLOG_WARN("Unknown data format on {}", tempInput.string());
51 SPDLOG_DEBUG(data.front());
52 return {};
53 }
54
55 std::optional<
56 std::pair<units::temperature::celsius_t, units::temperature::celsius_t>>
57 range;
58
59 data = Utils::File::readFileLines(path.value() / "temp2_crit");
60 if (!data.empty()) {
61 if (Utils::String::toNumber<int>(value, data.front()) &&
62 // do not use bogus values, see #103
63 (value >= 0 && value < 150000)) {
64 range = {units::temperature::celsius_t(0),
65 units::temperature::celsius_t(value / 1000.0)};
66 }
67 }
68
69 std::vector<std::unique_ptr<IDataSource<int>>> dataSources;
70 dataSources.emplace_back(std::make_unique<SysFSDataSource<int>>(
71 tempInput, [](std::string const &data, int &output) {
72 int value;
73 Utils::String::toNumber<int>(value, data);
74 output = value / 1000;
75 }));
76
77 std::vector<std::unique_ptr<ISensor>> sensors;
78 sensors.emplace_back(
79 std::make_unique<Sensor<units::temperature::celsius_t, int>>(
80 AMD::JunctionTemp::ItemID, std::move(dataSources), std::move(range)));
81
82 return sensors;
83 }
84 };
85
86 static bool register_()
87 {
88 GPUSensorProvider::registerProvider(
89 std::make_unique<AMD::JunctionTemp::Provider>());
90
91 ProfilePartProvider::registerProvider(AMD::JunctionTemp::ItemID, []() {
92 return std::make_unique<GraphItemProfilePart>(AMD::JunctionTemp::ItemID,
93 "red");
94 });
95
96 ProfilePartXMLParserProvider::registerProvider(AMD::JunctionTemp::ItemID, []() {
97 return std::make_unique<GraphItemXMLParser>(AMD::JunctionTemp::ItemID);
98 });
99
100 return true;
101 }
102
103 static bool const registered_ = register_();
104
105 } // namespace AMD::JunctionTemp