如何获得RAM和CPU的时间消耗?Linux上的Python应用程序



我正试图使用PsutilMemory_Profiler在Linux嵌入式环境中获取Python应用程序的CPU消耗和RAM消耗值。问题是我做不到,或者更准确地说,我想我不明白自己到底得到了什么。我发现Psutil对方法返回的值非常不清楚,所以我不知道在我的情况下应该使用什么。

我已经写了这篇文章,但我认为这并不能给我真正想要的东西:

f = open("mem_info.txt", "w") 
txt = "Memory RAM : " + str(process.memory_info().vms) + " bytes"
txt3 = "RAM details : " + str(virtual_memory())
txt2 = "Memory Percent : " + str(process.memory_percent(memtype="vms")) + " %"
txt4 = "CPU Time  : " + str(process.cpu_times()) + " s"
TXT = txt + "n" + txt2 + "nn" + txt3 + "nn" + txt4 + "n"
f.write(TXT)
f.close()

输出:

Memory RAM : 605085696 bytes
Memory Percent : 19.297597042513864 %
RAM details : svmem(total=3135549440, available=2531147776, percent=19.3, used=417918976, free=104349696, active=1263509504, inactive=1590898688, buffers=13201408, cached=2600079360, shared=17809408, slab=109674496)
CPU Time  : pcputimes(user=0.54, system=0.07, children_user=6.99, children_system=0.34, iowait=0.61) s

有人能告诉我是否应该用其他方法吗?或者,如果我使用了正确的,我应该看到什么结果?我应该对CPU时间中打印的不同结果求和吗?但如果我这样做的话,那么CPU使用的时间就太长了。。。请帮帮我!:(

您也可以使用top命令来获得最准确的统计

运行"顶部";直接在您的shell中获取交互式视图,如windows任务管理器

OR命令行将输出保存在txt文件中

top-n 1-b>top output.txt

您可以通过将其保存为shell脚本来对此进行cron

相当广泛的文档,包括在几乎所有的Linux发行版中

https://man7.org/linux/man-pages/man1/top.1.html

https://psutil.readthedocs.io/en/latest/index.html?highlight=oneshot#psutil.Process.oneshot

实用程序上下文管理器,它大大加快了同时检索多个进程信息的速度。内部不同的进程信息(例如name((、ppid((、uids((、create_time((…(可以通过使用相同的例程来获取,但只返回一个值,其他值则被丢弃。当使用此上下文管理器时,内部例程执行一次(在下面的name((示例中(,返回感兴趣的值,并缓存其他值。共享相同内部例程的后续调用将返回缓存的值。退出上下文管理器块时会清除缓存。建议是,每次检索到有关流程的多个信息时都要使用此方法。如果你运气好,你会得到一个地狱般的加速。

我运行了这个:

import psutil
import time
p = psutil.Process(pid=10500)
with p.oneshot():
while p.status() == 'running':
print(p.name())  # execute internal routine once collecting multiple info
print(p.cpu_times())  # return cached value
print(p.memory_info().vms)  # return cached value
print(p.cpu_percent())  # return cached value
print(p.create_time())  # return cached value
print(p.ppid())  # return cached value
print(p.status())  # return cached value
time.sleep(1)

在多线程python.exe上(我在Windows上(。它返回有关进程的信息,与资源监视器相比似乎是准确的。你对自己的产出有什么不了解的地方?

此外,关于cpu_times():

以命名元组的形式返回系统CPU时间。每个属性表示CPU在给定模式下花费的秒数。属性可用性因平台而异:

user:正常进程在用户模式下执行所花费的时间;在Linux上这还包括来宾时间
system:进程花费的时间在内核模式下执行
空闲:什么都不做的时间

要查找进程id(在本例中,我们正在查找python脚本(,请使用:

{p.pid: p.info for p in psutil.process_iter() if p.info['name'] == 'python.exe'}

user:在用户模式下花费的时间
系统:在内核模式下花费的时间。

因此,在运行用户程序时,请确保使用pid,并且您将查找user输出。

这是我使用以下答案中第一个单元格的代码监视多线程python.exe的两次迭代的输出:

python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
0.0
1642441782.2310572
15272
running
python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
100.1
1642441782.2310572
15272
running
python.exe
pcputimes(user=8.546875, system=20.046875, children_user=0.0, children_system=0.0)
436965376
100.0
1642441782.2310572
15272
running

CCD_ 5为>100,因为我有多个核心。我应该注意的一点是,user在这个循环中不会改变。我在一个jupyter笔记本细胞中运行,只有当我停止细胞并重新启动它时,它才会改变。所以使用psutil-lib需要一些修补。

最新更新