• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
HintManager(sp<NodeLooperThread> nm,const std::map<std::string,std::vector<NodeAction>> & actions)38     HintManager(sp<NodeLooperThread> nm,
39                 const std::map<std::string, std::vector<NodeAction>>& actions)
40         : nm_(std::move(nm)), actions_(actions) {}
~HintManager()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     // Query if given hint supported.
64     bool IsHintSupported(const std::string& hint_type) const;
65 
66     // Static method to construct HintManager from the JSON config file.
67     static std::unique_ptr<HintManager> GetFromJSON(
68         const std::string& config_path, bool start = true);
69 
70     // Return available hints managed by HintManager
71     std::vector<std::string> GetHints() const;
72 
73     // Dump internal status to fd
74     void DumpToFd(int fd);
75 
76     // Start thread loop
77     bool Start();
78 
79   protected:
80     static std::vector<std::unique_ptr<Node>> ParseNodes(
81         const std::string& json_doc);
82     static std::map<std::string, std::vector<NodeAction>> ParseActions(
83         const std::string& json_doc,
84         const std::vector<std::unique_ptr<Node>>& nodes);
85 
86   private:
87     HintManager(HintManager const&) = delete;
88     void operator=(HintManager const&) = delete;
89     bool ValidateHint(const std::string& hint_type) const;
90 
91     sp<NodeLooperThread> nm_;
92     const std::map<std::string, std::vector<NodeAction>> actions_;
93 };
94 
95 }  // namespace perfmgr
96 }  // namespace android
97 
98 #endif  // ANDROID_LIBPERFMGR_HINTMANAGER_H_
99