Linux 休眠进程的 CPU 利用率高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10628037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:21:46  来源:igfitidea点击:

CPU Utilization high for sleeping processes

linuxprocesscpudeadlocksleep

提问by blindsnowmobile

I have a process that appears to be deadlocked:

我有一个似乎陷入僵局的进程:

# strace -p 5075
Process 5075 attached - interrupt to quit
futex(0x419cf9d0, FUTEX_WAIT, 5095, NULL

It is sitting on the "futex" system call, and seems to be indefinitely waiting on a lock. The process is shown to be consuming a large amount of CPU when "top" is run:

它坐在“futex”系统调用上,似乎无限期地等待锁。运行“top”时,进程显示消耗大量 CPU:

# top -b -n 1
top - 23:13:18 up 113 days,  4:19,  1 user,  load average: 1.69, 1.74, 1.72
Tasks: 269 total,   1 running, 268 sleeping,   0 stopped,   0 zombie
Cpu(s):  8.1%us,  0.1%sy,  0.0%ni, 91.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  12165696k total,  3810476k used,  8355220k free,    29440k buffers
Swap:  8388600k total,    43312k used,  8345288k free,   879988k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
5075 omdb      18   0 2373m 1.7g  26m S 199.7 14.9 102804:11 java

The process is also shown to be in a "S" - Sleep state, which makes sense if it's waiting on some resource. However, I don't understand why CPU utilization would be close to 200% if the process is in the sleep state. Why does top report such high CPU utilization on a sleeping process? Shouldn't its CPU utilization be zero?

该进程还显示为处于“S” - 睡眠状态,如果它正在等待某个资源,这是有道理的。但是,我不明白如果进程处于睡眠状态,为什么 CPU 利用率会接近 200%。为什么 top 在睡眠进程中报告如此高的 CPU 使用率?它的 CPU 利用率不应该为零吗?

回答by Frédéric Hamidi

There is no correlation between CPU usage as reported by topand process state. The man pagesays (emphasismine):

由报告的 CPU 使用率top和进程状态之间没有相关性。该手册页说(重点煤矿):

%CPU-- CPU usage

The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.

%CPU-- CPU 使用率

自上次屏幕更新以来,任务在 CPU 时间中所占的份额,以占总 CPU 时间的百分比表示。

So, your process indeed used a huge amount of processor time since the last screen update. It is sleeping, yes, but that's because the currently running process is topitself (which makes sense, since it's currently updating the screen).

因此,自上次屏幕更新以来,您的进程确实使用了大量处理器时间。它正在休眠,是的,但那是因为当前正在运行的进程是top它自己(这是有道理的,因为它当前正在更新屏幕)。

回答by Michael Slade

The topoutput is perfectly normal.

top输出是完全正常的。

The load average calculations include processes that are waiting on something (mutexes/futexes, IO etc) as well as processes that are actually using the CPU. Test it by, say, running something like:

平均负载计算包括等待某些东西(互斥体/futexes、IO 等)的进程以及实际使用 CPU 的进程。比如说,通过运行类似的东西来测试它:

dd if=/dev/sda of=/dev/null

and watching top output to see what happens. It will increase the load average by 1.

并观察顶部输出,看看会发生什么。它将平均负载增加 1。

If you look at this line:

如果你看这一行:

Cpu(s):  8.1%us,  0.1%sy,  0.0%ni, 91.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

the "id" in "91.8%id" means "idle". So the CPU isn't actually doing much at all.

“91.8%id”中的“id”表示“空闲”。所以CPU实际上根本没有做太多事情。

回答by Aaron Novstrup

Does your application fork child processes? The strace output may indicate that the main process is just waiting for child processes to finish their work. If so, you could try running

您的应用程序是否分叉子进程?strace 输出可能表明主进程只是在等待子进程完成它们的工作。如果是这样,您可以尝试运行

strace -f -p 5075

to trace the child processes as well.

也可以跟踪子进程。

回答by Alex

let me add my two cents.

让我加上我的两分钱。

top shows state of the process at a particular moment of time.

top 显示特定时刻的进程状态。

but IT DOES NOT mean that this process was all the previous time in this state.

但这并不意味着这个过程之前一直处于这种状态。

this sugestion is completely wrong.

这个建议是完全错误的。

the process could switch between R and S state million times between previous top time and current top moment

该过程可以在之前的顶级时间和当前的顶级时刻之间在 R 和 S 状态之间切换百万次

so if process switches rapidly between R and S state you can easiky catch it in S state.

因此,如果进程在 R 和 S 状态之间快速切换,您可以在 S 状态下轻松捕获它。

However it uses cpu time between switches.

但是,它在交换机之间使用 CPU 时间。

So please feel the difference between cpu_usage thing ( it describes a period of time ) and state thing ( it describes a particular moment of time ).

所以请感受一下 cpu_usage 事物(它描述了一段时间)和 state 事物(它描述了特定时刻)之间的区别。

let me give a clear example.

让我举一个明确的例子。

some person have stolen 3 aplles from your pocket during last 10 minutes.

有人在过去 10 分钟内从你的口袋里偷了 3 个苹果。

however right now it does not steal apples from your pocket.

但是现在它不会从你的口袋里偷苹果。

stolen apples = cpu_usage, the fact that the person does not steal apples right now = state of process

偷来的苹果 = cpu_usage,这个人现在没有偷苹果的事实 = 进程状态

so its completely wrong to get one characteristic and try to predict another characteristic.

因此,获得一个特征并尝试预测另一个特征是完全错误的。

hope it helps

希望能帮助到你