home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hrtimer.zip / HRTEST.C next >
Text File  |  1992-05-31  |  14KB  |  310 lines

  1. /**********************************************************************
  2.  * MODULE NAME :  hrtimer.c              AUTHOR:  Rick Fishman        *
  3.  * DATE WRITTEN:  11-24-91                                            *
  4.  *                                                                    *
  5.  * DESCRIPTION:                                                       *
  6.  *                                                                    *
  7.  *  This program is a C Set/2 skeleton for using HRTIMER.SYS.         *
  8.  *                                                                    *
  9.  *  CommandLine: hrtest [milliseconds]                                *
  10.  *                                                                    *
  11.  *  milliseconds is the amount of time that this test program will    *
  12.  *  sleep for between calls to the timer. A default is provided if    *
  13.  *  no parameter is given.                                            *
  14.  *                                                                    *
  15.  *  A start timestamp will be retrieved from HRTIMER.SYS. Then        *
  16.  *  DosSleep will be issued for the milliseconds specified. Then a    *
  17.  *  stop timestamp will be retrieved and an elapsed time calculated.  *
  18.  *  The elapsed time should be close to the DosSleep interval. Keep   *
  19.  *  in mind that DosSleep has a granularity of 32 milliseconds so     *
  20.  *  a discrepency of elapsed time vs milliseconds passed to DosSleep  *
  21.  *  is DosSleep's fault, not the timer's.                             *
  22.  *                                                                    *
  23.  *  The overhead of a device driver read is also factored in.         *
  24.  *                                                                    *
  25.  **********************************************************************/
  26.  
  27. /*********************************************************************/
  28. /*------- Include relevant sections of the OS/2 header files --------*/
  29. /*********************************************************************/
  30.  
  31. #define INCL_DOSFILEMGR
  32. #define INCL_DOSPROCESS
  33.  
  34. /*********************************************************************/
  35. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  36. /*********************************************************************/
  37.  
  38. #define DEFAULT_TIME_PERIOD     5000      // in Milliseconds
  39.  
  40. /**********************************************************************/
  41. /*----------------------------- INCLUDES -----------------------------*/
  42. /**********************************************************************/
  43.  
  44. #include <os2.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include "hrtest.h"
  48.  
  49. /**********************************************************************/
  50. /*---------------------------- STRUCTURES ----------------------------*/
  51. /**********************************************************************/
  52.  
  53. /**********************************************************************/
  54. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  55. /**********************************************************************/
  56.  
  57. BOOL  Initialize     ( int argc, char *argv[] );
  58. VOID  TimeIt         ( VOID );
  59. ULONG CalcElapsedTime( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, ULONG ulOvHdMs,
  60.                        ULONG ulOvHdNs, PULONG pulNs );
  61. VOID  Terminate      ( VOID );
  62.  
  63. /**********************************************************************/
  64. /*------------------------ GLOBAL VARIABLES --------------------------*/
  65. /**********************************************************************/
  66.  
  67. HFILE   hfTimer;
  68.  
  69. ULONG   ulOverheadMs, ulOverheadNs, ulTimePeriod;
  70.  
  71. /**********************************************************************/
  72. /*------------------------------ MAIN --------------------------------*/
  73. /*                                                                    */
  74. /*  MAIN DRIVER FOR PROGRAM.                                          */
  75. /*                                                                    */
  76. /*  INPUT: nothing                                                    */
  77. /*                                                                    */
  78. /*  1. Initialize program.                                            */
  79. /*  1. Perform timings.                                               */
  80. /*  1. Terminage program.                                             */
  81. /*                                                                    */
  82. /*  OUTPUT: nothing                                                   */
  83. /*--------------------------------------------------------------------*/
  84. /**********************************************************************/
  85.  
  86. int main( int argc, char *argv[] )
  87. {
  88.     if( Initialize( argc, argv ) )
  89.         TimeIt();
  90.  
  91.     Terminate();
  92.  
  93.     return 0;
  94. }
  95.  
  96. /**********************************************************************/
  97. /*--------------------------- Initialize -----------------------------*/
  98. /*                                                                    */
  99. /*  INITIALIZE PROGRAM.                                               */
  100. /*                                                                    */
  101. /*  INPUT: command-line argument count,                               */
  102. /*         array of command-line arguments                            */
  103. /*                                                                    */
  104. /*  1. Set the time period that we will try to match with the high    */
  105. /*     resolution timer.                                              */
  106. /*  2. Open the timer device driver.                                  */
  107. /*  3. Read a timestamp from the driver to use as a starting time     */
  108. /*     used to calculate the overhead of a device-driver read.        */
  109. /*  4. Read a timestamp from the driver to use as a stop time for     */
  110. /*     the overhead calculation.                                      */
  111. /*  5. Calculate the overhead in the read.                            */
  112. /*                                                                    */
  113. /*  OUTPUT: TRUE or FALSE if successful or not                        */
  114. /*                                                                    */
  115. /*--------------------------------------------------------------------*/
  116. /**********************************************************************/
  117.  
  118. BOOL Initialize( int argc, char *argv[] )
  119. {
  120.     TIMESTAMP   tsStart, tsStop;
  121.     ULONG       ulRC, ulAction, ulBytes;
  122.  
  123.     if( argc < 2 )
  124.         ulTimePeriod = DEFAULT_TIME_PERIOD;
  125.     else
  126.         ulTimePeriod = atoi( argv[ 1 ] );
  127.  
  128.     printf( "\nMeasuring a DosSleep of %u milliseconds\n", ulTimePeriod );
  129.  
  130.     ulRC = DosOpen( "TIMER$", &hfTimer, &ulAction, 0,
  131.                     FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, NULL );
  132.  
  133.     if( ulRC )
  134.     {
  135.         printf( "\nDosOpen got a retcode of %u", ulRC );
  136.  
  137.         return FALSE;
  138.     }
  139.  
  140.     ulRC = DosRead( hfTimer, &tsStart, sizeof( TIMESTAMP ), &ulBytes );
  141.  
  142.     if( ulRC )
  143.     {
  144.         printf( "\nDosRead for start got a retcode of %u", ulRC );
  145.  
  146.         return FALSE;
  147.     }
  148.  
  149.     ulRC = DosRead( hfTimer, &tsStop, sizeof( TIMESTAMP ), &ulBytes );
  150.  
  151.     if( ulRC )
  152.     {
  153.         printf( "\nDosRead for stop got a retcode of %u", ulRC );
  154.  
  155.         return FALSE;
  156.     }
  157.  
  158.     ulOverheadMs = CalcElapsedTime( &tsStart, &tsStop, 0, 0, &ulOverheadNs );
  159.  
  160.     printf( "\nOverhead (ms:ns) = %06u:%06u\n", ulOverheadMs, ulOverheadNs );
  161.  
  162.     return TRUE;
  163. }
  164.  
  165. /**********************************************************************/
  166. /*----------------------------- TimeIt -------------------------------*/
  167. /*                                                                    */
  168. /*  DO THE TIMING.                                                    */
  169. /*                                                                    */
  170. /*  INPUT: nothing                                                    */
  171. /*                                                                    */
  172. /*  1. Until Ctrl-Break is pressed:                                   */
  173. /*     A. Read a starting timestamp from the timer.                   */
  174. /*     B. Sleep for a specified period of time.                       */
  175. /*     C. Read a stop timestamp from the timer.                       */
  176. /*     D. Calculate the elapsed time.                                 */
  177. /*                                                                    */
  178. /*  OUTPUT: nothing                                                   */
  179. /*                                                                    */
  180. /*--------------------------------------------------------------------*/
  181. /**********************************************************************/
  182.  
  183. VOID TimeIt()
  184. {
  185.     TIMESTAMP   tsStart, tsStop;
  186.     ULONG       ulRC, ulMsecs, ulNsecs, ulBytes;
  187.  
  188.     printf( "\nHit Ctrl-C to terminate this test program...\n" );
  189.  
  190.     for( ; ; )
  191.     {
  192.         printf( "\nSleeping for %u milliseconds...", ulTimePeriod );
  193.  
  194.         fflush( stdout );
  195.  
  196.         ulRC = DosRead( hfTimer, &tsStart, sizeof( TIMESTAMP ), &ulBytes );
  197.  
  198.         if( ulRC )
  199.         {
  200.             printf( "\nDosRead for start got a retcode of %u", ulRC );
  201.  
  202.             return;
  203.         }
  204.  
  205.         DosSleep( ulTimePeriod );
  206.  
  207.         ulRC = DosRead( hfTimer, &tsStop, sizeof( TIMESTAMP ), &ulBytes );
  208.  
  209.         if( ulRC )
  210.         {
  211.             printf( "\nDosRead for stop got a retcode of %u", ulRC );
  212.  
  213.             return;
  214.         }
  215.  
  216.         ulMsecs = CalcElapsedTime( &tsStart, &tsStop,
  217.                                    ulOverheadMs, ulOverheadNs, &ulNsecs );
  218.  
  219.         printf( " elapsed time (ms:ns) = %06u:%06u", ulMsecs, ulNsecs );
  220.     }
  221. }
  222.  
  223. /**********************************************************************/
  224. /*------------------------- CalcElapsedTime --------------------------*/
  225. /*                                                                    */
  226. /*  CALCULATE ELAPSED TIME GIVEN TWO TIMESTAMPS                       */
  227. /*                                                                    */
  228. /*  INPUT: pointer to Start timestamp structure,                      */
  229. /*         pointer to Stop  timestamp structure,                      */
  230. /*         number of overhead milliseconds,                           */
  231. /*         number of overhead nanoseconds,                            */
  232. /*         address of variable to return remainder nanoseconds        */
  233. /*                                                                    */
  234. /*  1.                                                                */
  235. /*                                                                    */
  236. /*  OUTPUT: Number of elapsed milliseconds                            */
  237. /*                                                                    */
  238. /*--------------------------------------------------------------------*/
  239. /**********************************************************************/
  240.  
  241. ULONG CalcElapsedTime( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop,
  242.                        ULONG ulOverhdMs, ULONG ulOverhdNs, PULONG pulNs )
  243. {
  244.     ULONG       ulMsecs, ulNsecs;
  245.     TIMESTAMP   tsStart, tsStop;
  246.  
  247.     tsStart = *ptsStart;                        // De-reference timestamp
  248.                                                 //     structures for speed
  249.     tsStop  = *ptsStop;
  250.  
  251.     ulMsecs = tsStop.ulMs - tsStart.ulMs;       // Elapsed milliseconds
  252.  
  253.     if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  254.     {
  255.         ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  256.  
  257.         ulMsecs--;                                        // Adjust milliseconds
  258.     }
  259.     else
  260.         ulNsecs = tsStop.ulNs - tsStart.ulNs;   // No overflow..Elapsed nanos
  261.  
  262.     if( ulOverhdMs || ulOverhdNs )              // If caller wants overhead
  263.     {                                           //    factored in ...
  264.         if( ulOverhdNs > ulNsecs )
  265.         {                                       // If nanosecond overflow
  266.             ulNsecs = (1000000 + ulNsecs) - ulOverhdNs;// Adjust nanoseconds
  267.  
  268.             ulMsecs--;                                 // Adjust milliseconds
  269.         }
  270.         else
  271.             ulNsecs -= ulOverhdNs;              // No overflow..Final nanosecs
  272.  
  273.         ulMsecs -= ulOverhdMs;                  // Final milliseconds
  274.     }
  275.  
  276.     *pulNs = ulNsecs;
  277.  
  278.     return ulMsecs;
  279. }
  280.  
  281. /**********************************************************************/
  282. /*--------------------------- Terminate ------------------------------*/
  283. /*                                                                    */
  284. /*  TERMINATE PROGRAM                                                 */
  285. /*                                                                    */
  286. /*  INPUT: nothing                                                    */
  287. /*                                                                    */
  288. /*  1. Close the device driver.                                       */
  289. /*                                                                    */
  290. /*  OUTPUT: nothing                                                   */
  291. /*                                                                    */
  292. /*--------------------------------------------------------------------*/
  293. /**********************************************************************/
  294. VOID Terminate()
  295. {
  296.     ULONG ulRC;
  297.  
  298.     if( hfTimer )
  299.     {
  300.         ulRC = DosClose( hfTimer );
  301.  
  302.         if( ulRC )
  303.         {
  304.             printf( "\nDosClose got a retcode of %u", ulRC );
  305.  
  306.             return;
  307.         }
  308.     }
  309. }
  310.