blob: 029849d646487a32d22b657ce50e903b6b3b8aee [file] [log] [blame]
Wei Wanga4823192018-10-19 21:57:04 -07001/*
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
27namespace android {
28namespace perfmgr {
29
30PropertyNode::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)
Wei Wangc43c2c02018-10-23 22:44:31 -070033 : Node(std::move(name), std::move(node_path), std::move(req_sorted),
34 default_val_index, reset_on_init) {
Wei Wanga4823192018-10-19 21:57:04 -070035 if (reset_on_init) {
Wei Wangc43c2c02018-10-23 22:44:31 -070036 Update(false);
Wei Wanga4823192018-10-19 21:57:04 -070037 }
38}
39
40std::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_) {
Wei Wanga9786b62018-10-22 15:19:46 -070054 const std::string& req_value =
55 req_sorted_[value_index].GetRequestValue();
Wei Wanga4823192018-10-19 21:57:04 -070056
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
68void 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