home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trash / part01 / histogram.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  912 b   |  57 lines

  1. #include    <stdio.h>
  2.  
  3. #define    PAGE_SIZE    4096
  4.  
  5. #define    addr_to_i(a)    (((a) - histo_addr) / PAGE_SIZE)
  6. #define    i_to_addr(i)    (((i) * PAGE_SIZE) + histo_addr)
  7.  
  8. extern char        *calloc();
  9. extern void        couldnot();
  10.  
  11. extern FILE        *outfp;
  12. extern int        Hflag;
  13.  
  14. static int        *histo_buf;
  15. static int        histo_length;
  16. static unsigned long    histo_addr;
  17.  
  18. int
  19. histo_init(first, length)
  20. unsigned long    first;
  21. int        length;
  22. {
  23.     histo_length = (length + PAGE_SIZE - 1) / PAGE_SIZE;
  24.  
  25.     if ((histo_buf = (int *)calloc(histo_length, sizeof(int))) == (int *)0)
  26.     {
  27.         couldnot("allocate memory for histogram buffer of %d entries", histo_length);
  28.         return -1;
  29.     }
  30.  
  31.     histo_addr = first;
  32.  
  33.     return 0;
  34. }
  35.  
  36. void
  37. histo_log(addr)
  38. unsigned long    addr;
  39. {
  40.     histo_buf[addr_to_i(addr)]++;
  41. }
  42.  
  43. void
  44. histo_dump()
  45. {
  46.     int    i;
  47.     int    p;
  48.  
  49.     p = getpid();
  50.  
  51.     if (Hflag)
  52.     {
  53.         for (i = 0; i < histo_length; i++)
  54.             fprintf(outfp, "%d:\t%d\t%d\n", p, i_to_addr(i), histo_buf[i]);
  55.     }
  56. }
  57.