home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / sprof / report.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.9 KB  |  149 lines

  1. /*-------------------------------------------------------------------*/
  2. /* Copyright (c) 1993 by SAS Institute Inc., Cary NC                 */
  3. /* All Rights Reserved                                               */
  4. /*                                                                   */
  5. /* SUPPORT:    walker - Doug Walker                                  */
  6. /* HISTORY:    action                                   date   name  */
  7. /*   Dont increment count for functions still on stack 31Aug93  hlc  */
  8. /*-------------------------------------------------------------------*/
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <exec/ports.h>
  14. #include <dos/dosextens.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include "sprofpriv.h"
  18.  
  19. extern char nullid[];
  20.  
  21. extern char *process_status;
  22.  
  23. static int GetNumCols(int num)
  24. {
  25.    int i;
  26.  
  27.    if(num <= 0)
  28.       i = 1, num = -num;
  29.    else
  30.       i = 0;
  31.  
  32.    while(num)
  33.    {
  34.       i++;
  35.       num /= 10;
  36.    }
  37.    return(i);
  38. }
  39.  
  40. static int GPByTime_array(struct GPInfo **a, struct GPInfo **b)
  41. {
  42.    return((*a)->time < (*b)->time ? 1 : (*a)->time > (*b)->time ?  -1 : 0);
  43. }
  44.  
  45. void Report(sptime now)
  46. {
  47.    int i, j;
  48.    int maxlen = 0;
  49.    int maxcount = 0;
  50.    long maxtot = 0;
  51.    long maxtime;
  52.    double sumtime;
  53.    double inctime;
  54.    double cumtime;
  55.    sptime elapsed, subelapsed;
  56.    struct GPInfo **rptGPInfo;
  57.    struct GPInfo *gpi;
  58.    int rptGPCur, rptGPMax;
  59.  
  60.    rptGPCur = GPCur;
  61.    rptGPMax = GPMax;
  62.  
  63.    if(GPMax)
  64.    {
  65.       if((rptGPInfo = malloc(sizeof(struct GPInfo *)*rptGPMax)) == NULL)
  66.       {
  67.          printf("Out of memory, cannot produce report!\n");
  68.          return;
  69.       }
  70.  
  71.       memcpy(rptGPInfo, GPInfo, sizeof(struct GPInfo *)*rptGPCur);
  72.    }
  73.    else
  74.       rptGPInfo = NULL;
  75.  
  76.    /* Accumulate data for functions that are still on the stack */
  77.    subelapsed = 0;
  78.    for(i=spcur-1; i>=0; i--)
  79.    {
  80.       elapsed = now - spdat[i].clk;
  81.       if(spdat[i].id && spdat[i].id != nullid && 
  82.            (gpi = FindGPI(&rptGPInfo, spdat[i].id,
  83.                                        &rptGPCur, &rptGPMax)))
  84.       {
  85.  
  86.          gpi->time += (elapsed - spdat[i].subrs - subelapsed);
  87.          gpi->tottime += elapsed;
  88.       }
  89.       subelapsed = elapsed;
  90.    }
  91.  
  92.    printf("\nSPROF Report\nStatus: %s\n", process_status);
  93.    if(!rptGPCur) return;
  94.  
  95.    qsort(rptGPInfo, rptGPCur, sizeof(struct GPInfo *), 
  96.          (int (*)(void *, void *))GPByTime_array);
  97.  
  98.    sumtime = cumtime = 0.0;
  99.    for(i=0; i<rptGPCur; i++)
  100.    {
  101.       if(!rptGPInfo[i]->count) continue;
  102.  
  103.       if((j=strlen(rptGPInfo[i]->name)) > maxlen)
  104.          maxlen = j;
  105.       if(rptGPInfo[i]->count > maxcount)
  106.          maxcount = rptGPInfo[i]->count;
  107.       if(rptGPInfo[i]->tottime > maxtot)
  108.          maxtot = rptGPInfo[i]->tottime;
  109.       sumtime += rptGPInfo[i]->time;
  110.    }
  111.  
  112.    /* Determine number of columns in each field */
  113.    maxcount = GetNumCols(maxcount);
  114.    maxtime  = GetNumCols((long)sumtime);
  115.    maxtot   = GetNumCols(maxtot);
  116.  
  117.    /* Make sure the column headers fit */
  118.    if(maxlen < 4)   maxlen = 4;
  119.    if(maxcount < 5) maxcount = 5;
  120.    if(maxtime < 5)  maxtime = 5;
  121.    if(maxtot < 5)   maxtot = 5;
  122.  
  123.    printf("%-*s %*.*s %*.*s %*.*s Pct\n", 
  124.                maxlen,  "Name", 
  125.       maxcount,maxcount,"Count",
  126.       maxtot, maxtot,   "ITime",
  127.       maxtime, maxtime, "ETime");
  128.    for(i=0; i<rptGPCur; i++)
  129.    {
  130.       if(!rptGPInfo[i]->count) continue;
  131.  
  132.       inctime = rptGPInfo[i]->time*100.0/sumtime;
  133.       cumtime += inctime;;
  134.       printf("%-*s %*ld %*ld %*ld %5.1f\n", 
  135.                  maxlen,   rptGPInfo[i]->name,
  136.                  maxcount, rptGPInfo[i]->count, 
  137.                  maxtot,   rptGPInfo[i]->tottime,
  138.                  maxtime,  rptGPInfo[i]->time,
  139.                  inctime);
  140.    }
  141.    printf("%*s %*s %*s %*.*s -----\n",
  142.       maxlen, "", maxcount, "", maxtot, "", maxtime, maxtime, "---------------");
  143.    printf("%*s %*s %*s %*lu %4.1f\n", 
  144.       maxlen, "", maxcount, "", maxtot, "", maxtime, 
  145.       (unsigned long)sumtime, cumtime);
  146.  
  147.    free(rptGPInfo);
  148. }
  149.