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
Wrap
C/C++ Source or Header
|
1988-01-11
|
4KB
|
141 lines
/*****************************************************************************/
/* TIME */
/* print runtime information of a program */
/* */
/* Author: Michael BΣhr (mb) */
/*****************************************************************************/
#include <stdio.h>
#include <procid.h>
#define FALSE 0
#define TRUE 1
#define PBIT_DEAD 0x100
extern os9fork();
main(argc, argv, envp)
int argc;
char *argv[], *envp[];
{
short pid;
int died = FALSE;
int time, date, tick;
int prog_start, out_save;
int quiet = FALSE;
int extended = FALSE;
short day;
double ticks_per_second, user, system, real;
char *arg;
procid proc_desc;
if (argc < 2)
{
usage();
exit(1);
}
prog_start = 1;
while (*argv[prog_start] == '-')
{
arg = argv[prog_start] + 1;
while (*arg)
{
switch (*arg)
{
case '?':
usage();
exit(0);
case 'q':
quiet = TRUE;
break;
case 'e':
extended = TRUE;
break;
default:
usage();
fprintf(stderr, "Unknown option -%c\n", *arg);
exit(1);
}
arg++;
}
prog_start++;
}
if (quiet)
{
out_save = dup(1);
close(1);
if (open("/nil", _WRITE) != 1)
{
fprintf("time: error redirecting stdout to nil\n");
exit(errno);
}
}
if ((pid = os9exec(os9fork, argv[prog_start], &argv[prog_start], envp,
0, 0, 3)) == -1)
{
fprintf(stderr,"time: can't fork %s, error %3u\n",
argv[prog_start], errno);
exit(1);
}
while (!died)
{
if (_get_process_desc(pid, sizeof(procid), &proc_desc) == -1)
{
fprintf(stderr,"time: can't get process descriptor, error %3u\n",
errno);
exit(1);
}
if (proc_desc._state & PBIT_DEAD)
died = TRUE;
else
tsleep(10);
}
_sysdate(3, &time, &date, &day, &tick);
ticks_per_second = tick >> 16;
wait(NULL);
if (quiet)
{
close(1);
dup(out_save);
}
real = time - proc_desc._timbeg;
user = proc_desc._uticks / ticks_per_second;
system = proc_desc._sticks / ticks_per_second;
if (user + system > real)
real += 1.0;
if (!quiet)
printf("\n");
printf("Real time : %3.f s\n", real);
printf("User time : %6.2f s\n", user);
printf("System time : %6.2f s\n", system);
if (extended)
{
printf("\n");
printf("F$Calls : %6d\n", proc_desc._fcalls);
printf("I$Calls : %6d\n", proc_desc._icalls);
printf("Bytes read : %6d\n", proc_desc._rbytes);
printf("Bytes written : %6d\n", proc_desc._wbytes);
}
}
usage()
{
fprintf(stderr,"Syntax: time <progname> {<progopts>}\n");
fprintf(stderr,"Function: prints user and system time expired by program\n");
fprintf(stderr,"Options:\n");
fprintf(stderr," -q quiet execution (redirect stdout to nil)\n");
fprintf(stderr," -e print extended information about process\n");
}