最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android如何獲取當(dāng)前CPU頻率和占用率

 更新時間:2025年03月12日 16:00:54   作者:microhex  
最近在優(yōu)化 App 的性能,需要獲取當(dāng)前 CPU視頻頻率和占用率,所以本文小編就來和大家總結(jié)一下如何在Android中獲取當(dāng)前CPU頻率和占用率吧

最近在優(yōu)化 App 的性能,需要獲取當(dāng)前 CPU視頻頻率和占用率,通過查詢資料,大致思路如下:

目前沒有標(biāo)準(zhǔn)的 API 來獲取 CPU 的使用頻率,只能通過讀取指定 CPU 文件獲取當(dāng)前 CPU 頻率,在某些機(jī)器或者特定版本中,可能需要ROOT 權(quán)限或者特殊權(quán)限,因此會存在一定幾率的失敗,因此需要做好 Try…catch 動作。又因為現(xiàn)在手機(jī) CPU 的多核數(shù)目,因此我們可能需要獲取多個 CPU 頻率數(shù),并取平均值。

獲取系統(tǒng) CPU 核心數(shù):

 val cpuCoreNum = Runtime.getRuntime().availableProcessors()

獲取指定 CPU 當(dāng)前頻率:

/sys/devices/system/cpu/cpu${index}/cpufreq/scaling_cur_freq

那么核心代碼為:

private fun getAllCpuCoreFrequency() : Long {

        var frequency = 0L

        for (index in 0 until  cpuCoreNum){
            frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
        }

        BLog.d("frequency : $frequency")

        return frequency / cpuCoreNum
    }


  private fun readFile(filePath: String): Long{
        try {
            val file = RandomAccessFile(filePath, "r")
            val content = file.readLine()
            file.close()

            if (TextUtils.isEmpty(content)){
                return 0L
            }

            BLog.d("readFile content : $content")

            return content.trim().toLong()

        }catch (e : Exception){
            e.printStackTrace()

           return 0L
        }
    }

如果需要獲取 CPU 的占用率,那么就需要知道每個核心的最大頻率和最小頻率,同樣是通過文件獲取:

//max frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq

???????//min frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq

那么核心代碼為:

object CPUUtils {

    private var cpuCoreNum = 0
    private var cpuMaxFrequency = 0L
    private var cpuMinFrequency = 0L

    fun initCpuCoreNum(){
        if (cpuCoreNum <= 0 || cpuMaxFrequency <= 0L || cpuMinFrequency <= 0L){

            cpuCoreNum = Runtime.getRuntime().availableProcessors()
            initMaxAndMinFrequency()

            if (cpuCoreNum > 0 && cpuMaxFrequency > 0L && cpuMinFrequency > 0L){
               SpManager.getInstance().setCanUseCPUFrequency(true)
            }
        }

        BLog.d("cpuCoreNum : $cpuCoreNum")
    }

    private fun initMaxAndMinFrequency()  {
        if (cpuCoreNum <= 0){
            return
        }

        cpuMaxFrequency = 0L
        cpuMinFrequency = 0L

        for (index in 0 until cpuCoreNum){
            cpuMaxFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq")
            cpuMinFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq")
        }


        BLog.d("cpuMaxFrequency : $cpuMaxFrequency, cpuMinFrequency : $cpuMinFrequency")
    }


    private fun readFile(filePath: String): Long{
        try {
            val file = RandomAccessFile(filePath, "r")
            val content = file.readLine()
            file.close()

            if (TextUtils.isEmpty(content)){
                return 0L
            }

            BLog.d("readFile content : $content")

            return content.trim().toLong()

        }catch (e : Exception){
           ExceptionHandler.recordException(e)

           return 0L
        }
    }
    
    private fun getAllCpuCoreFrequency() : Long {
        initCpuCoreNum()

        if (cpuCoreNum <=0){
            return 0L
        }

        var frequency = 0L

        for (index in 0 until  cpuCoreNum){
            frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
        }

        BLog.d("frequency : $frequency")

        return frequency
    }

    fun findCurrentFrequencyPercent() : Long {

        val currentFrequency = getAllCpuCoreFrequency()

        BLog.d("currentFrequency : $currentFrequency, cpuMinFrequency : $cpuMinFrequency, cpuMaxFrequency : $cpuMaxFrequency")

        if (cpuMaxFrequency - cpuMinFrequency <= 0L || currentFrequency - cpuMinFrequency < 0L || cpuMaxFrequency - currentFrequency < 0L){
            return 0L
        }

        return (currentFrequency - cpuMinFrequency) * 100 / (cpuMaxFrequency - cpuMinFrequency)
    }


    fun getCpuCoreFrequency() : Long {
        initCpuCoreNum()

        if (cpuCoreNum <=0){
            return 0L
        }

        return getAllCpuCoreFrequency() / cpuCoreNum
    }

}

獲取 CPU 頻率:

CPUUtils.getCpuCoreFrequency()

獲取 CPU 占用率:

CPUtils.findCurrentFrequencyPercent()

到此這篇關(guān)于Android如何獲取當(dāng)前CPU頻率和占用率的文章就介紹到這了,更多相關(guān)Android獲取CPU頻率和占用率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

藁城市| 临颍县| 柞水县| 巴里| 新丰县| 伽师县| 化德县| 和硕县| 镇巴县| 历史| 泸州市| 广昌县| 福鼎市| 十堰市| 太康县| 德安县| 天长市| 垣曲县| 体育| 海淀区| 昌宁县| 大冶市| 城固县| 镇赉县| 昌都县| 平江县| 璧山县| 涟水县| 理塘县| 温泉县| 蓝山县| 湾仔区| 长沙县| 依兰县| 师宗县| 封丘县| 辽源市| 巍山| 攀枝花市| 洪湖市| 从江县|