home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum3.lzh / C / time.c
C/C++ Source or Header  |  1988-01-11  |  4KB  |  141 lines

  1. /*****************************************************************************/
  2. /* TIME                                                                      */
  3. /* print runtime information of a program                                    */
  4. /*                                                                           */
  5. /* Author: Michael BΣhr (mb)                                                 */
  6. /*****************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <procid.h>
  10.  
  11. #define FALSE 0
  12. #define TRUE  1
  13.  
  14. #define PBIT_DEAD 0x100
  15.  
  16. extern os9fork();
  17.  
  18. main(argc, argv, envp)
  19. int argc;
  20. char *argv[], *envp[];
  21. {
  22.     short pid;
  23.     int died = FALSE;
  24.     int time, date, tick;
  25.     int prog_start, out_save;
  26.     int quiet = FALSE;
  27.     int extended = FALSE;
  28.     short day;
  29.     double ticks_per_second, user, system, real;
  30.     char *arg;
  31.         
  32.     procid proc_desc;
  33.         
  34.     if (argc < 2)
  35.     {
  36.         usage();
  37.         exit(1);
  38.     }
  39.     
  40.     prog_start = 1;
  41.     while (*argv[prog_start] == '-')
  42.     {
  43.         arg = argv[prog_start] + 1;
  44.         while (*arg)
  45.         {
  46.             switch (*arg)
  47.             {
  48.                 case '?':
  49.                     usage();
  50.                     exit(0);
  51.                 case 'q':
  52.                     quiet = TRUE;
  53.                     break;
  54.                 case 'e':
  55.                     extended = TRUE;
  56.                     break;
  57.                 default:
  58.                     usage();
  59.                     fprintf(stderr, "Unknown option -%c\n", *arg);
  60.                     exit(1);
  61.             }
  62.             arg++;
  63.         }
  64.         prog_start++;
  65.     }
  66.     
  67.     if (quiet)
  68.     {
  69.         out_save = dup(1);
  70.         close(1);
  71.         if (open("/nil", _WRITE) != 1)
  72.         {
  73.             fprintf("time: error redirecting stdout to nil\n");
  74.             exit(errno);
  75.         }
  76.     }
  77.  
  78.     if ((pid = os9exec(os9fork, argv[prog_start], &argv[prog_start], envp, 
  79.                        0, 0, 3)) == -1)
  80.     {
  81.         fprintf(stderr,"time: can't fork %s, error %3u\n", 
  82.                         argv[prog_start], errno);
  83.         exit(1);
  84.     }
  85.     
  86.     while (!died)
  87.     {
  88.         if (_get_process_desc(pid, sizeof(procid), &proc_desc) == -1)
  89.         {
  90.             fprintf(stderr,"time: can't get process descriptor, error %3u\n",
  91.                            errno);
  92.             exit(1);
  93.         }
  94.         if (proc_desc._state & PBIT_DEAD)
  95.             died = TRUE;
  96.         else
  97.             tsleep(10);
  98.     }
  99.     _sysdate(3, &time, &date, &day, &tick);
  100.     ticks_per_second = tick >> 16;        
  101.     wait(NULL);
  102.  
  103.     if (quiet)
  104.     {
  105.         close(1);
  106.         dup(out_save);
  107.     }
  108.  
  109.     
  110.     real   = time - proc_desc._timbeg;
  111.     user   = proc_desc._uticks / ticks_per_second;
  112.     system = proc_desc._sticks / ticks_per_second;
  113.     
  114.     if (user + system > real)
  115.         real += 1.0;
  116.         
  117.     if (!quiet)
  118.         printf("\n");
  119.     
  120.     printf("Real time     : %3.f    s\n", real);
  121.     printf("User time     : %6.2f s\n", user);
  122.     printf("System time   : %6.2f s\n", system);
  123.     if (extended)
  124.     {
  125.         printf("\n");
  126.         printf("F$Calls       : %6d\n", proc_desc._fcalls);
  127.         printf("I$Calls       : %6d\n", proc_desc._icalls);
  128.         printf("Bytes read    : %6d\n", proc_desc._rbytes);
  129.         printf("Bytes written : %6d\n", proc_desc._wbytes);
  130.     }
  131. }
  132.  
  133. usage()
  134. {
  135.     fprintf(stderr,"Syntax: time <progname> {<progopts>}\n");
  136.     fprintf(stderr,"Function: prints user and system time expired by program\n");
  137.     fprintf(stderr,"Options:\n");
  138.     fprintf(stderr,"     -q        quiet execution (redirect stdout to nil)\n");
  139.     fprintf(stderr,"     -e        print extended information about process\n");
  140. }
  141.