Android 主线程绑定 CPU 大核(提升应用整体性能)

在 Android 开发中,主线程是负责处理用户界面操作的线程。绑定 CPU 大核的作用是为主线程提供更高的计算性能和更快的响应速度。

通常情况下,Android 设备的 CPU 由多个小核和少量大核组成,小核主要负责处理低功耗的后台任务,而大核则拥有更高的性能更大的计算能力

通过绑定,主线程可以充分利用大核的计算能力,提升程序的运行速度和响应性能。这对于需要处理大量计算或需要实时更新界面的应用程序来说尤为重要,可以提高用户体验,并减少界面卡顿和延迟的问题。


每一部 Android 设备的 CPU 的核心数都不太一样,所以需要先获取当前设备的 CPU 核心数

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int getCpuCoreNumber() {
    std::ifstream cpuinfo("/proc/cpuinfo"); // 读取 cpuinfo 文件
    int cores = 0;
    std::string line;
    while (std::getline(cpuinfo, line)) {
        if (line.substr(0, 9) == "processor") {
            cores++;
        }
    }
    cpuinfo.close();
    return cores;
}

 在获取到当前设备的 CPU 核心数后,就可以通过循环来找出频率最高的核:

int getMaxFreqCPUIndex(int coreNum) {
    int maxFreq = -1; // 频率
    int index = -1;   // CPU 位置
    try {
        for (int i = 0; i < coreNum; i++) {
            std::string filename = "/sys/devices/system/cpu/cpu" + std::to_string(i) + "/cpufreq/cpuinfo_max_freq";
            std::ifstream cpuFile(filename);
            if (cpuFile.good()) {
                std::string line;
                std::getline(cpuFile, line);
                std::stringstream ss(line);
                int freqBound;
                ss >> freqBound;
                if (freqBound > maxFreq) {
                    maxFreq = freqBound;
                    index = i;
                }
                cpuFile.close();
            }
        }
    }
    catch (const std::exception& e) {
        // 处理异常
    }
    return index;
}

获取到大核的位置后,使用 C++ 的库来完成主线程和大核的绑定:

#include <sched.h>

extern "C"
JNIEXPORT void JNICALL
Java_com_xxx_bindCore(JNIEnv *env, jobject thiz) {
    int coreNum = getCpuCoreNumber(); // 核心数量
    int cpuIndex = getMaxFreqCPUIndex(coreNum); // 频率最高的核

    cpu_set_t mask;
    CPU_ZERO(&mask);
    CPU_SET(cpuIndex, &mask);    // 将需要绑定的 cpu 核设置给 mask
    if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { // 将主线程绑核
        // 返回 -1 绑定失败
    }

}

绑定时有可能会失败,可以自行处理绑定失败后的操作


 在上层就比较简单了,直接调用这个 JNI 函数即可:

    external fun bindCore()

尽管将主线程绑定到 CPU 大核上可以提升性能,但需要注意的是,在进行绑定操作时要谨慎。过高的绑定可能会导致系统资源不足,造成其他应用程序运行缓慢或不稳定。因此,在进行绑定操作时需要根据具体的应用需求和设备性能进行合理调整。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>