shithub: scc

Download patch

ref: 4959ae65d686d2ad095cff104428040f65bb05fa
parent: c1c79f43a6084eddc83de124b371a8b689e310a6
author: Naveen Narayanan <[email protected]>
date: Thu Aug 20 08:38:41 EDT 2020

libc: Implement clock (linux/amd64)

--- 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;
--- 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 {
--- a/src/libc/arch/amd64/linux/Makefile
+++ b/src/libc/arch/amd64/linux/Makefile
@@ -15,6 +15,7 @@
 	_brk.$O\
 	_gettimeofday.$O\
 	_sigaction.$O\
+	_getrusage.$O\
 	_unlink.$O\
 
 GENSRC = $(GENOBJS:.$O=.s)
@@ -25,6 +26,7 @@
 	raise.$O\
 	signal.$O\
 	time.$O\
+	clock.$O\
 	_cerrno.$O\
 	_getheap.$O\
 	_tzone.$O\
--- /dev/null
+++ b/src/libc/arch/amd64/linux/clock.c
@@ -1,0 +1,1 @@
+#include "../../posix/clock.c"
--- 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
--- /dev/null
+++ b/src/libc/arch/posix/clock.c
@@ -1,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);
+}
--- /dev/null
+++ b/src/libc/arch/posix/clock.h
@@ -1,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