From 7ed1d30aa7be38c16f851480c692e9e4fbd76a3a Mon Sep 17 00:00:00 2001 From: "Peter W. Draper" <p.w.draper@durham.ac.uk> Date: Mon, 7 Mar 2016 12:14:39 +0000 Subject: [PATCH] Don't include date in message time stamps, increase accuracy to 1/10th second, if no HAVE_GETTIME etc. report ticks since the program started. --- examples/main.c | 11 +++++++---- src/clocks.c | 29 +++++++++++++++++++---------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/main.c b/examples/main.c index 9869dbecad..07e1360ce6 100644 --- a/examples/main.c +++ b/examples/main.c @@ -126,6 +126,9 @@ int main(int argc, char *argv[]) { &initial_partition.grid[1], &initial_partition.grid[0]); #endif + /* Initialize CPU frequency, this also starts time. */ + clocks_set_cpufreq(cpufreq); + /* Greeting message */ if (myrank == 0) greetings(); @@ -332,11 +335,11 @@ int main(int argc, char *argv[]) { aFactor(&us, UNIT_CONV_ENTROPY), hFactor(&us, UNIT_CONV_ENTROPY)); } - /* Initialize CPU frequency. */ - clocks_set_cpufreq(cpufreq); - cpufreq = clocks_get_cpufreq(); - if (myrank == 0) + /* Report CPU frequency. */ + if (myrank == 0) { + cpufreq = clocks_get_cpufreq(); message("CPU frequency used for tick conversion: %llu Hz", cpufreq); + } /* Check we have sensible time step bounds */ if (dt_min > dt_max) diff --git a/src/clocks.c b/src/clocks.c index d572e9d850..ce220855d9 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -41,6 +41,9 @@ /* The CPU frequency used to convert ticks to seconds. */ static unsigned long long clocks_cpufreq = 0; +/* Ticks when the CPU frequency was initialised. Used in elapsed. */ +static ticks clocks_start = 0; + /* The units of any returned times. */ static char *clocks_units[] = {"ms", "ticks"}; static int clocks_units_index = 0; @@ -102,6 +105,7 @@ void clocks_set_cpufreq(unsigned long long freq) { } else { clocks_estimate_cpufreq(); } + clocks_start = getticks(); } /** @@ -223,29 +227,34 @@ double clocks_from_ticks(ticks tics) { const char *clocks_getunit() { return clocks_units[clocks_units_index]; } /** - * @brief returns a string containing the local date and time + * @brief returns the time of day to 1/10th second accuracy. * - * The date is return in the format [YYYY-MM-DD hh:mm:ss] + * The date is return in the format [hh:mm:ss.s] * - * @result the current time. + * @result the current time of day. */ const char *clocks_get_timeofday() { -#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_LOCALTIME) && \ + static char buffer[40]; + +#if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_LOCALTIME) && \ defined(HAVE_STRFTIME) - struct timeval time; + struct timespec time; struct tm *local_time; - static char buffer[40]; + char fmttime[40]; - /* Get the local time of day */ - gettimeofday(&time, NULL); + clock_gettime(CLOCK_REALTIME, &time); local_time = localtime(&time.tv_sec); /* Make it a string */ - strftime(buffer, 40, "[%F %T]", local_time); + strftime(fmttime, 40, "%T", local_time); + + /* 1/10 seconds. */ + int tseconds = time.tv_nsec / 100000000; + sprintf(buffer, "[%s.%01d]", fmttime, tseconds); #else - static char buffer[40] = "[Unknown time]"; + sprintf(buffer, "[%08.1f]", clocks_diff_ticks(getticks(),clocks_start)/100.0); #endif return buffer; } -- GitLab