| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "activity.h" |
| 5 | |
| 6 | #include "../gpusensorprovider.h" |
| 7 | #include "../graphitemprofilepart.h" |
| 8 | #include "../graphitemxmlparser.h" |
| 9 | #include "../sensor.h" |
| 10 | #include "core/components/amdutils.h" |
| 11 | #include "core/devfsdatasource.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 <memory> |
| 19 | #include <string> |
| 20 | #include <units.h> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace AMD::Activity { |
| 25 | |
| 26 | class Provider final : public IGPUSensorProvider::IProvider |
| 27 | { |
| 28 | public: |
| 29 | std::vector<std::unique_ptr<ISensor>> |
| 30 | provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override |
| 31 | { |
| 32 | if (gpuInfo.vendor() != Vendor::AMD) |
| 33 | return {}; |
| 34 | |
| 35 | std::unique_ptr<ISensor> sensor; |
| 36 | |
| 37 | auto driver = gpuInfo.info(IGPUInfo::Keys::driver); |
| 38 | if (driver == "amdgpu") |
| 39 | sensor = createSensor(gpuInfo); |
| 40 | |
| 41 | if (!sensor) |
| 42 | return {}; |
| 43 | |
| 44 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 45 | sensors.emplace_back(std::move(sensor)); |
| 46 | |
| 47 | return sensors; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | std::unique_ptr<ISensor> createSensor(IGPUInfo const &gpuInfo) const |
| 52 | { |
| 53 | #if defined(AMDGPU_INFO_SENSOR_GPU_LOAD) |
| 54 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 55 | dataSources.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>( |
| 56 | gpuInfo.path().dev, [](int fd) { |
| 57 | unsigned int value; |
| 58 | bool success = Utils::AMD::readAMDGPUInfoSensor( |
| 59 | fd, &value, AMDGPU_INFO_SENSOR_GPU_LOAD); |
| 60 | return success ? value : 0; |
| 61 | })); |
| 62 | |
| 63 | return std::make_unique<Sensor<units::dimensionless::scalar_t, unsigned int>>( |
| 64 | AMD::Activity::ItemID, std::move(dataSources), |
| 65 | std::make_pair(units::dimensionless::scalar_t(0), |
| 66 | units::dimensionless::scalar_t(100))); |
| 67 | #else |
| 68 | return {}; |
| 69 | #endif |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | static bool register_() |
| 74 | { |
| 75 | GPUSensorProvider::registerProvider( |
| 76 | std::make_unique<AMD::Activity::Provider>()); |
| 77 | |
| 78 | ProfilePartProvider::registerProvider(AMD::Activity::ItemID, []() { |
| 79 | return std::make_unique<GraphItemProfilePart>(AMD::Activity::ItemID, |
| 80 | "yellowgreen"); |
| 81 | }); |
| 82 | |
| 83 | ProfilePartXMLParserProvider::registerProvider(AMD::Activity::ItemID, []() { |
| 84 | return std::make_unique<GraphItemXMLParser>(AMD::Activity::ItemID); |
| 85 | }); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static bool const registered_ = register_(); |
| 91 | |
| 92 | } // namespace AMD::Activity |