1 /*
2 * Copyright (C) 2021 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 TAG "HintManagerService-JNI"
18
19 //#define LOG_NDEBUG 0
20
21 #include <android-base/stringprintf.h>
22 #include <android/hardware/power/IPower.h>
23 #include <android_runtime/AndroidRuntime.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <nativehelper/ScopedPrimitiveArray.h>
26 #include <powermanager/PowerHalController.h>
27 #include <utils/Log.h>
28
29 #include <unistd.h>
30 #include <cinttypes>
31
32 #include <sys/types.h>
33
34 #include "jni.h"
35
36 using android::hardware::power::IPowerHintSession;
37 using android::hardware::power::SessionHint;
38 using android::hardware::power::WorkDuration;
39
40 using android::base::StringPrintf;
41
42 namespace android {
43
44 static power::PowerHalController gPowerHalController;
45
createHintSession(JNIEnv * env,int32_t tgid,int32_t uid,std::vector<int32_t> threadIds,int64_t durationNanos)46 static jlong createHintSession(JNIEnv* env, int32_t tgid, int32_t uid,
47 std::vector<int32_t> threadIds, int64_t durationNanos) {
48 auto result =
49 gPowerHalController.createHintSession(tgid, uid, std::move(threadIds), durationNanos);
50 if (result.isOk()) {
51 sp<IPowerHintSession> appSession = result.value();
52 if (appSession) appSession->incStrong(env);
53 return reinterpret_cast<jlong>(appSession.get());
54 }
55 return 0;
56 }
57
pauseHintSession(JNIEnv * env,int64_t session_ptr)58 static void pauseHintSession(JNIEnv* env, int64_t session_ptr) {
59 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
60 appSession->pause();
61 }
62
resumeHintSession(JNIEnv * env,int64_t session_ptr)63 static void resumeHintSession(JNIEnv* env, int64_t session_ptr) {
64 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
65 appSession->resume();
66 }
67
closeHintSession(JNIEnv * env,int64_t session_ptr)68 static void closeHintSession(JNIEnv* env, int64_t session_ptr) {
69 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
70 appSession->close();
71 appSession->decStrong(env);
72 }
73
updateTargetWorkDuration(int64_t session_ptr,int64_t targetDurationNanos)74 static void updateTargetWorkDuration(int64_t session_ptr, int64_t targetDurationNanos) {
75 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
76 appSession->updateTargetWorkDuration(targetDurationNanos);
77 }
78
reportActualWorkDuration(int64_t session_ptr,const std::vector<WorkDuration> & actualDurations)79 static void reportActualWorkDuration(int64_t session_ptr,
80 const std::vector<WorkDuration>& actualDurations) {
81 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
82 appSession->reportActualWorkDuration(actualDurations);
83 }
84
sendHint(int64_t session_ptr,SessionHint hint)85 static void sendHint(int64_t session_ptr, SessionHint hint) {
86 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
87 appSession->sendHint(hint);
88 }
89
setThreads(int64_t session_ptr,const std::vector<int32_t> & threadIds)90 static void setThreads(int64_t session_ptr, const std::vector<int32_t>& threadIds) {
91 sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
92 appSession->setThreads(threadIds);
93 }
94
getHintSessionPreferredRate()95 static int64_t getHintSessionPreferredRate() {
96 int64_t rate = -1;
97 auto result = gPowerHalController.getHintSessionPreferredRate();
98 if (result.isOk()) {
99 rate = result.value();
100 }
101 return rate;
102 }
103
104 // ----------------------------------------------------------------------------
nativeInit(JNIEnv * env,jobject obj)105 static void nativeInit(JNIEnv* env, jobject obj) {
106 gPowerHalController.init();
107 }
108
nativeCreateHintSession(JNIEnv * env,jclass,jint tgid,jint uid,jintArray tids,jlong durationNanos)109 static jlong nativeCreateHintSession(JNIEnv* env, jclass /* clazz */, jint tgid, jint uid,
110 jintArray tids, jlong durationNanos) {
111 ScopedIntArrayRO tidArray(env, tids);
112 if (nullptr == tidArray.get() || tidArray.size() == 0) {
113 ALOGW("GetIntArrayElements returns nullptr.");
114 return 0;
115 }
116 std::vector<int32_t> threadIds(tidArray.size());
117 for (size_t i = 0; i < tidArray.size(); i++) {
118 threadIds[i] = tidArray[i];
119 }
120 return createHintSession(env, tgid, uid, std::move(threadIds), durationNanos);
121 }
122
nativePauseHintSession(JNIEnv * env,jclass,jlong session_ptr)123 static void nativePauseHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
124 pauseHintSession(env, session_ptr);
125 }
126
nativeResumeHintSession(JNIEnv * env,jclass,jlong session_ptr)127 static void nativeResumeHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
128 resumeHintSession(env, session_ptr);
129 }
130
nativeCloseHintSession(JNIEnv * env,jclass,jlong session_ptr)131 static void nativeCloseHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
132 closeHintSession(env, session_ptr);
133 }
134
nativeUpdateTargetWorkDuration(JNIEnv *,jclass,jlong session_ptr,jlong targetDurationNanos)135 static void nativeUpdateTargetWorkDuration(JNIEnv* /* env */, jclass /* clazz */, jlong session_ptr,
136 jlong targetDurationNanos) {
137 updateTargetWorkDuration(session_ptr, targetDurationNanos);
138 }
139
nativeReportActualWorkDuration(JNIEnv * env,jclass,jlong session_ptr,jlongArray actualDurations,jlongArray timeStamps)140 static void nativeReportActualWorkDuration(JNIEnv* env, jclass /* clazz */, jlong session_ptr,
141 jlongArray actualDurations, jlongArray timeStamps) {
142 ScopedLongArrayRO arrayActualDurations(env, actualDurations);
143 ScopedLongArrayRO arrayTimeStamps(env, timeStamps);
144
145 std::vector<WorkDuration> actualList(arrayActualDurations.size());
146 for (size_t i = 0; i < arrayActualDurations.size(); i++) {
147 actualList[i].timeStampNanos = arrayTimeStamps[i];
148 actualList[i].durationNanos = arrayActualDurations[i];
149 }
150 reportActualWorkDuration(session_ptr, actualList);
151 }
152
nativeSendHint(JNIEnv * env,jclass,jlong session_ptr,jint hint)153 static void nativeSendHint(JNIEnv* env, jclass /* clazz */, jlong session_ptr, jint hint) {
154 sendHint(session_ptr, static_cast<SessionHint>(hint));
155 }
156
nativeSetThreads(JNIEnv * env,jclass,jlong session_ptr,jintArray tids)157 static void nativeSetThreads(JNIEnv* env, jclass /* clazz */, jlong session_ptr, jintArray tids) {
158 ScopedIntArrayRO arrayThreadIds(env, tids);
159
160 std::vector<int32_t> threadIds(arrayThreadIds.size());
161 for (size_t i = 0; i < arrayThreadIds.size(); i++) {
162 threadIds[i] = arrayThreadIds[i];
163 }
164 setThreads(session_ptr, threadIds);
165 }
166
nativeGetHintSessionPreferredRate(JNIEnv *,jclass)167 static jlong nativeGetHintSessionPreferredRate(JNIEnv* /* env */, jclass /* clazz */) {
168 return static_cast<jlong>(getHintSessionPreferredRate());
169 }
170
171 // ----------------------------------------------------------------------------
172 static const JNINativeMethod sHintManagerServiceMethods[] = {
173 /* name, signature, funcPtr */
174 {"nativeInit", "()V", (void*)nativeInit},
175 {"nativeCreateHintSession", "(II[IJ)J", (void*)nativeCreateHintSession},
176 {"nativePauseHintSession", "(J)V", (void*)nativePauseHintSession},
177 {"nativeResumeHintSession", "(J)V", (void*)nativeResumeHintSession},
178 {"nativeCloseHintSession", "(J)V", (void*)nativeCloseHintSession},
179 {"nativeUpdateTargetWorkDuration", "(JJ)V", (void*)nativeUpdateTargetWorkDuration},
180 {"nativeReportActualWorkDuration", "(J[J[J)V", (void*)nativeReportActualWorkDuration},
181 {"nativeSendHint", "(JI)V", (void*)nativeSendHint},
182 {"nativeSetThreads", "(J[I)V", (void*)nativeSetThreads},
183 {"nativeGetHintSessionPreferredRate", "()J", (void*)nativeGetHintSessionPreferredRate},
184 };
185
register_android_server_HintManagerService(JNIEnv * env)186 int register_android_server_HintManagerService(JNIEnv* env) {
187 return jniRegisterNativeMethods(env,
188 "com/android/server/power/hint/"
189 "HintManagerService$NativeWrapper",
190 sHintManagerServiceMethods, NELEM(sHintManagerServiceMethods));
191 }
192
193 } /* namespace android */
194