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/Node.h"
20 
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 
26 namespace android {
27 namespace perfmgr {
28 
Node(std::string name,std::string node_path,std::vector<RequestGroup> req_sorted,std::size_t default_val_index,bool reset_on_init)29 Node::Node(std::string name, std::string node_path,
30            std::vector<RequestGroup> req_sorted, std::size_t default_val_index,
31            bool reset_on_init)
32     : name_(std::move(name)),
33       node_path_(std::move(node_path)),
34       req_sorted_(std::move(req_sorted)),
35       default_val_index_(default_val_index),
36       reset_on_init_(reset_on_init),
37       current_val_index_(default_val_index) {}
38 
AddRequest(std::size_t value_index,const std::string & hint_type,ReqTime end_time)39 bool Node::AddRequest(std::size_t value_index, const std::string& hint_type,
40                       ReqTime end_time) {
41     if (value_index >= req_sorted_.size()) {
42         LOG(ERROR) << "Value index out of bound: " << value_index
43                    << " ,size: " << req_sorted_.size();
44         return false;
45     }
46     // Add/Update request to the new end_time for the specific hint_type
47     req_sorted_[value_index].AddRequest(hint_type, end_time);
48     return true;
49 }
50 
RemoveRequest(const std::string & hint_type)51 bool Node::RemoveRequest(const std::string& hint_type) {
52     bool ret = false;
53     // Remove all requests for the specific hint_type
54     for (auto& value : req_sorted_) {
55         ret = value.RemoveRequest(hint_type) || ret;
56     }
57     return ret;
58 }
59 
GetName() const60 const std::string& Node::GetName() const {
61     return name_;
62 }
63 
GetPath() const64 const std::string& Node::GetPath() const {
65     return node_path_;
66 }
67 
GetValueIndex(const std::string & value,std::size_t * index) const68 bool Node::GetValueIndex(const std::string& value, std::size_t* index) const {
69     bool found = false;
70     for (std::size_t i = 0; i < req_sorted_.size(); i++) {
71         if (req_sorted_[i].GetRequestValue() == value) {
72             *index = i;
73             found = true;
74             break;
75         }
76     }
77     return found;
78 }
79 
GetDefaultIndex() const80 std::size_t Node::GetDefaultIndex() const {
81     return default_val_index_;
82 }
83 
GetResetOnInit() const84 bool Node::GetResetOnInit() const {
85     return reset_on_init_;
86 }
87 
GetValues() const88 std::vector<std::string> Node::GetValues() const {
89     std::vector<std::string> values;
90     for (const auto& value : req_sorted_) {
91         values.emplace_back(value.GetRequestValue());
92     }
93     return values;
94 }
95 
96 }  // namespace perfmgr
97 }  // namespace android
98