home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 February / PCO2_97.ISO / filesbbs / os2 / sysb091c.arj / SYSBENCH / SRC / PMB_FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-07  |  7.9 KB  |  263 lines

  1.  
  2.  
  3. #define INCL_DOS
  4. #define INCL_DOSFILEMGR
  5. //#define INCL_DOSNLS        /* National Language Support values */
  6. #define INCL_DOSERRORS     /* DOS error values */
  7. #define INCL_DOSSEMAPHORES   /* Semaphore values */
  8. #define INCL_DOSDATETIME     /* Timer support    */
  9. #define INCL_DOSMEMMGR
  10.  
  11. #include <stdio.h>
  12. #include <os2.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. //#include <stdarg.h>
  17.  
  18. #define FILESIZE 4*1024*1024
  19.  
  20. HEV     hevEvent1     = 0;                   /* Event semaphore handle    */
  21. HTIMER  htimerEvent1  = 0;                   /* Timer handle              */
  22. ULONG   ulPostCount   = 0;                   /* Semaphore post count      */
  23. volatile int Timeout  = 0;
  24.  
  25. void _Optlink TimeThread(void*);
  26. double DoFileIO(ULONG, BOOL, BOOL, BOOL);
  27. extern double dtime(void);
  28. extern void logit(char*);
  29.  
  30.  
  31. void _Optlink TimeThread(void* arg)
  32. {
  33. APIRET     rc;
  34. DATETIME   datetime = {0};
  35.     rc = DosCreateEventSem(NULL,           /* Unnamed semaphore            */
  36.                            &hevEvent1,     /* Handle of semaphore returned */
  37.                            DC_SEM_SHARED,  /* Indicate a shared semaphore  */
  38.                            FALSE);         /* Put in RESET state           */
  39.  
  40.     rc = DosStartTimer(15000L,             /* 15 second interval            */
  41.                       (HSEM)hevEvent1,     /* Semaphore to post            */
  42.                       &htimerEvent1);      /* Timer handler (returned)     */
  43.  
  44.     rc = DosWaitEventSem(hevEvent1, SEM_INDEFINITE_WAIT); /* Wait indefinitely for timer or post */
  45.  
  46.     rc = DosResetEventSem(hevEvent1,        /* Reset the semaphore         */
  47.                          &ulPostCount);     /* And get count (should be 1) */
  48.     Timeout = 0;                            /* stop disk i/o loop */
  49.  
  50.     rc = DosStopTimer(htimerEvent1);       /* Stop the timer             */
  51.  
  52.     rc = DosCloseEventSem(hevEvent1);      /* Get rid of semaphore       */
  53.  
  54.     _endthread();
  55.  
  56.  }
  57.  
  58.  
  59. double DoFileIO(ULONG buffersize, BOOL cache, BOOL reading, BOOL random)
  60. {
  61. PVOID dummy;
  62. ULONG iocount     = 0;
  63. HFILE hFilehandle = 0;
  64. ULONG ulAction    = 0;
  65. ULONG ulOpenmode  = 0;
  66. ULONG ulActual    = 0;
  67. ULONG ulPosition  = 0;
  68. APIRET  rrc       = 0;
  69. char filename[15] = "diskte$t.tmp";
  70. char tmp[256];
  71. double starttime, endtime, elaptime;
  72. ULONG  numblks    = FILESIZE/buffersize;
  73. int    i          = 0;
  74.  
  75.  rrc = DosAllocMem(&dummy,             /* place to put data buffer ptr */
  76.                   buffersize,
  77.                   PAG_WRITE | PAG_COMMIT); /* we want to write to it and we want it now */
  78.  
  79.  if (rrc != NO_ERROR)
  80.     {
  81.     sprintf(tmp, "DosAllocMem failed with return code %d", rrc);
  82.     logit(tmp);
  83.     return 0;
  84.     }
  85.  
  86.  memset(dummy, 0, buffersize);      /* set data buffer to zeros */
  87.  
  88.  ulOpenmode = OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE; /* set open mode */
  89.  
  90.  if (!cache)
  91.     {
  92.     ulOpenmode = ulOpenmode | OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_NO_CACHE; /* if not caching disable it */
  93.     }
  94.  
  95.  if (random)
  96.     {
  97.     ulOpenmode = ulOpenmode | OPEN_FLAGS_RANDOM;
  98.     }
  99.  else
  100.     {
  101.     ulOpenmode = ulOpenmode | OPEN_FLAGS_SEQUENTIAL;
  102.     }
  103.  
  104.  sprintf(filename, "dis%05u.tmp", buffersize);  /* choose filename */
  105.  
  106.  if (!reading)  /* if not reading */
  107.     {
  108.     rrc = DosOpen(filename,         /* open file for write */
  109.                  &hFilehandle,
  110.                  &ulAction,
  111.                  FILESIZE,
  112.                  FILE_ARCHIVED | FILE_NORMAL,
  113.                  OPEN_ACTION_CREATE_IF_NEW |
  114.                  OPEN_ACTION_OPEN_IF_EXISTS,
  115.                  ulOpenmode,
  116.                  0L);
  117.     }
  118.  else        /* if reading file */
  119.     {
  120.     rrc = DosOpen(filename,
  121.                  &hFilehandle,
  122.                  &ulAction,
  123.                  FILESIZE,
  124.                  FILE_NORMAL,
  125.                  OPEN_ACTION_FAIL_IF_NEW |
  126.                  OPEN_ACTION_OPEN_IF_EXISTS,
  127.                  ulOpenmode,
  128.                  0L);
  129.     }
  130.  
  131.  if (rrc != NO_ERROR)
  132.     {
  133.     sprintf(tmp, "DosOpen of file %s for %s failed with return code %d", filename,
  134.           reading ? "write" : "read",
  135.           rrc);
  136.     logit(tmp);
  137.     DosFreeMem(dummy); /* free of buffer */
  138.     return 0;
  139.     }
  140.  
  141.  starttime = dtime();   /* log start time of actual i/o loop */
  142.  
  143.  while (Timeout)        /* do until timer pops */
  144.     {
  145.     if (!reading)       /* if writing file */
  146.        {
  147.        if (random)      /* randomly */
  148.           {
  149.           int rnm = rand() / (RAND_MAX/(numblks-1)); /* set record number between 0 and numblks-1 */
  150.           if (rnm > numblks-1)
  151.              {
  152.              logit("Oops, managed to generate a file pointer beyond EOF");
  153.              }
  154.           ulPosition = rnm * buffersize;            /* set byte displacement into file */
  155.           rrc = DosSetFilePtr(hFilehandle, ulPosition, FILE_BEGIN, &ulActual); /* point there */
  156.           if (rrc != NO_ERROR)
  157.              {
  158.              sprintf(tmp, "DosSetFilePtr write random error code %d", rrc);
  159.              }
  160.           }      /* end if random write */
  161.  
  162.        memset(dummy, iocount, sizeof(iocount)); /* make this buffer different from last */
  163.  
  164.        rrc = DosWrite(hFilehandle,              /* write data */
  165.                      dummy,
  166.                      buffersize,
  167.                      &ulActual);
  168.  
  169.        if (rrc == NO_ERROR)
  170.           {
  171.           iocount++;    /* increment count if OK */
  172.           }
  173.        else
  174.           {
  175.           sprintf(tmp, "DosWrite error code %d", rrc);
  176.           logit(tmp);
  177.           DosFreeMem(dummy); /* free of buffer */
  178.           return 0;
  179.           }
  180.  
  181.        if (!(iocount%numblks) && !random) /* if sequential access and eof */
  182.           {
  183.           ulPosition = 0;
  184.           rrc = DosSetFilePtr(hFilehandle, 0, FILE_BEGIN, &ulActual); /* start at beginning again */
  185.           if (rrc != NO_ERROR)
  186.              {
  187.              sprintf(tmp, "DosSetFilePtr write eof error code %d", rrc);
  188.              logit(tmp);
  189.              }
  190.           }
  191.        }
  192.     else   /* if reading from file */
  193.        {
  194.        if (random)
  195.           {
  196.           int rnm = rand() / (RAND_MAX/(numblks-1)); /* set blknumber between start and eof */
  197.           if (rnm > numblks-1)
  198.              {
  199.              logit("Oops, managed to generate a file pointer beyond EOF");
  200.              }
  201.           ulPosition = rnm * buffersize;              /*  set byte displacement */
  202.           rrc =DosSetFilePtr(hFilehandle, ulPosition, FILE_BEGIN, &ulActual);
  203.           if (rrc != NO_ERROR)
  204.              {
  205.              sprintf(tmp, "DosSetFilePtr read random error code %d", rrc);
  206.              logit(tmp);
  207.              }
  208.           }
  209.  
  210.        rrc = DosRead(hFilehandle,      /* read data */
  211.                dummy,
  212.                buffersize,
  213.                &ulActual);
  214.  
  215.        if (rrc != NO_ERROR)
  216.           {
  217.           sprintf(tmp, "DosRead error code %d", rrc);
  218.           logit(tmp);
  219.           DosFreeMem(dummy); /* free of buffer */
  220.           return 0;
  221.           }
  222.        else
  223.           {
  224.           iocount++; /* increment counter if OK */
  225.           }
  226.        if (ulActual < buffersize)  /* if data read < amount requested */
  227.           {
  228.           ulPosition = 0;
  229.           rrc = DosSetFilePtr(hFilehandle, 0, FILE_BEGIN, &ulPosition);  /* reset to start of file */
  230.           if (rrc != NO_ERROR)
  231.              {
  232.              sprintf(tmp, "DosSetFilePtr read eof error code %d", rrc);
  233.              logit(tmp);
  234.              }
  235.           }
  236.        }
  237.     }
  238.  
  239.  endtime = dtime();  /* log time at end of disk i/o loop */
  240.  
  241.  DosClose(hFilehandle); /* close file */
  242.  
  243.  if (reading)
  244.     {
  245.     if (random)
  246.        {
  247.        if (cache)
  248.           {
  249.           DosDelete(filename); /* if last use of file */
  250.           }
  251.        }
  252.     }
  253.  
  254.  DosFreeMem(dummy); /* free of buffer */
  255.  
  256.  elaptime = endtime-starttime;  /* get elapsed time for i/o */
  257.  
  258.  return (((double)buffersize*(double)iocount)/elaptime); /* return kb/sec */
  259.  
  260. }
  261.  
  262.  
  263.