diff options
-rw-r--r-- | configure.ac | 19 | ||||
-rw-r--r-- | libdimension/platform.c | 33 | ||||
-rw-r--r-- | libdimension/platform.h | 7 | ||||
-rw-r--r-- | libdimension/timer.c | 34 |
4 files changed, 66 insertions, 27 deletions
diff --git a/configure.ac b/configure.ac index 336c996..6fbf585 100644 --- a/configure.ac +++ b/configure.ac @@ -125,6 +125,25 @@ AC_COMPILE_IFELSE([ AC_MSG_RESULT([no])] ) +AC_MSG_CHECKING([for times()]) +AC_COMPILE_IFELSE([ + AC_LANG_PROGRAM( + [ + #include <sys/times.h> + #include <unistd.h> + ], + [ + long clk_tck = sysconf(_SC_CLK_TCK); + struct tms buf; + clock_t real = times(&buf); + ] + )], + [AC_DEFINE([DMNSN_TIMES], [1]) + AC_MSG_RESULT([yes])], + [AC_DEFINE([DMNSN_TIMES], [0]) + AC_MSG_RESULT([no])] +) + AC_MSG_CHECKING([for ioctl(TIOCGWINSZ)]) AC_COMPILE_IFELSE([ AC_LANG_PROGRAM( 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 <sched.h> /* For sched_getaffinity() */ #endif +#if DMNSN_TIMES + #include <sys/times.h> +#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 +} diff --git a/libdimension/platform.h b/libdimension/platform.h index fbf467b..83b5c37 100644 --- a/libdimension/platform.h +++ b/libdimension/platform.h @@ -55,4 +55,11 @@ bool dmnsn_is_little_endian(void); */ size_t dmnsn_ncpus(void); +/** + * Calculate process times. + * @param[out] timer The timer in which to store the current total process + * times. + */ +void dmnsn_get_times(dmnsn_timer *timer); + #endif /* DIMENSION_IMPL_PLATFORM_H */ diff --git a/libdimension/timer.c b/libdimension/timer.c index 53d7101..fa96b06 100644 --- a/libdimension/timer.c +++ b/libdimension/timer.c @@ -23,44 +23,24 @@ * Performance counter. */ -#include "dimension.h" -#include <sys/times.h> -#include <unistd.h> - -/** Clock ticks per second. */ -static long clk_tck = 0; +#include "dimension-impl.h" dmnsn_timer * dmnsn_new_timer(void) { - /* 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; - } - } - dmnsn_timer *timer = dmnsn_malloc(sizeof(dmnsn_timer)); - - 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; - + dmnsn_get_times(timer); return timer; } void dmnsn_complete_timer(dmnsn_timer *timer) { - struct tms buf; - clock_t real = times(&buf); - timer->real = (double)real/clk_tck - timer->real; - timer->user = (double)buf.tms_utime/clk_tck - timer->user; - timer->system = (double)buf.tms_stime/clk_tck - timer->system; + dmnsn_timer now; + dmnsn_get_times(&now); + timer->real = now.real - timer->real; + timer->user = now.user - timer->user; + timer->system = now.system - timer->system; } void |