• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         const std::string& req_value =
53             req_sorted_[value_index].GetRequestValue();
54         if (ATRACE_ENABLED()) {
55             const std::string tag = GetName() + ":" + req_value;
56             ATRACE_BEGIN(tag.c_str());
57         }
58         if (!android::base::SetProperty(node_path_, req_value)) {
59             LOG(WARNING) << "Failed to set property to : " << node_path_
60                          << " with value: " << req_value;
61         } else {
62             // Update current index only when succeed
63             current_val_index_ = value_index;
64             reset_on_init_ = false;
65         }
66         if (ATRACE_ENABLED()) {
67             ATRACE_END();
68         }
69     }
70     return expire_time;
71 }
72 
DumpToFd(int fd) const73 void PropertyNode::DumpToFd(int fd) const {
74     std::string node_value = android::base::GetProperty(node_path_, "");
75     std::string buf(android::base::StringPrintf(
76             "Node Name\t"
77             "Property Name\t"
78             "Current Index\t"
79             "Current Value\n"
80             "%s\t%s\t%zu\t%s\n",
81             name_.c_str(), node_path_.c_str(), current_val_index_, node_value.c_str()));
82     if (!android::base::WriteStringToFd(buf, fd)) {
83         LOG(ERROR) << "Failed to dump fd: " << fd;
84     }
85     for (std::size_t i = 0; i < req_sorted_.size(); i++) {
86         req_sorted_[i].DumpToFd(
87             fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
88     }
89 }
90 
91 }  // namespace perfmgr
92 }  // namespace android
93