2

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
0
1

With top, you can show the "last used CPU":

Fields Management for window 1:Def, whose current sort field is %CPU
   Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
   'd' or <Space> toggles display, 's' sets sort.  Use 'q' or <Esc> to end!

* PID     = Process Id             SUSER   = Saved User Name        vMj     = Major Faults delta  
  PPID    = Parent Process pid     GID     = Group Id               vMn     = Minor Faults delta  
  TGID    = Thread Group Id        GROUP   = Group Name             USED    = Res+Swap Size (KiB) 
* USER    = Effective User Name    PGRP    = Process Group Id       nsIPC   = IPC namespace Inode 
* PR      = Priority               TTY     = Controlling Tty        nsMNT   = MNT namespace Inode 
* NI      = Nice Value             TPGID   = Tty Process Grp Id     nsNET   = NET namespace Inode 
* VIRT    = Virtual Image (KiB)    SID     = Session Id             nsPID   = PID namespace Inode 
* RES     = Resident Size (KiB)    TIME    = CPU Time               nsUSER  = USER namespace Inode
  SHR     = Shared Memory (KiB)    SWAP    = Swapped Size (KiB)     nsUTS   = UTS namespace Inode 
* %CPU    = CPU Usage              CODE    = Code Size (KiB)        LXC     = LXC container name  
* %MEM    = Memory Usage (RES)     DATA    = Data+Stack (KiB)       RSan    = RES Anonymous (KiB) 
* TIME+   = CPU Time, hundredths   nMaj    = Major Page Faults      RSfd    = RES File-based (KiB)
  nTH     = Number of Threads      nMin    = Minor Page Faults      RSlk    = RES Locked (KiB)    
  P       = Last Used Cpu (SMP)    nDRT    = Dirty Pages Count      RSsh    = RES Shared (KiB)    
* S       = Process Status         Flags   = Task Flags <sched.h>   CGNAME  = Control Group name  
  WCHAN   = Sleeping in Function   CGROUPS = Control Groups         NU      = Last Used NUMA node 
* COMMAND = Command Name/Line      SUPGIDS = Supp Groups IDs     
  UID     = Effective User Id      SUPGRPS = Supp Groups Names   
  RUID    = Real User Id           OOMa    = OOMEM Adjustment    
  RUSER   = Real User Name         OOMs    = OOMEM Score current 
  SUID    = Saved User Id          ENVIRON = Environment vars    

Press f, navigate to P (Last Used CPU), press d to mark it for display, and press q 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 press H after starting it.

Example with Firefox:

top - 03:25:57 up  2:36,  1 user,  load average: 0.61, 0.43, 0.45
Threads:  86 total,   0 running,  86 sleeping,   0 stopped,   0 zombie
%Cpu(s):   2.4/1.2     4[|||                                                                                                 ]
GiB Mem : 23.7/15.4     [                                                                                                    ]
GiB Swap:  0.0/0.0      [                                                                                                    ]

    PID USER      PR  NI    VIRT    RES  %CPU  %MEM     TIME+  P S COMMAND                                                                                                                                                                   
   3870 muru      20   0 3354.0m 461.1m   2.0   2.9  13:43.39  4 S firefox                                                                                                                                                                   
   3875 muru      20   0 3354.0m 461.1m   0.7   2.9   2:54.10  0 S Gecko_IOThread                                                                                                                                                            
   4063 muru      20   0 3354.0m 461.1m   0.7   2.9   0:12.78  9 S URL Classifier                                                                                                                                                            
   3873 muru      20   0 3354.0m 461.1m   0.0   2.9   0:00.00  6 S gmain                                                                                                                                                                     
   3874 muru      20   0 3354.0m 461.1m   0.0   2.9   0:08.25  6 S gdbus                                                                                                                                                                     
   3876 muru      20   0 3354.0m 461.1m   0.0   2.9   0:31.17  1 S Timer                                                                                                                                                                     
   3877 muru      20   0 3354.0m 461.1m   0.0   2.9   0:00.00  8 S Netlink Monitor                                                                                                                                                           
   3878 muru      20   0 3354.0m 461.1m   0.0   2.9   0:16.99  9 S Socket Thread                                                                                                                                                             
   3879 muru      20   0 3354.0m 461.1m   0.0   2.9   0:00.43 11 S Permission                                                                                                                                                                
   3882 muru      20   0 3354.0m 461.1m   0.0   2.9   0:00.25  6 S JS Watchdog                                                                                                                                                               
   3883 muru      20   0 3354.0m 461.1m   0.0   2.9   0:02.15 11 S JS Helper              
1

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.

enter image description here

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:

  1. Press F2 (or fn + F2 for some computers).
  2. Move down to columns section under Setup.
  3. Press the right arrow key to move to Active Columns, you can move up or down to choose the place where to add the new column.
  4. Press the right arrow key to move to the Available Columns.
  5. Move to the column you want to add and press enter once it is selected.
  6. Once the new column added, press Esc.

Here is a screenshot after I added the new column:

enter image description here

2
  • 1
    Note that most of what you see here can be shown in top too (try pressing 1 and c).
    – muru
    May 14 '20 at 8:54
  • @muru, you're right, I've checked man top and I discovered a lot of tricks that can be done with top
    – singrium
    May 14 '20 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.