2 * Copyright © 2015 Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com>
3 * Ran Benita <ran234@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #if defined(HAVE_CLOCK_GETTIME)
26 #define USE_CLOCK_GETTIME
28 #elif defined(__MACH__) && __MACH__ == 1
29 #define USE_MACH_ABSOLUTE_TIME
30 #include <mach/mach_time.h>
32 /* gettimeofday() - a last resort */
43 set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
45 dest->seconds = seconds;
46 dest->milliseconds = milliseconds;
50 normalize_bench_time(struct bench_time *obj)
52 if (obj->milliseconds >= 0) {
55 obj->milliseconds += 1000000;
60 bench_timer_reset(struct bench_timer *self)
62 #if defined(USE_MACH_ABSOLUTE_TIME)
63 mach_timebase_info_data_t info;
64 if (mach_timebase_info(&info) == 0) {
65 self->scaling_factor = info.numer / info.denom;
68 self->start.seconds = 0L;
69 self->start.milliseconds = 0L;
70 self->stop.seconds = 0L;
71 self->stop.milliseconds = 0L;
75 bench_timer_start(struct bench_timer *self)
77 #if defined(USE_CLOCK_GETTIME)
80 (void)clock_gettime(CLOCK_MONOTONIC, &val);
82 /* With conversion from nanosecond to millisecond */
83 set_bench_time(&self->start, val.tv_sec, val.tv_nsec / 1000);
84 #elif defined(USE_MACH_ABSOLUTE_TIME)
87 val = mach_absolute_time();
89 /* With conversion from nanosecond to millisecond */
90 set_bench_time(&self->start,
91 self->scaling_factor * val / 1000000000,
92 self->scaling_factor * val % 1000000000 / 1000);
96 (void)gettimeofday(&val, NULL);
98 set_bench_time(&self->start, val.tv_sec, val.tv_usec);
103 bench_timer_stop(struct bench_timer *self)
105 #if defined(USE_CLOCK_GETTIME)
108 (void)clock_gettime(CLOCK_MONOTONIC, &val);
110 /* With conversion from nanosecond to millisecond */
111 set_bench_time(&self->stop, val.tv_sec, val.tv_nsec / 1000);
112 #elif defined(USE_MACH_ABSOLUTE_TIME)
115 val = mach_absolute_time();
117 /* With conversion from nanosecond to millisecond */
118 set_bench_time(&self->stop,
119 self->scaling_factor * val / 1000000000,
120 self->scaling_factor * val % 1000000000 / 1000);
124 (void)gettimeofday(&val, NULL);
126 set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
131 bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
133 result->seconds = self->stop.seconds - self->start.seconds;
134 result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
138 bench_timer_get_elapsed_time_str(struct bench_timer *self)
140 struct bench_time elapsed;
143 bench_timer_get_elapsed_time(self, &elapsed);
144 normalize_bench_time(&elapsed);
145 asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);