home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / swaptest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  3.0 KB  |  139 lines

  1. /* swaptest.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <io.h>
  6.  
  7. #define FALSE 0
  8. #define TRUE  1
  9.  
  10. static int pages;
  11. static long *count;
  12. static char *mem;
  13.  
  14. static void usage (void)
  15. {
  16.   fprintf (stderr, "Usage: swaptest <pages>\n");
  17.   exit (1);
  18. }
  19.  
  20.  
  21. static void test (int i, int flag)
  22. {
  23.   long *pm;
  24.  
  25.   pm = (long *)(mem + 4096 * i);
  26.   if (count[i] == 0)
  27.     {
  28.       if (flag)
  29.         {
  30.           if (*pm != 0)
  31.             {
  32.               fprintf (stderr, "Page contents not zero\n");
  33.               exit (2);
  34.             }
  35.           pm[0] = i;
  36.           pm[1] = 1;
  37.           count[i] = 1;
  38.         }
  39.     }
  40.   else
  41.     {
  42.       if (pm[0] != i)
  43.         {
  44.           fprintf (stderr, "Pages confused\n");
  45.           exit (2);
  46.         }
  47.       if (pm[1] != count[i])
  48.         {
  49.           fprintf (stderr, "Dirty page not restored from swap file\n");
  50.           exit (2);
  51.         }
  52.       if (flag && rand () < RAND_MAX / 3)
  53.         pm[1] = ++count[i];
  54.     }
  55. }
  56.  
  57.  
  58. int main (int argc, char *argv[])
  59. {
  60.   long l, iter, w;
  61.   char *p;
  62.   int c, i, d, u, m;
  63.   int interactive;
  64.  
  65.   interactive = _isterm (fileno (stdout));
  66.   if (argc != 2)
  67.     usage ();
  68.   errno = 0;
  69.   l = strtol (argv[1], &p, 0);
  70.   if (errno != 0 || l <= 0 || l >= 0x7fff || *p != 0)
  71.     usage ();
  72.   pages = (int)l;
  73.   count = malloc (pages * sizeof (*count));
  74.   if (count == NULL)
  75.     {
  76.       fprintf (stderr, "malloc: Out of memory\n");
  77.       exit (2);
  78.     }
  79.   for (i = 0; i < pages; ++i)
  80.     count[i] = 0;
  81.   d = pages * (RAND_MAX / pages);
  82.   fprintf (stdout, "This is swaptest\n"); fflush (stdout);
  83.   fprintf (stderr, "\n"); fflush (stderr);
  84.   mem = sbrk (pages * 4096);
  85.   if ((int)mem == -1)
  86.     {
  87.       perror ("sbrk");
  88.       exit (2);
  89.     }
  90.   m = 0;
  91.   for (iter = 0; ; ++iter)
  92.     {
  93.       c = _read_kbd (0, 0, 1);
  94.       switch (c)
  95.         {
  96.         case 0x1b:
  97.           return (0);
  98.         case ' ':
  99.           u = 0; w = 0;
  100.           for (i = 0; i < pages; ++i)
  101.             {
  102.               w += count[i];
  103.               if (count[i] == m)
  104.                 ++u;
  105.             }
  106.           printf ("%ld iterations, count=%d for %d pages, average count=%g\n",
  107.                   iter, m, u, (double)w / (double)pages);
  108.           if (u == 0)
  109.             ++m;
  110.           break;
  111.         case 'b':
  112.           __asm__ ("int $3");
  113.           break;
  114.         case 't':
  115.           printf ("Testing..."); fflush (stdout);
  116.           for (i = 0; i < pages; ++i)
  117.             {
  118.               test (i, FALSE);
  119.               if (interactive && (i % 10 == 0))
  120.                 {
  121.                   printf ("\rTesting (%d)...", i); fflush (stdout);
  122.                 }
  123.             }
  124.           if (interactive)
  125.             printf ("\rTesting...OK     \n");
  126.           else
  127.             printf ("OK\n");
  128.           fflush (stdout);
  129.           break;
  130.         }
  131.       do
  132.         {
  133.           i = rand ();
  134.         } while (i >= d);
  135.       test (i % pages, TRUE);
  136.     }
  137.   return (0);
  138. }
  139.