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