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_REQUESTGROUP_H_
18 #define ANDROID_LIBPERFMGR_REQUESTGROUP_H_
19 
20 #include <chrono>
21 #include <map>
22 #include <string>
23 #include <utility>
24 
25 namespace android {
26 namespace perfmgr {
27 
28 using ReqTime = std::chrono::time_point<std::chrono::steady_clock>;
29 
30 // The RequestGroup type represents the set of requests for a given value on a
31 // particular sysfs node, and the interface is simple: there is a function to
32 // add requests, a function to remove requests, and a function to check for the
33 // next expiration time if there is an outstanding request, and a function to
34 // check the requested value. There may only be one request per PowerHint, so
35 // the representation is simple: a map from PowerHint to the expiration time for
36 // that hint.
37 class RequestGroup {
38   public:
RequestGroup(std::string request_value)39     RequestGroup(std::string request_value)  // NOLINT(runtime/explicit)
40         : request_value_(std::move(request_value)) {}
41 
42     // Remove expired request in the map and return true when request_map_ is
43     // not empty, false when request_map_ is empty; also update expire_time with
44     // nearest timeout in request_map_ or std::chrono::milliseconds::max() when
45     // request_map_ is empty.
46     bool GetExpireTime(std::chrono::milliseconds* expire_time);
47     // Return the request value.
48     const std::string& GetRequestValue() const;
49     // Return true for adding request, false for extending expire time of
50     // existing active request on given hint_type.
51     bool AddRequest(const std::string& hint_type, ReqTime end_time);
52     // Return true for removing request, false if request is not active on given
53     // hint_type. If request exits and the new end_time is less than the active
54     // time, expire time will not be updated; also returns false.
55     bool RemoveRequest(const std::string& hint_type);
56     // Dump internal status to fd
57     void DumpToFd(int fd, const std::string& prefix) const;
58 
59   private:
60     const std::string request_value_;
61     std::map<std::string, ReqTime> request_map_;
62 };
63 
64 }  // namespace perfmgr
65 }  // namespace android
66 
67 #endif  // ANDROID_LIBPERFMGR_REQUESTGROUP_H_
68