shithub: scc

Download patch

ref: 1775d6b56d55aade3641e5b8676ba79ec50f4f51
parent: c0a73e7704df4edf73f81020b9432a794c655232
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Sep 14 09:06:17 EDT 2021

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.

--- 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;
--- 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 {
--- 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;
--- 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 {
--- 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;
--- 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;
--- 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;
--- 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;
--- 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 {
--- 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;
--- 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 {
--- 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;
--- 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 {
--- 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;
--- 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);
 }
--- a/src/libc/arch/posix/clock.h
+++ /dev/null
@@ -1,23 +1,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;
-};