BSD 4_1_snap release
[unix-history] / usr / src / cmd / prof / prof.c
index 6b254cd..931b0d7 100644 (file)
@@ -1,3 +1,4 @@
+static char *sccsid = "@(#)prof.c      4.5 (Berkeley) 81/07/02";
 /*
  * prof
  */
 /*
  * prof
  */
@@ -141,12 +142,11 @@ main(argc, argv)
        }
        asgnsamples();          /* assign samples to procedures */
 #ifdef plot
        }
        asgnsamples();          /* assign samples to procedures */
 #ifdef plot
-       if (vflag)
+       if (vflg)
                plotprof();     /* a plotted or ... */
        else
                plotprof();     /* a plotted or ... */
        else
-#else
-               printprof();    /* a printed profile */
 #endif
 #endif
+               printprof();    /* a printed profile */
        if ( sflag != 0 ) {
                putprof();
        }
        if ( sflag != 0 ) {
                putprof();
        }
@@ -268,34 +268,35 @@ getsymtab()
        npe++;
 }
 
        npe++;
 }
 
-       /*
       *      information from a mon.out file is in two parts:
       *      the counters of how many times each procedure was called,
       *      if it was called at all;
       *      and an array of sampling hits within pc ranges.
       *      the counters must be dealt with on a file-by-file basis,
       *      since which procedures are represented may vary.
       *      the samples ranges are fixed, but must be summed across
       *      files, and then distributed amoung procedures, because
       *      of the wierd way the plotting is done.
       */
-getpfile( filename )
-       char    *filename;
+/*
* information from a mon.out file is in two parts:
* the counters of how many times each procedure was called,
* if it was called at all;
* and an array of sampling hits within pc ranges.
* the counters must be dealt with on a file-by-file basis,
* since which procedures are represented may vary.
* the samples ranges are fixed, but must be summed across
* files, and then distributed among procedures, because
* of the wierd way the plotting is done.
+ */
+getpfile(filename)
+       char *filename;
 {
 {
-       openpfile( filename );
+
+       openpfile(filename);
        readcntrs();
        asgncntrs();            /* assign counts to procedures */
        readsamples();
        closepfile();
 }
 
        readcntrs();
        asgncntrs();            /* assign counts to procedures */
        readsamples();
        closepfile();
 }
 
-openpfile( filename )
-       char    *filename;
+openpfile(filename)
+       char *filename;
 {
        struct stat stb;
 
 {
        struct stat stb;
 
-       if((pfile = fopen( filename ,"r")) == NULL) {
-               perror( filename );
+       if((pfile = fopen(filename, "r")) == NULL) {
+               perror(filename);
                done();
        }
        fstat(fileno(pfile), &stb);
                done();
        }
        fstat(fileno(pfile), &stb);
@@ -310,13 +311,10 @@ openpfile( filename )
 closepfile()
 {
 
 closepfile()
 {
 
-       fclose( pfile );
-       free( cbuf );
+       fclose(pfile);
+       free(cbuf);
 }
 
 }
 
-       /*
-        * read in procedure call counters
-        */
 readcntrs()
 {
        struct cnt *kp;
 readcntrs()
 {
        struct cnt *kp;
@@ -363,7 +361,8 @@ asgncntrs()
        while (--np>=nl) {
                if (kp<cbuf || np->value > kp->cvalue)
                        continue;
        while (--np>=nl) {
                if (kp<cbuf || np->value > kp->cvalue)
                        continue;
-               while (kp >= cbuf && kp->cvalue - np->value > 11)
+                       /* skip ``static'' functions */
+               while (kp >= cbuf && kp->cvalue > np->value + 11)
                        --kp;
                if (kp->cvalue >= np->value) {
                        np->ncall += kp->cncall;
                        --kp;
                if (kp->cvalue >= np->value) {
                        np->ncall += kp->cncall;
@@ -372,37 +371,36 @@ asgncntrs()
        }
 }
 
        }
 }
 
-       /*
-        *      read pc samples
-        */
 readsamples()
 {
 readsamples()
 {
-       register        i;
+       register i;
        unsigned UNIT   sample;
        unsigned UNIT   sample;
+       int totalt;
        
        
-       if ( samples == 0 ) {
+       if (samples == 0) {
                samples = (unsigned UNIT *)
                samples = (unsigned UNIT *)
-                               calloc( sampbytes , sizeof (unsigned UNIT) );
-               if ( samples == 0 ) {
-                       printf( "prof: No room for %d sample pc's\n" 
-                               sampbytes / sizeof (unsigned UNIT) );
+                   calloc(sampbytes, sizeof (unsigned UNIT));
+               if (samples == 0) {
+                       printf("prof: No room for %d sample pc's\n"
+                           sampbytes / sizeof (unsigned UNIT));
                        done();
                }
        }
                        done();
                }
        }
-       for ( i = 0 ; ; i++ ) {
-               fread( &sample , sizeof (unsigned UNIT) , 1 , pfile );
-               if ( feof( pfile ) ) {
+       for (i = 0; ; i++) {
+               fread(&sample, sizeof (unsigned UNIT), 1, pfile);
+               if (feof(pfile))
                        break;
                        break;
-               }
-               samples[ i ] += sample;
+               samples[i] += sample;
+               totalt += sample;
        }
        }
-       if ( i != nsamples ) {
-               fprintf( stderr ,
-                       "prof: unexpected EOF after reading %d/%d samples\n" ,
-                       --i , nsamples );
+       if (i != nsamples) {
+               fprintf(stderr,
+                   "prof: unexpected EOF after reading %d/%d samples\n",
+                       --i, nsamples);
                done();
        }
 }
                done();
        }
 }
+
 /*
  * Assign samples to the procedures to which they belong.
  */
 /*
  * Assign samples to the procedures to which they belong.
  */
@@ -418,7 +416,7 @@ asgnsamples()
        /* read samples and assign to namelist symbols */
        scale = highpc - lowpc;
        scale /= nsamples;
        /* read samples and assign to namelist symbols */
        scale = highpc - lowpc;
        scale /= nsamples;
-       for(i=0 ; i < nsamples ; i++) {
+       for (i=0; i < nsamples; i++) {
                ccnt = samples[i];
                if (ccnt == 0)
                        continue;
                ccnt = samples[i];
                if (ccnt == 0)
                        continue;
@@ -447,53 +445,45 @@ asgnsamples()
        }
 }
 
        }
 }
 
-       /*
       *      dump what you have out to a mon.out style file.
       */
+/*
* dump what you have out to a mon.out style file.
+ */
 putprof()
 {
 putprof()
 {
-       FILE            *sfile;
-       struct nl       *np;
-       struct cnt      kp;
-       int             i;
-
-       sfile = fopen( MON_SUMNAME , "w" );
-       if ( sfile == NULL ) {
-               perror( MON_SUMNAME );
+       FILE *sfile;
+       struct nl *np;
+       struct cnt kp;
+       int i;
+
+       sfile = fopen(MON_SUMNAME, "w");
+       if (sfile == NULL) {
+               perror(MON_SUMNAME);
                done();
        }
                done();
        }
-               /*
-                *      build a new header.
-                *      h.lowpc and h.highpc are already fine.
-                *      fix h.ncount to count non-zero calls,
-                *      and the one zero call which marks the end.
-                */
+       /*
+        * build a new header.
+        * h.lowpc and h.highpc are already fine.
+        * fix h.ncount to count non-zero calls,
+        * and the one zero call which marks the end.
+        */
        h.ncount = 0;
        h.ncount = 0;
-       for ( np = nl ; np < npe -1 ; np++ ) {
-               if ( np -> ncall > 0 ) {
+       for (np = nl; np < npe-1 ; np++)
+               if (np->ncall > 0)
                        h.ncount++;
                        h.ncount++;
-               }
-       }
        h.ncount++;
        h.ncount++;
-       fwrite( &h , sizeof (struct hdr) , 1 , sfile );
-               /*
-                *      write out the counters
-                */
-       for ( np = nl ; np < npe - 1 ; np++ ) {
-               if ( np -> ncall > 0 ) {
-                       kp.cvalue = np -> value * sizeof (unsigned UNIT);
-                       kp.cncall = np -> ncall;
-                       fwrite( &kp , sizeof (struct cnt) , 1 , sfile );
+       fwrite(&h, sizeof (struct hdr), 1, sfile);
+       for (np = nl; np < npe-1; np++) {
+               if (np->ncall > 0) {
+                       kp.cvalue = np->value * sizeof (unsigned UNIT);
+                       kp.cncall = np->ncall;
+                       fwrite(&kp, sizeof (struct cnt), 1, sfile);
                }
        }
        kp.cvalue = 0;
        kp.cncall = 0;
                }
        }
        kp.cvalue = 0;
        kp.cncall = 0;
-       fwrite( &kp , sizeof (struct cnt) , 1 , sfile );
-               /*
-                *      write out the samples
-                */
-       fwrite( samples , sizeof (unsigned UNIT) , nsamples , sfile );
-       fclose( sfile );
+       fwrite(&kp, sizeof (struct cnt), 1, sfile);
+       fwrite(samples, sizeof (unsigned UNIT), nsamples, sfile);
+       fclose(sfile);
 }
 
 min(a, b)
 }
 
 min(a, b)
@@ -555,7 +545,7 @@ done()
 plotprof()
 {
        double time, lastx, lasty, lastsx;
 plotprof()
 {
        double time, lastx, lasty, lastsx;
-       register        i;
+       register i;
 
        openpl();
        erase();
 
        openpl();
        erase();
@@ -568,7 +558,7 @@ plotprof()
        lasty = ranoff;
        scale = (4080.*ransca)/(sampbytes/sizeof(UNIT));
        lastsx = 0.0;
        lasty = ranoff;
        scale = (4080.*ransca)/(sampbytes/sizeof(UNIT));
        lastsx = 0.0;
-       for(i = 0 ; i < nsamples ; i++ ) {
+       for(i = 0; i < nsamples; i++) {
                unsigned UNIT ccnt;
                double tx, ty;
                ccnt = samples[i];
                unsigned UNIT ccnt;
                double tx, ty;
                ccnt = samples[i];
@@ -601,8 +591,8 @@ plotprof()
                        register j;
                        line(0, (int)lasty, 50, (int)lasty);
                        line((int)(lastx-50),(int)lasty,(int)lastx,(int)lasty);
                        register j;
                        line(0, (int)lasty, 50, (int)lasty);
                        line((int)(lastx-50),(int)lasty,(int)lastx,(int)lasty);
-                       point((int)(lastx+30), (int)(lasty+10));
-                       sprintf(bufl, "%s\n", np->name + np->name[0] == '_');
+                       move((int)(lastx+30), (int)(lasty+10));
+                       sprintf(bufl, "%s", np->name + (np->name[0] == '_'));
                        label(bufl);
                }
                lastx =+ 500.;
                        label(bufl);
                }
                lastx =+ 500.;