9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

time.c (920B)


      1 #include <u.h>
      2 #include <sys/time.h>
      3 #include <time.h>
      4 #include <sys/resource.h>
      5 #define NOPLAN9DEFINES
      6 #include <libc.h>
      7 
      8 long
      9 p9times(long *t)
     10 {
     11 	struct rusage ru, cru;
     12 
     13 	if(getrusage(0, &ru) < 0 || getrusage(-1, &cru) < 0)
     14 		return -1;
     15 
     16 	t[0] = ru.ru_utime.tv_sec*1000 + ru.ru_utime.tv_usec/1000;
     17 	t[1] = ru.ru_stime.tv_sec*1000 + ru.ru_stime.tv_usec/1000;
     18 	t[2] = cru.ru_utime.tv_sec*1000 + cru.ru_utime.tv_usec/1000;
     19 	t[3] = cru.ru_stime.tv_sec*1000 + cru.ru_stime.tv_usec/1000;
     20 
     21 	/* BUG */
     22 	return t[0]+t[1]+t[2]+t[3];
     23 }
     24 
     25 double
     26 p9cputime(void)
     27 {
     28 	long t[4];
     29 	double d;
     30 
     31 	if(p9times(t) < 0)
     32 		return -1.0;
     33 
     34 	d = (double)t[0]+(double)t[1]+(double)t[2]+(double)t[3];
     35 	return d/1000.0;
     36 }
     37 
     38 vlong
     39 p9nsec(void)
     40 {
     41 	struct timeval tv;
     42 
     43 	if(gettimeofday(&tv, 0) < 0)
     44 		return -1;
     45 
     46 	return (vlong)tv.tv_sec*1000*1000*1000 + tv.tv_usec*1000;
     47 }
     48 
     49 long
     50 p9time(long *tt)
     51 {
     52 	long t;
     53 	t = time(0);
     54 	if(tt)
     55 		*tt = t;
     56 	return t;
     57 }
     58