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 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 
24 #include <sstream>
25 
26 namespace android {
27 namespace perfmgr {
28 
AddRequest(const std::string & hint_type,ReqTime end_time)29 bool RequestGroup::AddRequest(const std::string& hint_type, ReqTime end_time) {
30     if (request_map_.find(hint_type) == request_map_.end()) {
31         request_map_.emplace(hint_type, end_time);
32         return true;
33     } else {
34         if (request_map_[hint_type] < end_time) {
35             request_map_[hint_type] = end_time;
36         }
37         return false;
38     }
39 }
40 
RemoveRequest(const std::string & hint_type)41 bool RequestGroup::RemoveRequest(const std::string& hint_type) {
42     return request_map_.erase(hint_type);
43 }
44 
GetRequestValue() const45 const std::string& RequestGroup::GetRequestValue() const {
46     return request_value_;
47 }
48 
GetExpireTime(std::chrono::milliseconds * expire_time)49 bool RequestGroup::GetExpireTime(std::chrono::milliseconds* expire_time) {
50 
51     *expire_time = std::chrono::milliseconds::max();
52 
53     if (request_map_.empty()) return false;
54 
55     ReqTime now = std::chrono::steady_clock::now();
56 
57     bool active = false;
58     for (auto it = request_map_.begin(); it != request_map_.end();) {
59         auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
60             it->second - now);
61         if (duration <= std::chrono::milliseconds::zero()) {
62             it = request_map_.erase(it);
63         } else {
64             *expire_time = std::min(duration, *expire_time);
65             active = true;
66             ++it;
67         }
68     }
69     return active;
70 }
71 
DumpToFd(int fd,const std::string & prefix) const72 void RequestGroup::DumpToFd(int fd, const std::string& prefix) const {
73     std::ostringstream dump_buf;
74     ReqTime now = std::chrono::steady_clock::now();
75     for (auto it = request_map_.begin(); it != request_map_.end(); it++) {
76         auto remaining_duration =
77             std::chrono::duration_cast<std::chrono::milliseconds>(it->second -
78                                                                   now);
79         dump_buf << prefix << it->first << "\t" << remaining_duration.count()
80                  << "\t" << request_value_ << "\n";
81     }
82     if (!android::base::WriteStringToFd(dump_buf.str(), fd)) {
83         LOG(ERROR) << "Failed to dump fd: " << fd;
84     }
85 }
86 
87 }  // namespace perfmgr
88 }  // namespace android
89