diff --git a/libc/Makefile b/libc/Makefile index 27ddc314..ac0f20e5 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -365,6 +365,7 @@ time/timer_delete.o \ time/timer_getoverrun.o \ time/timer_gettime.o \ time/timer_settime.o \ +time/times.o \ tmpfile.o \ tmpnam.o \ truncateat.o \ diff --git a/libc/include/sys/times.h b/libc/include/sys/times.h new file mode 100644 index 00000000..e54a0254 --- /dev/null +++ b/libc/include/sys/times.h @@ -0,0 +1,46 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + sys/times.h + Declaration for the times function. + +*******************************************************************************/ + +#ifndef INCLUDE_SYS_TIMES_H +#define INCLUDE_SYS_TIMES_H + +#include + +__BEGIN_DECLS + +@include(clock_t.h); + +struct tms +{ + clock_t tms_utime; + clock_t tms_stime; + clock_t tms_cutime; + clock_t tms_cstime; +}; + +clock_t times(struct tms*); + +__END_DECLS + +#endif diff --git a/libc/sysconf.cpp b/libc/sysconf.cpp index 505329a8..ecb86816 100644 --- a/libc/sysconf.cpp +++ b/libc/sysconf.cpp @@ -30,6 +30,7 @@ extern "C" long sysconf(int name) { switch ( name ) { + case _SC_CLK_TCK: return 1000; case _SC_PAGESIZE: case _SC_PAGE_SIZE: return getpagesize(); default: diff --git a/libc/time/times.cpp b/libc/time/times.cpp new file mode 100644 index 00000000..91fd5c50 --- /dev/null +++ b/libc/time/times.cpp @@ -0,0 +1,65 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013 + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + time/times.cpp + Get process execution time statistics. + +*******************************************************************************/ + +#include + +#include +#include +#include + +// TODO: It is a bit needless to use 64-bit integers on 32-bit platforms and the +// division is probably very inefficient. In addition, this implementation +// doesn't hanndle a full 64-bit clock_t, however it won't cause problems +// before a lot of years has passed, which is unlikely to happen, and the +// clock function probably shouldn't be used in programs with that long +// running times. +// TODO: Does this function overflow correctly? +clock_t timespec_to_clock(struct timespec t) +{ + uint64_t ticks_per_second = (uint64_t) sysconf(_SC_CLK_TCK); + uint64_t sec_contrib = (uint64_t) t.tv_sec * ticks_per_second; + uint64_t nsec_contrib = (uint64_t) t.tv_nsec * ticks_per_second; + return (clock_t) (sec_contrib + nsec_contrib / 1000000000); +} + +// TODO: This function is crap and has been replaced by timens or clock_gettime. +extern "C" clock_t times(struct tms* buf) +{ + if ( buf ) + { + struct tmns tmns; + if ( timens(&tmns) < 0 ) + return (clock_t) -1; + buf->tms_utime = timespec_to_clock(tmns.tmns_utime); + buf->tms_stime = timespec_to_clock(tmns.tmns_stime); + buf->tms_cutime = timespec_to_clock(tmns.tmns_cutime); + buf->tms_cstime = timespec_to_clock(tmns.tmns_cstime); + } + + // TODO: Should this be CLOCK_REALTIME instead? + struct timespec now; + if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 ) + return -1; + return timespec_to_clock(now); +} diff --git a/sortix/include/sortix/__/types.h b/sortix/include/sortix/__/types.h index 3a9433d1..e89cb510 100644 --- a/sortix/include/sortix/__/types.h +++ b/sortix/include/sortix/__/types.h @@ -45,7 +45,7 @@ typedef __intmax_t __blkcnt_t; typedef unsigned int __nlink_t; typedef __uintmax_t __ino_t; typedef __uintptr_t __dev_t; -typedef __intmax_t __clock_t; +typedef long __clock_t; typedef int __clockid_t; typedef long __time_t; /* TODO: Increase on 32-bit systems! */ typedef long __suseconds_t;