Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include "NvidiaGpuDevice.hpp" |
| 8 | |
| 9 | #include "NvidiaDeviceDiscovery.hpp" |
| 10 | #include "NvidiaGpuSensor.hpp" |
| 11 | #include "Thresholds.hpp" |
| 12 | #include "Utils.hpp" |
| 13 | |
| 14 | #include <bits/basic_string.h> |
| 15 | |
| 16 | #include <MctpRequester.hpp> |
| 17 | #include <boost/asio/io_context.hpp> |
| 18 | #include <phosphor-logging/lg2.hpp> |
| 19 | #include <sdbusplus/asio/connection.hpp> |
| 20 | #include <sdbusplus/asio/object_server.hpp> |
| 21 | |
| 22 | #include <chrono> |
| 23 | #include <cstdint> |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name, |
| 29 | const std::string& path, |
| 30 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 31 | uint8_t eid, boost::asio::io_context& io, |
| 32 | mctp::MctpRequester& mctpRequester, |
| 33 | sdbusplus::asio::object_server& objectServer) : |
| 34 | eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}), |
| 35 | waitTimer(io, std::chrono::steady_clock::duration(0)), |
| 36 | mctpRequester(mctpRequester), conn(conn), objectServer(objectServer), |
| 37 | configs(configs), name(escapeName(name)), path(path) |
| 38 | { |
| 39 | makeSensors(); |
| 40 | } |
| 41 | |
| 42 | void GpuDevice::makeSensors() |
| 43 | { |
| 44 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( |
Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame^] | 45 | conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId, |
| 46 | objectServer, std::vector<thresholds::Threshold>{}); |
| 47 | |
| 48 | tLimitSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 49 | conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId, |
| 50 | objectServer, std::vector<thresholds::Threshold>{}); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 51 | |
| 52 | lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME", |
| 53 | name, "PATH", path); |
| 54 | |
| 55 | read(); |
| 56 | } |
| 57 | |
| 58 | void GpuDevice::read() |
| 59 | { |
| 60 | tempSensor->update(); |
Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame^] | 61 | tLimitSensor->update(); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 62 | |
| 63 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 64 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 65 | if (ec) |
| 66 | { |
| 67 | return; |
| 68 | } |
| 69 | read(); |
| 70 | }); |
| 71 | } |