我想检查我的 Python 脚本使用了多少个核心。
假设我有以下代码:
while True:
print('Hello World!')
当我运行时top
,它会提供 CPU、内存、百分比以及其他信息,但不提供核心 ID 或该进程正在使用的核心数。
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
我进行了一些谷歌搜索,发现了这个命令:
watch -tdn0.5 ps -mo pid,tid,%cpu,psr -p \`pgrep python\`
输出:
PID TID %CPU PSR
15329 - 95.2 -
- 15329 95.2 1
嗯,我不确定前面的命令是否真的有用。
如果有人能给出解释,我将不胜感激。
编辑:
我使用的是 Ubuntu 18.04 桌面版。
内核版本:
4.15.0-99-generic #100-Ubuntu SMP Wed Apr 22 20:32:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
答案1
使用top
,您可以显示“最后使用的 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
按f
,导航到P
(上次使用的 CPU),按d
将其标记为显示,然后按q
返回到进程视图。
要显示线程而不是进程(一个进程通过在多个 CPU 上调度其线程同时在多个 CPU 上运行),请top
使用该-H
选项运行,或H
在启动后按。
以 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
答案2
我发现了一个很棒的工具,类似于top
,但具有更多细节。
顶部,可以通过运行来安装sudo apt install htop
。
这是运行后的默认界面htop
。
如您所见,有关正在运行的进程、CPU 和内存使用情况的信息要多得多。
与类似top
,也可以添加包含更多信息的新列。
在我的例子中,要显示进程/线程在哪个核心上运行:
- 按下F2(对于某些计算机,则按下fn+ )。F2
- 下移至列下的部分设置。
- 按右箭头键移动到活动列,您可以向上或向下移动来选择添加新列的位置。
- 按右箭头键移动到可用列。
- 移动到您想要添加的列,选择后按 Enter 键。
- 添加新列后,按Esc。
这是我添加新列后的屏幕截图: