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