简体   繁体   English

我的应用程序在 linux c 中的 CPU 使用率

[英]My application's cpu usage in linux c

I want to find CPU usage of my own c application.我想找到我自己的 c 应用程序的 CPU 使用率。 I have already use ps -p pid -o %cpu,%mem,cmd It works fine in my Ubuntu 10.04 desktop.我已经使用了ps -p pid -o %cpu,%mem,cmd它在我的 Ubuntu 10.04 桌面上运行良好。 But not work in ARM architecture.但不适用于 ARM 架构。

It shows following error.它显示以下错误。

ps: invalid option -- 'p' ps: 无效选项 -- 'p'

BusyBox v1.13.2 (2011-03-24 18:58:44 CDT) multi-call binary BusyBox v1.13.2 (2011-03-24 18:58:44 CDT) 多调用二进制

Usage: ps用法:ps

Report process status报告进程状态

Options: w Wide output选项: w 宽输出

So I need c code for finding cpu usage.所以我需要 c 代码来查找 cpu 使用情况。

Busybox is a small footprint set of tools which only contains the most useful subset of the functionality you will find in a desktop system. Busybox 是一组占用空间很小的工具,它只包含桌面系统中最有用的功能子集。

For a more complete ps you might want to use the ps from http://procps.sourceforge.net/要获得更完整的 ps,您可能需要使用http://procps.sourceforge.net/ 中的 ps

You might want to replace the ps in busybox, or you could take snippets of C source from procps if your program has a GPL compatible license.您可能想要替换 busybox 中的 ps,或者如果您的程序具有 GPL 兼容许可证,您可以从 procps 中获取 C 源代码片段。

Busybox also provides "top", which can display the CPU usage of processes. Busybox 还提供了“top”,可以显示进程的 CPU 使用情况。

This doesn't directly answer your question since it's not using C, but maybe it will solve your problem without the extra hassle since you were already willing to use ps.这不会直接回答您的问题,因为它没有使用 C,但也许它可以解决您的问题而不会带来额外的麻烦,因为您已经愿意使用 ps。

If you want to measure CPU time and if you're on a (mostly) POSIX-supporting platform (maybe Android?), then you should look at clock_gettime() and getrusage() .如果您想测量 CPU 时间,并且您使用(主要是)支持 POSIX 的平台(也许是 Android?),那么您应该查看clock_gettime()getrusage() You can find something to start here .您可以从这里开始。

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

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