I want to check how many cores my Python script is using.
Suppose that I have this code:
while True:
print('Hello World!')
When I run top
, it gives the CPU, Memory, percentage in addition to other information but not the cores id or how many cores the process is using.
top - 11:44:15 up 1 day, 23:08, 1 user, load average: 2.88, 2.39, 2.15
Tasks: 289 total, 5 running, 238 sleeping, 0 stopped, 0 zombie
%Cpu(s): 55.2 us, 31.8 sy, 0.0 ni, 12.8 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 7945496 total, 1027328 free, 4707680 used, 2210488 buff/cache
KiB Swap: 15999996 total, 14991876 free, 1008120 used. 2615420 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15336 lenovo 20 0 4393044 470488 192616 S 8.6 5.9 148:18.31 firefox
4412 lenovo 20 0 4521092 437940 65416 R 15.6 5.5 87:58.54 gnome-shell
4221 root 20 0 561840 82500 45652 R 9.9 1.0 84:25.21 Xorg
15395 lenovo 20 0 3871472 319376 151876 S 0.0 4.0 83:57.26 Web Content
2838 lenovo 20 0 3993616 823816 158412 R 68.5 10.4 36:28.10 Web Content
4435 lenovo 9 -11 2915368 11596 8836 S 0.3 0.1 35:46.08 pulseaudio
3342 lenovo 20 0 3060860 324000 127948 S 8.6 4.1 27:53.92 atom
29632 lenovo 20 0 3717204 750864 152688 S 10.3 9.5 22:25.05 Web Content
15443 lenovo 20 0 3273020 245772 50128 S 2.3 3.1 19:49.64 WebExtensions
3293 lenovo 20 0 1291576 215972 112612 S 4.6 2.7 15:38.90 atom
3319 lenovo 20 0 734208 226016 104340 S 5.6 2.8 14:52.60 atom
1446 root -51 0 0 0 0 S 1.0 0.0 14:21.71 irq/132-nvidia
16083 lenovo 20 0 446420 35468 27844 S 1.3 0.4 11:06.27 RDD Process
29733 lenovo 20 0 3537988 363160 235184 S 0.0 4.6 9:29.13 Web Content
29780 lenovo 20 0 3414220 363812 174640 S 0.3 4.6 7:50.64 Web Content
29570 lenovo 20 0 3068760 245412 125788 S 0.3 3.1 4:14.76 Web Content
1733 gdm 20 0 3464584 75712 45704 S 0.0 1.0 4:08.74 gnome-shell
I did some googling, and I found this command:
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep python\`
Which outputs:
PID TID %CPU PSR
15329 - 95.2 -
- 15329 95.2 1
Well I am not sure if the previous command really does the trick or not.
If anyone could give an explanation, I would be grateful.
EDIT:
I am using Ubuntu 18.04 the desktop version.
Kernel version :
4.15.0-99-generic #100-Ubuntu SMP Wed Apr 22 20:32:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
With
top
, you can show the "last used CPU":Press
f
, navigate toP
(Last Used CPU), pressd
to mark it for display, and pressq
to go back to the process view.To show the threads instead of processes (a process runs on multiple CPUs at once by having its threads scheduled on them), run
top
with the-H
option, or pressH
after starting it.Example with Firefox:
I found a great tool, similar to
top
, but with more details.htop, it can be installed by running
sudo apt install htop
.This is the default interface after running
htop
.As you can see, there are much more information about the running processes, CPU and memory usage.
Similar to
top
, it is also possible to add new columns with more information.In my case, to display on which core the process/thread is running:
Here is a screenshot after I added the new column: