From ab66e3a69a7cf5e13cd028b2a459915a107e6bb0 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Sat, 21 May 2016 14:15:49 -0300 Subject: [PATCH] AP_HAL_Linux: Perf: simplify function to get current time --- libraries/AP_HAL_Linux/Perf.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/libraries/AP_HAL_Linux/Perf.cpp b/libraries/AP_HAL_Linux/Perf.cpp index 25486fc942..6a554751ba 100644 --- a/libraries/AP_HAL_Linux/Perf.cpp +++ b/libraries/AP_HAL_Linux/Perf.cpp @@ -52,6 +52,13 @@ struct perf_counter_elapsed_t { static const AP_HAL::HAL& hal = AP_HAL::get_HAL(); +static inline uint64_t now_nsec() +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_nsec + (ts.tv_sec * NSEC_PER_SEC); +} + Util::perf_counter_t Util::perf_alloc(perf_counter_type type, const char *name) { struct perf_counter_base_t *base; @@ -95,11 +102,6 @@ Util::perf_counter_t Util::perf_alloc(perf_counter_type type, const char *name) return (perf_counter_t)base; } -static inline uint64_t timespec_to_nsec(const struct timespec *ts) -{ - return ts->tv_nsec + (ts->tv_sec * NSEC_PER_SEC); -} - void Util::perf_begin(perf_counter_t perf) { struct perf_counter_elapsed_t *perf_elapsed = (struct perf_counter_elapsed_t *)perf; @@ -114,9 +116,7 @@ void Util::perf_begin(perf_counter_t perf) return; } - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - perf_elapsed->start = timespec_to_nsec(&ts); + perf_elapsed->start = now_nsec(); } void Util::perf_end(perf_counter_t perf) @@ -139,10 +139,7 @@ void Util::perf_end(perf_counter_t perf) return; } - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - const uint64_t elapsed = timespec_to_nsec(&ts) - perf_elapsed->start; - + const uint64_t elapsed = now_nsec() - perf_elapsed->start; perf_elapsed->count++; perf_elapsed->total += elapsed;