From 7f869a1b5a909e54300e43194f804430c7bd3739 Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Sun, 1 Jul 2012 23:32:47 +0200 Subject: [PATCH] demos: spiv: Update cpu counter for multithread filters. --- demos/spiv/cpu_timer.c | 38 ++++++++++++++++++++++++++------------ demos/spiv/cpu_timer.h | 6 ++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/demos/spiv/cpu_timer.c b/demos/spiv/cpu_timer.c index 88455eb6..6c26d610 100644 --- a/demos/spiv/cpu_timer.c +++ b/demos/spiv/cpu_timer.c @@ -23,26 +23,40 @@ #include #include "cpu_timer.h" +static void to_time(int *sec, int *nsec, struct timespec *start, + struct timespec *stop) +{ + if (stop->tv_nsec < start->tv_nsec) { + *sec = stop->tv_sec - start->tv_sec - 1; + *nsec = stop->tv_nsec + 1000000000 - start->tv_nsec; + } else { + *sec = stop->tv_sec - start->tv_sec; + *nsec = stop->tv_nsec - start->tv_nsec; + } +} + void cpu_timer_start(struct cpu_timer *self, const char *name) { self->name = name; - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &self->t_start); + + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &self->t_cpu_start); + clock_gettime(CLOCK_MONOTONIC, &self->t_real_start); } void cpu_timer_stop(struct cpu_timer *self) { - clock_gettime(CLOCK_THREAD_CPUTIME_ID, &self->t_stop); + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &self->t_cpu_stop); + clock_gettime(CLOCK_MONOTONIC, &self->t_real_stop); - int sec; - int nsec; + int cpu_sec; + int cpu_nsec; - if (self->t_stop.tv_nsec < self->t_start.tv_nsec) { - sec = self->t_stop.tv_sec - self->t_start.tv_sec - 1; - nsec = self->t_stop.tv_nsec + 1000000000 - self->t_start.tv_nsec; - } else { - sec = self->t_stop.tv_sec - self->t_start.tv_sec; - nsec = self->t_stop.tv_nsec - self->t_start.tv_nsec; - } + int real_sec; + int real_nsec; + + to_time(&cpu_sec, &cpu_nsec, &self->t_cpu_start, &self->t_cpu_stop); + to_time(&real_sec, &real_nsec, &self->t_real_start, &self->t_real_stop); - printf("TIMER '%s' %i.%09i sec\n", self->name, sec, nsec); + printf("TIMER '%s' CPU=%i.%09is REAL=%i.%09is\n", self->name, + cpu_sec, cpu_nsec, real_sec, real_nsec); } diff --git a/demos/spiv/cpu_timer.h b/demos/spiv/cpu_timer.h index 1b411441..1d8c8e7d 100644 --- a/demos/spiv/cpu_timer.h +++ b/demos/spiv/cpu_timer.h @@ -32,8 +32,10 @@ #include struct cpu_timer { - struct timespec t_start; - struct timespec t_stop; + struct timespec t_cpu_start; + struct timespec t_cpu_stop; + struct timespec t_real_start; + struct timespec t_real_stop; const char *name; }; -- 2.11.4.GIT