• 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 #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     ReqTime now = std::chrono::steady_clock::now();
51     *expire_time = std::chrono::milliseconds::max();
52 
53     bool active = false;
54     for (auto it = request_map_.begin(); it != request_map_.end();) {
55         auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
56             it->second - now);
57         if (duration <= std::chrono::milliseconds::zero()) {
58             it = request_map_.erase(it);
59         } else {
60             *expire_time = std::min(duration, *expire_time);
61             active = true;
62             ++it;
63         }
64     }
65     return active;
66 }
67 
DumpToFd(int fd,const std::string & prefix) const68 void RequestGroup::DumpToFd(int fd, const std::string& prefix) const {
69     std::ostringstream dump_buf;
70     ReqTime now = std::chrono::steady_clock::now();
71     for (auto it = request_map_.begin(); it != request_map_.end(); it++) {
72         auto remaining_duration =
73             std::chrono::duration_cast<std::chrono::milliseconds>(it->second -
74                                                                   now);
75         dump_buf << prefix << it->first << "\t" << remaining_duration.count()
76                  << "\t" << request_value_ << "\n";
77     }
78     if (!android::base::WriteStringToFd(dump_buf.str(), fd)) {
79         LOG(ERROR) << "Failed to dump fd: " << fd;
80     }
81 }
82 
83 }  // namespace perfmgr
84 }  // namespace android
85