简体   繁体   English

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

[英]How to get RAM and CPU time consumption ? Python App on Linux

I'm trying to get the value of the CPU consumption and the RAM consumption of my Python App on a Linux embedded environnement using Psutil and Memory_Profiler .我正在尝试使用PsutilMemory_Profiler在 Linux 嵌入式环境中获取我的 Python 应用程序的 CPU 消耗和 RAM 消耗的值。 The thing is I can't get it done, or more accurately I think I don't understand exactly what I'm getting as a result.问题是我无法完成它,或者更准确地说,我认为我不完全理解我得到的结果。 I find Psutil quite unclear about the values returned by the methods so I don't know exactly what I should use in my case.我发现 Psutil 对方法返回的值非常不清楚,所以我不知道在我的情况下应该使用什么。

I've written this but I think this can't give me what I actually want:我已经写了这个,但我认为这不能给我我真正想要的东西:

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 + "\n\n" + txt3 + "\n\n" + txt4 + "\n"
f.write(TXT)
f.close()

Output: Output:

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

Could someone tell me if I should use an other method?有人可以告诉我是否应该使用其他方法吗? Or If I used the right one, what result should I look at?或者如果我使用了正确的,我应该看什么结果? Should I like sum the different results printed in CPU Time?我是否应该对 CPU Time 中打印的不同结果求和? But if I do so it's way to long for being the time of CPU used... PLease Help me: :(但是,如果我这样做,那就太渴望使用 CPU 的时间了……请帮助我::(

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

Utility context manager which considerably speeds up the retrieval of multiple process information at the same time.实用上下文管理器,可显着加快同时检索多个进程信息。 Internally different process info (eg name(), ppid(), uids(), create_time(), …) may be fetched by using the same routine, but only one value is returned and the others are discarded.内部不同的进程信息(例如 name()、ppid()、uids()、create_time() 等)可以通过使用相同的例程来获取,但只返回一个值,而其他值则被丢弃。 When using this context manager the internal routine is executed once (in the example below on name()) the value of interest is returned and the others are cached.当使用这个上下文管理器时,内部例程被执行一次(在下面的例子中,name()),感兴趣的值被返回,其他的被缓存。 The subsequent calls sharing the same internal routine will return the cached value.共享相同内部例程的后续调用将返回缓存值。 The cache is cleared when exiting the context manager block.退出上下文管理器块时会清除缓存。 The advice is to use this every time you retrieve more than one information about the process.建议是在每次检索有关该过程的多个信息时使用它。 If you're lucky, you'll get a hell of a speedup.如果你幸运的话,你会得到一个地狱般的加速。

I ran this:我跑了这个:

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)

On a multi-threaded python.exe (I'm on Windows).在多线程 python.exe 上(我在 Windows 上)。 It returns information about the process and seems to be accurate when comparing with the Resource Monitor.它返回有关进程的信息,并且在与资源监视器进行比较时似乎是准确的。 What is it that you don't understand about your output?你的 output 有什么不明白的地方?

Also, regarding cpu_times() :另外,关于cpu_times()

Return system CPU times as a named tuple.以命名元组的形式返回系统 CPU 时间。 Every attribute represents the seconds the CPU has spent in the given mode.每个属性代表 CPU 在给定模式下花费的秒数。 The attributes availability varies depending on the platform:属性可用性因平台而异:

user: time spent by normal processes executing in user mode; user:正常进程在用户模式下执行所花费的时间; on Linux this also includes guest time在 Linux 这也包括访客时间
system: time spent by processes executing in kernel mode system:在 kernel 模式下执行的进程所花费的时间
idle: time spent doing nothing idle:无所事事的时间

You can use the top command also, to get the most accurate stats您也可以使用 top 命令来获得最准确的统计信息

Run " top " directly in your shell to get interactive view like windows task manager直接在您的 shell 中运行“top”以获得像 windows 任务管理器这样的交互式视图

OR for command line to save output in txt file或用于命令行将 output 保存在 txt 文件中

top -n 1 -b > top-output.txt顶部 -n 1 -b > 顶部输出.txt

you can cron this by saving it as a shell script,您可以通过将其保存为 shell 脚本来 cron,

Fairly extensive documentation, included in almost all Linux distributions相当广泛的文档,包含在几乎所有 Linux 发行版中

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

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

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