当前位置:   article > 正文

Android CPU频率设置(MTK平台)_mtk cpu调频方法

mtk cpu调频方法

1.CPU 频率设置

主要设置CPU的最大和最小频率

  • mPerfServiceWrapper.userReg 注册
  • mPerfServiceWrapper.userRegScnConfig 参数设定
  • mPerfServiceWrapper.userEnable 激活设置
  • mPerfServiceWrapper.userDisable 反激活设置
  • mPerfServiceWrapper.userUnreg 反注册

示例代码

    /**
     * CPU mode freq settings
     *
     * @param minCpuNumber
     * @param maxCpuFreq
     * @param mode
     * @param freqMin
     * @param freqMax
     * @param coreMin
     * @param coreMax
     */
    public void setCpu(int minCpuNumber, int maxCpuFreq, int mode, int freqMin, int freqMax, int coreMin, int coreMax, boolean screenOffEnable) {
        userDisableIfNeed();
        if (null != mPerfServiceWrapper) {
            mCurPerfHandle = mPerfServiceWrapper.userReg(minCpuNumber, maxCpuFreq);

            if (PERFHANDLE_ERROR_CODE != mCurPerfHandle) {
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_VCORE, mode, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_CORE_MIN, coreMin, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_CORE_MAX, coreMax, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_FREQ_MIN, freqMin, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_FREQ_MAX, freqMax, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                if (screenOffEnable) {
                    mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_SCREEN_OFF_STATE, mPerfServiceWrapper.SCREEN_OFF_ENABLE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                }

                mPerfServiceWrapper.userEnable(mCurPerfHandle);

                Log.d(TAG, "setCpu minCpuNumber = " + minCpuNumber + ", maxCpuFreq = " + maxCpuFreq + ", mode = " + mode + ", freqMin = " + freqMin + ", freqMax = " + freqMax + ", coreMin = " + coreMin + ", coreMax = " + coreMax + ", mCurPerfHandle = " + mCurPerfHandle);
            }
        }
    }

    /**
     * 退出注册
     */
    public void userUnreg() {
        if (mCurPerfHandle != PERFHANDLE_ERROR_CODE) {
            mPerfServiceWrapper.userDisable(mCurPerfHandle);
            mPerfServiceWrapper.userUnreg(mCurPerfHandle);
            Log.d(TAG, "userUnreg  mCurPerfHandle = " + mCurPerfHandle);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

2.运行效果

运行效果

3.示例代码下载

https://github.com/sufadi/AndroidCpuTools

4.涉及源码

CpuSettingsUtils.java

package com.fadisu.cpurun.util;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.mediatek.perfservice.PerfServiceWrapper;

public class CpuSettingsUtils {

    private static String TAG = CpuSettingsUtils.class.getSimpleName();

    public static final int PERFHANDLE_ERROR_CODE = -1;
    public static final int PARAM_DEFAULT_VALUE = -1;

    public static final int CPU_NUMBER = CpuUtils.getNumCpuCores();
    public static final int MAX_CPU_FREQ = (int) CpuUtils.getCpuMaxFreq();
    public static final int MIN_CPU_FREQ = (int) CpuUtils.getCpuMinFreq();

    private int mCurPerfHandle;

    private Context mContext;
    private static CpuSettingsUtils mInstance;
    private PerfServiceWrapper mPerfServiceWrapper;

    public static CpuSettingsUtils getInstance(Context mContext) {
        if (null == mInstance) {
            synchronized (CpuSettingsUtils.class) {
                if (null == mInstance) {
                    mInstance = new CpuSettingsUtils(mContext);
                }
            }
        }

        return mInstance;
    }

    public CpuSettingsUtils(Context mContext) {
        this.mContext = mContext;
        mCurPerfHandle = PERFHANDLE_ERROR_CODE;
        mPerfServiceWrapper = new PerfServiceWrapper(mContext);

    }

    /**
     * CPU 核数和频率全开,达到瞬间性能最优
     */
    public static void perfBoost(Context mContext) {
        final PerfServiceWrapper mPerfServiceWrapper = new PerfServiceWrapper(mContext);
        if (null != mPerfServiceWrapper) {
            final int mPerfHandle = mPerfServiceWrapper.userReg(CPU_NUMBER, MAX_CPU_FREQ);

            if (PERFHANDLE_ERROR_CODE != mPerfHandle) {
                mPerfServiceWrapper.userEnableTimeoutMs(mPerfHandle, 500);
                Log.d(TAG, "userEnableTimeoutMs = " + mPerfHandle);

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPerfServiceWrapper.userUnreg(mPerfHandle);
                        Log.d(TAG, "userUnreg = " + mPerfHandle);
                    }
                }, 600);
            }
        }
    }

    /**
     * 设置图形模式,参数1个:DRAM模式:0-默认模式,1-低功耗模式,2-还是默认模式,3-高性能模式
     *
     * @param mContext
     * @param minCpuNumber
     * @param maxCpuFreq
     */
    public static final int VCORE_DEFAULT = 0;
    public static final int VCORE_POWERSAVE = 1;
    public static final int VCORE_DEFAULT_2 = 2;
    public static final int VCORE_PERF = 3;

    public void setCpuVcoreMode(int minCpuNumber, int maxCpuFreq, int mode) {
        userDisableIfNeed();

        if (null != mPerfServiceWrapper) {
            mCurPerfHandle = mPerfServiceWrapper.userReg(minCpuNumber, maxCpuFreq);

            if (PERFHANDLE_ERROR_CODE != mCurPerfHandle) {
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_VCORE, mode, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                // screenOffEnable();
                mPerfServiceWrapper.userEnable(mCurPerfHandle);

                Log.d(TAG, "setCpuVcoreMode mode = " + mode + ", minCpuNumber = " + minCpuNumber + ", maxCpuFreq = " + maxCpuFreq + ", mCurPerfHandle = " + mCurPerfHandle);
            }
        }
    }

    /**
     * CPU 频率设置
     * 效果:维持最高频率为设定值,但是CPU场景调频不受控制,场景比如切换应用时,默认是开核升频率,暗屏失效
     *
     * @param minCpuNumber
     * @param maxCpuFreq
     * @param freq
     */
    public void setCpuFreq(int minCpuNumber, int maxCpuFreq, int freq) {
        userDisableIfNeed();

        if (null != mPerfServiceWrapper) {
            mCurPerfHandle = mPerfServiceWrapper.userReg(minCpuNumber, maxCpuFreq);

            if (PERFHANDLE_ERROR_CODE != mCurPerfHandle) {
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_FREQ_MAX, freq, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                // screenOffEnable();
                mPerfServiceWrapper.userEnable(mCurPerfHandle);

                Log.d(TAG, "setCpuFreq freq = " + freq + ", minCpuNumber = " + minCpuNumber + ", maxCpuFreq = " + maxCpuFreq + ", mCurPerfHandle = " + mCurPerfHandle);
            }
        }
    }

    /**
     * CPU mode freq settings
     *
     * @param minCpuNumber
     * @param maxCpuFreq
     * @param mode
     * @param freqMin
     * @param freqMax
     * @param coreMin
     * @param coreMax
     */
    public void setCpu(int minCpuNumber, int maxCpuFreq, int mode, int freqMin, int freqMax, int coreMin, int coreMax, boolean screenOffEnable) {
        userDisableIfNeed();
        if (null != mPerfServiceWrapper) {
            mCurPerfHandle = mPerfServiceWrapper.userReg(minCpuNumber, maxCpuFreq);

            if (PERFHANDLE_ERROR_CODE != mCurPerfHandle) {
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_VCORE, mode, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_CORE_MIN, coreMin, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_CORE_MAX, coreMax, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_FREQ_MIN, freqMin, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_CPU_FREQ_MAX, freqMax, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);

                if (screenOffEnable) {
                    mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_SCREEN_OFF_STATE, mPerfServiceWrapper.SCREEN_OFF_ENABLE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
                }

                mPerfServiceWrapper.userEnable(mCurPerfHandle);

                Log.d(TAG, "setCpu minCpuNumber = " + minCpuNumber + ", maxCpuFreq = " + maxCpuFreq + ", mode = " + mode + ", freqMin = " + freqMin + ", freqMax = " + freqMax + ", coreMin = " + coreMin + ", coreMax = " + coreMax + ", mCurPerfHandle = " + mCurPerfHandle);
            }
        }
    }

    /**
     * 默认灭屏持续生效
     */
    public void screenOffEnable() {
        mPerfServiceWrapper.userRegScnConfig(mCurPerfHandle, mPerfServiceWrapper.CMD_SET_SCREEN_OFF_STATE, mPerfServiceWrapper.SCREEN_OFF_ENABLE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE, PARAM_DEFAULT_VALUE);
    }

    /**
     * 取消之前的设置
     */
    public void userDisableIfNeed() {
        if (mCurPerfHandle != PERFHANDLE_ERROR_CODE) {
            mPerfServiceWrapper.userDisable(mCurPerfHandle);
            Log.d(TAG, "userDisableIfNeed  mCurPerfHandle = " + mCurPerfHandle);
        }
    }

    /**
     * 退出注册
     */
    public void userUnreg() {
        if (mCurPerfHandle != PERFHANDLE_ERROR_CODE) {
            mPerfServiceWrapper.userDisable(mCurPerfHandle);
            mPerfServiceWrapper.userUnreg(mCurPerfHandle);
            Log.d(TAG, "userUnreg  mCurPerfHandle = " + mCurPerfHandle);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185

CpuSettingsFragment.java

    private void startCpuSettings() {
        String vcoreStr = (String) sp_cpu_vcore.getSelectedItem();
        int vcoreMode = 0;
        if (mContext.getString(R.string.settings_cpu_vcore_default).equals(vcoreStr)) {
            vcoreMode = CpuSettingsUtils.VCORE_DEFAULT;
        } else if (mContext.getString(R.string.settings_cpu_vcore_powersave).equals(vcoreStr)) {
            vcoreMode = CpuSettingsUtils.VCORE_POWERSAVE;
        } else if (mContext.getString(R.string.settings_cpu_vcore_perf).equals(vcoreStr)) {
            vcoreMode = CpuSettingsUtils.VCORE_PERF;
        }

        int coreMin = (int) sp_cpu_core_min.getSelectedItem();
        int coreMax = (int) sp_cpu_core_max.getSelectedItem();
        int freqMin = (int) sp_cpu_freq_min.getSelectedItem();
        int freqMax = (int) sp_cpu_freq_max.getSelectedItem();

        String screenOffStr = (String) sp_screenof_enable.getSelectedItem();
        boolean isScreenOffEnable = false;
        if (mContext.getString(R.string.settings_screenoff_enable_yes).equals(screenOffStr)) {
            isScreenOffEnable = true;
        } else {
            isScreenOffEnable = false;
        }

        mCpuSettingsUtils.setCpu(coreMax, freqMax, vcoreMode, freqMin, freqMax, coreMin, coreMax, isScreenOffEnable);
    }

    private void stopCpuSettings() {
        mCpuSettingsUtils.userUnreg();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/773274
推荐阅读
相关标签
  

闽ICP备14008679号