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,bool write_only)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, bool write_only)
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 write_only_(write_only),
40 warn_timeout_(android::base::GetBoolProperty("ro.debuggable", false) ? 5ms : 50ms) {}
41
Update(bool log_error)42 std::chrono::milliseconds FileNode::Update(bool log_error) {
43 std::size_t value_index = default_val_index_;
44 std::chrono::milliseconds expire_time = std::chrono::milliseconds::max();
45
46 // Find the highest outstanding request's expire time
47 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
48 if (req_sorted_[i].GetExpireTime(&expire_time)) {
49 value_index = i;
50 break;
51 }
52 }
53
54 // Update node only if request index changes
55 if (value_index != current_val_index_ || reset_on_init_) {
56 const std::string& req_value =
57 req_sorted_[value_index].GetRequestValue();
58 if (ATRACE_ENABLED()) {
59 const std::string tag = GetName() + ":" + req_value;
60 ATRACE_BEGIN(tag.c_str());
61 }
62 android::base::Timer t;
63 int flags = O_WRONLY | O_CLOEXEC;
64 if (GetTruncate()) {
65 flags |= O_TRUNC;
66 }
67 fd_.reset(TEMP_FAILURE_RETRY(open(node_path_.c_str(), flags)));
68
69 if (fd_ == -1 || !android::base::WriteStringToFd(req_value, fd_)) {
70 if (log_error) {
71 LOG(WARNING) << "Failed to write to node: " << node_path_
72 << " with value: " << req_value << ", fd: " << fd_;
73 }
74 // Retry in 500ms or sooner
75 expire_time = std::min(expire_time, std::chrono::milliseconds(500));
76 } else {
77 // For regular file system, we need fsync
78 fsync(fd_);
79 // Some dev node requires file to remain open during the entire hint
80 // duration e.g. /dev/cpu_dma_latency, so fd_ is intentionally kept
81 // open during any requested value other than default one. If
82 // request a default value, node will write the value and then
83 // release the fd.
84 if ((!hold_fd_) || value_index == default_val_index_) {
85 fd_.reset();
86 }
87 auto duration = t.duration();
88 if (duration > warn_timeout_) {
89 LOG(WARNING) << "Slow writing to file: '" << node_path_
90 << "' with value: '" << req_value
91 << "' took: " << duration.count() << " ms";
92 }
93 // Update current index only when succeed
94 current_val_index_ = value_index;
95 reset_on_init_ = false;
96 }
97 if (ATRACE_ENABLED()) {
98 ATRACE_END();
99 }
100 }
101 return expire_time;
102 }
103
GetHoldFd() const104 bool FileNode::GetHoldFd() const {
105 return hold_fd_;
106 }
107
GetTruncate() const108 bool FileNode::GetTruncate() const {
109 return truncate_;
110 }
111
DumpToFd(int fd) const112 void FileNode::DumpToFd(int fd) const {
113 std::string node_value;
114 if (!write_only_ && !android::base::ReadFileToString(node_path_, &node_value)) {
115 LOG(ERROR) << "Failed to read node path: " << node_path_;
116 }
117 node_value = android::base::Trim(node_value);
118 std::string buf(
119 android::base::StringPrintf("Node Name\t"
120 "Node Path\t"
121 "Current Index\t"
122 "Current Value\t"
123 "Hold FD\t"
124 "Truncate\n"
125 "%s\t%s\t%zu\t%s\t%d\t%d\n",
126 name_.c_str(), node_path_.c_str(), current_val_index_,
127 node_value.c_str(), hold_fd_, truncate_));
128 if (!android::base::WriteStringToFd(buf, fd)) {
129 LOG(ERROR) << "Failed to dump fd: " << fd;
130 }
131 for (std::size_t i = 0; i < req_sorted_.size(); i++) {
132 req_sorted_[i].DumpToFd(
133 fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
134 }
135 }
136
137 } // namespace perfmgr
138 } // namespace android
139