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_NODE_H_
18 #define ANDROID_LIBPERFMGR_NODE_H_
19 
20 #include <android-base/unique_fd.h>
21 
22 #include <cstddef>
23 #include <string>
24 #include <vector>
25 
26 #include "perfmgr/RequestGroup.h"
27 
28 namespace android {
29 namespace perfmgr {
30 
31 // The Node class provides an interface for adding and cancelling powerhint
32 // requests, as well as checking the next time that an in-progress powerhint
33 // request will expire. There are additional methods for getting the Node’s name
34 // and the index of a value, which may be used for initialization, debugging,
35 // and request management. The core of the Node class is a vector of
36 // RequestGroups named req_sorted_, which is used to track the in-progress
37 // requests on the node. Each entry in the vector corresponds to a possible
38 // value for the node, in priority order. For example, the first entry in the
39 // vector for the cpu0 cluster represents the in-progress requests to boost the
40 // cluster’s frequency to the highest available value. The next entry represents
41 // the in-progress requests to boost the cluster’s frequency to the next highest
42 // value. For each value, there may be multiple requests because different
43 // powerhints may request the same value, and the requests may have different
44 // expiration times. All of the in-progress powerhints for a given value are
45 // collected in a RequestGroup. Node class is not thread safe so it needs
46 // protection from caller e.g. NodeLooperThread.
47 class Node {
48   public:
~Node()49     virtual ~Node() {}
50 
51     // Return true if successfully add a request
52     bool AddRequest(std::size_t value_index, const std::string& hint_type,
53                     ReqTime end_time);
54 
55     // Return true if successfully remove a request
56     bool RemoveRequest(const std::string& hint_type);
57 
58     // Return the nearest expire time of active requests; return
59     // std::chrono::milliseconds::max() if no active request on Node; update
60     // node's controlled file node value and the current value index based on
61     // active request.
62     virtual std::chrono::milliseconds Update(bool log_error) = 0;
63 
64     const std::string& GetName() const;
65     const std::string& GetPath() const;
66     std::vector<std::string> GetValues() const;
67     std::size_t GetDefaultIndex() const;
68     bool GetResetOnInit() const;
69     bool GetValueIndex(const std::string& value, std::size_t* index) const;
70     virtual void DumpToFd(int fd) const = 0;
71 
72   protected:
73     Node(std::string name, std::string node_path,
74          std::vector<RequestGroup> req_sorted, std::size_t default_val_index,
75          bool reset_on_init);
76     Node(const Node& other) = delete;
77     Node& operator=(Node const&) = delete;
78 
79     const std::string name_;
80     const std::string node_path_;
81     // request vector, one entry per possible value, sorted by priority.
82     std::vector<RequestGroup> req_sorted_;
83     const std::size_t default_val_index_;
84     // node will be explicitly initialized when first time called Update().
85     bool reset_on_init_;
86     std::size_t current_val_index_;
87 };
88 
89 }  // namespace perfmgr
90 }  // namespace android
91 
92 #endif  // ANDROID_LIBPERFMGR_NODE_H_
93