diff --git a/src/clocks.c b/src/clocks.c index 0d0e89bac3ccb05420a5ca21ccb54dbabfa93783..5843ec8c49d229fcbaf4db40f24e83233ae69ed8 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -94,40 +94,40 @@ unsigned long long clocks_cpufreq() { if (cpufreq > 0) return cpufreq; - /* Look for the system value, if available. */ -#ifdef __linux__ - FILE *file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", - "r"); - if (file != NULL) { - unsigned long long maxfreq; - if (fscanf(file, "%llu", &maxfreq) == 1) { - cpufreq = maxfreq; - } - fclose(file); - } -#endif - #ifdef HAVE_CLOCK_GETTIME - /* If not try to time a nanosleep() in ticks. */ - if (cpufreq == 0) { - struct clockstime time1; - struct clockstime time2; + /* Try to time a nanosleep() in ticks. */ + struct clockstime time1; + struct clockstime time2; - struct timespec sleep; - sleep.tv_sec = 0; - sleep.tv_nsec = SLEEPTIME; + struct timespec sleep; + sleep.tv_sec = 0; + sleep.tv_nsec = SLEEPTIME; - clocks_gettime(&time1); - ticks tic = getticks(); + clocks_gettime(&time1); + ticks tic = getticks(); - /* Could do some calculation, but constant_tsc should protect us. */ - nanosleep(&sleep, NULL); + /* Could do some calculation, but constant_tsc should protect us. */ + nanosleep(&sleep, NULL); - clocks_gettime(&time2); - ticks toc = getticks(); - double realsleep = clocks_diff(&time1, &time2); + clocks_gettime(&time2); + ticks toc = getticks(); + double realsleep = clocks_diff(&time1, &time2); + + cpufreq = (signed long long) (double)(toc - tic) * 1.0/realsleep * 1000.0; +#endif - cpufreq = (signed long long) (double)(toc - tic) * 1.0 / realsleep; + /* Look for the system value, if available. Tends to be too large. */ +#ifdef __linux__ + if (cpufreq == 0) { + FILE *file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", + "r"); + if (file != NULL) { + unsigned long long maxfreq; + if (fscanf(file, "%llu", &maxfreq) == 1) { + cpufreq = maxfreq * 1000; + } + fclose(file); + } } #endif diff --git a/src/engine.c b/src/engine.c index d842f4835c51b29f9b017867c30e12ee78df5d32..61774c3abaa6d0b2abac2db711f7a4ede9a3ddf3 100644 --- a/src/engine.c +++ b/src/engine.c @@ -28,7 +28,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <time.h> #include <unistd.h> #include <stdbool.h> @@ -47,6 +46,7 @@ /* Local headers. */ #include "atomic.h" #include "cell.h" +#include "clocks.h" #include "cycle.h" #include "debug.h" #include "error.h" @@ -1465,29 +1465,6 @@ void engine_init_particles(struct engine *e) { e->step = -1; } - -/** - * brief Get difference in milli-seconds between two timespecs. - * - * @param start the start time. - * @param end the end time. - * - * @return the difference in milli-secinds. - */ -static double time_diff(struct timespec start, struct timespec end) -{ - struct timespec temp; - if ((end.tv_nsec-start.tv_nsec)<0) { - temp.tv_sec = end.tv_sec-start.tv_sec-1; - temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; - } else { - temp.tv_sec = end.tv_sec-start.tv_sec; - temp.tv_nsec = end.tv_nsec-start.tv_nsec; - } - return (double)temp.tv_sec*1000.0 + (double)temp.tv_nsec*1.0E-6; -} - - /** * @brief Let the #engine loose to compute the forces. * @@ -1506,8 +1483,8 @@ void engine_step(struct engine *e) { TIMER_TIC2; - struct timespec time1, time2; - clock_gettime(CLOCK_REALTIME, &time1); + struct clockstime time1, time2; + clocks_gettime(&time1); /* Collect the cell data. */ for (k = 0; k < s->nr_cells; k++) @@ -1647,11 +1624,12 @@ void engine_step(struct engine *e) { TIMER_TOC2(timer_step); - clock_gettime(CLOCK_REALTIME, &time2); + clocks_gettime(&time2); - e->wallclock_time_ticks = ((double)timers[timer_count - 1]) / CPU_TPS * 1000; + e->wallclock_time_ticks = + ((double)timers[timer_count - 1]) / clocks_cpufreq() * 1000; - e->wallclock_time = (float) time_diff(time1, time2); + e->wallclock_time = (float) clocks_diff(&time1, &time2); // printParticle(e->s->parts, e->s->xparts,1000, e->s->nr_parts); // printParticle(e->s->parts, e->s->xparts,515050, e->s->nr_parts); }