From: Mathieu Desnoyers Date: Mon, 28 Sep 2015 16:58:59 +0000 (-0400) Subject: caa_get_cycles: caa_ prefix for type, use CLOCK_MONOTONIC X-Git-Tag: v0.9.0~18 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=3fa182868e25068413fdaa1bef290365e99ab246;p=userspace-rcu.git caa_get_cycles: caa_ prefix for type, use CLOCK_MONOTONIC clock_gettime() of CLOCK_MONOTONIC mimicks better the behavior of a cycle counter, since it is not affected by NTP major time updates. Also, now prefix "cycles_t" with a caa_ prefix, so it does not clash with application namespaces. Anyway, caa_get_cycles() is mostly used in testing. Signed-off-by: Mathieu Desnoyers --- diff --git a/tests/benchmark/test_cycles_per_loop.c b/tests/benchmark/test_cycles_per_loop.c index 6ff100b..ca66ffa 100644 --- a/tests/benchmark/test_cycles_per_loop.c +++ b/tests/benchmark/test_cycles_per_loop.c @@ -33,7 +33,7 @@ static inline void loop_sleep(unsigned long loops) int main() { - cycles_t time1, time2; + caa_cycles_t time1, time2; time1 = caa_get_cycles(); loop_sleep(NR_LOOPS); diff --git a/tests/benchmark/test_looplen.c b/tests/benchmark/test_looplen.c index 16674e7..c8b5a02 100644 --- a/tests/benchmark/test_looplen.c +++ b/tests/benchmark/test_looplen.c @@ -54,8 +54,8 @@ static inline void loop_sleep(unsigned long loops) int main(int argc, char **argv) { unsigned long i; - cycles_t time1, time2; - cycles_t time_tot = 0; + caa_cycles_t time1, time2; + caa_cycles_t time_tot = 0; double cpl; for (i = 0; i < TESTS; i++) { diff --git a/tests/benchmark/test_perthreadlock_timing.c b/tests/benchmark/test_perthreadlock_timing.c index 97bdd04..c9cc65f 100644 --- a/tests/benchmark/test_perthreadlock_timing.c +++ b/tests/benchmark/test_perthreadlock_timing.c @@ -64,13 +64,13 @@ static int num_write; #define NR_READ num_read #define NR_WRITE num_write -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; void *thr_reader(void *arg) { int i, j; - cycles_t time1, time2; + caa_cycles_t time1, time2; long tidx = (long)arg; printf("thread_begin %s, tid %lu\n", @@ -100,7 +100,7 @@ void *thr_writer(void *arg) { int i, j; long tidx; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "writer", urcu_get_thread_id()); @@ -133,8 +133,8 @@ int main(int argc, char **argv) pthread_t *tid_reader, *tid_writer; void *tret; int i; - cycles_t tot_rtime = 0; - cycles_t tot_wtime = 0; + caa_cycles_t tot_rtime = 0; + caa_cycles_t tot_wtime = 0; if (argc < 2) { printf("Usage : %s nr_readers nr_writers\n", argv[0]); diff --git a/tests/benchmark/test_rwlock_timing.c b/tests/benchmark/test_rwlock_timing.c index d916071..fb9e41f 100644 --- a/tests/benchmark/test_rwlock_timing.c +++ b/tests/benchmark/test_rwlock_timing.c @@ -61,13 +61,13 @@ static int num_write; #define NR_READ num_read #define NR_WRITE num_write -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; void *thr_reader(void *arg) { int i, j; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "reader", urcu_get_thread_id()); @@ -95,7 +95,7 @@ void *thr_reader(void *arg) void *thr_writer(void *arg) { int i, j; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "writer", urcu_get_thread_id()); @@ -124,8 +124,8 @@ int main(int argc, char **argv) pthread_t *tid_reader, *tid_writer; void *tret; int i; - cycles_t tot_rtime = 0; - cycles_t tot_wtime = 0; + caa_cycles_t tot_rtime = 0; + caa_cycles_t tot_wtime = 0; if (argc < 2) { printf("Usage : %s nr_readers nr_writers\n", argv[0]); diff --git a/tests/benchmark/test_urcu_qsbr_timing.c b/tests/benchmark/test_urcu_qsbr_timing.c index 9b5195d..7619390 100644 --- a/tests/benchmark/test_urcu_qsbr_timing.c +++ b/tests/benchmark/test_urcu_qsbr_timing.c @@ -80,14 +80,14 @@ static int num_write; #define NR_READ num_read #define NR_WRITE num_write -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; void *thr_reader(void *arg) { int i, j; struct test_array *local_ptr; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "reader", urcu_get_thread_id()); @@ -124,7 +124,7 @@ void *thr_writer(void *arg) { int i, j; struct test_array *new, *old; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "writer", urcu_get_thread_id()); @@ -165,8 +165,8 @@ int main(int argc, char **argv) pthread_t *tid_reader, *tid_writer; void *tret; int i; - cycles_t tot_rtime = 0; - cycles_t tot_wtime = 0; + caa_cycles_t tot_rtime = 0; + caa_cycles_t tot_wtime = 0; if (argc < 2) { printf("Usage : %s nr_readers nr_writers\n", argv[0]); diff --git a/tests/benchmark/test_urcu_timing.c b/tests/benchmark/test_urcu_timing.c index e31b676..d586b1d 100644 --- a/tests/benchmark/test_urcu_timing.c +++ b/tests/benchmark/test_urcu_timing.c @@ -80,14 +80,14 @@ static int num_write; #define NR_READ num_read #define NR_WRITE num_write -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; -static cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *reader_time; +static caa_cycles_t __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *writer_time; void *thr_reader(void *arg) { int i, j; struct test_array *local_ptr; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "reader", urcu_get_thread_id()); @@ -123,7 +123,7 @@ void *thr_writer(void *arg) { int i, j; struct test_array *new, *old; - cycles_t time1, time2; + caa_cycles_t time1, time2; printf("thread_begin %s, tid %lu\n", "writer", urcu_get_thread_id()); @@ -164,8 +164,8 @@ int main(int argc, char **argv) pthread_t *tid_reader, *tid_writer; void *tret; int i; - cycles_t tot_rtime = 0; - cycles_t tot_wtime = 0; + caa_cycles_t tot_rtime = 0; + caa_cycles_t tot_wtime = 0; if (argc < 2) { printf("Usage : %s nr_readers nr_writers\n", argv[0]); diff --git a/urcu/arch/generic.h b/urcu/arch/generic.h index e53ac56..d423595 100644 --- a/urcu/arch/generic.h +++ b/urcu/arch/generic.h @@ -152,17 +152,19 @@ extern "C" { #ifndef HAS_CAA_GET_CYCLES #define HAS_CAA_GET_CYCLES -typedef unsigned long long cycles_t; -static inline cycles_t caa_get_cycles (void) +#include +#include + +typedef uint64_t caa_cycles_t; + +static inline caa_cycles_t caa_get_cycles (void) { - cycles_t thetime; - struct timeval tv; + struct timespec ts; - if (gettimeofday(&tv, NULL) != 0) - return 0; - thetime = ((cycles_t)tv.tv_sec) * 1000000ULL + ((cycles_t)tv.tv_usec); - return (cycles_t)thetime; + if (caa_unlikely(clock_gettime(CLOCK_MONOTONIC, &ts))) + return -1ULL; + return ((uint64_t) ts.tv_sec * 1000000000ULL) + ts.tv_nsec; } #endif /* HAS_CAA_GET_CYCLES */ diff --git a/urcu/arch/hppa.h b/urcu/arch/hppa.h index 82d2a42..f92b0ab 100644 --- a/urcu/arch/hppa.h +++ b/urcu/arch/hppa.h @@ -33,11 +33,11 @@ extern "C" { #include #define HAS_CAA_GET_CYCLES -typedef unsigned long cycles_t; +typedef unsigned long caa_cycles_t; -static inline cycles_t caa_get_cycles(void) +static inline caa_cycles_t caa_get_cycles(void) { - cycles_t cycles; + caa_cycles_t cycles; asm volatile("mfctl 16, %0" : "=r" (cycles)); return cycles; diff --git a/urcu/arch/ppc.h b/urcu/arch/ppc.h index dbdfec8..7e2b621 100644 --- a/urcu/arch/ppc.h +++ b/urcu/arch/ppc.h @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -84,15 +85,15 @@ extern "C" { #define HAS_CAA_GET_CYCLES -typedef unsigned long long cycles_t; +typedef uint64_t caa_cycles_t; #ifdef __powerpc64__ -static inline cycles_t caa_get_cycles(void) +static inline caa_cycles_t caa_get_cycles(void) { - return (cycles_t) mftb(); + return (caa_cycles_t) mftb(); } #else -static inline cycles_t caa_get_cycles(void) +static inline caa_cycles_t caa_get_cycles(void) { unsigned long h, l; @@ -102,7 +103,7 @@ static inline cycles_t caa_get_cycles(void) l = mftbl(); cmm_barrier(); if (mftbu() == h) - return (((cycles_t) h) << 32) + l; + return (((caa_cycles_t) h) << 32) + l; } } #endif diff --git a/urcu/arch/s390.h b/urcu/arch/s390.h index e12c88c..e71f99e 100644 --- a/urcu/arch/s390.h +++ b/urcu/arch/s390.h @@ -31,6 +31,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -42,11 +43,11 @@ extern "C" { #define HAS_CAA_GET_CYCLES -typedef unsigned long long cycles_t; +typedef uint64_t caa_cycles_t; -static inline cycles_t caa_get_cycles (void) +static inline caa_cycles_t caa_get_cycles (void) { - cycles_t cycles; + caa_cycles_t cycles; __asm__ __volatile__("stck %0" : "=m" (cycles) : : "cc", "memory" ); diff --git a/urcu/arch/x86.h b/urcu/arch/x86.h index a5b3a23..b4d3694 100644 --- a/urcu/arch/x86.h +++ b/urcu/arch/x86.h @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -79,11 +80,11 @@ extern "C" { | (((unsigned long long)__d) << 32); \ } while(0) -typedef unsigned long long cycles_t; +typedef uint64_t caa_cycles_t; -static inline cycles_t caa_get_cycles(void) +static inline caa_cycles_t caa_get_cycles(void) { - cycles_t ret = 0; + caa_cycles_t ret = 0; rdtscll(ret); return ret;