From 161c21384b91799c2b26f1aa776e081eed002add Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 5 Sep 2018 13:51:43 -0400 Subject: [PATCH 2/3] Invert the sense of psll_linux_clock_ticks. It is always used to divide some other number, so instead save its reciprocal. Since division is generally slower than multiplication, doing one reciprocal and multiplication everywhere else should be slightly faster. Signed-off-by: Elliott Sales de Andrade --- src/api-linux.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/api-linux.c b/src/api-linux.c index 02f6268..d2b6193 100644 --- a/src/api-linux.c +++ b/src/api-linux.c @@ -15,7 +15,7 @@ #include "common.h" double psll_linux_boot_time = 0; -double psll_linux_clock_ticks = 0; +double psll_linux_clock_period = 0; typedef struct { char state; @@ -29,7 +29,7 @@ typedef struct { #define PS__TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) #define PS__CHECK_STAT(stat, handle) \ - if (psll_linux_boot_time + (stat).starttime / psll_linux_clock_ticks != \ + if (psll_linux_boot_time + (stat).starttime * psll_linux_clock_period != \ handle->create_time) { \ ps__no_such_process(handle->pid, 0); \ ps__throw_error(); \ @@ -226,7 +226,7 @@ void ps__check_for_zombie(ps_handle_t *handle, int err) { ps__throw_error(); } - if (psll_linux_boot_time + stat.starttime / psll_linux_clock_ticks != + if (psll_linux_boot_time + stat.starttime * psll_linux_clock_period != handle->create_time) { ps__no_such_process(handle->pid, 0); err = 1; @@ -261,12 +261,13 @@ int psll_linux_get_boot_time() { return 0; } -int psll_linux_get_clock_ticks(void) { - psll_linux_clock_ticks = sysconf(_SC_CLK_TCK); +int psll_linux_get_clock_period(void) { + double psll_linux_clock_ticks = sysconf(_SC_CLK_TCK); if (psll_linux_clock_ticks == -1) { ps__set_error_from_errno(); return -1; } + psll_linux_clock_period = 1.0 / psll_linux_clock_ticks; return 0; } @@ -280,14 +281,14 @@ int psll_linux_ctime(long pid, double *ctime) { if (ret) return ret; } - if (!psll_linux_clock_ticks) { - ret = psll_linux_get_clock_ticks(); + if (!psll_linux_clock_period) { + ret = psll_linux_get_clock_period(); if (ret) { ps__throw_error(); } } - *ctime = psll_linux_boot_time + stat.starttime / psll_linux_clock_ticks; + *ctime = psll_linux_boot_time + stat.starttime * psll_linux_clock_period; return 0; } @@ -716,10 +717,10 @@ SEXP psll_cpu_times(SEXP p) { PS__CHECK_STAT(stat, handle); PROTECT(result = allocVector(REALSXP, 4)); - REAL(result)[0] = stat.utime / psll_linux_clock_ticks; - REAL(result)[1] = stat.stime / psll_linux_clock_ticks; - REAL(result)[2] = stat.cutime / psll_linux_clock_ticks; - REAL(result)[3] = stat.cstime / psll_linux_clock_ticks; + REAL(result)[0] = stat.utime * psll_linux_clock_period; + REAL(result)[1] = stat.stime * psll_linux_clock_period; + REAL(result)[2] = stat.cutime * psll_linux_clock_period; + REAL(result)[3] = stat.cstime * psll_linux_clock_period; PROTECT(names = ps__build_string("user", "system", "childen_user", "children_system", NULL)); setAttrib(result, R_NamesSymbol, names); -- 2.17.1