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/FileNode.h"
21
22 #include <android-base/chrono_utils.h>
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/properties.h>
26 #include <android-base/stringprintf.h>
27 #include <android-base/strings.h>
28 #include <utils/Trace.h>
29
30 namespace android {
31 namespace perfmgr {
32
FileNode(std::string name,std::string node_path,std::vector<RequestGroup> req_sorted,std::size_t default_val_index,bool reset_on_init,bool hold_fd)33 FileNode::FileNode(std::string name, std::string node_path,
34 std::vector<RequestGroup> req_sorted,
35 std::size_t default_val_index, bool reset_on_init,
36 bool hold_fd)
37 : Node(std::move(name), std::move(node_path), std::move(req_sorted),
38 default_val_index, reset_on_init),
39 hold_fd_(hold_fd),
40 warn_timeout_(
41 android::base::GetBoolProperty("ro.debuggable", false) ? 5ms : 50ms) {
42 }
43
Update(bool log_error)44 std::chrono::milliseconds FileNode::Update(bool log_error) {
45 std::size_t value_index = default_val_index_;
46 std::chrono::milliseconds expire_time = std::chrono::milliseconds::max();
47
48 // Find the highest outstanding request's expire time
49 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
50 if (req_sorted_[i].GetExpireTime(&expire_time)) {
51 value_index = i;
52 break;
53 }
54 }
55
56 // Update node only if request index changes
57 if (value_index != current_val_index_ || reset_on_init_) {
58 const std::string& req_value =
59 req_sorted_[value_index].GetRequestValue();
60 if (ATRACE_ENABLED()) {
61 const std::string tag = GetName() + ":" + req_value;
62 ATRACE_BEGIN(tag.c_str());
63 }
64 android::base::Timer t;
65 fd_.reset(TEMP_FAILURE_RETRY(
66 open(node_path_.c_str(), O_WRONLY | O_CLOEXEC | O_TRUNC)));
67
68 if (fd_ == -1 || !android::base::WriteStringToFd(req_value, fd_)) {
69 if (log_error) {
70 LOG(WARNING) << "Failed to write to node: " << node_path_
71 << " with value: " << req_value << ", fd: " << fd_;
72 }
73 // Retry in 500ms or sooner
74 expire_time = std::min(expire_time, std::chrono::milliseconds(500));
75 } else {
76 // For regular file system, we need fsync
77 fsync(fd_);
78 // Some dev node requires file to remain open during the entire hint
79 // duration e.g. /dev/cpu_dma_latency, so fd_ is intentionally kept
80 // open during any requested value other than default one. If
81 // request a default value, node will write the value and then
82 // release the fd.
83 if ((!hold_fd_) || value_index == default_val_index_) {
84 fd_.reset();
85 }
86 auto duration = t.duration();
87 if (duration > warn_timeout_) {
88 LOG(WARNING) << "Slow writing to file: '" << node_path_
89 << "' with value: '" << req_value
90 << "' took: " << duration.count() << " ms";
91 }
92 // Update current index only when succeed
93 current_val_index_ = value_index;
94 reset_on_init_ = false;
95 }
96 if (ATRACE_ENABLED()) {
97 ATRACE_END();
98 }
99 }
100 return expire_time;
101 }
102
GetHoldFd() const103 bool FileNode::GetHoldFd() const {
104 return hold_fd_;
105 }
106
DumpToFd(int fd) const107 void FileNode::DumpToFd(int fd) const {
108 std::string node_value;
109 if (!android::base::ReadFileToString(node_path_, &node_value)) {
110 LOG(ERROR) << "Failed to read node path: " << node_path_;
111 }
112 node_value = android::base::Trim(node_value);
113 std::string buf(android::base::StringPrintf(
114 "%s\t%s\t%zu\t%s\n", name_.c_str(), node_path_.c_str(),
115 current_val_index_, node_value.c_str()));
116 if (!android::base::WriteStringToFd(buf, fd)) {
117 LOG(ERROR) << "Failed to dump fd: " << fd;
118 }
119 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
120 req_sorted_[i].DumpToFd(
121 fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
122 }
123 }
124
125 } // namespace perfmgr
126 } // namespace android
127