blob: e08e22c43c6e1bab20842dc656e23f52d5f4d48e [file] [log] [blame]
Wei Wanga4823192018-10-19 21:57:04 -07001/*
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 LOG_TAG "libperfmgr"
18
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/stringprintf.h>
22#include <android-base/strings.h>
23
24#include "perfmgr/FileNode.h"
25
26namespace android {
27namespace perfmgr {
28
29FileNode::FileNode(std::string name, std::string node_path,
30 std::vector<RequestGroup> req_sorted,
31 std::size_t default_val_index, bool reset_on_init,
32 bool hold_fd)
Wei Wangc43c2c02018-10-23 22:44:31 -070033 : Node(std::move(name), std::move(node_path), std::move(req_sorted),
34 default_val_index, reset_on_init),
Wei Wanga4823192018-10-19 21:57:04 -070035 hold_fd_(hold_fd) {
36 if (reset_on_init) {
Wei Wangc43c2c02018-10-23 22:44:31 -070037 Update(false);
Wei Wanga4823192018-10-19 21:57:04 -070038 }
39}
40
41std::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_) {
Wei Wanga9786b62018-10-22 15:19:46 -070055 const std::string& req_value =
56 req_sorted_[value_index].GetRequestValue();
Wei Wanga4823192018-10-19 21:57:04 -070057
58 fd_.reset(TEMP_FAILURE_RETRY(
59 open(node_path_.c_str(), O_WRONLY | O_CLOEXEC | O_TRUNC)));
60
61 if (fd_ == -1 || !android::base::WriteStringToFd(req_value, fd_)) {
62 if (log_error) {
63 LOG(WARNING) << "Failed to write to node: " << node_path_
64 << " with value: " << req_value << ", fd: " << fd_;
65 }
66 // Retry in 500ms or sooner
67 expire_time = std::min(expire_time, std::chrono::milliseconds(500));
68 } else {
69 // For regular file system, we need fsync
70 fsync(fd_);
71 // Some dev node requires file to remain open during the entire hint
72 // duration e.g. /dev/cpu_dma_latency, so fd_ is intentionally kept
73 // open during any requested value other than default one. If
74 // request a default value, node will write the value and then
75 // release the fd.
76 if ((!hold_fd_) || value_index == default_val_index_) {
77 fd_.reset();
78 }
79 // Update current index only when succeed
80 current_val_index_ = value_index;
81 }
82 }
83 return expire_time;
84}
85
86bool FileNode::GetHoldFd() const {
87 return hold_fd_;
88}
89
90void FileNode::DumpToFd(int fd) const {
91 std::string node_value;
92 if (!android::base::ReadFileToString(node_path_, &node_value)) {
93 LOG(ERROR) << "Failed to read node path: " << node_path_;
94 }
95 node_value = android::base::Trim(node_value);
96 std::string buf(android::base::StringPrintf(
97 "%s\t%s\t%zu\t%s\n", name_.c_str(), node_path_.c_str(),
98 current_val_index_, node_value.c_str()));
99 if (!android::base::WriteStringToFd(buf, fd)) {
100 LOG(ERROR) << "Failed to dump fd: " << fd;
101 }
102}
103
104} // namespace perfmgr
105} // namespace android