Debian Codegraph @

libperfmgr/include/perfmgr/HintManager.h

raw
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specic language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_LIBPERFMGR_HINTMANAGER_H_
18 #define ANDROID_LIBPERFMGR_HINTMANAGER_H_
19
20 #include <cstddef>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "perfmgr/NodeLooperThread.h"
28
29 namespace android {
30 namespace perfmgr {
31
32 // HintManager is the external interface of the library to be used by PowerHAL
33 // to do power hints with sysfs nodes. HintManager maintains a representation of
34 // the actions that are parsed from the configuration file as a mapping from a
35 // PowerHint to the set of actions that are performed for that PowerHint.
36 class HintManager {
37 public:
38 HintManager(sp<NodeLooperThread> nm,
39 const std::map<std::string, std::vector<NodeAction>>& actions)
40 : nm_(std::move(nm)), actions_(actions) {}
41 ~HintManager() {
42 if (nm_.get() != nullptr) nm_->Stop();
43 }
44
45 // Return true if the sysfs manager thread is running.
46 bool IsRunning() const;
47
48 // Do hint based on hint_type which defined as PowerHint in the actions
49 // section of the JSON config. Return true with valid hint_type and also
50 // NodeLooperThread::Request succeeds; otherwise return false.
51 bool DoHint(const std::string& hint_type);
52
53 // Do hint with the override time for all actions defined for the given
54 // hint_type. Return true with valid hint_type and also
55 // NodeLooperThread::Request succeeds; otherwise return false.
56 bool DoHint(const std::string& hint_type,
57 std::chrono::milliseconds timeout_ms_override);
58
59 // End hint early. Return true with valid hint_type and also
60 // NodeLooperThread::Cancel succeeds; otherwise return false.
61 bool EndHint(const std::string& hint_type);
62
63 // Static method to construct HintManager from the JSON config file.
64 static std::unique_ptr<HintManager> GetFromJSON(
65 const std::string& config_path);
66
67 // Return available hints managed by HintManager
68 std::vector<std::string> GetHints() const;
69
70 // Dump internal status to fd
71 void DumpToFd(int fd);
72
73 protected:
74 static std::vector<std::unique_ptr<Node>> ParseNodes(
75 const std::string& json_doc);
76 static std::map<std::string, std::vector<NodeAction>> ParseActions(
77 const std::string& json_doc,
78 const std::vector<std::unique_ptr<Node>>& nodes);
79
80 private:
81 HintManager(HintManager const&) = delete;
82 void operator=(HintManager const&) = delete;
83 bool ValidateHint(const std::string& hint_type) const;
84
85 sp<NodeLooperThread> nm_;
86 const std::map<std::string, std::vector<NodeAction>> actions_;
87 };
88
89 } // namespace perfmgr
90 } // namespace android
91
92 #endif // ANDROID_LIBPERFMGR_HINTMANAGER_H_