home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / SWPTST.ZIP / SWAPTST1.C < prev    next >
Text File  |  1993-02-15  |  6KB  |  150 lines

  1. /*
  2.     Swap Test 1 repeats the following in a loop:
  3.  
  4.     1.  Allocate a 256KB block of memory.
  5.     2.  Do an update in every 4KB page in the block.
  6.     3.  Measure the time this takes.
  7.     4.  Query the available memory.
  8.     5.  Query the size of SWAPPER.DAT.
  9.     6.  Display values for this pass thru the loop.
  10.     7.  Break out of the loop if the time for this pass thru the loop exceeds
  11.         a limiting value, or if the swap file grows.
  12.  
  13.     Parameters:
  14.  
  15.         cTimeLim:   If one pass through the loop exceeds this time limit (ms),
  16.                     the loop breaks out.  Default is 200 ms (safe on most
  17.                     systems??).  (You can also stop it with Ctrl-C.)
  18.  
  19.         szSwapper:  The path to the swap file.  Defaults to
  20.                     C:\OS2\SYSTEM\SWAPPER.DAT.
  21.  
  22.     There's little or no error checking on the input parameter values.
  23.  
  24.     Output is via printf() to stdout.  It's useful to redirect it to a file for
  25.     later review.
  26.  
  27.     This program is in the public domain.  Anyone using it assumes all
  28.     responsibility for consequences; the author will not be liable for
  29.     anything.  (Don't run it on a system where the response time of other
  30.     applications is important!!)
  31.  
  32.     Author: Bill Lee, Edmonton, Alberta, Canada; CompuServe 70441,2372.
  33. */
  34.  
  35. #define INCL_DOSFILEMGR         // File Manager values
  36. #define INCL_DOSMEMMGR          // Memory Manager values
  37. #define INCL_DOSMISC            // QSV_MAX_.... values
  38. #include <os2.h>
  39. #include <stdio.h>
  40.  
  41. USHORT APIENTRY16 Dos16MemAvail( PULONG );
  42.  
  43. #define IFERR( f ) if ( lRC = (LONG) ( f ) ) \
  44.     { printf( "Stopping at source line %d due to API error code %ld.\n", \
  45.     __LINE__, lRC ); exit( lRC ); }
  46.  
  47. #define BLKMAX 2048             // BLKMAX*cbBlk should be > max possible RAM
  48. PCHAR apBlk[BLKMAX];            // array of ptrs to memory blocks, [0] not used
  49.  
  50. int main( USHORT argc, PCHAR argv[] )
  51. {
  52.  
  53.     ULONG lParm = 0;            // workarea for input parameter
  54.     ULONG cbTotPhysMem;         // total physical memory via DosQuerySysInfo()
  55.     ULONG cMemAvail;            // available memory via DosMemAvail()
  56.     ULONG cTime1, cTime2;       // ms times via DosQuerySysInfo()
  57.     ULONG cTimeLim = 200;       // time limit in ms to get/update memory block
  58.     ULONG cBlk;                 // count of memory blocks
  59.     ULONG cbBlk = 256*1024;     // Size in bytes of each memory block
  60.     ULONG flAllocation = PAG_WRITE | PAG_READ | PAG_COMMIT;
  61.     PUCHAR pszSwapper = "C:\\OS2\\SYSTEM\\SWAPPER.DAT"; // default path
  62.     ULONG ulSwapperPathInfo = FIL_STANDARD; // for DosQueryPathInfo()
  63.     FILESTATUS3 fs3SwapperInfoBuf; // for DosQueryPathInfo() of SWAPPER.DAT
  64.     ULONG cbFileAlloc1;         // initial size of SWAPPER.DAT
  65.     ULONG i;                    // index
  66.     APIRET lRC;                 // return code
  67.  
  68.     //  If there are input argument(s), convert and put in variables.
  69.     if ( argc > 1 ) {
  70.         lParm = atol( argv[1] );    // convert
  71.         if ( lParm )                // copy parm to variable if result non-0.
  72.             cTimeLim = lParm;
  73.         if ( argc > 2 )
  74.             pszSwapper = argv[2];   // save pointer
  75.     }
  76.  
  77.     printf( "\nSwap Test 1:\n\n" );
  78.  
  79.     //  Get total physical memeory.
  80.     IFERR( DosQuerySysInfo( QSV_TOTPHYSMEM, QSV_TOTPHYSMEM,
  81.                                        &cbTotPhysMem, sizeof(cbTotPhysMem) ) );
  82.     printf( "Total physical memory is %ld bytes (%ldMB + %ldKB).\n\n",
  83.             cbTotPhysMem, cbTotPhysMem/(1024*1024),
  84.             cbTotPhysMem%(1024*1024)/1024 );
  85.  
  86.     printf( "Memory will be allocated/updated in %ldKB blocks.\n", 
  87.             cbBlk/1024 );
  88.  
  89.     printf( "Time limit to allocate/update one block is %ld ms.\n\n",
  90.             cTimeLim );
  91.  
  92.     printf( "Swap file path is \"%s\".\n\n", pszSwapper );
  93.  
  94.     //  Get initial size of SWAPPER.DAT and save it.
  95.     IFERR( DosQueryPathInfo( pszSwapper, FIL_STANDARD, &fs3SwapperInfoBuf,
  96.                                                  sizeof(fs3SwapperInfoBuf) ) );
  97.     cbFileAlloc1 = fs3SwapperInfoBuf.cbFileAlloc;       // save initial size
  98.  
  99.     IFERR( Dos16MemAvail( &cMemAvail ) );               // initial value
  100.  
  101.     printf( "  --MemAlloc--  MemAvail  time  Swap\n" ); // header 1
  102.     printf( "   N  MB +  KB  MB +  KB  (ms)  (KB)\n" ); // header 2
  103.  
  104.     printf( "%4lu%4lu +%4lu%4lu +%4lu%6lu%6lu\n",       // initial conditions
  105.             0, 0, 0,
  106.             cMemAvail/(1024*1024), ( cMemAvail%(1024*1024) )/1024,
  107.             0, fs3SwapperInfoBuf.cbFileAlloc/1024 );
  108.  
  109.     for ( cBlk = 1; cBlk <= BLKMAX; ++cBlk ) {
  110.  
  111.         //  Get millisecond count before memory allocation and update.
  112.         IFERR( DosQuerySysInfo( QSV_MS_COUNT, QSV_MS_COUNT,
  113.                                                  &cTime1, sizeof( cTime1 ) ) );
  114.  
  115.         IFERR( DosAllocMem( (PPVOID) &apBlk[cBlk], cbBlk, flAllocation ) );
  116.  
  117.         for ( i = 0; i < cbBlk; i += 4096 )     // go to every 4K page
  118.             *(apBlk[cBlk]+i) = 'X';             // and update a byte
  119.  
  120.         //  Get millisecond count after.
  121.         IFERR( DosQuerySysInfo( QSV_MS_COUNT, QSV_MS_COUNT,
  122.                                                  &cTime2, sizeof( cTime2 ) ) );
  123.  
  124.         IFERR( Dos16MemAvail( &cMemAvail ) );   // find out memory left
  125.  
  126.         //  Find out how big SWAPPER.DAT is.
  127.         IFERR( DosQueryPathInfo( pszSwapper, FIL_STANDARD, &fs3SwapperInfoBuf,
  128.                                                  sizeof(fs3SwapperInfoBuf) ) );
  129.  
  130.         printf( "%4lu%4lu +%4lu%4lu +%4lu%6lu%6lu\n",
  131.                 cBlk, cBlk*cbBlk/(1024*1024), ( cBlk*cbBlk%(1024*1024) )/1024,
  132.                 cMemAvail/(1024*1024), ( cMemAvail%(1024*1024) )/1024,
  133.                 cTime2 - cTime1, fs3SwapperInfoBuf.cbFileAlloc/1024 );
  134.  
  135.         if ( cTime2 - cTime1 > cTimeLim ) {
  136.             printf( "\nTime for 1 memory block (%ld ms) exceeded limit "
  137.                     "(%ld ms), stopping.\n", cTime2 - cTime1, cTimeLim );
  138.             break;
  139.         }
  140.  
  141.         if ( fs3SwapperInfoBuf.cbFileAlloc > cbFileAlloc1 ) {
  142.             printf( "\nThe swap file grew from %ldKB to %ldKB, stopping.\n",
  143.                     cbFileAlloc1/1024, fs3SwapperInfoBuf.cbFileAlloc/1024 );
  144.             break;
  145.         }
  146.  
  147.     }   //  for ( cBlk = 0; ... )
  148.  
  149. }   //  main()
  150.