home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / timer.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  3KB  |  90 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <procid.h>
  4. #include <signal.h>
  5. #include <errno.h>
  6.  
  7. #define PARTPD 724
  8. #define FALSE (1==0)
  9. #define TRUE !FALSE
  10.  
  11. extern char *getenv();
  12. extern int os9fork();
  13.  
  14. static procid Child_process;
  15.  
  16. static char *Stars = "***********************************\n";
  17. static char *Darrow= "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n";
  18. static char *Uarrow= "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
  19.  
  20. void argwrong()
  21. {
  22.     puts("");
  23.     puts("Usage: time [-r] <program_name> [arg1 arg2...argn]");
  24.     puts("       -r will allow full redirection of output.");
  25.     puts("       Normally, time outputs statistics on PORT (from environment).");
  26.     puts("");
  27.     exit(0);
  28. }
  29.  
  30. main(argc, argv, envp)
  31. int argc;
  32. char *argv[], *envp[];
  33. {
  34.     int procn;
  35.     int start, end;
  36.     char *ioport;
  37.     int fullredirect = FALSE;
  38.     double totalticks;
  39.     FILE *outpath;
  40.     
  41.     if ( argc == 1 )
  42.         argwrong();
  43.     if ( (argv[1][0] == '-') ) {
  44.         if( (argv[1][1] | 0x60) == 'r' ) {
  45.             fullredirect = TRUE;
  46.             argv++;
  47.         } else
  48.             argwrong();
  49.     }
  50.     if ( ! fullredirect ) {
  51.         ioport = getenv("PORT");
  52.         if ( ioport == (char *)0x0 )
  53.             exit(_errmsg(1,"'PORT' environment variable not defined\n"));
  54.         if ( (outpath = fopen(ioport, "w")) == (FILE *)-1 )
  55.             outpath = stdout;
  56.        }
  57.        else
  58.            outpath = stdout;
  59.        fprintf(outpath, "\n%s\t\tProgram: %s\n%s", Stars, argv[1], Darrow);
  60.        fflush(outpath);
  61.        
  62.     start = _getsys(D_Ticks, sizeof(int));
  63.  
  64.     if ( (procn = os9exec(os9fork, argv[1], &argv[1], envp, 0, 0)) == -1) {
  65.         fprintf(outpath, "unable to fork %s!\n", argv[1]);
  66.         exit(0);
  67.     }
  68.     do {
  69.         tsleep(1);
  70.         if (_get_process_desc(procn,PARTPD,&Child_process) == -1)
  71.             _exit(_errmsg(errno, "Could not get process descriptor for %s!\n",
  72.                                                                 argv[1]));
  73.         end = _getsys(D_Ticks, sizeof(int));
  74.     } while ( (Child_process._state & 0x100) == 0);
  75.     
  76.     while ( wait(0) != procn );
  77.     
  78.     fprintf(outpath, "%sProgram Statistics:\n CPU Ticks: ",Uarrow);
  79.     fprintf(outpath, "user = %d system = %d\n", Child_process._uticks,
  80.                                                 Child_process._sticks);  
  81.     totalticks = (double) (Child_process._uticks + Child_process._sticks);
  82.     fprintf(outpath,"Total CPU Time (seconds): %lg\n", totalticks/(double)CLK_TCK);
  83.     fprintf(outpath,"Clock Time (seconds): %lg\n", (double)(end-start)/(double)CLK_TCK);
  84.     fprintf(outpath,"%% of time used by %s: %lg\n",argv[1], (totalticks/(double)(end-start))*100);
  85.     fprintf(outpath,"F$ calls: %d     I$ calls: %d\n", Child_process._fcalls,
  86.                                                        Child_process._icalls);
  87.     fprintf(outpath,"Bytes written: %d     Bytes read: %d\n%s\n",
  88.                             Child_process._wbytes,Child_process._rbytes, Stars);
  89. }
  90.