Home | History | Annotate | Download | only in perfmgr
      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 Nodes 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 // clusters frequency to the highest available value. The next entry represents
     41 // the in-progress requests to boost the clusters 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:
     49     Node(std::string name, std::string node_path,
     50          std::vector<RequestGroup> req_sorted, std::size_t default_val_index,
     51          bool reset_on_init, bool hold_fd = false);
     52 
     53     // Return true if successfully add a request
     54     bool AddRequest(std::size_t value_index, const std::string& hint_type,
     55                     ReqTime end_time);
     56 
     57     // Return true if successfully remove a request
     58     bool RemoveRequest(const std::string& hint_type);
     59 
     60     // Return the nearest expire time of active requests; return
     61     // std::chrono::milliseconds::max() if no active request on Node; update
     62     // node's controlled file node value and the current value index based on
     63     // active request.
     64     std::chrono::milliseconds Update();
     65 
     66     std::string GetName() const;
     67     std::string GetPath() const;
     68     std::vector<std::string> GetValues() const;
     69     std::size_t GetDefaultIndex() const;
     70     bool GetResetOnInit() const;
     71     bool GetHoldFd() const;
     72     bool GetValueIndex(const std::string value, std::size_t* index) const;
     73     void DumpToFd(int fd);
     74 
     75   private:
     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     std::size_t current_val_index_;
     85     const bool reset_on_init_;
     86     const bool hold_fd_;
     87     android::base::unique_fd fd_;
     88 };
     89 
     90 }  // namespace perfmgr
     91 }  // namespace android
     92 
     93 #endif  // ANDROID_LIBPERFMGR_NODE_H_
     94