cmsdk: Pass the process name and pid for launch boosts

* We need this for vendor perf tools.
 * This also adds a NativeHelper class which loads the JNI
   library on-demand, since we don't have an entry point.

Change-Id: If76ad8f952e86366978ae9cf9d1f107febccc28b
This commit is contained in:
Steve Kondik 2016-05-06 04:30:23 -07:00 committed by Steve Kondik
parent 186ae8353d
commit b77b8b5a70
7 changed files with 162 additions and 49 deletions

View File

@ -18,19 +18,22 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
src/org_cyanogenmod_platform_internal_CMAudioService.cpp \
src/org_cyanogenmod_platform_internal_PerformanceManagerService.cpp \
src/onload.cpp
LOCAL_C_INCLUDES := \
$(JNI_H_INCLUDE) \
$(TOP)/frameworks/base/core/jni \
$(TOP)/frameworks/av/include
$(TOP)/frameworks/av/include \
$(TOP)/hardware/libhardware/include
LOCAL_SHARED_LIBRARIES := \
libandroid_runtime \
libmedia \
liblog \
libcutils \
libutils \
libhardware \
liblog \
libmedia \
libutils
LOCAL_MODULE := libcmsdk_platform_jni
LOCAL_MODULE_TAGS := optional

View File

@ -22,6 +22,7 @@
namespace android {
int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv* env);
int register_org_cyanogenmod_platform_internal_PerformanceManagerService(JNIEnv* env);
};
@ -39,6 +40,7 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
ALOG_ASSERT(env, "Could not retrieve the env!");
register_org_cyanogenmod_platform_internal_CMAudioService(env);
register_org_cyanogenmod_platform_internal_PerformanceManagerService(env);
return JNI_VERSION_1_4;
}

View File

@ -0,0 +1,82 @@
/*
**
** Copyright 2016, The CyanogenMod Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "PerformanceManagerService-JNI"
#include <utils/Log.h>
#include <JNIHelp.h>
#include <jni.h>
#include "core_jni_helpers.h"
#include <hardware/power.h>
// ----------------------------------------------------------------------------
namespace android {
static const char* const kClassPathName =
"org/cyanogenmod/platform/internal/PerformanceManagerService";
static struct power_module* gPowerModule;
// ----------------------------------------------------------------------------
static void
org_cyanogenmod_platform_internal_PerformanceManagerService_launchBoost(
JNIEnv *env, jobject thiz, jint pid, jstring jPackageName)
{
if (env == NULL || jPackageName == NULL) {
return;
}
if (gPowerModule && gPowerModule->powerHint) {
const char *packageName = env->GetStringUTFChars(jPackageName, 0);
launch_boost_info_t *info = (launch_boost_info_t *)malloc(sizeof(launch_boost_info_t));
info->pid = pid;
info->packageName = packageName;
gPowerModule->powerHint(gPowerModule, POWER_HINT_LAUNCH_BOOST, (void *)info);
ALOGV("Sent LAUNCH BOOST for %s (pid=%d)", info->packageName, info->pid);
env->ReleaseStringUTFChars(jPackageName, packageName);
free(info);
}
}
// ----------------------------------------------------------------------------
static JNINativeMethod gMethods[] = {
{"native_launchBoost", "(ILjava/lang/String;)V",
(void *)org_cyanogenmod_platform_internal_PerformanceManagerService_launchBoost},
};
int register_org_cyanogenmod_platform_internal_PerformanceManagerService(JNIEnv *env)
{
status_t err = hw_get_module(POWER_HARDWARE_MODULE_ID,
(hw_module_t const**)&gPowerModule);
if (!err) {
gPowerModule->init(gPowerModule);
} else {
ALOGE("Couldn't load %s module (%s)", POWER_HARDWARE_MODULE_ID, strerror(-err));
}
return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}
} /* namespace android */

View File

@ -48,19 +48,6 @@ public class CMAudioService extends SystemService {
//keep in sync with include/media/AudioPolicy.h
private final static int AUDIO_OUTPUT_SESSION_EFFECTS_UPDATE = 10;
private static boolean sNativeLibraryLoaded;
static {
try {
System.loadLibrary("cmsdk_platform_jni");
sNativeLibraryLoaded = true;
} catch (Throwable t) {
sNativeLibraryLoaded = false;
Log.w(TAG, "CMSDK native platform unavailable");
}
}
public CMAudioService(Context context) {
super(context);
@ -76,7 +63,7 @@ public class CMAudioService extends SystemService {
return;
}
if (!sNativeLibraryLoaded) {
if (!NativeHelper.isNativeLibraryAvailable()) {
Log.wtf(TAG, "CM Audio service started by system server by native library is" +
"unavailable. Service will be unavailable.");
return;
@ -87,7 +74,7 @@ public class CMAudioService extends SystemService {
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_BOOT_COMPLETED) {
if (sNativeLibraryLoaded) {
if (NativeHelper.isNativeLibraryAvailable()) {
native_registerAudioSessionCallback(true);
}
}
@ -98,7 +85,7 @@ public class CMAudioService extends SystemService {
@Override
public List<AudioSessionInfo> listAudioSessions(int streamType) throws RemoteException {
final ArrayList<AudioSessionInfo> sessions = new ArrayList<AudioSessionInfo>();
if (!sNativeLibraryLoaded) {
if (!NativeHelper.isNativeLibraryAvailable()) {
// no sessions for u
return sessions;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2016, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cyanogenmod.platform.internal;
import android.util.Log;
class NativeHelper {
private static final String TAG = "CMSDK-JNI";
private static Boolean sNativeLibraryLoaded;
/**
* Loads the JNI backend if not already loaded.
*
* @return true if JNI platform library is loaded
*/
synchronized static boolean isNativeLibraryAvailable() {
if (sNativeLibraryLoaded == null) {
try {
System.loadLibrary("cmsdk_platform_jni");
sNativeLibraryLoaded = true;
} catch (Throwable t) {
sNativeLibraryLoaded = false;
Log.w(TAG, "CMSDK native library unavailable");
}
}
return sNativeLibraryLoaded;
}
}

View File

@ -22,19 +22,17 @@ import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManagerInternal;
import android.os.Process;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;
import android.util.Slog;
import com.android.server.ServiceThread;
import com.android.server.SystemService;
import com.android.server.Watchdog;
import java.util.regex.Pattern;
import cyanogenmod.app.CMContextConstants;
import cyanogenmod.power.IPerformanceManager;
@ -42,9 +40,6 @@ import cyanogenmod.power.PerformanceManager;
import cyanogenmod.power.PerformanceManagerInternal;
import cyanogenmod.providers.CMSettings;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
/** @hide */
public class PerformanceManagerService extends SystemService {
@ -76,10 +71,10 @@ public class PerformanceManagerService extends SystemService {
// Max time (microseconds) to allow a CPU boost for
private static final int MAX_CPU_BOOST_TIME = 5000000;
private static final boolean DEBUG = false;
public PerformanceManagerService(Context context) {
super(context);
mContext = context;
String[] activities = context.getResources().getStringArray(
@ -233,11 +228,8 @@ public class PerformanceManagerService extends SystemService {
mCurrentProfile = profile;
mHandler.removeMessages(MSG_CPU_BOOST);
mHandler.removeMessages(MSG_LAUNCH_BOOST);
mHandler.sendMessage(
Message.obtain(mHandler, MSG_SET_PROFILE, profile,
(fromUser ? 1 : 0)));
mHandler.obtainMessage(MSG_SET_PROFILE, profile,
(fromUser ? 1 : 0)).sendToTarget();
Binder.restoreCallingIdentity(token);
@ -262,8 +254,7 @@ public class PerformanceManagerService extends SystemService {
mCurrentProfile == PerformanceManager.PROFILE_HIGH_PERFORMANCE) {
return;
}
mHandler.removeMessages(MSG_CPU_BOOST);
mHandler.sendMessage(Message.obtain(mHandler, MSG_CPU_BOOST, duration, 0));
mHandler.obtainMessage(MSG_CPU_BOOST, duration, 0).sendToTarget();
} else {
Slog.e(TAG, "Invalid boost duration: " + duration);
}
@ -300,7 +291,7 @@ public class PerformanceManagerService extends SystemService {
/**
* Boost the CPU
*
*
* @param duration Duration to boost the CPU for, in milliseconds.
* @hide
*/
@ -331,17 +322,15 @@ public class PerformanceManagerService extends SystemService {
public void cpuBoost(int duration) {
cpuBoostInternal(duration);
}
@Override
public void launchBoost() {
public void launchBoost(int pid, String packageName) {
// Don't send boosts if we're in another power profile
if (mCurrentProfile == PerformanceManager.PROFILE_POWER_SAVE ||
mCurrentProfile == PerformanceManager.PROFILE_HIGH_PERFORMANCE) {
return;
}
mHandler.removeMessages(MSG_CPU_BOOST);
mHandler.removeMessages(MSG_LAUNCH_BOOST);
mHandler.sendEmptyMessage(MSG_LAUNCH_BOOST);
mHandler.obtainMessage(MSG_LAUNCH_BOOST, pid, 0, packageName).sendToTarget();
}
@Override
@ -362,7 +351,7 @@ public class PerformanceManagerService extends SystemService {
private static final int MSG_CPU_BOOST = 1;
private static final int MSG_LAUNCH_BOOST = 2;
private static final int MSG_SET_PROFILE = 3;
/**
* Handler for asynchronous operations performed by the performance manager.
*/
@ -378,7 +367,11 @@ public class PerformanceManagerService extends SystemService {
mPm.powerHint(POWER_HINT_CPU_BOOST, msg.arg1);
break;
case MSG_LAUNCH_BOOST:
mPm.powerHint(POWER_HINT_LAUNCH_BOOST, 0);
int pid = msg.arg1;
String packageName = (String) msg.obj;
if (NativeHelper.isNativeLibraryAvailable() && packageName != null) {
native_launchBoost(pid, packageName);
}
break;
case MSG_SET_PROFILE:
mPm.powerHint(POWER_HINT_SET_PROFILE, msg.arg1);
@ -402,4 +395,6 @@ public class PerformanceManagerService extends SystemService {
applyProfile(true);
}
};
private native final void native_launchBoost(int pid, String packageName);
}

View File

@ -20,10 +20,10 @@ import android.content.Intent;
/** {@hide} */
public interface PerformanceManagerInternal {
void activityResumed(Intent intent);
void cpuBoost(int duration);
void launchBoost();
void launchBoost(int pid, String packageName);
}