How to execute terminal command on energy efficient cores on M1 chip?
I want to run nodejs process on E cores, but when I run it I see that it uses P cores even if there is a 0.3 second task.
Solution 1:
What you want to do is technically known as setting a "CPU affinity", i.e. letting the operating system scheduler know, which CPU core a process should be allowed to run on.
As far as I know, macOS does not come with a GUI or command line program for setting CPU affinity directly. This is unlike some other operating systems, such as for example Linux that features a simple taskset
command for that purpose.
However macOS does come with a command line tool for setting, amongst other things, the scheduling policies - amongst these the desired Quality of Service (QoS). This means that you can indicate that your prefer to run on the energy-efficient cores by starting your command line this:
taskpolicy -c background mycommand
Apple does provide an API for programmers that allow them to set CPU affinity directly.
As you indicate that you want to set the CPU affinity for various nodejs processes, it seems likely that you're developing software or running open-source software where you can modify the source code. In that case you can set the CPU affinity by way of the thread_policy_set()
function.
For example if you have a Mach thread named thread that you want to schedule only on core #7, you can do it like this:
thread_affinity_policy_data_t policy = { 7 };
thread_policy_set(thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1);