Python 使用CPU的失效进程

Python 使用CPU的失效进程,python,linux,unix,ps,Python,Linux,Unix,Ps,ps aux的输出包含以下内容: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ubuntu 1496 9.1 0.0 0 0 pts/0 Z+ 19:47 0:30 [python] <defunct> ubuntu 1501 14.6 0.0 0 0 pts/0 Z+ 19:47 0:48 [pyth

ps aux
的输出包含以下内容:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ubuntu    1496  9.1  0.0      0     0 pts/0    Z+   19:47   0:30 [python] <defunct>
ubuntu    1501 14.6  0.0      0     0 pts/0    Z+   19:47   0:48 [python] <defunct>
ubuntu    1502 14.8  0.0      0     0 pts/0    Z+   19:47   0:48 [python] <defunct>
ubuntu    1503 15.1  0.0      0     0 pts/0    Z+   19:47   0:49 [python] <defunct>
ubuntu    1504 15.4  0.0      0     0 pts/0    Z+   19:47   0:50 [python] <defunct>
ubuntu    1505 15.8  0.0      0     0 pts/0    Z+   19:47   0:52 [python] <defunct>
ubuntu    1506 16.0  0.0      0     0 pts/0    Z+   19:47   0:53 [python] <defunct>
ubuntu    1507 14.1  0.0      0     0 pts/0    Z+   19:47   0:46 [python] <defunct>
ubuntu    1508 14.3  0.0      0     0 pts/0    Z+   19:47   0:47 [python] <defunct>
ubuntu    1509 14.4  0.0      0     0 pts/0    Z+   19:47   0:47 [python] <defunct>
ubuntu    1510 14.6  0.0      0     0 pts/0    Z+   19:47   0:48 [python] <defunct>
ubuntu    1511 14.9  0.0      0     0 pts/0    Z+   19:47   0:49 [python] <defunct>
ubuntu    1512 10.7  0.0      0     0 pts/0    Z+   19:47   0:35 [python] <defunct>
ubuntu    1513 71.3  0.0      0     0 pts/0    Z+   19:47   3:55 [python] <defunct>
USER PID%CPU%MEM VSZ RSS TTY STAT START TIME命令
ubuntu 1496 9.1 0.0 0 pts/0 Z+19:47 0:30[python]
ubuntu 150114.6 0.0 0 0 pts/0 Z+19:47 0:48[python]
ubuntu 1502 14.8 0.0 0 pts/0 Z+19:47 0:48[python]
ubuntu 1503 15.1 0.0 0 pts/0 Z+19:47 0:49[python]
ubuntu 1504 15.4 0.0 0 0 pts/0 Z+19:47 0:50[python]
ubuntu 1505 15.8 0.0 0 0 pts/0 Z+19:47 0:52[python]
ubuntu 1506 16.0.0 pts/0 Z+19:47 0:53[python]
ubuntu 1507 14.1 0.0 0 pts/0 Z+19:47 0:46[python]
ubuntu 1508 14.3 0.0 0 pts/0 Z+19:47 0:47[python]
ubuntu 1509 14.4 0.0 0 0 pts/0 Z+19:47 0:47[python]
ubuntu 1510 14.6 0.0 0 pts/0 Z+19:47 0:48[python]
ubuntu 151114.9 0.0 0 0 pts/0 Z+19:47 0:49[python]
ubuntu 1512 10.7 0.0 0 0 pts/0 Z+19:47 0:35[python]
ubuntu 1513 71.3 0.0 0 0 pts/0 Z+19:47 3:55[python]
这些是通过多处理产生的一组进程,它们已经完成并等待父进程加入。他们为什么占用CPU

如果这只是
ps
的一个工件,我如何才能准确地了解CPU的使用量呢?

一个僵尸进程(即“失效”的进程)不消耗CPU:它只是由内核保留,以便父进程可以检索有关它的信息(例如返回状态、资源使用情况等)


ps
命令指示的CPU使用量对应于进程运行时的CPU使用量:即,在进程终止并成为僵尸之前。

这些是僵尸进程,如stat列中的Z所示-在父进程终止之前,不会清理它们。我对python了解不多,但大概您在python解释器中调用了fork或类似的工具来生成它们。杀死解释器,僵尸将被收割(清理)

如果您想了解CPU的最新信息,请尝试使用“top”命令

另外,我更喜欢“ps-ef”而不是“ps-aux”aux的输出,因为它总是让我觉得它是一种非标准的攻击(因此没有一个“-”来分隔命令和参数),它也不能在很多其他Unix系统上工作,如HPUX、AIX等


“ps-ef”显示了ppid(父pid),它可以帮助您跟踪类似的问题。

有趣的是,也许令人困惑的是,我现在有一个僵尸进程,它正在我的系统上累积CPU时间。所以问题是,为什么?一般的看法是,
ps
的任何显示僵尸进程的输出都意味着唯一使用的是进程表条目;来自wikipedia:“…僵尸进程或失效进程是一个已完成执行(通过退出系统调用)但仍在进程表中有一个条目的进程:它是一个处于“终止状态”的进程。”来自unix.stackexchange:“僵尸进程几乎不占用任何资源,因此让它们逗留不会产生性能成本。”

所以我有一个僵尸程序:

# ps -e -o pid,ppid,stat,comm| grep Z
 7296     1 Zl   myproc <defunct>
(在字段15处修剪)

所以我试图用一个术语来终止进程8009。。。没用。用杀戮杀死它也是徒劳的

对我来说,听起来像一个内核bug。我确实试过扫射它,这是愚蠢的,因为现在我的扫射不会退出

这是在内核为3.10.0-1062的RHEL7.7上。现在已经很老了,但还年轻到可以得出结论(在我看来),僵尸进程可能会因为某个地方的bug而积累系统资源

顺便说一下,根据
iotop
我们的i/o峰值为4 GBps,这是一个很大的数字。我认为这件事肯定会对我们的系统产生影响,我想重新启动

/proc/8009的ls输出返回:

# ls -l /proc/8009
ls: cannot read symbolic link /proc/8009/cwd: No such file or directory
ls: cannot read symbolic link /proc/8009/root: No such file or directory
ls: cannot read symbolic link /proc/8009/exe: No such file or directory
(随后是正常的/proc/pid输出……但我对其进行了修剪)

/proc/8009/fd为空。因此,即使我有大量的I/o发生,它也不会写入任何文件。我没有看到文件系统空间被使用,如
df-h
output所示

最后:尝试重新启动被证明是不可能的<代码>关机-r现在不工作。有两个systemd进程卡在i/o等待中:

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
22725 root       20   0  129M  2512  1548 R  0.0  0.0  0:00.19 htop
22227 root       20   0  195M  4776  2652 D  0.0  0.0  0:00.00 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    1 root       20   0  195M  4776  2652 D  0.0  0.0  0:58.41 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
这是关机输出。我认为init在这一点上相当混乱:

# shutdown -r now
Failed to open /dev/initctl: No such device or address
Failed to talk to init daemon.
reboot
也这么说。我得拔掉这台机器的插头

…更新:就在我登录控制台时,系统重新启动了!大概花了10分钟。所以我不知道systemd在做什么,但它在做些什么

…另一个更新:今天我有3台机器,它们都有相同的特征:相同的二进制文件,某种行为(没有打开的文件描述符,但是发生了I/o,两个线程,子线程正在累积CPU时间)。正如@Stephane Chazelas提到的,我执行了堆栈跟踪。这是一个典型的输出;我不是很精通内核,但也许将来会有一些入侵者对它感兴趣。。。请注意,242603是父线程,242919是忙碌的子线程:

# grep -H . /proc/242919/task/*/stack
/proc/242919/task/242603/stack:[<ffffffff898a131e>] do_exit+0x6ce/0xa50
/proc/242919/task/242603/stack:[<ffffffff898a171f>] do_group_exit+0x3f/0xa0
/proc/242919/task/242603/stack:[<ffffffff898b252e>] get_signal_to_deliver+0x1ce/0x5e0
/proc/242919/task/242603/stack:[<ffffffff8982c527>] do_signal+0x57/0x6f0
/proc/242919/task/242603/stack:[<ffffffff8982cc32>] do_notify_resume+0x72/0xc0
/proc/242919/task/242603/stack:[<ffffffff89f8c23b>] int_signal+0x12/0x17
/proc/242919/task/242603/stack:[<ffffffffffffffff>] 0xffffffffffffffff
/proc/242919/task/242919/stack:[<ffffffffc09cbb03>] ext4_mb_new_blocks+0x653/0xa20 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc09c0a36>] ext4_ext_map_blocks+0x4a6/0xf60 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc098fcf5>] ext4_map_blocks+0x155/0x6e0 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc0993cfa>] ext4_writepages+0x6da/0xcf0 [ext4]
/proc/242919/task/242919/stack:[<ffffffff899c8d31>] do_writepages+0x21/0x50
/proc/242919/task/242919/stack:[<ffffffff899bd4b5>] __filemap_fdatawrite_range+0x65/0x80
/proc/242919/task/242919/stack:[<ffffffff899bd59c>] filemap_flush+0x1c/0x20
/proc/242919/task/242919/stack:[<ffffffffc099116c>] ext4_alloc_da_blocks+0x2c/0x70 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc098a4d9>] ext4_release_file+0x79/0xc0 [ext4]
/proc/242919/task/242919/stack:[<ffffffff89a4a9cc>] __fput+0xec/0x260
/proc/242919/task/242919/stack:[<ffffffff89a4ac2e>] ____fput+0xe/0x10
/proc/242919/task/242919/stack:[<ffffffff898c1c0b>] task_work_run+0xbb/0xe0
/proc/242919/task/242919/stack:[<ffffffff898a0f24>] do_exit+0x2d4/0xa50
/proc/242919/task/242919/stack:[<ffffffff898a171f>] do_group_exit+0x3f/0xa0
/proc/242919/task/242919/stack:[<ffffffff898b252e>] get_signal_to_deliver+0x1ce/0x5e0
/proc/242919/task/242919/stack:[<ffffffff8982c527>] do_signal+0x57/0x6f0
/proc/242919/task/242919/stack:[<ffffffff8982cc32>] do_notify_resume+0x72/0xc0
/proc/242919/task/242919/stack:[<ffffffff89f8256c>] retint_signal+0x48/0x8c
/proc/242919/task/242919/stack:[<ffffffffffffffff>] 0xffffffffffffffff
#grep-H/proc/242919/task/*/stack
/proc/242919/task/242603/stack:[]do_exit+0x6ce/0xa50
/proc/242919/task/242603/stack:[]执行组退出+0x3f/0xa0
/proc/242919/task/242603/stack:[]获取发送信号+0x1ce/0x5e0
/proc/242919/task/242603/stack:[]do_信号+0x57/0x6f0
/proc/242919/task/242603/stack:[]通知恢复+0x72/0xc0
/proc/242919/task/242603/stack:[]整数信号+0x12/0x17
/proc/242919/task/242603/stack:[]0xffffffffffffff
/proc/242919/task/242919/stack:[]ext4_mb_new_块+0x653/0xa20[ext4]
/proc/242919/task/242919/stack:[]ext4_ext_map_块+0x4a6/0xf60[ext4]
/proc/242919/task/242919/stack:[]ext4_map_块+0x155/0x6e0[ext4]
/proc/242919/task/242919/stack:[]ext4_writepages+0x6da/0xcf0[ext4]
/
  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
22725 root       20   0  129M  2512  1548 R  0.0  0.0  0:00.19 htop
22227 root       20   0  195M  4776  2652 D  0.0  0.0  0:00.00 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    1 root       20   0  195M  4776  2652 D  0.0  0.0  0:58.41 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
# shutdown -r now
Failed to open /dev/initctl: No such device or address
Failed to talk to init daemon.
# grep -H . /proc/242919/task/*/stack
/proc/242919/task/242603/stack:[<ffffffff898a131e>] do_exit+0x6ce/0xa50
/proc/242919/task/242603/stack:[<ffffffff898a171f>] do_group_exit+0x3f/0xa0
/proc/242919/task/242603/stack:[<ffffffff898b252e>] get_signal_to_deliver+0x1ce/0x5e0
/proc/242919/task/242603/stack:[<ffffffff8982c527>] do_signal+0x57/0x6f0
/proc/242919/task/242603/stack:[<ffffffff8982cc32>] do_notify_resume+0x72/0xc0
/proc/242919/task/242603/stack:[<ffffffff89f8c23b>] int_signal+0x12/0x17
/proc/242919/task/242603/stack:[<ffffffffffffffff>] 0xffffffffffffffff
/proc/242919/task/242919/stack:[<ffffffffc09cbb03>] ext4_mb_new_blocks+0x653/0xa20 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc09c0a36>] ext4_ext_map_blocks+0x4a6/0xf60 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc098fcf5>] ext4_map_blocks+0x155/0x6e0 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc0993cfa>] ext4_writepages+0x6da/0xcf0 [ext4]
/proc/242919/task/242919/stack:[<ffffffff899c8d31>] do_writepages+0x21/0x50
/proc/242919/task/242919/stack:[<ffffffff899bd4b5>] __filemap_fdatawrite_range+0x65/0x80
/proc/242919/task/242919/stack:[<ffffffff899bd59c>] filemap_flush+0x1c/0x20
/proc/242919/task/242919/stack:[<ffffffffc099116c>] ext4_alloc_da_blocks+0x2c/0x70 [ext4]
/proc/242919/task/242919/stack:[<ffffffffc098a4d9>] ext4_release_file+0x79/0xc0 [ext4]
/proc/242919/task/242919/stack:[<ffffffff89a4a9cc>] __fput+0xec/0x260
/proc/242919/task/242919/stack:[<ffffffff89a4ac2e>] ____fput+0xe/0x10
/proc/242919/task/242919/stack:[<ffffffff898c1c0b>] task_work_run+0xbb/0xe0
/proc/242919/task/242919/stack:[<ffffffff898a0f24>] do_exit+0x2d4/0xa50
/proc/242919/task/242919/stack:[<ffffffff898a171f>] do_group_exit+0x3f/0xa0
/proc/242919/task/242919/stack:[<ffffffff898b252e>] get_signal_to_deliver+0x1ce/0x5e0
/proc/242919/task/242919/stack:[<ffffffff8982c527>] do_signal+0x57/0x6f0
/proc/242919/task/242919/stack:[<ffffffff8982cc32>] do_notify_resume+0x72/0xc0
/proc/242919/task/242919/stack:[<ffffffff89f8256c>] retint_signal+0x48/0x8c
/proc/242919/task/242919/stack:[<ffffffffffffffff>] 0xffffffffffffffff