Debian Codegraph @

src/core/components/gpu.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "gpu.h"
5
6 #include "controls/icontrol.h"
7 #include "core/info/igpuinfo.h"
8 #include "sensors/isensor.h"
9 #include <utility>
10
11 GPU::GPU(std::unique_ptr<IGPUInfo> &&info,
12 std::vector<std::unique_ptr<IControl>> &&controls,
13 std::vector<std::unique_ptr<ISensor>> &&sensors) noexcept
14 : id_(IGPU::ItemID)
15 , info_(std::move(info))
16 , controls_(std::move(controls))
17 , sensors_(std::move(sensors))
18 {
19 key_ = "GPU" + std::to_string(info_->index());
20 }
21
22 bool GPU::active() const
23 {
24 return active_;
25 }
26
27 void GPU::activate(bool active)
28 {
29 active_ = active;
30 }
31
32 std::string const &GPU::key() const
33 {
34 return key_;
35 }
36
37 std::pair<std::string, std::vector<std::pair<std::string, std::string>>>
38 GPU::componentInfo() const
39 {
40 std::pair<std::string, std::vector<std::pair<std::string, std::string>>> info;
41
42 auto name(info_->info(IGPUInfo::Keys::subdeviceName));
43 if (!name.empty())
44 name.append("\n");
45 name.append("[GPU ").append(std::to_string(info_->index())).append("]");
46 info.first = name;
47
48 auto infoKeys = info_->keys();
49 for (auto &infoKey : infoKeys)
50 info.second.emplace_back(infoKey, info_->info(infoKey));
51
52 return info;
53 }
54
55 void GPU::init()
56 {
57 for (auto &control : controls_)
58 control->init();
59 }
60
61 void GPU::preInit(ICommandQueue &ctlCmds)
62 {
63 for (auto &control : controls_)
64 control->preInit(ctlCmds);
65 }
66
67 void GPU::postInit(ICommandQueue &ctlCmds)
68 {
69 for (auto &control : controls_)
70 control->postInit(ctlCmds);
71 }
72
73 void GPU::sync(ICommandQueue &ctlCmds)
74 {
75 if (active_) {
76 // NOTE clean and sync commands generation cannot be interleaved.
77 // Interleaving them could cause conflicts between clean and sync
78 // commands of different controls, leading to an incorrect hardware
79 // state.
80 for (auto &control : controls_)
81 control->clean(ctlCmds);
82 for (auto &control : controls_)
83 control->sync(ctlCmds);
84 }
85 }
86
87 void GPU::updateSensors(
88 std::unordered_map<std::string, std::unordered_set<std::string>> const &ignored)
89 {
90 for (auto &sensor : sensors_) {
91 if (ignored.count(key_) > 0 && ignored.at(key_).count(sensor->ID()) > 0)
92 continue; // skip ignored sensors
93
94 sensor->update();
95 }
96 }
97
98 IGPUInfo const &GPU::info() const
99 {
100 return *info_;
101 }
102
103 std::string const &GPU::ID() const
104 {
105 return id_;
106 }
107
108 void GPU::importWith(Importable::Importer &i)
109 {
110 auto importer = i.provideImporter(*this);
111 if (importer.has_value()) {
112 auto &gpuImporter = dynamic_cast<IGPU::Importer &>(importer->get());
113
114 activate(gpuImporter.provideActive());
115
116 for (auto &control : controls_)
117 control->importWith(*importer);
118 }
119 }
120
121 void GPU::exportWith(Exportable::Exporter &e) const
122 {
123 auto exporter = e.provideExporter(*this);
124 if (exporter.has_value()) {
125 auto &gpuExporter = dynamic_cast<IGPU::Exporter &>(exporter->get());
126
127 gpuExporter.takeActive(active());
128 gpuExporter.takeInfo(info());
129
130 for (auto const &sensor : sensors_)
131 gpuExporter.takeSensor(*sensor);
132
133 for (auto const &control : controls_)
134 control->exportWith(*exporter);
135 }
136 }