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::WorkDuration;
38 
39 using android::base::StringPrintf;
40 
41 namespace android {
42 
43 static power::PowerHalController gPowerHalController;
44 
createHintSession(JNIEnv * env,int32_t tgid,int32_t uid,std::vector<int32_t> threadIds,int64_t durationNanos)45 static jlong createHintSession(JNIEnv* env, int32_t tgid, int32_t uid,
46                                std::vector<int32_t> threadIds, int64_t durationNanos) {
47     auto result =
48             gPowerHalController.createHintSession(tgid, uid, std::move(threadIds), durationNanos);
49     if (result.isOk()) {
50         sp<IPowerHintSession> appSession = result.value();
51         if (appSession) appSession->incStrong(env);
52         return reinterpret_cast<jlong>(appSession.get());
53     }
54     return 0;
55 }
56 
pauseHintSession(JNIEnv * env,int64_t session_ptr)57 static void pauseHintSession(JNIEnv* env, int64_t session_ptr) {
58     sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
59     appSession->pause();
60 }
61 
resumeHintSession(JNIEnv * env,int64_t session_ptr)62 static void resumeHintSession(JNIEnv* env, int64_t session_ptr) {
63     sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
64     appSession->resume();
65 }
66 
closeHintSession(JNIEnv * env,int64_t session_ptr)67 static void closeHintSession(JNIEnv* env, int64_t session_ptr) {
68     sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
69     appSession->close();
70     appSession->decStrong(env);
71 }
72 
updateTargetWorkDuration(int64_t session_ptr,int64_t targetDurationNanos)73 static void updateTargetWorkDuration(int64_t session_ptr, int64_t targetDurationNanos) {
74     sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
75     appSession->updateTargetWorkDuration(targetDurationNanos);
76 }
77 
reportActualWorkDuration(int64_t session_ptr,const std::vector<WorkDuration> & actualDurations)78 static void reportActualWorkDuration(int64_t session_ptr,
79                                      const std::vector<WorkDuration>& actualDurations) {
80     sp<IPowerHintSession> appSession = reinterpret_cast<IPowerHintSession*>(session_ptr);
81     appSession->reportActualWorkDuration(actualDurations);
82 }
83 
getHintSessionPreferredRate()84 static int64_t getHintSessionPreferredRate() {
85     int64_t rate = -1;
86     auto result = gPowerHalController.getHintSessionPreferredRate();
87     if (result.isOk()) {
88         rate = result.value();
89     }
90     return rate;
91 }
92 
93 // ----------------------------------------------------------------------------
nativeInit(JNIEnv * env,jobject obj)94 static void nativeInit(JNIEnv* env, jobject obj) {
95     gPowerHalController.init();
96 }
97 
nativeCreateHintSession(JNIEnv * env,jclass,jint tgid,jint uid,jintArray tids,jlong durationNanos)98 static jlong nativeCreateHintSession(JNIEnv* env, jclass /* clazz */, jint tgid, jint uid,
99                                      jintArray tids, jlong durationNanos) {
100     ScopedIntArrayRO tidArray(env, tids);
101     if (nullptr == tidArray.get() || tidArray.size() == 0) {
102         ALOGW("GetIntArrayElements returns nullptr.");
103         return 0;
104     }
105     std::vector<int32_t> threadIds(tidArray.size());
106     for (size_t i = 0; i < tidArray.size(); i++) {
107         threadIds[i] = tidArray[i];
108     }
109     return createHintSession(env, tgid, uid, std::move(threadIds), durationNanos);
110 }
111 
nativePauseHintSession(JNIEnv * env,jclass,jlong session_ptr)112 static void nativePauseHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
113     pauseHintSession(env, session_ptr);
114 }
115 
nativeResumeHintSession(JNIEnv * env,jclass,jlong session_ptr)116 static void nativeResumeHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
117     resumeHintSession(env, session_ptr);
118 }
119 
nativeCloseHintSession(JNIEnv * env,jclass,jlong session_ptr)120 static void nativeCloseHintSession(JNIEnv* env, jclass /* clazz */, jlong session_ptr) {
121     closeHintSession(env, session_ptr);
122 }
123 
nativeUpdateTargetWorkDuration(JNIEnv *,jclass,jlong session_ptr,jlong targetDurationNanos)124 static void nativeUpdateTargetWorkDuration(JNIEnv* /* env */, jclass /* clazz */, jlong session_ptr,
125                                            jlong targetDurationNanos) {
126     updateTargetWorkDuration(session_ptr, targetDurationNanos);
127 }
128 
nativeReportActualWorkDuration(JNIEnv * env,jclass,jlong session_ptr,jlongArray actualDurations,jlongArray timeStamps)129 static void nativeReportActualWorkDuration(JNIEnv* env, jclass /* clazz */, jlong session_ptr,
130                                            jlongArray actualDurations, jlongArray timeStamps) {
131     ScopedLongArrayRO arrayActualDurations(env, actualDurations);
132     ScopedLongArrayRO arrayTimeStamps(env, timeStamps);
133 
134     std::vector<WorkDuration> actualList(arrayActualDurations.size());
135     for (size_t i = 0; i < arrayActualDurations.size(); i++) {
136         actualList[i].timeStampNanos = arrayTimeStamps[i];
137         actualList[i].durationNanos = arrayActualDurations[i];
138     }
139     reportActualWorkDuration(session_ptr, actualList);
140 }
141 
nativeGetHintSessionPreferredRate(JNIEnv *,jclass)142 static jlong nativeGetHintSessionPreferredRate(JNIEnv* /* env */, jclass /* clazz */) {
143     return static_cast<jlong>(getHintSessionPreferredRate());
144 }
145 
146 // ----------------------------------------------------------------------------
147 static const JNINativeMethod sHintManagerServiceMethods[] = {
148         /* name, signature, funcPtr */
149         {"nativeInit", "()V", (void*)nativeInit},
150         {"nativeCreateHintSession", "(II[IJ)J", (void*)nativeCreateHintSession},
151         {"nativePauseHintSession", "(J)V", (void*)nativePauseHintSession},
152         {"nativeResumeHintSession", "(J)V", (void*)nativeResumeHintSession},
153         {"nativeCloseHintSession", "(J)V", (void*)nativeCloseHintSession},
154         {"nativeUpdateTargetWorkDuration", "(JJ)V", (void*)nativeUpdateTargetWorkDuration},
155         {"nativeReportActualWorkDuration", "(J[J[J)V", (void*)nativeReportActualWorkDuration},
156         {"nativeGetHintSessionPreferredRate", "()J", (void*)nativeGetHintSessionPreferredRate},
157 };
158 
register_android_server_HintManagerService(JNIEnv * env)159 int register_android_server_HintManagerService(JNIEnv* env) {
160     return jniRegisterNativeMethods(env,
161                                     "com/android/server/power/hint/"
162                                     "HintManagerService$NativeWrapper",
163                                     sHintManagerServiceMethods, NELEM(sHintManagerServiceMethods));
164 }
165 
166 } /* namespace android */
167