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