swithcing off cpus

As part of resurrecting my MacBook Pro and making it usable, one of the things I have been try is to control how many CPUs are active at a given time. I could have just disabled HyperThreading to halve the CPUs. But was curious how can I go about doing it in general.

As with all things in Linux, CPUs are represented as files. The act of disabling turbo-boost was one such example.

This time the command is lscpu. It is a fairly versatile command with many options. lscpu -h should print all the options and help, while just lscpu will print just the basic information about the CPU(s) in machine.

However it can list all CPUs online/offline with extended information, that what is printed here:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sarang@mbp:~$ lscpu -a -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE    MAXMHZ   MINMHZ
  0    0      0    0 0:0:0:0          yes 2900.0000 800.0000
  1    0      0    1 1:1:1:0          yes 2900.0000 800.0000
  2    0      0    2 2:2:2:0          yes 2900.0000 800.0000
  3    0      0    3 3:3:3:0          yes 2900.0000 800.0000
  4    0      0    0 0:0:0:0          yes 2900.0000 800.0000
  5    0      0    1 1:1:1:0          yes 2900.0000 800.0000
  6    0      0    2 2:2:2:0          yes 2900.0000 800.0000
  7    0      0    3 3:3:3:0          yes 2900.0000 800.0000


This MBP has just 1 chip, 4 Core & 8 Threads- thus 8 logical processors. Core column repeats from 0-3. Here I am just switching off 1 Logical Processor from each core by writing 0 in each CPU file. Well this is equivalent to switching off HyperThreading, but to conserve battery one could have just switched off entire cores. Perhaps more on this later with powertop.

1
2
3
4
echo "0" | sudo tee /sys/devices/system/cpu/cpu4/online
echo "0" | sudo tee /sys/devices/system/cpu/cpu5/online
echo "0" | sudo tee /sys/devices/system/cpu/cpu6/online
echo "0" | sudo tee /sys/devices/system/cpu/cpu7/online


After this, said CPUs will be taken offline, ONLINE column has no for such CPUs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sarang@mbp:~$ lscpu -a -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE    MAXMHZ   MINMHZ
  0    0      0    0 0:0:0:0          yes 2900.0000 800.0000
  1    0      0    1 1:1:1:0          yes 2900.0000 800.0000
  2    0      0    2 2:2:2:0          yes 2900.0000 800.0000
  3    0      0    3 3:3:3:0          yes 2900.0000 800.0000
  4    -      -    - :::               no 2900.0000 800.0000
  5    -      -    - :::               no 2900.0000 800.0000
  6    -      -    - :::               no 2900.0000 800.0000
  7    -      -    - :::               no 2900.0000 800.0000


There are other ways to check which CPUs are online & offline:

1
2
3
4
5
sarang@mbp:~$ cat /sys/devices/system/cpu/online
0-3
sarang@mbp:~$ cat /sys/devices/system/cpu/offline
4-7
sarang@mbp:~$