1 // Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 #include "stopwatch.hpp"
7 
8 #include <cassert>
9 #include <ctime>
10 
11 
12 #define USEC_PER_SEC  1000000ULL
13 #define NSEC_PER_USEC 1000ULL
14 
15 
get_total_elapsed_microseconds() const16 stop_watch::usec_t stop_watch::get_total_elapsed_microseconds() const {
17     usec_t elapsed = total_elapsed_;
18 
19     if (started_)
20         elapsed += get_elapsed();
21 
22     return elapsed;
23 }
24 
get_total_elapsed_seconds() const25 stop_watch::usec_t stop_watch::get_total_elapsed_seconds() const {
26     return get_total_elapsed_microseconds() / USEC_PER_SEC;
27 }
28 
now()29 stop_watch::usec_t stop_watch::now() {
30     struct timespec ts;
31 
32     const int ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
33     assert(!ret);
34     static_cast<void>(ret); // prevent warning in release build
35 
36     return (usec_t) ts.tv_sec * USEC_PER_SEC + (usec_t) ts.tv_nsec / NSEC_PER_USEC;
37 }
38 
39