如何在强力CPU核心上运行Termux程序?

发布于 2025-01-18 16:31:09 字数 326 浏览 3 评论 0原文

我的手机有一个 ARM big.LITTLE cpu,我正在制作一个将永远运行的程序

while(1) {
    do_stuff();
    sleep_seconds(60);
}

,并且 do_stuff() 函数可能使用太多的 cpu,以至于每次运行时都会唤醒性能核心..我想避免如果可能的话。我可以推测,给它尽可能低的CPU优先级,

setpriority(PRIO_PROCESS, 0, 19);

可能会成功,但这只是一个疯狂的猜测,我不知道这是否真的有效......帮忙?

my phone has an ARM big.LITTLE cpu, and i'm making a program that will run forever

while(1) {
    do_stuff();
    sleep_seconds(60);
}

and that do_stuff() function probably use so much cpu that it wakes up a performance-core every time it runs.. i want to avoid that if possible. i can theorize that giving it the lowest possible cpu priority,

setpriority(PRIO_PROCESS, 0, 19);

might do the trick, but that's just a wild guess, i have no idea if that actually works or not.. help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2025-01-25 16:31:09

我想我想了!假设有效的核心是最高频率最低的核心(我认为总是如此?),我们可以使用

cat/sys/设备/系统/cpu/cpu*/cpufreq/cpuinfo_max_freq

to deduce which cores are the performance cores and which cores are the power-efficient cores, then we can use tasket to restrict ourselves to那些核心。 for example on my Mediatek Helio G95 (Ulefone Power Armor 13):

u0_a210@localhost:~$ cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq
2000000
2000000
2000000
2000000
2000000
2000000
2050000
2050000

here the last 2 cores are the performance cores.. and thus we can use

taskset --pid --all-tasks 0,1,2,3,4,5 PID

to restrict PID to only the power-efficient cores. example implementation in PHP:

function restrict_to_slowest_cores(bool $print_debug_info = false): void {
        // cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq
        $slowest_cores = [ ];
        $slowest_freq = INF;
        $path = "";
        for($i = 0;; ++ $i) {
                $path = "/sys/devices/system/cpu/cpu{$i}/cpufreq/cpuinfo_max_freq";
                if (! file_exists ( $path )) {
                        // found the last core i hope..
                        break;
                }
                $freq = ( float ) file_get_contents ( $path );
                if ($freq < $slowest_freq) {
                        // slower than any previously checked core! trash the entire previous list.
                        // (the previous list is either empty or contain exclusively faster cores)
                        $slowest_cores = [
                                        $i
                        ];
                        $slowest_freq = $freq;
                } elseif ($freq === $slowest_freq) {
                        // this is among the slowest cores we've seen thus far, add it to the list.
                        $slowest_cores [] = $i;
                } else {
                        // this is one of the faster cores, ignore it.
                }
        }
        if ($i === 0) {
                throw new \RuntimeException ( "unable to find any cpu freq! file missing/unreadable: {$path}" );
        }
        $cmd = "taskset --pid --all-tasks " . implode ( ",", $slowest_cores ) . " " . getmypid ();
        if (! $print_debug_info) {
                $cmd .= " >/dev/null";
        }
        if ($print_debug_info) {
                var_dump ( [
                                "slowest cores" => $slowest_cores,
                                "last path" => $path,
                                "cmd" => $cmd
                ] );
        }
        $ret = null;
        passthru ( $cmd, $ret );
        if ($ret !== 0) {
                throw new RuntimeException ( "taskset failed! should return 0 but returned: {$ret} - cmd: {$cmd}" );
        }
}
restrict_to_slowest_cores (true);

prints:

u0_a210@localhost:~$ php test.php
array(3) {
  ["slowest cores"]=>
  array(6) {
    [0]=>
    int(0)
    [1]=>
    int(1)
    [2]=>
    int(2)
    [3]=>
    int(3)
    [4]=>
    int(4)
    [5]=>
    int(5)
  }
  ["last path"]=>
  string(53) "/sys/devices/system/cpu/cpu8/cpufreq/cpuinfo_max_freq"
  ["cmd"]=>
  string(43) "taskset --pid --all-tasks 0,1,2,3,4,5 30402"
}
pid 30402's current affinity mask: ff
pid 30402's new affinity mask: 45

success!

i think i figured it out! assuming that the power-efficient cores are the cores with the lowest max frequency (i think that is always the case?), we can use

cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq

to deduce which cores are the performance cores and which cores are the power-efficient cores, then we can use tasket to restrict ourselves to those cores. for example on my Mediatek Helio G95 (Ulefone Power Armor 13):

u0_a210@localhost:~$ cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq
2000000
2000000
2000000
2000000
2000000
2000000
2050000
2050000

here the last 2 cores are the performance cores.. and thus we can use

taskset --pid --all-tasks 0,1,2,3,4,5 PID

to restrict PID to only the power-efficient cores. example implementation in PHP:

function restrict_to_slowest_cores(bool $print_debug_info = false): void {
        // cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_max_freq
        $slowest_cores = [ ];
        $slowest_freq = INF;
        $path = "";
        for($i = 0;; ++ $i) {
                $path = "/sys/devices/system/cpu/cpu{$i}/cpufreq/cpuinfo_max_freq";
                if (! file_exists ( $path )) {
                        // found the last core i hope..
                        break;
                }
                $freq = ( float ) file_get_contents ( $path );
                if ($freq < $slowest_freq) {
                        // slower than any previously checked core! trash the entire previous list.
                        // (the previous list is either empty or contain exclusively faster cores)
                        $slowest_cores = [
                                        $i
                        ];
                        $slowest_freq = $freq;
                } elseif ($freq === $slowest_freq) {
                        // this is among the slowest cores we've seen thus far, add it to the list.
                        $slowest_cores [] = $i;
                } else {
                        // this is one of the faster cores, ignore it.
                }
        }
        if ($i === 0) {
                throw new \RuntimeException ( "unable to find any cpu freq! file missing/unreadable: {$path}" );
        }
        $cmd = "taskset --pid --all-tasks " . implode ( ",", $slowest_cores ) . " " . getmypid ();
        if (! $print_debug_info) {
                $cmd .= " >/dev/null";
        }
        if ($print_debug_info) {
                var_dump ( [
                                "slowest cores" => $slowest_cores,
                                "last path" => $path,
                                "cmd" => $cmd
                ] );
        }
        $ret = null;
        passthru ( $cmd, $ret );
        if ($ret !== 0) {
                throw new RuntimeException ( "taskset failed! should return 0 but returned: {$ret} - cmd: {$cmd}" );
        }
}
restrict_to_slowest_cores (true);

prints:

u0_a210@localhost:~$ php test.php
array(3) {
  ["slowest cores"]=>
  array(6) {
    [0]=>
    int(0)
    [1]=>
    int(1)
    [2]=>
    int(2)
    [3]=>
    int(3)
    [4]=>
    int(4)
    [5]=>
    int(5)
  }
  ["last path"]=>
  string(53) "/sys/devices/system/cpu/cpu8/cpufreq/cpuinfo_max_freq"
  ["cmd"]=>
  string(43) "taskset --pid --all-tasks 0,1,2,3,4,5 30402"
}
pid 30402's current affinity mask: ff
pid 30402's new affinity mask: 45

success!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文