5

I want to see statistics of a small C program, but is a small program that begins and ends. (Not some program that is long time running). I want to improve this program in terms of access to memory, cache hits, context switches, and that sort of things.

The parameters in /proc/[pid]/status are great, but I cannot find the way to see them after execution.

How can I see this file after the proccess has finished execution?

jperelli
  • 6,988
  • 5
  • 50
  • 85

4 Answers4

6

Tossing this system() call at the end of your program will print /proc/[pid]/status to stdout at the very last moment before your program quits.

#include <stdlib.h>

int main() {
        system("cat /proc/$PPID/status");
        return 0;
}

+1 for external programs like valgrind. You can trap your program's exit with debugging utils and check complete statistics without modifying the program.

lunixbochs
  • 21,757
  • 2
  • 39
  • 47
  • I was thinking of doing something like like `./myapp; cat /proc/``pidof myapp``/status`, trying `&`, `&&`, `|`, `||` where the `;` is, all trying to make both process (myapp and cat) as only one, and then both be destructed when both are finished. (crazy theory) – jperelli Apr 10 '12 at 18:36
  • 2
    bash will execute those commands in order - so you can't just say "I want this to run right *before* my command finishes" – lunixbochs Apr 10 '12 at 20:40
4

You can't. The proc filesystem is an interface to kernel data structures. Once the process is gone the associated information is gone too.

If you're looking to improve the performance and memory footprint of the application you may want to investigate valgrind and its friends like cachegrind.

Kristof Provost
  • 26,018
  • 2
  • 26
  • 28
1

/proc/$pid/ only exists while a process is running. If you really want to see it after the process finishes (and I don't see what you would get beyond VmPeak), you could copy it shortly before the process exits.

Kevin
  • 53,822
  • 15
  • 101
  • 132
0

Another silly workaround is to GDB it and make it stop at exit (or any other point of interest):

gdb -ex start -ex 'b exit' -ex c -ex 'info inferior' hello.out

info inferior gives the PID as mentioned at: How does one obtain the pid of the currently debugged process in gdb?

  Num  Description       Connection           Executable
* 1    process 275096    1 (native)           /home/ciro/ello.out

and then in another terminal we can:

cat /proc/275096/status
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985