From 94584e8b20f67a342c625a674590e8bc1aad88cb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 1 Dec 2010 00:29:34 -0500 Subject: Check for platform support for times(). --- libdimension/platform.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libdimension/platform.c') diff --git a/libdimension/platform.c b/libdimension/platform.c index e8f090d..b506c10 100644 --- a/libdimension/platform.c +++ b/libdimension/platform.c @@ -36,6 +36,9 @@ #if DMNSN_SCHED_GETAFFINITY #include /* For sched_getaffinity() */ #endif +#if DMNSN_TIMES + #include +#endif void dmnsn_backtrace(FILE *file) @@ -96,3 +99,33 @@ dmnsn_ncpus(void) return 1; #endif } + +#if DMNSN_TIMES +/** Clock ticks per second. */ +static long clk_tck = 0; +#endif + +void +dmnsn_get_times(dmnsn_timer *timer) +{ +#if DMNSN_TIMES + /* Figure out the clock ticks per second */ + if (!clk_tck) { + clk_tck = sysconf(_SC_CLK_TCK); + if (clk_tck == -1) { + dmnsn_error(DMNSN_SEVERITY_MEDIUM, "sysconf(_SC_CLK_TCK) failed."); + clk_tck = 1000000L; + } + } + + struct tms buf; + clock_t real = times(&buf); + timer->real = (double)real/clk_tck; + timer->user = (double)buf.tms_utime/clk_tck; + timer->system = (double)buf.tms_stime/clk_tck; +#else + timer->real = 0.0; + timer->user = 0.0; + timer->system = 0.0; +#endif +} -- cgit v1.2.3