1 /*
2 * Copyright (C) 2025 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 LOG_TAG "powerhal-libperfmgr"
18 #include "TaskRampupMultNode.h"
19
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22
23 namespace aidl {
24 namespace google {
25 namespace hardware {
26 namespace power {
27 namespace impl {
28 namespace pixel {
29
30 static constexpr char taskRampupSetPath[] = "/proc/vendor_sched/sched_qos/rampup_multiplier_set";
31
TaskRampupMultNode()32 TaskRampupMultNode::TaskRampupMultNode() {
33 if (access(taskRampupSetPath, W_OK) != 0) {
34 mTaskRampupFd = -1;
35 LOG(WARNING) << "Can't find vendor node: " << taskRampupSetPath;
36 return;
37 }
38
39 int flags = O_WRONLY | O_TRUNC | O_CLOEXEC;
40 mTaskRampupFd = TEMP_FAILURE_RETRY(open(taskRampupSetPath, flags));
41 if (mTaskRampupFd < 0) {
42 LOG(ERROR) << "Failed to open the node: " << taskRampupSetPath;
43 }
44 }
45
~TaskRampupMultNode()46 TaskRampupMultNode::~TaskRampupMultNode() {
47 if (mTaskRampupFd >= 0) {
48 ::close(mTaskRampupFd);
49 }
50 }
51
updateTaskRampupMult(int32_t tid,int32_t val)52 bool TaskRampupMultNode::updateTaskRampupMult(int32_t tid, int32_t val) {
53 std::lock_guard lock(mMutex);
54 if (mTaskRampupFd < 0) {
55 LOG(WARNING) << "Invalid task tampup multiplier file descriptor, skipping the update";
56 return false;
57 }
58
59 // Prepare the tid and value pair in the required format for the vendor procfs node.
60 std::string tidValPair = std::to_string(tid) + ":" + std::to_string(val);
61 if (!::android::base::WriteStringToFd(tidValPair, mTaskRampupFd)) {
62 LOG(ERROR) << "Failed to write the new value " << tidValPair
63 << " to task rampup multiplier node.";
64 return false;
65 }
66 return true;
67 }
68
isValid() const69 bool TaskRampupMultNode::isValid() const {
70 return mTaskRampupFd >= 0;
71 }
72
73 } // namespace pixel
74 } // namespace impl
75 } // namespace power
76 } // namespace hardware
77 } // namespace google
78 } // namespace aidl
79