| Wei Wang | f72cfad | 2017-10-26 22:41:03 -0700 | [diff] [blame] | 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_NODELOOPERTHREAD_H_ |
| 18 | #define ANDROID_LIBPERFMGR_NODELOOPERTHREAD_H_ |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <utils/Thread.h> |
| 25 | |
| 26 | #include "perfmgr/Node.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace perfmgr { |
| 30 | |
| 31 | // The NodeAction specifies the sysfs node, the value to be assigned, and the |
| 32 | // timeout for this action: |
| 33 | struct NodeAction { |
| 34 | NodeAction(std::size_t node_index, std::size_t value_index, |
| 35 | std::chrono::milliseconds timeout_ms) |
| 36 | : node_index(node_index), |
| 37 | value_index(value_index), |
| 38 | timeout_ms(timeout_ms) {} |
| 39 | std::size_t node_index; |
| 40 | std::size_t value_index; |
| 41 | std::chrono::milliseconds timeout_ms; // 0ms for forever |
| 42 | }; |
| 43 | |
| 44 | // The NodeLooperThread is responsible for managing each of the sysfs nodes |
| 45 | // specified in the configuration. At initialization, the NodeLooperThrea holds |
| 46 | // a vector containing the nodes defined in the configuration. The NodeManager |
| 47 | // gets powerhint requests and cancellations from the HintManager, maintains |
| 48 | // state about the current set of powerhint requests on each sysfs node, and |
| 49 | // decides how to apply the requests. The NodeLooperThread contains a ThreadLoop |
| 50 | // to maintain the sysfs nodes, and that thread is woken up both to handle |
| 51 | // powerhint requests and when the timeout expires for an in-progress powerhint. |
| 52 | class NodeLooperThread : public ::android::Thread { |
| 53 | public: |
| 54 | NodeLooperThread(std::vector<std::unique_ptr<Node>> nodes) |
| 55 | : Thread(false), nodes_(std::move(nodes)) {} |
| 56 | virtual ~NodeLooperThread() { Stop(); } |
| 57 | |
| 58 | // Need call Stop() as the threadloop will hold a strong pointer |
| 59 | // itself and wait for Condition fired or timeout (60s) before |
| 60 | // the out looper can call deconstructor to Stop() thread |
| 61 | void Stop(); |
| 62 | |
| 63 | // Return true when successfully adds request from actions for the hint_type |
| 64 | // in each individual node. Return false if any of the actions has either |
| 65 | // invalid node index or value index. |
| 66 | bool Request(const std::vector<NodeAction>& actions, |
| 67 | const std::string& hint_type); |
| 68 | // Return when successfully cancels request from actions for the hint_type |
| 69 | // in each individual node. Return false if any of the actions has invalid |
| 70 | // node index. |
| 71 | bool Cancel(const std::vector<NodeAction>& actions, |
| 72 | const std::string& hint_type); |
| 73 | |
| Wei Wang | 6ac764e | 2018-01-30 15:14:09 -0800 | [diff] [blame] | 74 | // Dump all nodes to fd |
| 75 | void DumpToFd(int fd); |
| 76 | |
| Wei Wang | f72cfad | 2017-10-26 22:41:03 -0700 | [diff] [blame] | 77 | private: |
| 78 | NodeLooperThread(NodeLooperThread const&) = delete; |
| 79 | void operator=(NodeLooperThread const&) = delete; |
| 80 | bool threadLoop() override; |
| 81 | void onFirstRef() override; |
| 82 | |
| Wei Wang | f8bea16 | 2018-01-01 22:23:52 -0800 | [diff] [blame] | 83 | static constexpr auto kMaxUpdatePeriod = std::chrono::milliseconds::max(); |
| Wei Wang | f72cfad | 2017-10-26 22:41:03 -0700 | [diff] [blame] | 84 | |
| 85 | std::vector<std::unique_ptr<Node>> nodes_; // parsed from Config |
| 86 | |
| 87 | // conditional variable from C++ standard library can be affected by wall |
| 88 | // time change as it is using CLOCK_REAL (b/35756266). The component should |
| 89 | // not be impacted by wall time, thus need use Android specific Condition |
| 90 | // class for waking up threadloop. |
| 91 | ::android::Condition wake_cond_; |
| 92 | |
| 93 | // lock to protect nodes_ |
| 94 | ::android::Mutex lock_; |
| 95 | }; |
| 96 | |
| 97 | } // namespace perfmgr |
| 98 | } // namespace android |
| 99 | |
| 100 | #endif // ANDROID_LIBPERFMGR_NODELOOPERTHREAD_H_ |