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/properties.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/strings.h>
24
25 #include "perfmgr/PropertyNode.h"
26
27 namespace android {
28 namespace perfmgr {
29
PropertyNode(std::string name,std::string node_path,std::vector<RequestGroup> req_sorted,std::size_t default_val_index,bool reset_on_init)30 PropertyNode::PropertyNode(std::string name, std::string node_path,
31 std::vector<RequestGroup> req_sorted,
32 std::size_t default_val_index, bool reset_on_init)
33 : Node(std::move(name), std::move(node_path), std::move(req_sorted),
34 default_val_index, reset_on_init) {
35 if (reset_on_init) {
36 Update(false);
37 }
38 }
39
Update(bool)40 std::chrono::milliseconds PropertyNode::Update(bool) {
41 std::size_t value_index = default_val_index_;
42 std::chrono::milliseconds expire_time = std::chrono::milliseconds::max();
43
44 // Find the highest outstanding request's expire time
45 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
46 if (req_sorted_[i].GetExpireTime(&expire_time)) {
47 value_index = i;
48 break;
49 }
50 }
51
52 // Update node only if request index changes
53 if (value_index != current_val_index_) {
54 const std::string& req_value =
55 req_sorted_[value_index].GetRequestValue();
56
57 if (!android::base::SetProperty(node_path_, req_value)) {
58 LOG(WARNING) << "Failed to set property to : " << node_path_
59 << " with value: " << req_value;
60 } else {
61 // Update current index only when succeed
62 current_val_index_ = value_index;
63 }
64 }
65 return expire_time;
66 }
67
DumpToFd(int fd) const68 void PropertyNode::DumpToFd(int fd) const {
69 std::string node_value = android::base::GetProperty(node_path_, "");
70 std::string buf(android::base::StringPrintf(
71 "%s\t%s\t%zu\t%s\n", name_.c_str(), node_path_.c_str(),
72 current_val_index_, node_value.c_str()));
73 if (!android::base::WriteStringToFd(buf, fd)) {
74 LOG(ERROR) << "Failed to dump fd: " << fd;
75 }
76 }
77
78 } // namespace perfmgr
79 } // namespace android
80