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         ATRACE_BEGIN(GetName().c_str());
59         const std::string& req_value =
60             req_sorted_[value_index].GetRequestValue();
61 
62         android::base::Timer t;
63         fd_.reset(TEMP_FAILURE_RETRY(
64             open(node_path_.c_str(), O_WRONLY | O_CLOEXEC | O_TRUNC)));
65 
66         if (fd_ == -1 || !android::base::WriteStringToFd(req_value, fd_)) {
67             if (log_error) {
68                 LOG(WARNING) << "Failed to write to node: " << node_path_
69                              << " with value: " << req_value << ", fd: " << fd_;
70             }
71             // Retry in 500ms or sooner
72             expire_time = std::min(expire_time, std::chrono::milliseconds(500));
73         } else {
74             // For regular file system, we need fsync
75             fsync(fd_);
76             // Some dev node requires file to remain open during the entire hint
77             // duration e.g. /dev/cpu_dma_latency, so fd_ is intentionally kept
78             // open during any requested value other than default one. If
79             // request a default value, node will write the value and then
80             // release the fd.
81             if ((!hold_fd_) || value_index == default_val_index_) {
82                 fd_.reset();
83             }
84             auto duration = t.duration();
85             if (duration > warn_timeout_) {
86                 LOG(WARNING) << "Slow writing to file: '" << node_path_
87                              << "' with value: '" << req_value
88                              << "' took: " << duration.count() << " ms";
89             }
90             // Update current index only when succeed
91             current_val_index_ = value_index;
92             reset_on_init_ = false;
93         }
94         ATRACE_END();
95     }
96     return expire_time;
97 }
98 
GetHoldFd() const99 bool FileNode::GetHoldFd() const {
100     return hold_fd_;
101 }
102 
DumpToFd(int fd) const103 void FileNode::DumpToFd(int fd) const {
104     std::string node_value;
105     if (!android::base::ReadFileToString(node_path_, &node_value)) {
106         LOG(ERROR) << "Failed to read node path: " << node_path_;
107     }
108     node_value = android::base::Trim(node_value);
109     std::string buf(android::base::StringPrintf(
110         "%s\t%s\t%zu\t%s\n", name_.c_str(), node_path_.c_str(),
111         current_val_index_, node_value.c_str()));
112     if (!android::base::WriteStringToFd(buf, fd)) {
113         LOG(ERROR) << "Failed to dump fd: " << fd;
114     }
115     for (std::size_t i = 0; i < req_sorted_.size(); i++) {
116         req_sorted_[i].DumpToFd(
117             fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
118     }
119 }
120 
121 }  // namespace perfmgr
122 }  // namespace android
123