home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / SW.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  2KB  |  75 lines

  1. /* These routines, plus the assembler hooks in stopwatch.asm, implement a
  2.  * general purpose "stopwatch" facility useful for timing the execution of
  3.  * code segments. The PC's "timer 2" channel (the one ordinarily
  4.  * used to drive the speaker) is used. It is driven with a 838 ns
  5.  * clock. The timer is 16 bits wide, so it "wraps around" in only 55 ms.
  6.  *
  7.  * There is an array of "stopwatch" structures used for recording the number
  8.  * of uses and the min/max times for each. Since only one hardware timer is
  9.  * available, only one stopwatch can actually be running at any one time.
  10.  *
  11.  * This facility is useful mainly for timing routines that must execute with
  12.  * interrupts disabled. An interrupt that occurs while the timer is running
  13.  * will not stop it, so it would show an errneously large value.
  14.  *
  15.  * To start a timer, call swstart(). To stop it and record its value,
  16.  * call swstop(n), where n is the number of the stopwatch structure.
  17.  * The stopwatch structures can be displayed with the "watch" command.
  18.  *
  19.  * Copyright 1991 Phil Karn, KA9Q
  20.  */
  21.  
  22. #include "global.h"
  23. #include "pc.h"
  24.  
  25. struct stopwatch Sw[NSW];
  26.  
  27. /* Stop a stopwatch and record its value.
  28.  * Uses stopval() routine in stopwatch.asm
  29.  */
  30. void
  31. swstop(n)
  32. int n;
  33. {
  34.     register int16 cnt;
  35.     register struct stopwatch *sw;
  36.  
  37.     cnt = stopval();
  38.     sw = &Sw[n];
  39.  
  40.     if(sw->calls++ == 0){
  41.         sw->maxval = cnt;
  42.         sw->minval = cnt;
  43.     } else if(cnt > sw->maxval){
  44.         sw->maxval = cnt;
  45.     } else if(cnt < sw->minval){
  46.         sw->minval = cnt;
  47.     }
  48. }
  49. int
  50. doswatch(argc,argv,p)
  51. int argc;
  52. char *argv[];
  53. void *p;
  54. {
  55.     register struct stopwatch *sw;
  56.     long maxval,minval;
  57.     int i;
  58.  
  59.     if(argc > 1){
  60.         /* Clear timers */
  61.         for(i=0,sw=Sw;i < NSW;i++,sw++){
  62.             sw->calls = 0;
  63.         }
  64.     }
  65.     for(i=0,sw=Sw;sw < &Sw[NSW];i++,sw++){
  66.         if(sw->calls != 0){
  67.             minval = (65536 - sw->maxval) * 838 / 1000;
  68.             maxval = (65536 - sw->minval) * 838 / 1000;
  69.             tprintf("%d: calls %ld min %ld max %ld\n",
  70.              i,sw->calls,minval,maxval);
  71.         }
  72.     }
  73.     return 0;
  74. }
  75.