MTK平台CPU/GPU动态调频的实现之PerfService的源码分析
2018-01-04 16:56
856 查看
转自:http://blog.csdn.net/zhangyongfeiyong/article/details/52946781
Zygote进程启动后会启动System进程,在System进程启动过程中会启动系统中的关键服务,如AMS、PMS以及这里要分析的PerfService。先看下流程图:
SystemServer启动PerfService服务是通过实例化PerfServiceImpl的对象perfService,并把该服务的Binder对象添加到ServiceManager中。
先看SystemServer类中的startOtherServices方法:
[java]
view plain
copy
print?
private void startOtherServices() {
. . .
PerfServiceStateNotifier perfServiceNotifier = null;
IPerfServiceManager perfServiceMgr = null;
. . .
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
try {
Slog.i(TAG, "PerfService state notifier");
// 实例化PerfService的状态通知者
perfServiceNotifier = new PerfServiceStateNotifier();
// 把通知者注册到AMS的观察者中,有状态变化时会通知所有注册过的通知者
mActivityManagerService.registerActivityStateNotifier(perfServiceNotifier);
} catch (Throwable e) {
Slog.e(TAG, "FAIL starting PerfServiceStateNotifier", e);
}
// Create PerfService manager thread and add service
try {
// 实例化PerfServiceManager线程
perfServiceMgr = new PerfServiceManager(context);
IPerfService perfService = null;
// 实例化服务
perfService = new PerfServiceImpl(context, perfServiceMgr);
Slog.d("perfservice", "perfService=" + perfService);
if (perfService != null) {
// 把服务添加到ServiceManager中
ServiceManager.addService(Context.MTK_PERF_SERVICE, perfService.asBinder());
}
} catch (Throwable e) {
Slog.e(TAG, "perfservice Failure starting PerfService", e);
}
}
/// @}
. . .
}
. . .
/// M: add for hdmi feature
final IPerfServiceManager perfServiceF = perfServiceMgr;
// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
// Notify PerfService manager of system ready
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePerfServiceReady");
// 系统启动后回调IPerfServiceManager的systemReady方法
if (perfServiceF != null) perfServiceF.systemReady();
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
} catch (Throwable e) {
reportWtf("making PerfServiceManager ready", e);
}
}
/// @}
. . .
}
});
. . .
}
[java]
view plain
copy
print?
public interface IActivityStateNotifier {
public enum ActivityState {
Paused,
Resumed,
Destroyed,
Stopped
}
/**
* Notify activity state change.
*
* @param packageName The target package name.
* @param pid The process id package belongs to.
* @param className The class name of the package.
* @param actState Current lifecycle state of the package.
*/
public void notifyActivityState(String packageName, int pid,
String className, ActivityState actState);
/**
* Notify the process of activity has died.
*
* @param pid The process id has died.
* @param packageList The whole packages runs on the process.
*/
public void notifyAppDied(int pid, HashSet<String> packageList);
}
再看PerfServiceStateNotifier类的实现:
[java]
view plain
copy
print?
// 观察者模式的使用
public final class PerfServiceStateNotifier implements IActivityStateNotifier {
static final String TAG = "PerfServiceStateNotifier";
IPerfServiceWrapper mPerfService;
public PerfServiceStateNotifier() {
// 实例化PerfServiceWrapper
mPerfService = new PerfServiceWrapper(null);
}
/**
* Notify activity state change
* Activity状态变化时回调该方法
*/
public void notifyActivityState(String packageName, int pid,
String className, IActivityStateNotifier.ActivityState actState) {
int state;
//Slog.i(TAG,"[notifyActivityState] "+ packageName+ ", "+ className+ ", "+ actState);
switch(actState) {
case Paused:
state = IPerfServiceWrapper.STATE_PAUSED;
break;
case Resumed:
state = IPerfServiceWrapper.STATE_RESUMED;
break;
case Destroyed:
state = IPerfServiceWrapper.STATE_DESTROYED;
break;
case Stopped:
state = IPerfServiceWrapper.STATE_STOPPED;
break;
default:
return;
}
// 调用mPerfService的状态变化方法
mPerfService.notifyAppState(packageName, className, state, pid);
}
/**
* Notify the process of activity has died
* Activity销毁时回调该方法
*/
public void notifyAppDied(int pid, HashSet<String> packageList)
{
Iterator i = packageList.iterator();
while (i.hasNext()) {
String packageName = (String) i.next();
// 调用mPerfService的状态变化方法
mPerfService.notifyAppState(packageName, null, IPerfServiceWrapper.STATE_DEAD, pid);
}
}
}
当所监听的Activity生命周期方法被调用时会调用PerfServiceWrapper类的notifyAppState方法,那么下面先看下PerfServiceWrapper实现的接口IPerfServiceWrapper:
[java]
view plain
copy
print?
public interface IPerfServiceWrapper {
// 已经实现的场景
public static final int SCN_NONE = 0;
public static final int SCN_APP_SWITCH = 1; /* apply for both launch/exit */ // 切换应用
public static final int SCN_APP_ROTATE = 2; // 旋转应用
public static final int SCN_APP_TOUCH = 3; // 触摸应用,即点击屏幕
public static final int SCN_DONT_USE1 = 4; // 空闲???
public static final int SCN_SW_FRAME_UPDATE = 5; // 刷新帧率
public static final int SCN_APP_LAUNCH = 6; // 应用启动 同SCN_APP_SWITCH
public static final int SCN_GAMING = 7; // 游戏中。。。
public static final int SCN_MAX = 8; /* should be (last scenario + 1) */
public static final int STATE_PAUSED = 0;
public static final int STATE_RESUMED = 1;
public static final int STATE_DESTROYED = 2;
public static final int STATE_DEAD = 3;
public static final int STATE_STOPPED = 4;
public static final int DISPLAY_TYPE_GAME = 0; // 游戏模式显示类型
public static final int DISPLAY_TYPE_OTHERS = 1; // 非游戏模式
public static final int NOTIFY_USER_TYPE_PID = 0;
public static final int NOTIFY_USER_TYPE_FRAME_UPDATE = 1;
public static final int NOTIFY_USER_TYPE_DISPLAY_TYPE = 2;
public static final int NOTIFY_USER_TYPE_SCENARIO_ON = 3;
public static final int NOTIFY_USER_TYPE_SCENARIO_OFF = 4;
public static final int CMD_GET_CPU_FREQ_LEVEL_COUNT = 0; // 获取CPU频率级别总数
public static final int CMD_GET_CPU_FREQ_LITTLE_LEVEL_COUNT = 1; // 获取小核CPU频率级别总数
public static final int CMD_GET_CPU_FREQ_BIG_LEVEL_COUNT = 2; // 获取大核CPU频率级别总数
public static final int CMD_GET_GPU_FREQ_LEVEL_COUNT = 3; // 获取GPU频率级别总数
public static final int CMD_GET_MEM_FREQ_LEVEL_COUNT = 4; // 获取内存频率级别总数
public static final int CMD_GET_PERF_INDEX_MIN = 5; // 获取性能优化最小索引值
public static final int CMD_GET_PERF_INDEX_MAX = 6; // 获取性能优化最大索引值
public static final int CMD_GET_PERF_NORMALIZED_INDEX_MAX = 7; // 获取普通性能优化最大索引值
public static final int CMD_SET_CPU_CORE_MIN = 0; // 设置CPU最少核数
public static final int CMD_SET_CPU_CORE_MAX = 1; // 设置CPU最多核数
public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MIN = 2; // 设置大核小核的最少核数
public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MAX = 3; // 设置大核小核的最多核数
public static final int CMD_SET_CPU_FREQ_MIN = 4; // 设置CPU的最低频率值
public static final int CMD_SET_CPU_FREQ_MAX = 5; // 设置CPU的最高频率值
public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MIN = 6; // 设置大核小核的最低频率值
public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MAX = 7; // 设置大核小核的最高频率值
public static final int CMD_SET_GPU_FREQ_MIN = 8; // 设置GPU的最低频率值
public static final int CMD_SET_GPU_FREQ_MAX = 9; // 设置GPU的最高频率值
public static final int CMD_SET_VCORE = 10; // 设置图形模式,0/2:默认模式,1:低功耗模式,3:高性能模式
public static final int CMD_SET_SCREEN_OFF_STATE = 11; // 设置锁屏时状态,0:锁屏无效,1:锁屏有效,2:锁屏暂停,开屏恢复
public static final int CMD_SET_CPUFREQ_HISPEED_FREQ = 12; // 设置CPU高速频率
public static final int CMD_SET_CPUFREQ_MIN_SAMPLE_TIME = 13; // 设置采样CPU频率值的最小间隔时间
public static final int CMD_SET_CPUFREQ_ABOVE_HISPEED_DELAY = 14; // 设置CPU超高速频率延迟时间
public static final int CMD_SET_CLUSTER_CPU_CORE_MIN = 15; // 设置CPU簇的最少核数
public static final int CMD_SET_CLUSTER_CPU_CORE_MAX = 16; // 设置CPU簇的最多核数
public static final int CMD_SET_CLUSTER_CPU_FREQ_MIN = 17; // 设置CPU簇的最低频率值
public static final int CMD_SET_CLUSTER_CPU_FREQ_MAX = 18; // 设置CPU簇的最高频率值
public static final int CMD_SET_ROOT_CLUSTER = 19; // 设置root簇
public static final int CMD_SET_CPU_UP_THRESHOLD = 20; // 设置CPU温度的最高阈值
public static final int CMD_SET_CPU_DOWN_THRESHOLD = 21; // 设置CPU温度的最低阈值
public static final int CMD_SET_PERF_INDEX = 22; // 设置性能优化的索引值
public static final int CMD_SET_NORMALIZED_PERF_INDEX = 23; // 设置普通性能优化的索引值
// 其他方法的声明
. . .
public void notifyAppState(String packName, String className, int state, int pid);
// 其他方法的声明
. . .
}
再看PerfServiceWrapper类的实现:
[java]
view plain
copy
print?
public class PerfServiceWrapper implements IPerfServiceWrapper {
private static final String TAG = "PerfServiceWrapper";
private IPerfService sService = null;
private Context mContext;
private int inited = 0;
private int setTid = 0;
private long mPreviousTime = 0;
private static final int RENDER_THREAD_UPDATE_DURATION = 400;
public static native int nativeGetPid();
public static native int nativeGetTid();
private void init() {
if (inited == 0) {
IBinder b = ServiceManager.checkService(Context.MTK_PERF_SERVICE);
if (b != null) {
// 获取服务
sService = IPerfService.Stub.asInterface(b);
if (sService != null)
inited = 1;
else
log("ERR: getService() sService is still null..");
}
}
}
public PerfServiceWrapper(Context context) {
mContext = context;
init();
}
// 其他接口中声明方法的实现
. . .
public void notifyAppState(String packName, String className, int state, int pid) {
//log("boostEnable");
try {
init();
if (sService != null)
// 调用sService的notifyAppState方法
sService.notifyAppState(packName, className, state, pid);
} catch (RemoteException e) {
loge("ERR: RemoteException in notifyAppState:" + e);
}
}
// 其他接口中声明方法的实现
. . .
private void log(String info) {
Log.d("@M_" + TAG, "[PerfServiceWrapper] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfServiceWrapper] ERR: " + info + " ");
}
}
该类中的方法都是通过Binder进程间通信方式调用PerfServiceImpl中的方法实现的。由下面分析知,最后调用的是PerfServiceManager中的方法。
下面看PerfServiceManager的实例化过程,该类实现了IPerfServiceManager接口:
[java]
view plain
copy
print?
public class PerfServiceManager implements IPerfServiceManager {
private static final String TAG = "PerfServiceManager";
private HandlerThread mHandlerThread;
private PerfServiceThreadHandler mHandler;
private Context mContext;
final List<Integer> mTimeList;
private boolean bDuringTouch;
private boolean bRenderAwareValid; // render aware is only valid within 3 sec.渲染只在3秒内有效
private static final int RENDER_AWARE_DURATION_MS = 3000;
private static final int UI_UPDATE_DURATION_MS = 500;
private static final int RENDER_BIT = 0x800000;
private static final float HEAP_UTILIZATION_DURING_FRAME_UPDATE = 0.5f; // 刷新帧时的堆栈利用率
private int mDisplayType;
private VMRuntime mRuntime;
private float mDefaultUtilization;
// 声明的一些native方法,追踪到native层后,MTK只提供了一个so库,具体实现是看不到的。。。
. . .
public static native int nativePerfNotifyAppState(String packName, String className,
int state, int pid);
// 声明的一些native方法
. . .
public class PerfServiceAppState {
private String mPackName;
private String mClassName;
private int mState;
private int mPid;
PerfServiceAppState(String packName, String className, int state, int pid) {
mPackName = packName;
mClassName = className;
mState = state;
mPid = pid;
}
}
//static
//{
// Log.w(TAG, "load libperfservice_jni.so");
// System.loadLibrary("perfservice_jni");
//}
public PerfServiceManager(Context context) {
super();
mContext = context;
// 创建并开启名为PerfServiceManager的线程
mHandlerThread = new HandlerThread("PerfServiceManager", Process.THREAD_PRIORITY_FOREGROUND);
mHandlerThread.start();
Looper looper = mHandlerThread.getLooper();
if (looper != null) {
// 初始化Handler
mHandler = new PerfServiceThreadHandler(looper);
}
mTimeList = new ArrayList<Integer>();
bDuringTouch = false;
bRenderAwareValid = false;
// 默认显示类型:非游戏模式
mDisplayType = IPerfServiceWrapper.DISPLAY_TYPE_OTHERS;
mRuntime = VMRuntime.getRuntime();
mDefaultUtilization = mRuntime.getTargetHeapUtilization();
log("Created and started PerfService thread");
}
// 接口中其他方法的实现
. . .
public void notifyAppState(String packName, String className, int state, int pid) {
//log("notifyAppState");
Message msg = mHandler.obtainMessage();
msg.what = PerfServiceThreadHandler.MESSAGE_NOTIFY_APP_STATE;
msg.obj = new PerfServiceAppState(packName, className, state, pid);
msg.sendToTarget();
}
. . .
private class PerfServiceThreadHandler extends Handler {
. . .
private static final int MESSAGE_NOTIFY_APP_STATE = 4;
. . .
public PerfServiceThreadHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
try {
switch (msg.what) {
. . .
case MESSAGE_NOTIFY_APP_STATE:
{
PerfServiceAppState passedObject = (PerfServiceAppState) msg.obj;
//log("MESSAGE_NOTIFY_APP_STATE");
// 通过调用native方法实现
nativePerfNotifyAppState(passedObject.mPackName, passedObject.mClassName,
passedObject.mState, passedObject.mPid);
passedObject = null;
msg.obj = null;
break;
}
. . .
default:
{
. . .
}
}
} catch (NullPointerException e) {
loge("Exception in PerfServiceThreadHandler.handleMessage: " + e);
}
}
. . .
}
private void log(String info) {
Log.d("@M_" + TAG, "[PerfService] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");
}
}
再看PerfServiceImpl类的实现:
[java]
view plain
copy
print?
public class PerfServiceImpl extends IPerfService.Stub {
private static final String TAG = "PerfService";
private IPerfServiceManager perfServiceMgr;
final Context mContext;
// 接收锁屏/开屏的广播
class PerfServiceBroadcastReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
log("Intent.ACTION_SCREEN_OFF");
perfServiceMgr.userDisableAll();
return;
}
else if (Intent.ACTION_SCREEN_ON.equals(action)) {
log("Intent.ACTION_SCREEN_ON");
perfServiceMgr.userRestoreAll();
return;
}
else {
log("Unexpected intent " + intent);
}
}
}
public PerfServiceImpl(Context context, IPerfServiceManager pm) {
perfServiceMgr = pm;
mContext = context;
final IntentFilter broadcastFilter = new IntentFilter();
broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
mContext.registerReceiver(new PerfServiceBroadcastReceiver(), broadcastFilter);
}
// aidl中声明的其他方法
. . .
public void notifyAppState(java.lang.String packName, java.lang.String className,
int state, int pid) {
//log("notifyAppState");
// 调用PerfServiceManager中的方法,最后调用的是native方法
perfServiceMgr.notifyAppState(packName, className, state, pid);
}
. . .
private void log(String info) {
Log.d("@M_" + TAG, "[PerfService] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");
}
}
另外:
在vendor\mediatek\proprietary\hardware\perfservice\mt**\app_list目录下的perfservapplist.txt文件中可以自定义应用白名单,默认值为:
CMD_SET_CPU_CORE_MIN com.imangi.templerun2 3
意思是用户使用“神庙逃亡”应用时,CPU最少核心数为3
在vendor\mediatek\proprietary\hardware\perfservice\mt**\scn_tbl目录下的perfservscntbl.txt文件中可以自定义场景,默认场景有:
CMD_SET_CPU_CORE_MIN, SCN_APP_TOUCH, 3
CMD_SET_CPU_FREQ_MIN, SCN_APP_TOUCH, 1014000
CMD_SET_CPU_CORE_MIN, SCN_SW_FRAME_UPDATE, 3
CMD_SET_CPU_FREQ_MIN, SCN_SW_FRAME_UPDATE, 1014000
CMD_SET_CPU_UP_THRESHOLD, SCN_SW_FRAME_UPDATE, 80
CMD_SET_CPU_DOWN_THRESHOLD, SCN_SW_FRAME_UPDATE, 65
CMD_SET_CPU_FREQ_BIG_LITTLE_MIN, SCN_APP_SWITCH, 1950000, 1144000
CMD_SET_CPU_CORE_BIG_LITTLE_MIN, SCN_APP_SWITCH, 4, 0
CMD_SET_VCORE, SCN_APP_SWITCH, 3
CMD_SET_CPU_FREQ_BIG_LITTLE_MIN, SCN_APP_ROTATE, 1950000, 1144000
CMD_SET_VCORE, SCN_APP_ROTATE, 3
CMD_SET_VCORE, SCN_GAMING, 1
Zygote进程启动后会启动System进程,在System进程启动过程中会启动系统中的关键服务,如AMS、PMS以及这里要分析的PerfService。先看下流程图:
SystemServer启动PerfService服务是通过实例化PerfServiceImpl的对象perfService,并把该服务的Binder对象添加到ServiceManager中。
先看SystemServer类中的startOtherServices方法:
[java]
view plain
copy
print?
private void startOtherServices() {
. . .
PerfServiceStateNotifier perfServiceNotifier = null;
IPerfServiceManager perfServiceMgr = null;
. . .
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
try {
Slog.i(TAG, "PerfService state notifier");
// 实例化PerfService的状态通知者
perfServiceNotifier = new PerfServiceStateNotifier();
// 把通知者注册到AMS的观察者中,有状态变化时会通知所有注册过的通知者
mActivityManagerService.registerActivityStateNotifier(perfServiceNotifier);
} catch (Throwable e) {
Slog.e(TAG, "FAIL starting PerfServiceStateNotifier", e);
}
// Create PerfService manager thread and add service
try {
// 实例化PerfServiceManager线程
perfServiceMgr = new PerfServiceManager(context);
IPerfService perfService = null;
// 实例化服务
perfService = new PerfServiceImpl(context, perfServiceMgr);
Slog.d("perfservice", "perfService=" + perfService);
if (perfService != null) {
// 把服务添加到ServiceManager中
ServiceManager.addService(Context.MTK_PERF_SERVICE, perfService.asBinder());
}
} catch (Throwable e) {
Slog.e(TAG, "perfservice Failure starting PerfService", e);
}
}
/// @}
. . .
}
. . .
/// M: add for hdmi feature
final IPerfServiceManager perfServiceF = perfServiceMgr;
// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
// Notify PerfService manager of system ready
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePerfServiceReady");
// 系统启动后回调IPerfServiceManager的systemReady方法
if (perfServiceF != null) perfServiceF.systemReady();
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
} catch (Throwable e) {
reportWtf("making PerfServiceManager ready", e);
}
}
/// @}
. . .
}
});
. . .
}
private void startOtherServices() { . . . PerfServiceStateNotifier perfServiceNotifier = null; IPerfServiceManager perfServiceMgr = null; . . . if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) { . . . /// M: add for PerfService feature @{ if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) { try { Slog.i(TAG, "PerfService state notifier"); // 实例化PerfService的状态通知者 perfServiceNotifier = new PerfServiceStateNotifier(); // 把通知者注册到AMS的观察者中,有状态变化时会通知所有注册过的通知者 mActivityManagerService.registerActivityStateNotifier(perfServiceNotifier); } catch (Throwable e) { Slog.e(TAG, "FAIL starting PerfServiceStateNotifier", e); } // Create PerfService manager thread and add service try { // 实例化PerfServiceManager线程 perfServiceMgr = new PerfServiceManager(context); IPerfService perfService = null; // 实例化服务 perfService = new PerfServiceImpl(context, perfServiceMgr); Slog.d("perfservice", "perfService=" + perfService); if (perfService != null) { // 把服务添加到ServiceManager中 ServiceManager.addService(Context.MTK_PERF_SERVICE, perfService.asBinder()); } } catch (Throwable e) { Slog.e(TAG, "perfservice Failure starting PerfService", e); } } /// @} . . . } . . . /// M: add for hdmi feature final IPerfServiceManager perfServiceF = perfServiceMgr; // We now tell the activity manager it is okay to run third party // code. It will call back into us once it has gotten to the state // where third party code can really run (but before it has actually // started launching the initial applications), for us to complete our // initialization. mActivityManagerService.systemReady(new Runnable() { @Override public void run() { Slog.i(TAG, "Making services ready"); . . . /// M: add for PerfService feature @{ if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) { // Notify PerfService manager of system ready try { Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePerfServiceReady"); // 系统启动后回调IPerfServiceManager的systemReady方法 if (perfServiceF != null) perfServiceF.systemReady(); Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); } catch (Throwable e) { reportWtf("making PerfServiceManager ready", e); } } /// @} . . . } }); . . . }再看PerfServiceStateNotifier类,先看下它实现的接口IActivityStateNotifier:
[java]
view plain
copy
print?
public interface IActivityStateNotifier {
public enum ActivityState {
Paused,
Resumed,
Destroyed,
Stopped
}
/**
* Notify activity state change.
*
* @param packageName The target package name.
* @param pid The process id package belongs to.
* @param className The class name of the package.
* @param actState Current lifecycle state of the package.
*/
public void notifyActivityState(String packageName, int pid,
String className, ActivityState actState);
/**
* Notify the process of activity has died.
*
* @param pid The process id has died.
* @param packageList The whole packages runs on the process.
*/
public void notifyAppDied(int pid, HashSet<String> packageList);
}
public interface IActivityStateNotifier { public enum ActivityState { Paused, Resumed, Destroyed, Stopped } /** * Notify activity state change. * * @param packageName The target package name. * @param pid The process id package belongs to. * @param className The class name of the package. * @param actState Current lifecycle state of the package. */ public void notifyActivityState(String packageName, int pid, String className, ActivityState actState); /** * Notify the process of activity has died. * * @param pid The process id has died. * @param packageList The whole packages runs on the process. */ public void notifyAppDied(int pid, HashSet<String> packageList); }
再看PerfServiceStateNotifier类的实现:
[java]
view plain
copy
print?
// 观察者模式的使用
public final class PerfServiceStateNotifier implements IActivityStateNotifier {
static final String TAG = "PerfServiceStateNotifier";
IPerfServiceWrapper mPerfService;
public PerfServiceStateNotifier() {
// 实例化PerfServiceWrapper
mPerfService = new PerfServiceWrapper(null);
}
/**
* Notify activity state change
* Activity状态变化时回调该方法
*/
public void notifyActivityState(String packageName, int pid,
String className, IActivityStateNotifier.ActivityState actState) {
int state;
//Slog.i(TAG,"[notifyActivityState] "+ packageName+ ", "+ className+ ", "+ actState);
switch(actState) {
case Paused:
state = IPerfServiceWrapper.STATE_PAUSED;
break;
case Resumed:
state = IPerfServiceWrapper.STATE_RESUMED;
break;
case Destroyed:
state = IPerfServiceWrapper.STATE_DESTROYED;
break;
case Stopped:
state = IPerfServiceWrapper.STATE_STOPPED;
break;
default:
return;
}
// 调用mPerfService的状态变化方法
mPerfService.notifyAppState(packageName, className, state, pid);
}
/**
* Notify the process of activity has died
* Activity销毁时回调该方法
*/
public void notifyAppDied(int pid, HashSet<String> packageList)
{
Iterator i = packageList.iterator();
while (i.hasNext()) {
String packageName = (String) i.next();
// 调用mPerfService的状态变化方法
mPerfService.notifyAppState(packageName, null, IPerfServiceWrapper.STATE_DEAD, pid);
}
}
}
// 观察者模式的使用 public final class PerfServiceStateNotifier implements IActivityStateNotifier { static final String TAG = "PerfServiceStateNotifier"; IPerfServiceWrapper mPerfService; public PerfServiceStateNotifier() { // 实例化PerfServiceWrapper mPerfService = new PerfServiceWrapper(null); } /** * Notify activity state change * Activity状态变化时回调该方法 */ public void notifyActivityState(String packageName, int pid, String className, IActivityStateNotifier.ActivityState actState) { int state; //Slog.i(TAG,"[notifyActivityState] "+ packageName+ ", "+ className+ ", "+ actState); switch(actState) { case Paused: state = IPerfServiceWrapper.STATE_PAUSED; break; case Resumed: state = IPerfServiceWrapper.STATE_RESUMED; break; case Destroyed: state = IPerfServiceWrapper.STATE_DESTROYED; break; case Stopped: state = IPerfServiceWrapper.STATE_STOPPED; break; default: return; } // 调用mPerfService的状态变化方法 mPerfService.notifyAppState(packageName, className, state, pid); } /** * Notify the process of activity has died * Activity销毁时回调该方法 */ public void notifyAppDied(int pid, HashSet<String> packageList) { Iterator i = packageList.iterator(); while (i.hasNext()) { String packageName = (String) i.next(); // 调用mPerfService的状态变化方法 mPerfService.notifyAppState(packageName, null, IPerfServiceWrapper.STATE_DEAD, pid); } } }
当所监听的Activity生命周期方法被调用时会调用PerfServiceWrapper类的notifyAppState方法,那么下面先看下PerfServiceWrapper实现的接口IPerfServiceWrapper:
[java]
view plain
copy
print?
public interface IPerfServiceWrapper {
// 已经实现的场景
public static final int SCN_NONE = 0;
public static final int SCN_APP_SWITCH = 1; /* apply for both launch/exit */ // 切换应用
public static final int SCN_APP_ROTATE = 2; // 旋转应用
public static final int SCN_APP_TOUCH = 3; // 触摸应用,即点击屏幕
public static final int SCN_DONT_USE1 = 4; // 空闲???
public static final int SCN_SW_FRAME_UPDATE = 5; // 刷新帧率
public static final int SCN_APP_LAUNCH = 6; // 应用启动 同SCN_APP_SWITCH
public static final int SCN_GAMING = 7; // 游戏中。。。
public static final int SCN_MAX = 8; /* should be (last scenario + 1) */
public static final int STATE_PAUSED = 0;
public static final int STATE_RESUMED = 1;
public static final int STATE_DESTROYED = 2;
public static final int STATE_DEAD = 3;
public static final int STATE_STOPPED = 4;
public static final int DISPLAY_TYPE_GAME = 0; // 游戏模式显示类型
public static final int DISPLAY_TYPE_OTHERS = 1; // 非游戏模式
public static final int NOTIFY_USER_TYPE_PID = 0;
public static final int NOTIFY_USER_TYPE_FRAME_UPDATE = 1;
public static final int NOTIFY_USER_TYPE_DISPLAY_TYPE = 2;
public static final int NOTIFY_USER_TYPE_SCENARIO_ON = 3;
public static final int NOTIFY_USER_TYPE_SCENARIO_OFF = 4;
public static final int CMD_GET_CPU_FREQ_LEVEL_COUNT = 0; // 获取CPU频率级别总数
public static final int CMD_GET_CPU_FREQ_LITTLE_LEVEL_COUNT = 1; // 获取小核CPU频率级别总数
public static final int CMD_GET_CPU_FREQ_BIG_LEVEL_COUNT = 2; // 获取大核CPU频率级别总数
public static final int CMD_GET_GPU_FREQ_LEVEL_COUNT = 3; // 获取GPU频率级别总数
public static final int CMD_GET_MEM_FREQ_LEVEL_COUNT = 4; // 获取内存频率级别总数
public static final int CMD_GET_PERF_INDEX_MIN = 5; // 获取性能优化最小索引值
public static final int CMD_GET_PERF_INDEX_MAX = 6; // 获取性能优化最大索引值
public static final int CMD_GET_PERF_NORMALIZED_INDEX_MAX = 7; // 获取普通性能优化最大索引值
public static final int CMD_SET_CPU_CORE_MIN = 0; // 设置CPU最少核数
public static final int CMD_SET_CPU_CORE_MAX = 1; // 设置CPU最多核数
public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MIN = 2; // 设置大核小核的最少核数
public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MAX = 3; // 设置大核小核的最多核数
public static final int CMD_SET_CPU_FREQ_MIN = 4; // 设置CPU的最低频率值
public static final int CMD_SET_CPU_FREQ_MAX = 5; // 设置CPU的最高频率值
public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MIN = 6; // 设置大核小核的最低频率值
public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MAX = 7; // 设置大核小核的最高频率值
public static final int CMD_SET_GPU_FREQ_MIN = 8; // 设置GPU的最低频率值
public static final int CMD_SET_GPU_FREQ_MAX = 9; // 设置GPU的最高频率值
public static final int CMD_SET_VCORE = 10; // 设置图形模式,0/2:默认模式,1:低功耗模式,3:高性能模式
public static final int CMD_SET_SCREEN_OFF_STATE = 11; // 设置锁屏时状态,0:锁屏无效,1:锁屏有效,2:锁屏暂停,开屏恢复
public static final int CMD_SET_CPUFREQ_HISPEED_FREQ = 12; // 设置CPU高速频率
public static final int CMD_SET_CPUFREQ_MIN_SAMPLE_TIME = 13; // 设置采样CPU频率值的最小间隔时间
public static final int CMD_SET_CPUFREQ_ABOVE_HISPEED_DELAY = 14; // 设置CPU超高速频率延迟时间
public static final int CMD_SET_CLUSTER_CPU_CORE_MIN = 15; // 设置CPU簇的最少核数
public static final int CMD_SET_CLUSTER_CPU_CORE_MAX = 16; // 设置CPU簇的最多核数
public static final int CMD_SET_CLUSTER_CPU_FREQ_MIN = 17; // 设置CPU簇的最低频率值
public static final int CMD_SET_CLUSTER_CPU_FREQ_MAX = 18; // 设置CPU簇的最高频率值
public static final int CMD_SET_ROOT_CLUSTER = 19; // 设置root簇
public static final int CMD_SET_CPU_UP_THRESHOLD = 20; // 设置CPU温度的最高阈值
public static final int CMD_SET_CPU_DOWN_THRESHOLD = 21; // 设置CPU温度的最低阈值
public static final int CMD_SET_PERF_INDEX = 22; // 设置性能优化的索引值
public static final int CMD_SET_NORMALIZED_PERF_INDEX = 23; // 设置普通性能优化的索引值
// 其他方法的声明
. . .
public void notifyAppState(String packName, String className, int state, int pid);
// 其他方法的声明
. . .
}
public interface IPerfServiceWrapper { // 已经实现的场景 public static final int SCN_NONE = 0; public static final int SCN_APP_SWITCH = 1; /* apply for both launch/exit */ // 切换应用 public static final int SCN_APP_ROTATE = 2; // 旋转应用 public static final int SCN_APP_TOUCH = 3; // 触摸应用,即点击屏幕 public static final int SCN_DONT_USE1 = 4; // 空闲??? public static final int SCN_SW_FRAME_UPDATE = 5; // 刷新帧率 public static final int SCN_APP_LAUNCH = 6; // 应用启动 同SCN_APP_SWITCH public static final int SCN_GAMING = 7; // 游戏中。。。 public static final int SCN_MAX = 8; /* should be (last scenario + 1) */ public static final int STATE_PAUSED = 0; public static final int STATE_RESUMED = 1; public static final int STATE_DESTROYED = 2; public static final int STATE_DEAD = 3; public static final int STATE_STOPPED = 4; public static final int DISPLAY_TYPE_GAME = 0; // 游戏模式显示类型 public static final int DISPLAY_TYPE_OTHERS = 1; // 非游戏模式 public static final int NOTIFY_USER_TYPE_PID = 0; public static final int NOTIFY_USER_TYPE_FRAME_UPDATE = 1; public static final int NOTIFY_USER_TYPE_DISPLAY_TYPE = 2; public static final int NOTIFY_USER_TYPE_SCENARIO_ON = 3; public static final int NOTIFY_USER_TYPE_SCENARIO_OFF = 4; public static final int CMD_GET_CPU_FREQ_LEVEL_COUNT = 0; // 获取CPU频率级别总数 public static final int CMD_GET_CPU_FREQ_LITTLE_LEVEL_COUNT = 1; // 获取小核CPU频率级别总数 public static final int CMD_GET_CPU_FREQ_BIG_LEVEL_COUNT = 2; // 获取大核CPU频率级别总数 public static final int CMD_GET_GPU_FREQ_LEVEL_COUNT = 3; // 获取GPU频率级别总数 public static final int CMD_GET_MEM_FREQ_LEVEL_COUNT = 4; // 获取内存频率级别总数 public static final int CMD_GET_PERF_INDEX_MIN = 5; // 获取性能优化最小索引值 public static final int CMD_GET_PERF_INDEX_MAX = 6; // 获取性能优化最大索引值 public static final int CMD_GET_PERF_NORMALIZED_INDEX_MAX = 7; // 获取普通性能优化最大索引值 public static final int CMD_SET_CPU_CORE_MIN = 0; // 设置CPU最少核数 public static final int CMD_SET_CPU_CORE_MAX = 1; // 设置CPU最多核数 public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MIN = 2; // 设置大核小核的最少核数 public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MAX = 3; // 设置大核小核的最多核数 public static final int CMD_SET_CPU_FREQ_MIN = 4; // 设置CPU的最低频率值 public static final int CMD_SET_CPU_FREQ_MAX = 5; // 设置CPU的最高频率值 public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MIN = 6; // 设置大核小核的最低频率值 public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MAX = 7; // 设置大核小核的最高频率值 public static final int CMD_SET_GPU_FREQ_MIN = 8; // 设置GPU的最低频率值 public static final int CMD_SET_GPU_FREQ_MAX = 9; // 设置GPU的最高频率值 public static final int CMD_SET_VCORE = 10; // 设置图形模式,0/2:默认模式,1:低功耗模式,3:高性能模式 public static final int CMD_SET_SCREEN_OFF_STATE = 11; // 设置锁屏时状态,0:锁屏无效,1:锁屏有效,2:锁屏暂停,开屏恢复 public static final int CMD_SET_CPUFREQ_HISPEED_FREQ = 12; // 设置CPU高速频率 public static final int CMD_SET_CPUFREQ_MIN_SAMPLE_TIME = 13; // 设置采样CPU频率值的最小间隔时间 public static final int CMD_SET_CPUFREQ_ABOVE_HISPEED_DELAY = 14; // 设置CPU超高速频率延迟时间 public static final int CMD_SET_CLUSTER_CPU_CORE_MIN = 15; // 设置CPU簇的最少核数 public static final int CMD_SET_CLUSTER_CPU_CORE_MAX = 16; // 设置CPU簇的最多核数 public static final int CMD_SET_CLUSTER_CPU_FREQ_MIN = 17; // 设置CPU簇的最低频率值 public static final int CMD_SET_CLUSTER_CPU_FREQ_MAX = 18; // 设置CPU簇的最高频率值 public static final int CMD_SET_ROOT_CLUSTER = 19; // 设置root簇 public static final int CMD_SET_CPU_UP_THRESHOLD = 20; // 设置CPU温度的最高阈值 public static final int CMD_SET_CPU_DOWN_THRESHOLD = 21; // 设置CPU温度的最低阈值 public static final int CMD_SET_PERF_INDEX = 22; // 设置性能优化的索引值 public static final int CMD_SET_NORMALIZED_PERF_INDEX = 23; // 设置普通性能优化的索引值 // 其他方法的声明 . . . public void notifyAppState(String packName, String className, int state, int pid); // 其他方法的声明 . . . }
再看PerfServiceWrapper类的实现:
[java]
view plain
copy
print?
public class PerfServiceWrapper implements IPerfServiceWrapper {
private static final String TAG = "PerfServiceWrapper";
private IPerfService sService = null;
private Context mContext;
private int inited = 0;
private int setTid = 0;
private long mPreviousTime = 0;
private static final int RENDER_THREAD_UPDATE_DURATION = 400;
public static native int nativeGetPid();
public static native int nativeGetTid();
private void init() {
if (inited == 0) {
IBinder b = ServiceManager.checkService(Context.MTK_PERF_SERVICE);
if (b != null) {
// 获取服务
sService = IPerfService.Stub.asInterface(b);
if (sService != null)
inited = 1;
else
log("ERR: getService() sService is still null..");
}
}
}
public PerfServiceWrapper(Context context) {
mContext = context;
init();
}
// 其他接口中声明方法的实现
. . .
public void notifyAppState(String packName, String className, int state, int pid) {
//log("boostEnable");
try {
init();
if (sService != null)
// 调用sService的notifyAppState方法
sService.notifyAppState(packName, className, state, pid);
} catch (RemoteException e) {
loge("ERR: RemoteException in notifyAppState:" + e);
}
}
// 其他接口中声明方法的实现
. . .
private void log(String info) {
Log.d("@M_" + TAG, "[PerfServiceWrapper] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfServiceWrapper] ERR: " + info + " ");
}
}
public class PerfServiceWrapper implements IPerfServiceWrapper { private static final String TAG = "PerfServiceWrapper"; private IPerfService sService = null; private Context mContext; private int inited = 0; private int setTid = 0; private long mPreviousTime = 0; private static final int RENDER_THREAD_UPDATE_DURATION = 400; public static native int nativeGetPid(); public static native int nativeGetTid(); private void init() { if (inited == 0) { IBinder b = ServiceManager.checkService(Context.MTK_PERF_SERVICE); if (b != null) { // 获取服务 sService = IPerfService.Stub.asInterface(b); if (sService != null) inited = 1; else log("ERR: getService() sService is still null.."); } } } public PerfServiceWrapper(Context context) { mContext = context; init(); } // 其他接口中声明方法的实现 . . . public void notifyAppState(String packName, String className, int state, int pid) { //log("boostEnable"); try { init(); if (sService != null) // 调用sService的notifyAppState方法 sService.notifyAppState(packName, className, state, pid); } catch (RemoteException e) { loge("ERR: RemoteException in notifyAppState:" + e); } } // 其他接口中声明方法的实现 . . . private void log(String info) { Log.d("@M_" + TAG, "[PerfServiceWrapper] " + info + " "); } private void loge(String info) { Log.e("@M_" + TAG, "[PerfServiceWrapper] ERR: " + info + " "); } }
该类中的方法都是通过Binder进程间通信方式调用PerfServiceImpl中的方法实现的。由下面分析知,最后调用的是PerfServiceManager中的方法。
下面看PerfServiceManager的实例化过程,该类实现了IPerfServiceManager接口:
[java]
view plain
copy
print?
public class PerfServiceManager implements IPerfServiceManager {
private static final String TAG = "PerfServiceManager";
private HandlerThread mHandlerThread;
private PerfServiceThreadHandler mHandler;
private Context mContext;
final List<Integer> mTimeList;
private boolean bDuringTouch;
private boolean bRenderAwareValid; // render aware is only valid within 3 sec.渲染只在3秒内有效
private static final int RENDER_AWARE_DURATION_MS = 3000;
private static final int UI_UPDATE_DURATION_MS = 500;
private static final int RENDER_BIT = 0x800000;
private static final float HEAP_UTILIZATION_DURING_FRAME_UPDATE = 0.5f; // 刷新帧时的堆栈利用率
private int mDisplayType;
private VMRuntime mRuntime;
private float mDefaultUtilization;
// 声明的一些native方法,追踪到native层后,MTK只提供了一个so库,具体实现是看不到的。。。
. . .
public static native int nativePerfNotifyAppState(String packName, String className,
int state, int pid);
// 声明的一些native方法
. . .
public class PerfServiceAppState {
private String mPackName;
private String mClassName;
private int mState;
private int mPid;
PerfServiceAppState(String packName, String className, int state, int pid) {
mPackName = packName;
mClassName = className;
mState = state;
mPid = pid;
}
}
//static
//{
// Log.w(TAG, "load libperfservice_jni.so");
// System.loadLibrary("perfservice_jni");
//}
public PerfServiceManager(Context context) {
super();
mContext = context;
// 创建并开启名为PerfServiceManager的线程
mHandlerThread = new HandlerThread("PerfServiceManager", Process.THREAD_PRIORITY_FOREGROUND);
mHandlerThread.start();
Looper looper = mHandlerThread.getLooper();
if (looper != null) {
// 初始化Handler
mHandler = new PerfServiceThreadHandler(looper);
}
mTimeList = new ArrayList<Integer>();
bDuringTouch = false;
bRenderAwareValid = false;
// 默认显示类型:非游戏模式
mDisplayType = IPerfServiceWrapper.DISPLAY_TYPE_OTHERS;
mRuntime = VMRuntime.getRuntime();
mDefaultUtilization = mRuntime.getTargetHeapUtilization();
log("Created and started PerfService thread");
}
// 接口中其他方法的实现
. . .
public void notifyAppState(String packName, String className, int state, int pid) {
//log("notifyAppState");
Message msg = mHandler.obtainMessage();
msg.what = PerfServiceThreadHandler.MESSAGE_NOTIFY_APP_STATE;
msg.obj = new PerfServiceAppState(packName, className, state, pid);
msg.sendToTarget();
}
. . .
private class PerfServiceThreadHandler extends Handler {
. . .
private static final int MESSAGE_NOTIFY_APP_STATE = 4;
. . .
public PerfServiceThreadHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
try {
switch (msg.what) {
. . .
case MESSAGE_NOTIFY_APP_STATE:
{
PerfServiceAppState passedObject = (PerfServiceAppState) msg.obj;
//log("MESSAGE_NOTIFY_APP_STATE");
// 通过调用native方法实现
nativePerfNotifyAppState(passedObject.mPackName, passedObject.mClassName,
passedObject.mState, passedObject.mPid);
passedObject = null;
msg.obj = null;
break;
}
. . .
default:
{
. . .
}
}
} catch (NullPointerException e) {
loge("Exception in PerfServiceThreadHandler.handleMessage: " + e);
}
}
. . .
}
private void log(String info) {
Log.d("@M_" + TAG, "[PerfService] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");
}
}
public class PerfServiceManager implements IPerfServiceManager { private static final String TAG = "PerfServiceManager"; private HandlerThread mHandlerThread; private PerfServiceThreadHandler mHandler; private Context mContext; final List<Integer> mTimeList; private boolean bDuringTouch; private boolean bRenderAwareValid; // render aware is only valid within 3 sec.渲染只在3秒内有效 private static final int RENDER_AWARE_DURATION_MS = 3000; private static final int UI_UPDATE_DURATION_MS = 500; private static final int RENDER_BIT = 0x800000; private static final float HEAP_UTILIZATION_DURING_FRAME_UPDATE = 0.5f; // 刷新帧时的堆栈利用率 private int mDisplayType; private VMRuntime mRuntime; private float mDefaultUtilization; // 声明的一些native方法,追踪到native层后,MTK只提供了一个so库,具体实现是看不到的。。。 . . . public static native int nativePerfNotifyAppState(String packName, String className, int state, int pid); // 声明的一些native方法 . . . public class PerfServiceAppState { private String mPackName; private String mClassName; private int mState; private int mPid; PerfServiceAppState(String packName, String className, int state, int pid) { mPackName = packName; mClassName = className; mState = state; mPid = pid; } } //static //{ // Log.w(TAG, "load libperfservice_jni.so"); // System.loadLibrary("perfservice_jni"); //} public PerfServiceManager(Context context) { super(); mContext = context; // 创建并开启名为PerfServiceManager的线程 mHandlerThread = new HandlerThread("PerfServiceManager", Process.THREAD_PRIORITY_FOREGROUND); mHandlerThread.start(); Looper looper = mHandlerThread.getLooper(); if (looper != null) { // 初始化Handler mHandler = new PerfServiceThreadHandler(looper); } mTimeList = new ArrayList<Integer>(); bDuringTouch = false; bRenderAwareValid = false; // 默认显示类型:非游戏模式 mDisplayType = IPerfServiceWrapper.DISPLAY_TYPE_OTHERS; mRuntime = VMRuntime.getRuntime(); mDefaultUtilization = mRuntime.getTargetHeapUtilization(); log("Created and started PerfService thread"); } // 接口中其他方法的实现 . . . public void notifyAppState(String packName, String className, int state, int pid) { //log("notifyAppState"); Message msg = mHandler.obtainMessage(); msg.what = PerfServiceThreadHandler.MESSAGE_NOTIFY_APP_STATE; msg.obj = new PerfServiceAppState(packName, className, state, pid); msg.sendToTarget(); } . . . private class PerfServiceThreadHandler extends Handler { . . . private static final int MESSAGE_NOTIFY_APP_STATE = 4; . . . public PerfServiceThreadHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { try { switch (msg.what) { . . . case MESSAGE_NOTIFY_APP_STATE: { PerfServiceAppState passedObject = (PerfServiceAppState) msg.obj; //log("MESSAGE_NOTIFY_APP_STATE"); // 通过调用native方法实现 nativePerfNotifyAppState(passedObject.mPackName, passedObject.mClassName, passedObject.mState, passedObject.mPid); passedObject = null; msg.obj = null; break; } . . . default: { . . . } } } catch (NullPointerException e) { loge("Exception in PerfServiceThreadHandler.handleMessage: " + e); } } . . . } private void log(String info) { Log.d("@M_" + TAG, "[PerfService] " + info + " "); } private void loge(String info) { Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " "); } }
再看PerfServiceImpl类的实现:
[java]
view plain
copy
print?
public class PerfServiceImpl extends IPerfService.Stub {
private static final String TAG = "PerfService";
private IPerfServiceManager perfServiceMgr;
final Context mContext;
// 接收锁屏/开屏的广播
class PerfServiceBroadcastReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
log("Intent.ACTION_SCREEN_OFF");
perfServiceMgr.userDisableAll();
return;
}
else if (Intent.ACTION_SCREEN_ON.equals(action)) {
log("Intent.ACTION_SCREEN_ON");
perfServiceMgr.userRestoreAll();
return;
}
else {
log("Unexpected intent " + intent);
}
}
}
public PerfServiceImpl(Context context, IPerfServiceManager pm) {
perfServiceMgr = pm;
mContext = context;
final IntentFilter broadcastFilter = new IntentFilter();
broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
mContext.registerReceiver(new PerfServiceBroadcastReceiver(), broadcastFilter);
}
// aidl中声明的其他方法
. . .
public void notifyAppState(java.lang.String packName, java.lang.String className,
int state, int pid) {
//log("notifyAppState");
// 调用PerfServiceManager中的方法,最后调用的是native方法
perfServiceMgr.notifyAppState(packName, className, state, pid);
}
. . .
private void log(String info) {
Log.d("@M_" + TAG, "[PerfService] " + info + " ");
}
private void loge(String info) {
Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");
}
}
public class PerfServiceImpl extends IPerfService.Stub { private static final String TAG = "PerfService"; private IPerfServiceManager perfServiceMgr; final Context mContext; // 接收锁屏/开屏的广播 class PerfServiceBroadcastReceiver extends android.content.BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (Intent.ACTION_SCREEN_OFF.equals(action)) { log("Intent.ACTION_SCREEN_OFF"); perfServiceMgr.userDisableAll(); return; } else if (Intent.ACTION_SCREEN_ON.equals(action)) { log("Intent.ACTION_SCREEN_ON"); perfServiceMgr.userRestoreAll(); return; } else { log("Unexpected intent " + intent); } } } public PerfServiceImpl(Context context, IPerfServiceManager pm) { perfServiceMgr = pm; mContext = context; final IntentFilter broadcastFilter = new IntentFilter(); broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF); broadcastFilter.addAction(Intent.ACTION_SCREEN_ON); mContext.registerReceiver(new PerfServiceBroadcastReceiver(), broadcastFilter); } // aidl中声明的其他方法 . . . public void notifyAppState(java.lang.String packName, java.lang.String className, int state, int pid) { //log("notifyAppState"); // 调用PerfServiceManager中的方法,最后调用的是native方法 perfServiceMgr.notifyAppState(packName, className, state, pid); } . . . private void log(String info) { Log.d("@M_" + TAG, "[PerfService] " + info + " "); } private void loge(String info) { Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " "); } }
另外:
在vendor\mediatek\proprietary\hardware\perfservice\mt**\app_list目录下的perfservapplist.txt文件中可以自定义应用白名单,默认值为:
CMD_SET_CPU_CORE_MIN com.imangi.templerun2 3
意思是用户使用“神庙逃亡”应用时,CPU最少核心数为3
在vendor\mediatek\proprietary\hardware\perfservice\mt**\scn_tbl目录下的perfservscntbl.txt文件中可以自定义场景,默认场景有:
CMD_SET_CPU_CORE_MIN, SCN_APP_TOUCH, 3
CMD_SET_CPU_FREQ_MIN, SCN_APP_TOUCH, 1014000
CMD_SET_CPU_CORE_MIN, SCN_SW_FRAME_UPDATE, 3
CMD_SET_CPU_FREQ_MIN, SCN_SW_FRAME_UPDATE, 1014000
CMD_SET_CPU_UP_THRESHOLD, SCN_SW_FRAME_UPDATE, 80
CMD_SET_CPU_DOWN_THRESHOLD, SCN_SW_FRAME_UPDATE, 65
CMD_SET_CPU_FREQ_BIG_LITTLE_MIN, SCN_APP_SWITCH, 1950000, 1144000
CMD_SET_CPU_CORE_BIG_LITTLE_MIN, SCN_APP_SWITCH, 4, 0
CMD_SET_VCORE, SCN_APP_SWITCH, 3
CMD_SET_CPU_FREQ_BIG_LITTLE_MIN, SCN_APP_ROTATE, 1950000, 1144000
CMD_SET_VCORE, SCN_APP_ROTATE, 3
CMD_SET_VCORE, SCN_GAMING, 1
相关文章推荐
- MTK平台CPU/GPU动态调频的实现之PerfService的源码分析
- OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机动态迁移源码分析
- OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机动态迁移源码分析
- OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机动态迁移源码分析
- OpenStack基于Libvirt的虚拟化平台调度实现----Nova虚拟机动态迁移源码分析
- 应用框架的设计与实现——.NET平台(6 源码分析)
- NSIS脚本源码分析: 如何判断CPU平台类型
- mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)
- MTK平台配置多核cpu oneline和gpu freq提升系统性能
- 【Cocos2d-x 3.2源码分析】(一)类FileUtils -- 实现把资源放在Resources文件目录下达到多平台的引用
- Java动态代理详解,以及底层JDK源码实现分析(基于Java8)。
- Cloud Foundry Service Node源码分析及实现
- cocos2d-x3.2源码分析之 ---- 类FileUtils实现把资源放在Resources文件目录下达到多平台的引用
- cocos2d-x3.2源码分析(一)类FileUtils--实现把资源放在Resources文件目录下达到多平台的引用
- 应用框架的设计与实现——.NET平台(7.事件通知服务.源码分析)
- JAVA 动态代理(proxy)的实现和源码分析
- MTK平台动态加载功能实现
- 在MTK芯片上如何控制CPU的核数和频率-MTK PerfService
- Android Apk资源加载机制源码分析以及资源动态加载实现系列文章
- JNI源码分析(并实现JNI动态注册)