2

I want to find CPU usage of my own c application. I have already use ps -p pid -o %cpu,%mem,cmd It works fine in my Ubuntu 10.04 desktop. But not work in ARM architecture.

It shows following error.

ps: invalid option -- 'p'

BusyBox v1.13.2 (2011-03-24 18:58:44 CDT) multi-call binary

Usage: ps

Report process status

Options: w Wide output

So I need c code for finding cpu usage.

Pawan
  • 1,537
  • 1
  • 15
  • 19
Pankaj Vavadiya
  • 481
  • 7
  • 15

3 Answers3

1

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.

For a more complete ps you might want to use the ps from http://procps.sourceforge.net/

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.

Henrik Carlqvist
  • 1,138
  • 5
  • 6
  • Thanks. I understand that my ps command is not full featured. But is ther any another way in C code? – Pankaj Vavadiya Jul 31 '15 at 15:21
  • You could open and read files in the /proc directory. I haven't studied the procps source code myself, but from its name I would guess that is exactly how it is done. You can find the cmd fields in /proc/*/cmdline , but arguments are separated by nul characters instead of spaces. The proc file system is documented in the kernel sources in Documentation/filesystems/proc.txt – Henrik Carlqvist Jul 31 '15 at 17:06
1

Busybox also provides "top", which can display the CPU usage of processes.

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.

user5071535
  • 1,312
  • 8
  • 25
  • 42
0

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() . You can find something to start here.

Pawan
  • 1,537
  • 1
  • 15
  • 19
ChatterOne
  • 3,381
  • 1
  • 18
  • 24