home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / musbus / part04 / mem.c next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  6.2 KB  |  236 lines

  1. /*
  2.  * mem [ -ssize ] [ -niter ]
  3.  *
  4.  *  perform int array accesses
  5.  *  array size (if given) is in Kbytes, default is 8K
  6.  *  iter accesses of each type, default is iter=100000
  7.  *  Configuration options
  8.  *  #ifdef random    random access
  9.  *  #ifndef random    sequential access
  10.  *  #ifdef awk        output on stderr for benchmark script
  11.  *  #ifdef debug    output on stdout for diagnostics
  12.  *
  13.  *  #ifdef BSD4v1    4.1 BSD, ftime() exists
  14.  *  #ifdef BSD4v2    4.2 BSD, gettimeofday() exists
  15.  *  #ifdef SysV        System V, times() returns real time
  16.  *
  17.  *  $Header: mem.c,v 3.5 87/08/06 08:11:10 kenj Exp $
  18.  */
  19.  
  20. #include <sys/types.h>
  21. #include <sys/times.h>
  22. #ifdef BSD4v1
  23. #include <sys/timeb.h>
  24. #endif
  25. #ifdef BSD4v2
  26. #include <sys/time.h>
  27. #include <sys/resource.h>
  28. #endif
  29. #include <stdio.h>
  30.  
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.         int            *tab;
  36.         register char   *p;
  37.         register int    i;
  38.         register int    j;
  39.         register int    mask;
  40.         register int    k;
  41.     long             iter = 100000;
  42.     int         size = 8*1024 / sizeof(int);
  43.     struct tms    tbuffer;
  44.     long        access[2];
  45.     long        ohead[2];
  46. #ifdef BSD4v1
  47.     struct timeb    tbuf;
  48.     int        msec;
  49. #endif
  50. #ifdef BSD4v2
  51.     struct timeval    tbuf;
  52.     struct timezone    tzone;
  53.     struct rusage    rubuf;
  54.     long        rusec, cpusec;
  55. #endif
  56.     char        *prog = argv[0];
  57.         char            *malloc();
  58. #ifdef random
  59.     long        index[1000];
  60. #endif
  61.  
  62.         while (argc-- > 1) {
  63.                 switch (argv[1][1]) {
  64.                 case 's':
  65.                         size = atoi(&argv[1][2])*1024 / sizeof(int);
  66.                         break;
  67.                 case 'n':
  68.                         iter = atoi(&argv[1][2]);
  69.                         break;
  70.                 default:
  71.                         printf("Usage: %s [-ssize] [-niter]\n", prog);
  72.                         exit(1);
  73.                 }
  74.                 argv++;
  75.         }
  76.  
  77.         mask = size / sizeof(int);
  78.  
  79.         if ((tab = (int *)malloc(size*sizeof(int))) == (int *)0) {
  80.                 printf("%s: malloc failed for %d bytes\n", prog, size*sizeof(int));
  81.                 exit(1);
  82.         }
  83.  
  84. #ifdef random
  85.     /* build random array index map */
  86.     for (i = 0; i < 1000; i++)
  87.         index[i] = rand() % mask;
  88. #endif
  89.  
  90.         /* measure overhead for loop */
  91.     srand(1);
  92. #ifdef SysV
  93.         ohead[0] = -times(&tbuffer);
  94.     ohead[1] = -tbuffer.tms_utime - tbuffer.tms_stime;
  95. #else
  96. #ifdef BSD4v1
  97.         times(&tbuffer);
  98.     ftime(&tbuf);
  99.     ohead[0] = -tbuf.time;
  100.     msec = tbuf.millitm;
  101.     ohead[1] = -tbuffer.tms_utime - tbuffer.tms_stime;
  102. #else
  103. #ifdef BSD4v2
  104.     gettimeofday(&tbuf, &tzone);
  105.     getrusage(RUSAGE_SELF, &rubuf);
  106. #ifdef debug
  107.     printf("real %d+%d user %d+%d sys %d+%d\n", tbuf.tv_sec, tbuf.tv_usec,
  108.             rubuf.ru_utime.tv_sec, rubuf.ru_utime.tv_usec,
  109.             rubuf.ru_stime.tv_sec, rubuf.ru_stime.tv_usec);
  110. #endif
  111.     ohead[0] = -tbuf.tv_sec;
  112.     rusec = tbuf.tv_usec;
  113.     ohead[1] = -rubuf.ru_utime.tv_sec - rubuf.ru_stime.tv_sec;
  114.     cpusec = rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec;
  115. #else
  116.     What sort of Unix system if this?
  117. #endif
  118. #endif
  119. #endif
  120.         for (i = 0, k = 0; i < iter; i++) {
  121. #ifdef random
  122.                 j = index[k];
  123.         k++;
  124.         if (k >= 1000) k = 0;
  125. #else
  126.         j = i % mask;
  127. #endif
  128.     }
  129. #ifdef SysV
  130.     ohead[0] += times(&tbuffer);
  131.     ohead[1] += tbuffer.tms_utime + tbuffer.tms_stime;
  132. #else
  133. #ifdef BSD4v1
  134.         times(&tbuffer);
  135.     ftime(&tbuf);
  136.     ohead[0] += tbuf.time;
  137.     ohead[0] = ohead[0]*1000 + tbuf.millitm - msec;
  138.     ohead[1] += tbuffer.tms_utime + tbuffer.tms_stime;
  139. #else
  140. #ifdef BSD4v2
  141.     gettimeofday(&tbuf, &tzone);
  142.     getrusage(RUSAGE_SELF, &rubuf);
  143. #ifdef debug
  144.     printf("real %d+%d user %d+%d sys %d+%d\n", tbuf.tv_sec, tbuf.tv_usec,
  145.             rubuf.ru_utime.tv_sec, rubuf.ru_utime.tv_usec,
  146.             rubuf.ru_stime.tv_sec, rubuf.ru_stime.tv_usec);
  147. #endif
  148.     ohead[0] += tbuf.tv_sec;
  149.     ohead[0] = ohead[0]*1000 + (tbuf.tv_usec - rusec)/1000;
  150.     ohead[1] += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
  151.     ohead[1] = ohead[1]*60 + 60*(rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec - cpusec)/1000000;
  152. #endif
  153. #endif
  154. #endif
  155. #ifdef debug
  156.         printf("overhead real: %d msec cpu: %.1f sec\n", ohead[0], ((float)ohead[1])/60.);
  157. #endif
  158.         
  159.         /* perform accesses */
  160.     srand(1);
  161. #ifdef SysV
  162.         access[0] = -times(&tbuffer);
  163.     access[1] = -tbuffer.tms_utime - tbuffer.tms_stime;
  164. #else
  165. #ifdef BSD4v1
  166.         times(&tbuffer);
  167.     ftime(&tbuf);
  168.     access[0] = -tbuf.time;
  169.     msec = tbuf.millitm;
  170.     access[1] = -tbuffer.tms_utime - tbuffer.tms_stime;
  171. #else
  172. #ifdef BSD4v2
  173.     gettimeofday(&tbuf, &tzone);
  174.     getrusage(RUSAGE_SELF, &rubuf);
  175. #ifdef debug
  176.     printf("real %d+%d user %d+%d sys %d+%d\n", tbuf.tv_sec, tbuf.tv_usec,
  177.             rubuf.ru_utime.tv_sec, rubuf.ru_utime.tv_usec,
  178.             rubuf.ru_stime.tv_sec, rubuf.ru_stime.tv_usec);
  179. #endif
  180.     access[0] = -tbuf.tv_sec;
  181.     rusec = tbuf.tv_usec;
  182.     access[1] = -rubuf.ru_utime.tv_sec - rubuf.ru_stime.tv_sec;
  183.     cpusec = rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec;
  184. #endif
  185. #endif
  186. #endif
  187.         for (i = 0, k = 0; i < iter; i++) {
  188. #ifdef random
  189.                 j = tab[index[k]];
  190.         k++;
  191.         if (k >= 1000) k = 0;
  192. #else
  193.         j = tab[i % mask];
  194. #endif
  195.     }
  196. #ifdef SysV
  197.     access[0] += times(&tbuffer);
  198.     access[1] += tbuffer.tms_utime + tbuffer.tms_stime;
  199. #else
  200. #ifdef BSD4v1
  201.         times(&tbuffer);
  202.     ftime(&tbuf);
  203.     access[0] += tbuf.time;
  204.     access[0] = access[0]*1000 + tbuf.millitm - msec;
  205.     access[1] += tbuffer.tms_utime + tbuffer.tms_stime;
  206. #else
  207. #ifdef BSD4v2
  208.     gettimeofday(&tbuf, &tzone);
  209.     getrusage(RUSAGE_SELF, &rubuf);
  210. #ifdef debug
  211.     printf("real %d+%d user %d+%d sys %d+%d\n", tbuf.tv_sec, tbuf.tv_usec,
  212.             rubuf.ru_utime.tv_sec, rubuf.ru_utime.tv_usec,
  213.             rubuf.ru_stime.tv_sec, rubuf.ru_stime.tv_usec);
  214. #endif
  215.     access[0] += tbuf.tv_sec;
  216.     access[0] = access[0]*1000 + (tbuf.tv_usec - rusec)/1000;
  217.     access[1] += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
  218.     access[1] = access[1]*60 + 60*(rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec - cpusec)/1000000;
  219. #endif
  220. #endif
  221. #endif
  222. #ifdef awk
  223.     fprintf(stderr, "%d %d %.3f\n", iter, access[0] - ohead[0],
  224.             ((float)(access[1] - ohead[1]))/60.);
  225. #endif
  226. #ifdef debug
  227.         printf("total real: %d msec cpu: %.1f sec\n", access[0], ((float)access[1])/60.);
  228.     printf("access real: %d msec cpu: %.1f sec\n", access[0] - ohead[0],
  229.             ((float)(access[1] - ohead[1]))/60.);
  230.         printf("%d Kbyte array: %.1f accesses per second\n", size*sizeof(int)/1024,
  231.                 iter*1000.0/((float)(access[0] - ohead[0])));
  232. #endif
  233.  
  234.     exit(0);
  235. }
  236.