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 truncate,bool hold_fd)33 FileNode::FileNode(std::string name, std::string node_path, std::vector<RequestGroup> req_sorted,
34 std::size_t default_val_index, bool reset_on_init, bool truncate, bool hold_fd)
35 : Node(std::move(name), std::move(node_path), std::move(req_sorted), default_val_index,
36 reset_on_init),
37 hold_fd_(hold_fd),
38 truncate_(truncate),
39 warn_timeout_(android::base::GetBoolProperty("ro.debuggable", false) ? 5ms : 50ms) {}
40
Update(bool log_error)41 std::chrono::milliseconds FileNode::Update(bool log_error) {
42 std::size_t value_index = default_val_index_;
43 std::chrono::milliseconds expire_time = std::chrono::milliseconds::max();
44
45 // Find the highest outstanding request's expire time
46 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
47 if (req_sorted_[i].GetExpireTime(&expire_time)) {
48 value_index = i;
49 break;
50 }
51 }
52
53 // Update node only if request index changes
54 if (value_index != current_val_index_ || reset_on_init_) {
55 const std::string& req_value =
56 req_sorted_[value_index].GetRequestValue();
57 if (ATRACE_ENABLED()) {
58 const std::string tag = GetName() + ":" + req_value;
59 ATRACE_BEGIN(tag.c_str());
60 }
61 android::base::Timer t;
62 int flags = O_WRONLY | O_CLOEXEC;
63 if (GetTruncate()) {
64 flags |= O_TRUNC;
65 }
66 fd_.reset(TEMP_FAILURE_RETRY(open(node_path_.c_str(), flags)));
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
GetTruncate() const107 bool FileNode::GetTruncate() const {
108 return truncate_;
109 }
110
DumpToFd(int fd) const111 void FileNode::DumpToFd(int fd) const {
112 std::string node_value;
113 if (!android::base::ReadFileToString(node_path_, &node_value)) {
114 LOG(ERROR) << "Failed to read node path: " << node_path_;
115 }
116 node_value = android::base::Trim(node_value);
117 std::string buf(
118 android::base::StringPrintf("Node Name\t"
119 "Node Path\t"
120 "Current Index\t"
121 "Current Value\t"
122 "Hold FD\t"
123 "Truncate\n"
124 "%s\t%s\t%zu\t%s\t%d\t%d\n",
125 name_.c_str(), node_path_.c_str(), current_val_index_,
126 node_value.c_str(), hold_fd_, truncate_));
127 if (!android::base::WriteStringToFd(buf, fd)) {
128 LOG(ERROR) << "Failed to dump fd: " << fd;
129 }
130 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
131 req_sorted_[i].DumpToFd(
132 fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
133 }
134 }
135
136 } // namespace perfmgr
137 } // namespace android
138