Debian Codegraph @

src/core/components/controls/amd/pm/handlers/ppdpmhandler.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "ppdpmhandler.h"
5
6 #include "core/components/amdutils.h"
7 #include "core/icommandqueue.h"
8 #include <algorithm>
9 #include <iterator>
10
11 AMD::PpDpmHandler::PpDpmHandler(
12 std::unique_ptr<IDataSource<std::string>> &&perfLevelDataSource,
13 std::unique_ptr<IDataSource<std::vector<std::string>>> &&ppDpmDataSource) noexcept
14 : perfLevelDataSource_(std::move(perfLevelDataSource))
15 , ppDpmDataSource_(std::move(ppDpmDataSource))
16 , resync_(true)
17 {
18 if (ppDpmDataSource_->read(ppDpmLines_)) {
19 auto states = Utils::AMD::parseDPMStates(ppDpmLines_);
20 if (states.has_value())
21 std::swap(states_, states.value());
22
23 active_.reserve(states_.size());
24 std::transform(states_.cbegin(), states_.cend(), std::back_inserter(active_),
25 [](auto const &state) { return state.first; });
26 }
27 }
28
29 std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> const &
30 AMD::PpDpmHandler::states() const
31 {
32 return states_;
33 }
34
35 std::vector<unsigned int> const &AMD::PpDpmHandler::active() const
36 {
37 return active_;
38 }
39
40 void AMD::PpDpmHandler::activate(std::vector<unsigned int> const &states)
41 {
42 std::vector<unsigned int> active;
43 std::copy_if(states.cbegin(), states.cend(), std::back_inserter(active),
44 [&](unsigned int index) {
45 // skip unknown state indices
46 return std::find_if(states_.cbegin(), states_.cend(),
47 [&](auto const &state) {
48 return state.first == index;
49 }) != states_.cend();
50 });
51
52 if (!active.empty()) { // cannot deactivate all states!
53 std::swap(active, active_);
54 resync_ = true;
55 }
56 }
57
58 void AMD::PpDpmHandler::saveState()
59 {
60 // NOTE At the moment, there is no way to get the active states
61 // from pp_dpm_* files. The data source state cannot be saved.
62 }
63
64 void AMD::PpDpmHandler::restoreState(ICommandQueue &)
65 {
66 // NOTE At the moment, there is no way to get the active states
67 // from pp_dpm_* files. The data source state cannot be restored.
68 }
69
70 void AMD::PpDpmHandler::reset(ICommandQueue &ctlCmds)
71 {
72 std::string activeStatesStr;
73 for (auto const &state : states_)
74 activeStatesStr.append(std::to_string(state.first)).append(" ");
75 activeStatesStr.pop_back(); // remove trailing space
76
77 if (perfLevelDataSource_->read(perfLevelValue_) &&
78 perfLevelValue_ != "manual")
79 ctlCmds.add({perfLevelDataSource_->source(), "manual"});
80 ctlCmds.add({ppDpmDataSource_->source(), activeStatesStr});
81
82 resync_ = false;
83 }
84
85 void AMD::PpDpmHandler::sync(ICommandQueue &ctlCmds)
86 {
87 // NOTE As there is no way to obtain a list of active states from
88 // pp_dpm_* files, we cannot control external changes of the file
89 // when the external change selected one or more states within the
90 // current active states group of this handler.
91 // Also, we need to keep track manually when is necessary a re-sync.
92
93 if (perfLevelDataSource_->read(perfLevelValue_) &&
94 ppDpmDataSource_->read(ppDpmLines_)) {
95
96 if (perfLevelValue_ != "manual") {
97 apply(ctlCmds); // apply it unconditionally
98 }
99 else {
100 auto currentIndex = Utils::AMD::parseDPMCurrentStateIndex(ppDpmLines_);
101 if (currentIndex.has_value()) {
102 if (resync_ || // modified: needs syncing
103 std::find( // current index is not an active index
104 active_.cbegin(), active_.cend(), *currentIndex) ==
105 active_.cend())
106 apply(ctlCmds);
107 }
108 }
109 }
110 }
111
112 void AMD::PpDpmHandler::apply(ICommandQueue &ctlCmds)
113 {
114 std::string activeStatesStr;
115 for (auto index : active_)
116 activeStatesStr.append(std::to_string(index)).append(" ");
117 activeStatesStr.pop_back(); // remove trailing space
118
119 if (perfLevelValue_ != "manual")
120 ctlCmds.add({perfLevelDataSource_->source(), "manual"});
121 ctlCmds.add({ppDpmDataSource_->source(), activeStatesStr});
122
123 resync_ = false;
124 }