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