scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 0987903f101c4a6872d76769a98f55a1dbc8be62
parent 51bc6e51b5450481c791f26878dd2a9ac80521bd
Author: Naveen Narayanan <zerous@simple-cc.org>
Date:   Thu, 20 Aug 2020 12:38:41 +0200

libc: Implement clock (linux/amd64)

Diffstat:
Minclude/bits/amd64/arch/time.h | 1+
Minclude/time.h | 2--
Msrc/libc/arch/amd64/linux/Makefile | 2++
Asrc/libc/arch/amd64/linux/clock.c | 1+
Msrc/libc/arch/amd64/linux/syscall.lst | 1+
Asrc/libc/arch/posix/clock.c | 22++++++++++++++++++++++
Asrc/libc/arch/posix/clock.h | 34++++++++++++++++++++++++++++++++++
7 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/include/bits/amd64/arch/time.h b/include/bits/amd64/arch/time.h @@ -1,3 +1,4 @@ #define _MAXYEAR 9999 +#define CLOCKS_PER_SEC 128 typedef long time_t; diff --git a/include/time.h b/include/time.h @@ -7,8 +7,6 @@ #include <arch/cdefs.h> #include <arch/time.h> -#define CLOCKS_PER_SEC 1000000 - typedef long int clock_t; struct tm { diff --git a/src/libc/arch/amd64/linux/Makefile b/src/libc/arch/amd64/linux/Makefile @@ -15,6 +15,7 @@ GENOBJS =\ _brk.$O\ _gettimeofday.$O\ _sigaction.$O\ + _getrusage.$O\ _unlink.$O\ GENSRC = $(GENOBJS:.$O=.s) @@ -25,6 +26,7 @@ OBJS = \ raise.$O\ signal.$O\ time.$O\ + clock.$O\ _cerrno.$O\ _getheap.$O\ _tzone.$O\ diff --git a/src/libc/arch/amd64/linux/clock.c b/src/libc/arch/amd64/linux/clock.c @@ -0,0 +1 @@ +#include "../../posix/clock.c" diff --git a/src/libc/arch/amd64/linux/syscall.lst b/src/libc/arch/amd64/linux/syscall.lst @@ -11,3 +11,4 @@ 62 _kill 87 _unlink 96 _gettimeofday +98 _getrusage diff --git a/src/libc/arch/posix/clock.c b/src/libc/arch/posix/clock.c @@ -0,0 +1,22 @@ +#include "clock.h" + +extern int _getrusage(int, struct rusage*); + +static clock_t +convtick(struct rusage r) +{ + return r.ru_utime.tv_sec*CLOCKS_PER_SEC + + r.ru_stime.tv_usec / (1000000 / CLOCKS_PER_SEC); +} + + +clock_t +clock(void) +{ + struct rusage ru; + clock_t c; + + if (_getrusage(RUSAGE_SELF, &ru)) + return ((clock_t) -1); + return convtick(ru); +} diff --git a/src/libc/arch/posix/clock.h b/src/libc/arch/posix/clock.h @@ -0,0 +1,34 @@ +#ifndef CLOCK_H +#define CLOCK_H + +#include <time.h> + +#define RUSAGE_SELF 0 + +typedef long int clock_t; + +struct timeval { + long int tv_sec; + long int tv_usec; +}; + +struct rusage { + struct timeval ru_utime; + struct timeval ru_stime; + long int ru_maxrss; + long int ru_ixrss; + long int ru_idrss; + long int ru_isrss; + long int ru_minflt; + long int ru_majflt; + long int ru_nswap; + long int ru_inblock; + long int ru_oublock; + long int ru_msgsnd; + long int ru_msgrcv; + long int ru_nsignals; + long int ru_nvcsw; + long int ru_nivcsw; +}; + +#endif