scc

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

commit 89c5291c6acc69b3871cfe3edbc02c457261305b
parent 2f8add0bc5f01b92bde7bfd55faa95e2598397e6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 14 Sep 2021 13:06:17 +0200

libc: Fix POSIX clock()

Clock() in POSIX must include the system time as the user
time. The code was using part of the system time and part
of the user time. This commit also moves the definition
of struct timeval to the sys include directory.

Diffstat:
Minclude/bits/darwin/amd64/time.h | 2++
Minclude/bits/darwin/sys.h | 3+++
Minclude/bits/dragonfly/amd64/time.h | 2++
Minclude/bits/dragonfly/sys.h | 3+++
Minclude/bits/linux/amd64/arch/time.h | 2++
Minclude/bits/linux/arm/arch/time.h | 2++
Minclude/bits/linux/arm64/arch/time.h | 2++
Minclude/bits/linux/ppc/arch/time.h | 2++
Minclude/bits/linux/sys.h | 3+++
Minclude/bits/netbsd/amd64/time.h | 2++
Minclude/bits/netbsd/sys.h | 3+++
Minclude/bits/openbsd/amd64/time.h | 2++
Minclude/bits/openbsd/sys.h | 3+++
Minclude/time.h | 2--
Msrc/libc/arch/posix/clock.c | 41++++++++++++++++++++++++++++++-----------
Dsrc/libc/arch/posix/clock.h | 23-----------------------
16 files changed, 61 insertions(+), 36 deletions(-)

diff --git a/include/bits/darwin/amd64/time.h b/include/bits/darwin/amd64/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/darwin/sys.h b/include/bits/darwin/sys.h @@ -6,6 +6,9 @@ #define O_APPEND 0x00000008 #define O_CREAT 0x00000200 +#define CLOCKS_PER_SEC ((clock_t) 1000000) +#define RUSAGE_SELF 0 + typedef int pid_t; struct sigaction { diff --git a/include/bits/dragonfly/amd64/time.h b/include/bits/dragonfly/amd64/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/dragonfly/sys.h b/include/bits/dragonfly/sys.h @@ -6,6 +6,9 @@ #define O_APPEND 0x00000008 #define O_CREAT 0x00000200 +#define CLOCKS_PER_SEC ((clock_t) 128) +#define RUSAGE_SELF 0 + typedef int pid_t; struct sigaction { diff --git a/include/bits/linux/amd64/arch/time.h b/include/bits/linux/amd64/arch/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/linux/arm/arch/time.h b/include/bits/linux/arm/arch/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 2038 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/linux/arm64/arch/time.h b/include/bits/linux/arm64/arch/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/linux/ppc/arch/time.h b/include/bits/linux/ppc/arch/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 2038 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/linux/sys.h b/include/bits/linux/sys.h @@ -6,6 +6,9 @@ #define O_APPEND 0x00000400 #define O_CREAT 0x00000040 +#define CLOCKS_PER_SEC ((clock_t) 1000000) +#define RUSAGE_SELF 0 + typedef int pid_t; struct sigaction { diff --git a/include/bits/netbsd/amd64/time.h b/include/bits/netbsd/amd64/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/netbsd/sys.h b/include/bits/netbsd/sys.h @@ -6,6 +6,9 @@ #define O_APPEND 0x00000008 #define O_CREAT 0x00000200 +#define CLOCKS_PER_SEC ((clock_t) 100) +#define RUSAGE_SELF 0 + typedef int pid_t; struct sigaction { diff --git a/include/bits/openbsd/amd64/time.h b/include/bits/openbsd/amd64/time.h @@ -1,3 +1,5 @@ #define _MAXYEAR 9999 typedef long time_t; +typedef long clock_t; +typedef long __suseconds_t; diff --git a/include/bits/openbsd/sys.h b/include/bits/openbsd/sys.h @@ -6,6 +6,9 @@ #define O_APPEND 0x00000008 #define O_CREAT 0x00000200 +#define CLOCKS_PER_SEC ((clock_t) 100) +#define RUSAGE_SELF 0 + typedef int pid_t; struct sigaction { diff --git a/include/time.h b/include/time.h @@ -7,8 +7,6 @@ #include <arch/cdefs.h> #include <arch/time.h> -typedef long int clock_t; - struct tm { int tm_sec; int tm_min; diff --git a/src/libc/arch/posix/clock.c b/src/libc/arch/posix/clock.c @@ -1,21 +1,40 @@ -#include "clock.h" +#include <time.h> -extern int _getrusage(int, struct rusage*); +#include <sys.h> -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); -} +#include "time.h" + +#define TOCLOCK(r) (r.tv_sec * CLOCKS_PER_SEC +\ + r.tv_usec / (1000000 / CLOCKS_PER_SEC)) + +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; +}; + +extern int _getrusage(int, struct rusage*); clock_t clock(void) { struct rusage ru; - clock_t c; if (_getrusage(RUSAGE_SELF, &ru)) - return ((clock_t) -1); - return convtick(ru); + return -1; + + return TOCLOCK(ru.ru_utime) + TOCLOCK(ru_ru_stime); } diff --git a/src/libc/arch/posix/clock.h b/src/libc/arch/posix/clock.h @@ -1,23 +0,0 @@ -struct timeval { - time_t tv_sec; - suseconds_t 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; -};