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 <string> 22 #include <vector> 23 24 #include "perfmgr/NodeLooperThread.h" 25 26 namespace android { 27 namespace perfmgr { 28 29 // HintManager is the external interface of the library to be used by PowerHAL 30 // to do power hints with sysfs nodes. HintManager maintains a representation of 31 // the actions that are parsed from the configuration file as a mapping from a 32 // PowerHint to the set of actions that are performed for that PowerHint. 33 class HintManager { 34 public: 35 HintManager(sp<NodeLooperThread> nm, 36 const std::map<std::string, std::vector<NodeAction>>& actions) 37 : nm_(std::move(nm)), actions_(actions) {} 38 ~HintManager() { 39 if (nm_.get() != nullptr) nm_->Stop(); 40 } 41 42 // Return true if the sysfs manager thread is running. 43 bool IsRunning() const; 44 45 // Do hint based on hint_type which defined as PowerHint in the actions 46 // section of the JSON config. Return true with valid hint_type and also 47 // NodeLooperThread::Request succeeds; otherwise return false. 48 bool DoHint(const std::string& hint_type); 49 50 // Do hint with the override time for all actions defined for the given 51 // hint_type. Return true with valid hint_type and also 52 // NodeLooperThread::Request succeeds; otherwise return false. 53 bool DoHint(const std::string& hint_type, 54 std::chrono::milliseconds timeout_ms_override); 55 56 // End hint early. Return true with valid hint_type and also 57 // NodeLooperThread::Cancel succeeds; otherwise return false. 58 bool EndHint(const std::string& hint_type); 59 60 // Static method to construct HintManager from the JSON config file. 61 static std::unique_ptr<HintManager> GetFromJSON( 62 const std::string& config_path); 63 64 // Return available hints managed by HintManager 65 std::vector<std::string> GetHints() const; 66 67 // Dump internal status to fd 68 void DumpToFd(int fd); 69 70 protected: 71 static std::vector<std::unique_ptr<Node>> ParseNodes( 72 const std::string& json_doc); 73 static std::map<std::string, std::vector<NodeAction>> ParseActions( 74 const std::string& json_doc, 75 const std::vector<std::unique_ptr<Node>>& nodes); 76 77 private: 78 HintManager(HintManager const&) = delete; 79 void operator=(HintManager const&) = delete; 80 bool ValidateHint(const std::string& hint_type) const; 81 82 sp<NodeLooperThread> nm_; 83 const std::map<std::string, std::vector<NodeAction>> actions_; 84 }; 85 86 } // namespace perfmgr 87 } // namespace android 88 89 #endif // ANDROID_LIBPERFMGR_HINTMANAGER_H_ 90