简体   繁体   English

获取Linux中进程的最大CPU / RAM使用率

[英]Getting the max CPU / RAM usage for a process in Linux

How can I get the max CPU / RAM usage in Linux, when starting a process inside my python code ? 在我的python代码中启动进程时,如何在Linux中获得最大的CPU / RAM使用率?

I want to calculate from the start of the process till the process end 我想从流程开始到流程结束进行计算

"Maximum CPU usage" is essentially meaningless. “最大CPU使用率”本质上是没有意义的。 At any instant in time, a process is either running on one (or more) CPUs, or is not running. 在任何时候,一个进程正在一个(或多个)CPU上运行,或者没有运行。 As such, the maximum CPU usage of any process will always be at least 100%, because there was at least one instant at which the process is running. 因此,任何进程的最大CPU使用率将始终至少为100%,因为该进程至少在一个时刻运行。 Tools which display CPU usage as a percentage are measuring the ratio of CPU time consumed by a process to its age. 将CPU使用率显示为百分比的工具正在测量进程消耗的CPU时间与其寿命的比率。

The only situation where this might be a useful measure would be for multithreaded processes, where the "maximum CPU usage" is the maximum number of threads belonging to the process which are all running simultaneously. 对于多线程进程,唯一可能有用的情况是“最大CPU使用率”是属于该进程的,同时运行的最大线程数。 I'm not aware of any specific way to measure this. 我不知道有什么具体方法可以衡量这一点。

To get the memory, a very simple way would be to get the output of: 要获取内存,一种非常简单的方法是获取以下内容的输出:

/proc/YOUR_PROGRAM_PID/status

... capture this at the start, and end, of the python process, and extract the memory fields you care about (eg VmPeak, VmSize) ...在python进程的开始和结束时捕获它,并提取您关心的内存字段(例如VmPeak,VmSize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM