1 /*
2  * Copyright (C) 2024 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 specific 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/EventNode.h"
21 
22 #include <android-base/file.h>
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android-base/stringprintf.h>
26 #include <android-base/strings.h>
27 #include <utils/Trace.h>
28 
29 namespace android {
30 namespace perfmgr {
31 
EventNode(std::string name,std::string node_path,std::vector<RequestGroup> req_sorted,std::size_t default_val_index,bool reset_on_init,std::function<void (const std::string &,const std::string &,const std::string &)> update_callback)32 EventNode::EventNode(
33         std::string name, std::string node_path, std::vector<RequestGroup> req_sorted,
34         std::size_t default_val_index, bool reset_on_init,
35         std::function<void(const std::string &, const std::string &, const std::string &)>
36                 update_callback)
37     : Node(std::move(name), std::move(node_path), std::move(req_sorted), default_val_index,
38            reset_on_init),
39       update_callback_(update_callback) {}
40 
Update(bool)41 std::chrono::milliseconds EventNode::Update(bool) {
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 = req_sorted_[value_index].GetRequestValue();
56         if (ATRACE_ENABLED()) {
57             ATRACE_INT(("N:" + GetName()).c_str(), value_index);
58             const std::string tag =
59                     GetName() + ":" + req_value + ":" + std::to_string(expire_time.count());
60             ATRACE_BEGIN(tag.c_str());
61         }
62         update_callback_(name_, node_path_, req_value);
63         current_val_index_ = value_index;
64         reset_on_init_ = false;
65         if (ATRACE_ENABLED()) {
66             ATRACE_END();
67         }
68     }
69     return expire_time;
70 }
71 
DumpToFd(int fd) const72 void EventNode::DumpToFd(int fd) const {
73     const std::string &node_value = req_sorted_[current_val_index_].GetRequestValue();
74     std::string buf(android::base::StringPrintf(
75             "Node Name\t"
76             "Event Path\t"
77             "Current Index\t"
78             "Current Value\n"
79             "%s\t%s\t%zu\t%s\n",
80             name_.c_str(), node_path_.c_str(), current_val_index_, node_value.c_str()));
81     if (!android::base::WriteStringToFd(buf, fd)) {
82         LOG(ERROR) << "Failed to dump fd: " << fd;
83     }
84     for (std::size_t i = 0; i < req_sorted_.size(); i++) {
85         req_sorted_[i].DumpToFd(fd, android::base::StringPrintf("\t\tReq%zu:\t", i));
86     }
87 }
88 
89 }  // namespace perfmgr
90 }  // namespace android
91