Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gprof (Really Simple)

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
How do I run gprof such that I can pass command line paramaters to my binary.

ie. I want to run a.out -f file.txt -n 100

I tried gprof a.out -f file.txt -n 100 gmon.out > profile.txt

But it didn't work! This HAS to be pretty simple.
 
They're separate programs.

Assuming you've compiled for profiling
Code:
a.out -f file.txt -n 100 > results.txt
gprof
gprof without parameters assumes a.out as the program, and gmon.out as the profile.

Of course, gprof has a whole bunch of options for extracting lots of different information.

--
 
*blink*

According to the gprof docs you do something like:

1) Run your application; it will run slower than normal, and will produce a file called gmon.out in the current directory

2) Run: % gprof EXECUTABLE gmon.out > gprof.txt

What I need to do is run: % gprof EXECUTABLE <flags passed to EXECUTABLE > gmon.out > gprof.txt

(after getting my gmon.out file)


 
Here's an example transcript
Code:
$ ./t1
#!/bin/sh
cat t1
echo -----
cat hello.c
gcc -pg hello.c
echo -----
wc *.c
echo -----
./a.out *.c
echo -----
gprof a.out gmon.out | head
echo -----
-----
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void processline ( char *line ) {
}

void doit ( char *filename ) {
    FILE *fp;
    if ( (fp=fopen(filename,"r")) != NULL ) {
        char buff[BUFSIZ];
        int lines = 0;
        while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            processline(buff);
            lines++;
        }
        printf( "%d %s\n", lines, filename );
        fclose( fp );
    }
}

int main ( int argc, char *argv[] ) {
    while ( --argc ) doit( *(++argv) );
    return 0;
}
-----
    174     731    4297 board.c
    158     667    4891 cli_ser_pipes.c
     82     355    2295 file-r-rb.c
      1       2      19 float.c
     51     219    1564 hello-back.c
     25      77     522 hello.c
     75     256    1481 hello-old.c
      1       8      27 null.c
     59     249    1519 num2words.c
     16      45     261 rdtsc.c
     22      69     362 realloc.c
     41     172    1096 roman.c
     85     365    2369 rosette.c
    290    1272    7634 speed-read.c
   1080    4487   28337 total
-----
174 board.c
158 cli_ser_pipes.c
82 file-r-rb.c
1 float.c
51 hello-back.c
25 hello.c
75 hello-old.c
1 null.c
59 num2words.c
16 rdtsc.c
22 realloc.c
41 roman.c
85 rosette.c
290 speed-read.c
-----
Flat profile:

Each sample counts as 0.00195312 seconds.
 no time accumulated

  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
  0.00      0.00     0.00     1080     0.00     0.00  processline
  0.00      0.00     0.00       14     0.00     0.00  doit

-----
doit is called once for every file
processline is called once for every line
Compare those counts with the results from the 'wc' program.

--
 
Heh heh. I think I missed the obvious part:

You only need to pass in the flags when you produce the gmon.out file. (This generates all the stats)

Running "gprof a.out gmon.out" simply produces the human readable stat output.


Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top