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 <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/strings.h>
23
24 #include "perfmgr/Node.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 // Assigning an invalid value so the next Update() will update the
38 // Node's value to default
39 current_val_index_(reset_on_init ? req_sorted_.size()
40 : default_val_index) {}
41
AddRequest(std::size_t value_index,const std::string & hint_type,ReqTime end_time)42 bool Node::AddRequest(std::size_t value_index, const std::string& hint_type,
43 ReqTime end_time) {
44 if (value_index >= req_sorted_.size()) {
45 LOG(ERROR) << "Value index out of bound: " << value_index
46 << " ,size: " << req_sorted_.size();
47 return false;
48 }
49 // Add/Update request to the new end_time for the specific hint_type
50 req_sorted_[value_index].AddRequest(hint_type, end_time);
51 return true;
52 }
53
RemoveRequest(const std::string & hint_type)54 bool Node::RemoveRequest(const std::string& hint_type) {
55 bool ret = false;
56 // Remove all requests for the specific hint_type
57 for (auto& value : req_sorted_) {
58 ret = value.RemoveRequest(hint_type) || ret;
59 }
60 return ret;
61 }
62
GetName() const63 const std::string& Node::GetName() const {
64 return name_;
65 }
66
GetPath() const67 const std::string& Node::GetPath() const {
68 return node_path_;
69 }
70
GetValueIndex(const std::string & value,std::size_t * index) const71 bool Node::GetValueIndex(const std::string& value, std::size_t* index) const {
72 bool found = false;
73 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
74 if (req_sorted_[i].GetRequestValue() == value) {
75 *index = i;
76 found = true;
77 break;
78 }
79 }
80 return found;
81 }
82
GetDefaultIndex() const83 std::size_t Node::GetDefaultIndex() const {
84 return default_val_index_;
85 }
86
GetResetOnInit() const87 bool Node::GetResetOnInit() const {
88 return reset_on_init_;
89 }
90
GetValues() const91 std::vector<std::string> Node::GetValues() const {
92 std::vector<std::string> values;
93 for (const auto& value : req_sorted_) {
94 values.emplace_back(value.GetRequestValue());
95 }
96 return values;
97 }
98
99 } // namespace perfmgr
100 } // namespace android
101