• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "libperfmgr"
18 
19 #include "perfmgr/RequestGroup.h"
20 
21 namespace android {
22 namespace perfmgr {
23 
AddRequest(const std::string & hint_type,ReqTime end_time)24 bool RequestGroup::AddRequest(const std::string& hint_type, ReqTime end_time) {
25     if (request_map_.find(hint_type) == request_map_.end()) {
26         request_map_.emplace(hint_type, end_time);
27         return true;
28     } else {
29         if (request_map_[hint_type] < end_time) {
30             request_map_[hint_type] = end_time;
31         }
32         return false;
33     }
34 }
35 
RemoveRequest(const std::string & hint_type)36 bool RequestGroup::RemoveRequest(const std::string& hint_type) {
37     return request_map_.erase(hint_type);
38 }
39 
GetRequestValue() const40 const std::string& RequestGroup::GetRequestValue() const {
41     return request_value_;
42 }
43 
GetExpireTime(std::chrono::milliseconds * expire_time)44 bool RequestGroup::GetExpireTime(std::chrono::milliseconds* expire_time) {
45     ReqTime now = std::chrono::steady_clock::now();
46     *expire_time = std::chrono::milliseconds::max();
47 
48     bool active = false;
49     for (auto it = request_map_.begin(); it != request_map_.end();) {
50         auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
51             it->second - now);
52         if (duration <= std::chrono::milliseconds::zero()) {
53             it = request_map_.erase(it);
54         } else {
55             *expire_time = std::min(duration, *expire_time);
56             active = true;
57             ++it;
58         }
59     }
60     return active;
61 }
62 
63 }  // namespace perfmgr
64 }  // namespace android
65