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 >
Wrap
C/C++ Source or Header
|
2009-11-06
|
3KB
|
90 lines
#include <stdio.h>
#include <time.h>
#include <procid.h>
#include <signal.h>
#include <errno.h>
#define PARTPD 724
#define FALSE (1==0)
#define TRUE !FALSE
extern char *getenv();
extern int os9fork();
static procid Child_process;
static char *Stars = "***********************************\n";
static char *Darrow= "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n";
static char *Uarrow= "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";
void argwrong()
{
puts("");
puts("Usage: time [-r] <program_name> [arg1 arg2...argn]");
puts(" -r will allow full redirection of output.");
puts(" Normally, time outputs statistics on PORT (from environment).");
puts("");
exit(0);
}
main(argc, argv, envp)
int argc;
char *argv[], *envp[];
{
int procn;
int start, end;
char *ioport;
int fullredirect = FALSE;
double totalticks;
FILE *outpath;
if ( argc == 1 )
argwrong();
if ( (argv[1][0] == '-') ) {
if( (argv[1][1] | 0x60) == 'r' ) {
fullredirect = TRUE;
argv++;
} else
argwrong();
}
if ( ! fullredirect ) {
ioport = getenv("PORT");
if ( ioport == (char *)0x0 )
exit(_errmsg(1,"'PORT' environment variable not defined\n"));
if ( (outpath = fopen(ioport, "w")) == (FILE *)-1 )
outpath = stdout;
}
else
outpath = stdout;
fprintf(outpath, "\n%s\t\tProgram: %s\n%s", Stars, argv[1], Darrow);
fflush(outpath);
start = _getsys(D_Ticks, sizeof(int));
if ( (procn = os9exec(os9fork, argv[1], &argv[1], envp, 0, 0)) == -1) {
fprintf(outpath, "unable to fork %s!\n", argv[1]);
exit(0);
}
do {
tsleep(1);
if (_get_process_desc(procn,PARTPD,&Child_process) == -1)
_exit(_errmsg(errno, "Could not get process descriptor for %s!\n",
argv[1]));
end = _getsys(D_Ticks, sizeof(int));
} while ( (Child_process._state & 0x100) == 0);
while ( wait(0) != procn );
fprintf(outpath, "%sProgram Statistics:\n CPU Ticks: ",Uarrow);
fprintf(outpath, "user = %d system = %d\n", Child_process._uticks,
Child_process._sticks);
totalticks = (double) (Child_process._uticks + Child_process._sticks);
fprintf(outpath,"Total CPU Time (seconds): %lg\n", totalticks/(double)CLK_TCK);
fprintf(outpath,"Clock Time (seconds): %lg\n", (double)(end-start)/(double)CLK_TCK);
fprintf(outpath,"%% of time used by %s: %lg\n",argv[1], (totalticks/(double)(end-start))*100);
fprintf(outpath,"F$ calls: %d I$ calls: %d\n", Child_process._fcalls,
Child_process._icalls);
fprintf(outpath,"Bytes written: %d Bytes read: %d\n%s\n",
Child_process._wbytes,Child_process._rbytes, Stars);
}