MTK平台CPU/GPU动态调频的实现之PerfService的源码分析

转自:http://blog.csdn.net/zhangyongfeiyong/article/details/52946781

Zygote进程启动后会启动System进程,在System进程启动过程中会启动系统中的关键服务,如AMS、PMS以及这里要分析的PerfService。先看下流程图:


SystemServer启动PerfService服务是通过实例化PerfServiceImpl的对象perfService,并把该服务的Binder对象添加到ServiceManager中。

先看SystemServer类中的startOtherServices方法:

  1. private void startOtherServices() {  
  2.     . . .  
  3.     PerfServiceStateNotifier perfServiceNotifier = null;  
  4.     IPerfServiceManager perfServiceMgr = null;  
  5.     . . .  
  6.     if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {  
  7.         . . .  
  8.         /// M: add for PerfService feature @{  
  9.         if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {  
  10.             try {  
  11.                 Slog.i(TAG, "PerfService state notifier");  
  12.                 // 实例化PerfService的状态通知者  
  13.                 perfServiceNotifier = new PerfServiceStateNotifier();  
  14.                 // 把通知者注册到AMS的观察者中,有状态变化时会通知所有注册过的通知者  
  15.                 mActivityManagerService.registerActivityStateNotifier(perfServiceNotifier);  
  16.             } catch (Throwable e) {  
  17.                 Slog.e(TAG, "FAIL starting PerfServiceStateNotifier", e);  
  18.             }  
  19.   
  20.             // Create PerfService manager thread and add service  
  21.             try {  
  22.                 // 实例化PerfServiceManager线程  
  23.                 perfServiceMgr = new PerfServiceManager(context);  
  24.   
  25.                 IPerfService perfService = null;  
  26.                 // 实例化服务  
  27.                 perfService = new PerfServiceImpl(context, perfServiceMgr);  
  28.   
  29.                 Slog.d("perfservice""perfService=" + perfService);  
  30.                 if (perfService != null) {  
  31.                     // 把服务添加到ServiceManager中  
  32.                     ServiceManager.addService(Context.MTK_PERF_SERVICE, perfService.asBinder());  
  33.                 }  
  34.   
  35.             } catch (Throwable e) {  
  36.                 Slog.e(TAG, "perfservice Failure starting PerfService", e);  
  37.             }  
  38.         }  
  39.         /// @}  
  40.         . . .  
  41.     }  
  42.     . . .  
  43.   
  44.     /// M: add for hdmi feature  
  45.     final IPerfServiceManager perfServiceF = perfServiceMgr;  
  46.   
  47.     // We now tell the activity manager it is okay to run third party  
  48.     // code.  It will call back into us once it has gotten to the state  
  49.     // where third party code can really run (but before it has actually  
  50.     // started launching the initial applications), for us to complete our  
  51.     // initialization.  
  52.     mActivityManagerService.systemReady(new Runnable() {  
  53.         @Override  
  54.         public void run() {  
  55.             Slog.i(TAG, "Making services ready");  
  56.             . . .  
  57.             /// M: add for PerfService feature @{  
  58.             if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {  
  59.                 // Notify PerfService manager of system ready  
  60.                 try {  
  61.                     Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePerfServiceReady");  
  62.                     // 系统启动后回调IPerfServiceManager的systemReady方法  
  63.                     if (perfServiceF != null) perfServiceF.systemReady();  
  64.                     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);  
  65.                 } catch (Throwable e) {  
  66.                     reportWtf("making PerfServiceManager ready", e);  
  67.                 }  
  68.             }  
  69.             /// @}  
  70.             . . .  
  71.         }  
  72.     });  
  73.     . . .  
  74. }  
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:

  1. public interface IActivityStateNotifier {  
  2.   
  3.     public enum ActivityState {  
  4.         Paused,  
  5.         Resumed,  
  6.         Destroyed,  
  7.         Stopped  
  8.     }  
  9.   
  10.     /** 
  11.      * Notify activity state change. 
  12.      * 
  13.      * @param packageName The target package name. 
  14.      * @param pid The process id package belongs to. 
  15.      * @param className The class name of the package. 
  16.      * @param actState Current lifecycle state of the package. 
  17.      */  
  18.     public void notifyActivityState(String packageName, int pid,  
  19.             String className, ActivityState actState);  
  20.   
  21.     /** 
  22.      * Notify the process of activity has died. 
  23.      * 
  24.      * @param pid The process id has died. 
  25.      * @param packageList The whole packages runs on the process. 
  26.      */  
  27.     public void notifyAppDied(int pid, HashSet<String> packageList);  
  28. }  
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类的实现:

  1. // 观察者模式的使用  
  2. public final class PerfServiceStateNotifier implements IActivityStateNotifier {  
  3.     static final String TAG = "PerfServiceStateNotifier";  
  4.   
  5.     IPerfServiceWrapper mPerfService;  
  6.   
  7.     public PerfServiceStateNotifier() {  
  8.         // 实例化PerfServiceWrapper  
  9.         mPerfService = new PerfServiceWrapper(null);  
  10.     }  
  11.     /** 
  12.      * Notify activity state change 
  13.      * Activity状态变化时回调该方法 
  14.      */  
  15.     public void notifyActivityState(String packageName, int pid,  
  16.                                     String className, IActivityStateNotifier.ActivityState actState) {  
  17.         int state;  
  18.         //Slog.i(TAG,"[notifyActivityState] "+ packageName+ ", "+ className+ ", "+ actState);  
  19.   
  20.         switch(actState) {  
  21.             case Paused:  
  22.                 state = IPerfServiceWrapper.STATE_PAUSED;  
  23.                 break;  
  24.             case Resumed:  
  25.                 state = IPerfServiceWrapper.STATE_RESUMED;  
  26.                 break;  
  27.             case Destroyed:  
  28.                 state = IPerfServiceWrapper.STATE_DESTROYED;  
  29.                 break;  
  30.             case Stopped:  
  31.                 state = IPerfServiceWrapper.STATE_STOPPED;  
  32.                 break;  
  33.             default:  
  34.                 return;  
  35.         }  
  36.         // 调用mPerfService的状态变化方法  
  37.         mPerfService.notifyAppState(packageName, className, state, pid);  
  38.     }  
  39.   
  40.     /** 
  41.      * Notify the process of activity has died 
  42.      * Activity销毁时回调该方法 
  43.      */  
  44.     public void notifyAppDied(int pid, HashSet<String> packageList)  
  45.     {  
  46.         Iterator i = packageList.iterator();  
  47.         while (i.hasNext()) {  
  48.             String packageName = (String) i.next();  
  49.             // 调用mPerfService的状态变化方法  
  50.             mPerfService.notifyAppState(packageName, null, IPerfServiceWrapper.STATE_DEAD, pid);  
  51.         }  
  52.     }  
  53.   
  54. }  
// 观察者模式的使用
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:

  1. public interface IPerfServiceWrapper {  
  2.   
  3.     // 已经实现的场景  
  4.     public static final int SCN_NONE       = 0;  
  5.     public static final int SCN_APP_SWITCH = 1/* apply for both launch/exit */ // 切换应用  
  6.     public static final int SCN_APP_ROTATE = 2// 旋转应用  
  7.     public static final int SCN_APP_TOUCH       = 3// 触摸应用,即点击屏幕  
  8.     public static final int SCN_DONT_USE1       = 4// 空闲???  
  9.     public static final int SCN_SW_FRAME_UPDATE = 5// 刷新帧率  
  10.     public static final int SCN_APP_LAUNCH      = 6// 应用启动  同SCN_APP_SWITCH  
  11.     public static final int SCN_GAMING          = 7// 游戏中。。。  
  12.     public static final int SCN_MAX             = 8/* should be (last scenario + 1) */  
  13.   
  14.     public static final int STATE_PAUSED    = 0;  
  15.     public static final int STATE_RESUMED   = 1;  
  16.     public static final int STATE_DESTROYED = 2;  
  17.     public static final int STATE_DEAD      = 3;  
  18.     public static final int STATE_STOPPED   = 4;  
  19.   
  20.     public static final int DISPLAY_TYPE_GAME   = 0// 游戏模式显示类型  
  21.     public static final int DISPLAY_TYPE_OTHERS = 1// 非游戏模式  
  22.   
  23.     public static final int NOTIFY_USER_TYPE_PID = 0;  
  24.     public static final int NOTIFY_USER_TYPE_FRAME_UPDATE = 1;  
  25.     public static final int NOTIFY_USER_TYPE_DISPLAY_TYPE = 2;  
  26.     public static final int NOTIFY_USER_TYPE_SCENARIO_ON  = 3;  
  27.     public static final int NOTIFY_USER_TYPE_SCENARIO_OFF = 4;  
  28.   
  29.     public static final int CMD_GET_CPU_FREQ_LEVEL_COUNT        = 0// 获取CPU频率级别总数  
  30.     public static final int CMD_GET_CPU_FREQ_LITTLE_LEVEL_COUNT = 1// 获取小核CPU频率级别总数  
  31.     public static final int CMD_GET_CPU_FREQ_BIG_LEVEL_COUNT    = 2// 获取大核CPU频率级别总数  
  32.     public static final int CMD_GET_GPU_FREQ_LEVEL_COUNT        = 3// 获取GPU频率级别总数  
  33.     public static final int CMD_GET_MEM_FREQ_LEVEL_COUNT        = 4// 获取内存频率级别总数  
  34.     public static final int CMD_GET_PERF_INDEX_MIN              = 5// 获取性能优化最小索引值  
  35.     public static final int CMD_GET_PERF_INDEX_MAX              = 6// 获取性能优化最大索引值  
  36.     public static final int CMD_GET_PERF_NORMALIZED_INDEX_MAX   = 7// 获取普通性能优化最大索引值  
  37.   
  38.     public static final int CMD_SET_CPU_CORE_MIN            = 0// 设置CPU最少核数  
  39.     public static final int CMD_SET_CPU_CORE_MAX            = 1// 设置CPU最多核数  
  40.     public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MIN = 2// 设置大核小核的最少核数  
  41.     public static final int CMD_SET_CPU_CORE_BIG_LITTLE_MAX = 3// 设置大核小核的最多核数  
  42.     public static final int CMD_SET_CPU_FREQ_MIN            = 4// 设置CPU的最低频率值  
  43.     public static final int CMD_SET_CPU_FREQ_MAX            = 5// 设置CPU的最高频率值  
  44.     public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MIN = 6// 设置大核小核的最低频率值  
  45.     public static final int CMD_SET_CPU_FREQ_BIG_LITTLE_MAX = 7// 设置大核小核的最高频率值  
  46.     public static final int CMD_SET_GPU_FREQ_MIN            = 8// 设置GPU的最低频率值  
  47.     public static final int CMD_SET_GPU_FREQ_MAX            = 9// 设置GPU的最高频率值  
  48.     public static final int CMD_SET_VCORE                   = 10// 设置图形模式,0/2:默认模式,1:低功耗模式,3:高性能模式  
  49.     public static final int CMD_SET_SCREEN_OFF_STATE        = 11// 设置锁屏时状态,0:锁屏无效,1:锁屏有效,2:锁屏暂停,开屏恢复  
  50.     public static final int CMD_SET_CPUFREQ_HISPEED_FREQ    = 12// 设置CPU高速频率  
  51.     public static final int CMD_SET_CPUFREQ_MIN_SAMPLE_TIME = 13// 设置采样CPU频率值的最小间隔时间  
  52.     public static final int CMD_SET_CPUFREQ_ABOVE_HISPEED_DELAY = 14// 设置CPU超高速频率延迟时间  
  53.     public static final int CMD_SET_CLUSTER_CPU_CORE_MIN    = 15// 设置CPU簇的最少核数  
  54.     public static final int CMD_SET_CLUSTER_CPU_CORE_MAX    = 16// 设置CPU簇的最多核数  
  55.     public static final int CMD_SET_CLUSTER_CPU_FREQ_MIN    = 17// 设置CPU簇的最低频率值  
  56.     public static final int CMD_SET_CLUSTER_CPU_FREQ_MAX    = 18// 设置CPU簇的最高频率值  
  57.     public static final int CMD_SET_ROOT_CLUSTER            = 19// 设置root簇  
  58.     public static final int CMD_SET_CPU_UP_THRESHOLD        = 20// 设置CPU温度的最高阈值  
  59.     public static final int CMD_SET_CPU_DOWN_THRESHOLD      = 21// 设置CPU温度的最低阈值  
  60.     public static final int CMD_SET_PERF_INDEX              = 22// 设置性能优化的索引值  
  61.     public static final int CMD_SET_NORMALIZED_PERF_INDEX   = 23// 设置普通性能优化的索引值  
  62.   
  63.     // 其他方法的声明  
  64.     . . .  
  65.       
  66.     public void notifyAppState(String packName, String className, int state, int pid);  
  67.       
  68.     // 其他方法的声明  
  69.     . . .  
  70. }  
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类的实现:

  1. public class PerfServiceWrapper implements IPerfServiceWrapper {  
  2.   
  3.     private static final String TAG = "PerfServiceWrapper";  
  4.   
  5.     private IPerfService sService = null;  
  6.     private Context mContext;  
  7.   
  8.     private int inited = 0;  
  9.   
  10.     private int setTid = 0;  
  11.     private long mPreviousTime = 0;  
  12.     private static final int RENDER_THREAD_UPDATE_DURATION = 400;  
  13.   
  14.     public static native int nativeGetPid();  
  15.     public static native int nativeGetTid();  
  16.   
  17.     private void init() {  
  18.         if (inited == 0) {  
  19.             IBinder b = ServiceManager.checkService(Context.MTK_PERF_SERVICE);  
  20.             if (b != null) {  
  21.                 // 获取服务  
  22.                 sService = IPerfService.Stub.asInterface(b);  
  23.                 if (sService != null)  
  24.                     inited = 1;  
  25.                 else  
  26.                     log("ERR: getService() sService is still null..");  
  27.             }  
  28.         }  
  29.     }  
  30.   
  31.     public PerfServiceWrapper(Context context) {  
  32.         mContext = context;  
  33.         init();  
  34.     }  
  35.       
  36.     // 其他接口中声明方法的实现  
  37.     . . .  
  38.   
  39.     public void notifyAppState(String packName, String className, int state, int pid) {  
  40.         //log("boostEnable");  
  41.         try {  
  42.             init();  
  43.             if (sService != null)  
  44.                 // 调用sService的notifyAppState方法  
  45.                 sService.notifyAppState(packName, className, state, pid);  
  46.         } catch (RemoteException e) {  
  47.             loge("ERR: RemoteException in notifyAppState:" + e);  
  48.         }  
  49.     }  
  50.   
  51.     // 其他接口中声明方法的实现  
  52.     . . .  
  53.       
  54.     private void log(String info) {  
  55.         Log.d("@M_" + TAG, "[PerfServiceWrapper] " + info + " ");  
  56.     }  
  57.   
  58.     private void loge(String info) {  
  59.         Log.e("@M_" + TAG, "[PerfServiceWrapper] ERR: " + info + " ");  
  60.     }  
  61. }  
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接口:

  1. public class PerfServiceManager implements IPerfServiceManager {  
  2.     private static final String TAG = "PerfServiceManager";  
  3.     private HandlerThread mHandlerThread;  
  4.     private PerfServiceThreadHandler mHandler;  
  5.     private Context mContext;  
  6.     final List<Integer> mTimeList;  
  7.   
  8.   
  9.     private boolean bDuringTouch;  
  10.     private boolean bRenderAwareValid; // render aware is only valid within 3 sec.渲染只在3秒内有效  
  11.     private static final int RENDER_AWARE_DURATION_MS = 3000;  
  12.     private static final int UI_UPDATE_DURATION_MS = 500;  
  13.     private static final int RENDER_BIT = 0x800000;  
  14.     private static final float HEAP_UTILIZATION_DURING_FRAME_UPDATE = 0.5f; // 刷新帧时的堆栈利用率  
  15.   
  16.     private int mDisplayType;  
  17.     private VMRuntime mRuntime;  
  18.     private float mDefaultUtilization;  
  19.   
  20.     // 声明的一些native方法,追踪到native层后,MTK只提供了一个so库,具体实现是看不到的。。。  
  21.     . . .  
  22.     public static native int nativePerfNotifyAppState(String packName, String className,  
  23.                                                       int state, int pid);  
  24.     // 声明的一些native方法  
  25.     . . .  
  26.   
  27.     public class PerfServiceAppState {  
  28.         private String mPackName;  
  29.         private String mClassName;  
  30.         private int mState;  
  31.         private int mPid;  
  32.   
  33.         PerfServiceAppState(String packName, String className, int state, int pid) {  
  34.             mPackName = packName;  
  35.             mClassName = className;  
  36.             mState = state;  
  37.             mPid = pid;  
  38.         }  
  39.     }  
  40.   
  41.     //static  
  42.     //{  
  43.     //    Log.w(TAG, "load libperfservice_jni.so");  
  44.     //    System.loadLibrary("perfservice_jni");  
  45.     //}  
  46.   
  47.     public PerfServiceManager(Context context) {  
  48.         super();  
  49.         mContext = context;  
  50.         // 创建并开启名为PerfServiceManager的线程  
  51.         mHandlerThread = new HandlerThread("PerfServiceManager", Process.THREAD_PRIORITY_FOREGROUND);  
  52.         mHandlerThread.start();  
  53.         Looper looper = mHandlerThread.getLooper();  
  54.         if (looper != null) {  
  55.             // 初始化Handler  
  56.             mHandler = new PerfServiceThreadHandler(looper);  
  57.         }  
  58.         mTimeList = new ArrayList<Integer>();  
  59.         bDuringTouch = false;  
  60.         bRenderAwareValid = false;  
  61.         // 默认显示类型:非游戏模式  
  62.         mDisplayType = IPerfServiceWrapper.DISPLAY_TYPE_OTHERS;  
  63.         mRuntime = VMRuntime.getRuntime();  
  64.         mDefaultUtilization = mRuntime.getTargetHeapUtilization();  
  65.         log("Created and started PerfService thread");  
  66.     }  
  67.   
  68.     // 接口中其他方法的实现  
  69.     . . .  
  70.   
  71.     public void notifyAppState(String packName, String className, int state, int pid) {  
  72.         //log("notifyAppState");  
  73.         Message msg = mHandler.obtainMessage();  
  74.         msg.what = PerfServiceThreadHandler.MESSAGE_NOTIFY_APP_STATE;  
  75.         msg.obj = new PerfServiceAppState(packName, className, state, pid);  
  76.         msg.sendToTarget();  
  77.     }  
  78.     . . .  
  79.   
  80.     private class PerfServiceThreadHandler extends Handler {  
  81.         . . .  
  82.         private static final int MESSAGE_NOTIFY_APP_STATE        = 4;  
  83.         . . .  
  84.   
  85.         public PerfServiceThreadHandler(Looper looper) {  
  86.             super(looper);  
  87.         }  
  88.   
  89.         @Override  
  90.         public void handleMessage(Message msg) {  
  91.             try {  
  92.                 switch (msg.what) {  
  93.                     . . .  
  94.                     case MESSAGE_NOTIFY_APP_STATE:  
  95.                     {  
  96.                         PerfServiceAppState passedObject = (PerfServiceAppState) msg.obj;  
  97.                         //log("MESSAGE_NOTIFY_APP_STATE");  
  98.                         // 通过调用native方法实现  
  99.                         nativePerfNotifyAppState(passedObject.mPackName, passedObject.mClassName,  
  100.                                 passedObject.mState, passedObject.mPid);  
  101.                         passedObject = null;  
  102.                         msg.obj = null;  
  103.                         break;  
  104.                     }  
  105.                     . . .  
  106.   
  107.                     default:  
  108.                     {  
  109.                         . . .  
  110.                     }  
  111.                 }  
  112.             } catch (NullPointerException e) {  
  113.                 loge("Exception in PerfServiceThreadHandler.handleMessage: " + e);  
  114.             }  
  115.         }  
  116.         . . .  
  117.   
  118.     }  
  119.   
  120.     private void log(String info) {  
  121.         Log.d("@M_" + TAG, "[PerfService] " + info + " ");  
  122.     }  
  123.   
  124.     private void loge(String info) {  
  125.         Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");  
  126.     }  
  127.   
  128. }  
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类的实现:

  1. public class PerfServiceImpl extends IPerfService.Stub {  
  2.   
  3.     private static final String TAG = "PerfService";  
  4.   
  5.     private IPerfServiceManager perfServiceMgr;  
  6.     final   Context mContext;  
  7.   
  8.     // 接收锁屏/开屏的广播  
  9.     class PerfServiceBroadcastReceiver extends android.content.BroadcastReceiver {  
  10.         @Override  
  11.         public void onReceive(Context context, Intent intent) {  
  12.             final String action = intent.getAction();  
  13.             if (Intent.ACTION_SCREEN_OFF.equals(action)) {  
  14.                 log("Intent.ACTION_SCREEN_OFF");  
  15.                 perfServiceMgr.userDisableAll();  
  16.                 return;  
  17.             }  
  18.             else if (Intent.ACTION_SCREEN_ON.equals(action)) {  
  19.                 log("Intent.ACTION_SCREEN_ON");  
  20.                 perfServiceMgr.userRestoreAll();  
  21.                 return;  
  22.             }  
  23.             else {  
  24.                 log("Unexpected intent " + intent);  
  25.             }  
  26.         }  
  27.     }  
  28.   
  29.     public PerfServiceImpl(Context context, IPerfServiceManager pm) {  
  30.         perfServiceMgr = pm;  
  31.         mContext = context;  
  32.   
  33.         final IntentFilter broadcastFilter = new IntentFilter();  
  34.         broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);  
  35.         broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);  
  36.         mContext.registerReceiver(new PerfServiceBroadcastReceiver(), broadcastFilter);  
  37.   
  38.     }  
  39.   
  40.     // aidl中声明的其他方法  
  41.     . . .  
  42.   
  43.     public void notifyAppState(java.lang.String packName, java.lang.String className,  
  44.                                int state, int pid) {  
  45.         //log("notifyAppState");  
  46.         // 调用PerfServiceManager中的方法,最后调用的是native方法  
  47.         perfServiceMgr.notifyAppState(packName, className, state, pid);  
  48.     }  
  49.   
  50.     . . .  
  51.   
  52.     private void log(String info) {  
  53.         Log.d("@M_" + TAG, "[PerfService] " + info + " ");  
  54.     }  
  55.   
  56.     private void loge(String info) {  
  57.         Log.e("@M_" + TAG, "[PerfService] ERR: " + info + " ");  
  58.     }  
  59. }  
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值