| 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_NODE_H_ |
| 18 | #define ANDROID_LIBPERFMGR_NODE_H_ |
| 19 | |
| 20 | #include <cstddef> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/unique_fd.h> |
| 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. |
| 46 | class Node { |
| 47 | public: |
| 48 | Node(std::string name, std::string node_path, |
| 49 | std::vector<RequestGroup> req_sorted, std::size_t default_val_index, |
| 50 | bool reset_on_init, bool hold_fd = false); |
| 51 | |
| 52 | // Return true if successfully add a request |
| 53 | bool AddRequest(std::size_t value_index, const std::string& hint_type, |
| 54 | ReqTime end_time); |
| 55 | |
| 56 | // Return true if successfully remove a request |
| 57 | bool RemoveRequest(const std::string& hint_type); |
| 58 | |
| 59 | // Return the nearest expire time of active requests; return |
| 60 | // std::chrono::milliseconds::max() if no active request on Node; update |
| 61 | // node's controlled file node value and the current value index based on |
| 62 | // active request. |
| 63 | std::chrono::milliseconds Update(); |
| 64 | |
| 65 | std::string GetName() const; |
| 66 | std::string GetPath() const; |
| 67 | std::vector<std::string> GetValues() const; |
| 68 | std::size_t GetDefaultIndex() const; |
| 69 | bool GetResetOnInit() const; |
| 70 | bool GetHoldFd() const; |
| 71 | bool GetValueIndex(const std::string value, std::size_t* index) const; |
| 72 | |
| 73 | private: |
| 74 | Node(const Node& other) = delete; |
| 75 | Node& operator=(Node const&) = delete; |
| 76 | |
| 77 | const std::string name_; |
| 78 | const std::string node_path_; |
| 79 | // request vector, one entry per possible value, sorted by priority |
| 80 | std::vector<RequestGroup> req_sorted_; |
| 81 | const std::size_t default_val_index_; |
| 82 | std::size_t current_val_index_; |
| 83 | const bool reset_on_init_; |
| 84 | const bool hold_fd_; |
| 85 | android::base::unique_fd fd_; |
| 86 | }; |
| 87 | |
| 88 | } // namespace perfmgr |
| 89 | } // namespace android |
| 90 | |
| 91 | #endif // ANDROID_LIBPERFMGR_NODE_H_ |