home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ASBENC.ZIP / ASBENCH.C next >
Text File  |  1992-11-23  |  11KB  |  437 lines

  1. //
  2. //    ASBENCH : ASPI SCSI benchmark test for hard-disk / MO / CD-ROM
  3. //        copyright(c) by TsuruZoh Tachibanaya
  4. //
  5. //        V0.1    Oct.30,1992    ;first release
  6. //        V0.2    Nov.23,1992    ;add bar-graph display
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <alloc.h>
  12. #include <time.h>
  13. #include "aspi.h"
  14.  
  15. #define TESTTIME 5    // measurement length is 5 sec.
  16.  
  17. static int commandRank[ 2 ] = { 25, 35 };
  18. static int seekRank[ 2 ] = { 100, 400 };
  19. static int readRank[ 2 ] = { 20000, 12000 };
  20.  
  21. int main( void );
  22. int GetTarget( void );
  23. int GetHostNo( void );
  24. int GetInquiry( int, unsigned char[7][7][40] );
  25. void DispInquiry( int, int, unsigned char * );
  26. int CommandBench( int, int, int );
  27. int SeekBench( int, int, unsigned long, int );
  28. int ReadBench( int, int, unsigned char far *, int, unsigned long, int );
  29.  
  30.  
  31. int main()
  32. {
  33.     int hostNo, targetId, sectorSize;
  34.     unsigned char far *dataBuffer;
  35.     unsigned char tempBuf[ 80 ];
  36.     unsigned long lbaMax;
  37.  
  38.     printf( "\nASPI SCSI benchmark test V0.2 \n" );
  39.     printf( " copyright(c) by Tsuru-Zoh, Nov.23,1992\n\n" );
  40.     
  41.     if( AspiInit() ){
  42.         return( -1 );
  43.     }
  44.     
  45.     if( ( targetId = GetTarget() ) == -1 ){
  46.         return( 0 );
  47.     }
  48.     hostNo = targetId >> 4;
  49.     targetId = targetId & 0x07;
  50.  
  51.     if( ReadCapacity( hostNo, targetId, tempBuf ) ){
  52.         printf( "Can't get sector size.\n" );
  53.         return( -1L );
  54.     }
  55.     sectorSize = tempBuf[ 6 ] * 256 + tempBuf[ 7 ];
  56.     lbaMax = tempBuf[ 1 ] * 65536L + tempBuf[ 2 ] * 256L + tempBuf[ 3 ];
  57.     printf( "  %d Bytes per sector, capacity is %ld MBytes.\n",
  58.             sectorSize, lbaMax * sectorSize / 1024L / 1024L );
  59.     printf( "------------------------+-------------+-------+-------+-------+-------+-------+\n" );
  60.     printf( "        Test mode       :    result   |  Poor |   OK  |  Good | Great | Superb|\n" );
  61.  
  62.  
  63.     if( CommandBench( hostNo, targetId, TESTTIME ) ){
  64.         /* command error occured */
  65.         return( -1 );
  66.     }
  67.  
  68.  
  69.     if( SeekBench( hostNo, targetId, lbaMax, TESTTIME ) ){
  70.         /* seek error occured */
  71.         return( -1 );
  72.     }
  73.  
  74.     if( ( dataBuffer = farmalloc( 65536L ) ) == NULL ){
  75.         printf( "Error : Can't allocate buffer.\n" );
  76.         return( -1 );
  77.     }
  78.  
  79.     if( ReadBench( hostNo, targetId, dataBuffer, sectorSize, lbaMax, TESTTIME )  ){
  80.         /* read error occured */
  81.         farfree( dataBuffer );
  82.         return( -1 );
  83.     }
  84.     printf( "------------------------+-------------+-------+-------+-------+-------+-------+\n" );
  85.  
  86.     farfree( dataBuffer );
  87.     return( 0 );
  88. }
  89.  
  90.  
  91. //
  92. //    get target device
  93. //         parameter:
  94. //            non
  95. //        return :
  96. //            -1    if error occured
  97. //            else bit 7..4 : host adapter no.
  98. //                 bit 3..0 : scsi id
  99. //
  100. int GetTarget()
  101. {
  102.     int hostNo, host, targetId, targetType, selPtr;
  103.     unsigned char inqBuf[ 7 ][ 7 ][ 40 ], buf[ 80 ];
  104.     struct{
  105.         int host;
  106.         int id;
  107.     }selNo[ 49 ];
  108.     
  109.     if( ( hostNo = HostInquiry( 0, buf ) ) == -1 ){
  110.         return( -1 );
  111.     }
  112.     if( GetInquiry( hostNo, inqBuf ) ){
  113.         return( -1 );
  114.     }
  115.     selPtr = 0;
  116.     for( host = 0; host < hostNo; host++ ){
  117.         for( targetId = 0; targetId < 7; targetId++ ){
  118.             targetType = inqBuf[ host ][ targetId ][ 0 ] & 0x1f;
  119.             if( targetType == 0 || targetType == 5 || targetType == 7 ){
  120.                 /* HDD or MO or CD-ROM */
  121.                 selNo[ selPtr ].host = host;
  122.                 selNo[ selPtr ].id = targetId;
  123.                 printf( "Target No.%d : ", selPtr );
  124.                 DispInquiry( host, targetId, inqBuf[ host ][ targetId ] );
  125.                 selPtr++;
  126.             }
  127.         }
  128.     }
  129.     do{
  130.         printf( "\nEnter target number ( 0 to %d ) : ", selPtr - 1 );
  131.         gets( buf );
  132.         targetId = atoi( buf );
  133.         if( targetId == -1 ){
  134.             return( -1 );
  135.         }
  136.     }while( ( targetId < 0 ) || ( targetId >= selPtr ) );
  137.     host = selNo[ targetId ].host;
  138.     targetId = selNo[ targetId ].id;
  139.  
  140.     HostInquiry( host, buf );
  141.     printf( "\n\n  Host Adapter is " );
  142.  
  143.     for( selPtr = 16; selPtr < 32; selPtr++ ){
  144.         putchar( buf[ selPtr ] );
  145.     }
  146.  
  147.     printf( "\n  Target device is " );
  148.     DispInquiry( host, targetId, inqBuf[ host ][ targetId ] );
  149.     
  150.     return( host * 16 + targetId );
  151. }
  152.  
  153.  
  154. //
  155. //    scsi command benchmark test
  156. //        parameter :
  157. //            int hostNo                : host adapter no.
  158. //            int targetId            : target scsi id
  159. //            int benchTime            : benchmark measurement time length
  160. //
  161. int CommandBench( hostNo, targetId, benchTime )
  162.     int hostNo, targetId, benchTime;
  163. {
  164.     long commandCount;
  165.     time_t stt;
  166.     int benchMode, rankCount, i;
  167.     static char *commandModeStr[] = {
  168.         " Test Unit Ready command",
  169.         " No Motion Seek command "
  170.     };
  171.     
  172.     printf( "------------------------+-------------+-------+-------+-------+-------+-------+\n" );
  173.     
  174.     for( benchMode = 0; benchMode < 2; benchMode++ ){
  175.         
  176.         printf( "%s|", commandModeStr[ benchMode ] );
  177.         
  178.         commandCount = 0L;
  179.         
  180.         if( ScsiSeek( hostNo, targetId, 0L ) ){
  181.             printf( "Seek error occured.\n" );\
  182.             return( -1 );
  183.         }
  184.         stt = time( NULL );
  185.         while( stt == time( NULL ) ){
  186.             /* wait until time change */
  187.         }
  188.         stt += ( benchTime + 1 );
  189.         while( time( NULL ) < stt ){
  190.             if( benchMode == 0 ){
  191.                 for( i = 0; i < 100; i++ ){
  192.                     if( TestUnitReady( hostNo, targetId ) ){
  193.                         return( -1 );
  194.                     }
  195.                 }
  196.             }else{
  197.                 for( i = 0; i < 100; i++ ){
  198.                     if( ScsiSeek( hostNo, targetId, 0L ) ){
  199.                         printf( "Seek error occured.\n" );\
  200.                         return( -1 );
  201.                     }
  202.                 }
  203.             }
  204.             commandCount++;
  205.         }
  206.         commandCount = (long)benchTime * 100L / commandCount;
  207.         printf( "  %3ld.%1ld[ms]  :",
  208.                      commandCount / 10L, commandCount % 10L );
  209.         rankCount = ( commandRank[ benchMode ] - commandCount ) * 40 / commandRank[ benchMode ] + 1;
  210.         for( i = 0; i < rankCount; i++ ){
  211.             putchar( '*' );
  212.         }
  213.         putchar( '\n' );
  214.     }
  215.     return( 0 );
  216. }
  217.  
  218.  
  219. //
  220. //    seek benchmark test
  221. //        parameter :
  222. //            int hostNo                : host adapter no.
  223. //            int targetId            : target scsi id
  224. //            unsigned long lbaMax    : maximum block number of target device
  225. //            int benchTime            : benchmark measurement time length
  226. //
  227. int SeekBench( hostNo, targetId, lbaMax, benchTime )
  228.     int hostNo, targetId, benchTime;
  229.     unsigned long lbaMax;
  230. {
  231.     long logicalAddr, randomScale, seekCount;
  232.     time_t stt;
  233.     int benchMode, rankCount, i;
  234.     static char *seekModeStr[] = {
  235.         " Sequential Seek command",
  236.         " Random Seek command    "
  237.     };
  238.     
  239.     
  240.     randomScale = lbaMax / 32767L;
  241.     if( randomScale == 0 ){
  242.         randomScale = 1;
  243.     }
  244.     
  245.     printf( "------------------------+-------------+-------+-------+-------+-------+-------+\n" );
  246.     for( benchMode = 0; benchMode < 2; benchMode++ ){
  247.         
  248.         printf( "%s|", seekModeStr[ benchMode ] );
  249.         
  250.         logicalAddr = 0L;
  251.         seekCount = 0L;
  252.         
  253.         stt = time( NULL );
  254.         while( stt == time( NULL ) ){
  255.             /* wait until time change */
  256.         }
  257.         stt += ( benchTime + 1 );
  258.         while( time( NULL ) < stt ){
  259.             if( ScsiSeek( hostNo, targetId, logicalAddr ) ){
  260.                 printf( "Seek error occured.\n" );\
  261.                 return( -1 );
  262.             }
  263.             if( benchMode == 0 ){
  264.                 logicalAddr += 32L;
  265.                 if( logicalAddr > lbaMax ){
  266.                     logicalAddr = 0L;
  267.                 }
  268.             }else{
  269.                 do{
  270.                     logicalAddr = random( 32768 ) * randomScale;
  271.                 }while( logicalAddr > lbaMax );
  272.             }
  273.             seekCount++;
  274.         }
  275.         seekCount = (long)benchTime * 10000L / seekCount;
  276.         printf( "  %3ld.%1ld[ms]  :",
  277.                      seekCount / 10L, seekCount % 10L );
  278.         rankCount = ( seekRank[ benchMode ] - seekCount ) * 40 / seekRank[ benchMode ] + 1;
  279.         for( i = 0; i < rankCount; i++ ){
  280.             putchar( '*' );
  281.         }
  282.         putchar( '\n' );
  283.     }
  284.     return( 0 );
  285. }
  286.  
  287.  
  288. //
  289. //    data read benchmark test
  290. //        parameter :
  291. //            int hostNo                        : host adapter no.
  292. //            int targetId                    : target scsi id
  293. //            unsigned char far *dataBuffer    : data buffer ptr. (65536 bytes)
  294. //            int sectorSize                    : logical block size of target
  295. //            unsigned long lbaMax            : maximum block number of target
  296. //            int benchTime                    : benchmark measurement time length
  297. //
  298. int ReadBench( hostNo, targetId, dataBuffer, sectorSize, lbaMax, benchTime )
  299.     int hostNo, targetId, sectorSize, benchTime;
  300.     unsigned char far *dataBuffer;
  301.     unsigned long lbaMax;
  302. {
  303.     long logicalAddr, randomScale, readCount;
  304.     int benchMode, blockCount, maxBlockCount, rankCount, i;
  305.     time_t stt;
  306.     static char *readModeStr[] = {
  307.         " Seq. Read",
  308.         " Rnd. Read"
  309.     };
  310.     
  311.     randomScale = ( lbaMax - 65536L / sectorSize ) / 32767L;
  312.     if( randomScale == 0 ){
  313.         randomScale = 1;
  314.     }
  315.     maxBlockCount = 65536L / sectorSize;
  316.  
  317.     for( benchMode = 0; benchMode < 2; benchMode++ ){
  318.         printf( "------------------------+-------------+-------+-------+-------+-------+-------+\n" );
  319.  
  320.         for( blockCount = 1; ; ){
  321.             logicalAddr = 0L;
  322.             readCount = 0L;
  323.  
  324.             printf( "%s  %5ldB/read |",
  325.                 readModeStr[ benchMode ], (long)blockCount * sectorSize );
  326.             stt = time( NULL );
  327.             while( stt == time( NULL ) ){
  328.                 /* wait until time change */
  329.             }
  330.             stt += ( benchTime + 1 );
  331.             while( time( NULL ) < stt ){
  332.                 if( ScsiRead( hostNo, targetId, logicalAddr, sectorSize, blockCount, dataBuffer ) ){
  333.                     printf( "Read error occured.\n" );
  334.                     return( -1 );
  335.                 }
  336.                 if( benchMode  == 0 ){
  337.                     logicalAddr += blockCount;
  338.                 }else{
  339.                     do{
  340.                         logicalAddr = random( 32768 ) * randomScale;
  341.                     }while( logicalAddr > lbaMax );
  342.                 }
  343.                 readCount += blockCount;
  344.             }
  345.             readCount = readCount * 10L * sectorSize / benchTime / 1024L;
  346.             printf( " %4ld.%1ld[KB/s]:",
  347.                  readCount / 10L, readCount % 10L );
  348.             rankCount = readCount * 40 / readRank[ benchMode ] + 1;
  349.             if( rankCount > 40 ){
  350.                 rankCount = 40;
  351.             }
  352.             for( i = 0; i < rankCount; i++ ){
  353.                 putchar( '*' );
  354.             }
  355.             putchar( '\n' );
  356.             if( blockCount == 1 ){
  357.                 blockCount = 16384L / sectorSize;
  358.             }else if( blockCount != maxBlockCount ){
  359.                 blockCount = maxBlockCount;
  360.             }else{
  361.                 break;
  362.             }
  363.         }
  364.     }
  365.     return( 0 );
  366. }
  367.  
  368.  
  369. //
  370. //    get inquiry data from all host adapters, all scsi targets
  371. //         parameter :
  372. //            int hostNo : number of host adapters
  373. //            unsigned char inqbuf[7][7][40] : data buffer for inquiry
  374. //        return :
  375. //             0    if no error
  376. //            -1    if error occured
  377. //
  378. int GetInquiry( hostNo, inqBuf )
  379.     int hostNo;
  380.     unsigned char inqBuf[][ 7 ][ 40 ];
  381. {
  382.     int host, targetId;
  383.     
  384.     for( host = 0; host < hostNo; host++ ){
  385.         for( targetId = 0; targetId < 7; targetId++ ){
  386.             inqBuf[ host ][ targetId ][ 0 ] = 0xff;
  387.             if( Inquiry( host, targetId, inqBuf[ host ][ targetId ] ) == -1 ){
  388.                 printf( "Host adapter error occured.\n" );
  389.                 return( -1 );
  390.             }
  391.         }
  392.     }
  393.     return( 0 );
  394. }
  395.  
  396.  
  397. //
  398. //    display inquiry data
  399. //        parameter :
  400. //            int hostno : host adapter no.
  401. //            int    targetid : target scsi is
  402. //            unsigned char *inqdata : inquiry data
  403. //        return :
  404. //            non
  405. //
  406. void DispInquiry( hostNo, targetId, inqData )
  407.     int hostNo, targetId;
  408.     unsigned char *inqData;
  409. {
  410.     int i;
  411.     
  412.     if( inqData[ 0 ] == 0xff ){
  413.         return;
  414.     }
  415.     printf( "HA#%d ID%d ", hostNo, targetId );
  416.     if( inqData[ 4 ] ){
  417.         putchar( ' ' );
  418.         for( i = 8; i < 36; i++ ){
  419.             if( inqData[ i ] == 0x00 ){
  420.                 break;
  421.             }else{
  422.                 putchar( inqData[ i ] );
  423.             }
  424.         }
  425.         putchar( ' ' );
  426.     }else{
  427.         printf( " Device name not available" );
  428.     }
  429.     printf( " CCS%d ",inqData[ 2 ] & 0x03 );
  430.     if( inqData[ 1 ] & 0x80 ){
  431.         printf( "Removable\n" );
  432.     }else{
  433.         printf( "Rigid\n" );
  434.     }
  435. }
  436.  
  437.