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 */
45 set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
47 dest->seconds = seconds;
48 dest->milliseconds = milliseconds;
52 normalize_bench_time(struct bench_time *obj)
54 if (obj->milliseconds >= 0) {
57 obj->milliseconds += 1000000;
62 bench_timer_reset(struct bench_timer *self)
64 #if defined(USE_MACH_ABSOLUTE_TIME)
65 mach_timebase_info_data_t info;
66 if (mach_timebase_info(&info) == 0) {
67 self->scaling_factor = info.numer / info.denom;
70 self->start.seconds = 0L;
71 self->start.milliseconds = 0L;
72 self->stop.seconds = 0L;
73 self->stop.milliseconds = 0L;
77 bench_timer_start(struct bench_timer *self)
79 #if defined(USE_CLOCK_GETTIME)
82 (void) clock_gettime(CLOCK_MONOTONIC, &val);
84 /* With conversion from nanosecond to millisecond */
85 set_bench_time(&self->start, val.tv_sec, val.tv_nsec / 1000);
86 #elif defined(USE_MACH_ABSOLUTE_TIME)
89 val = mach_absolute_time();
91 /* With conversion from nanosecond to millisecond */
92 set_bench_time(&self->start,
93 self->scaling_factor * val / 1000000000,
94 self->scaling_factor * val % 1000000000 / 1000);
98 (void) gettimeofday(&val, NULL);
100 set_bench_time(&self->start, val.tv_sec, val.tv_usec);
105 bench_timer_stop(struct bench_timer *self)
107 #if defined(USE_CLOCK_GETTIME)
110 (void) clock_gettime(CLOCK_MONOTONIC, &val);
112 /* With conversion from nanosecond to millisecond */
113 set_bench_time(&self->stop, val.tv_sec, val.tv_nsec / 1000);
114 #elif defined(USE_MACH_ABSOLUTE_TIME)
117 val = mach_absolute_time();
119 /* With conversion from nanosecond to millisecond */
120 set_bench_time(&self->stop,
121 self->scaling_factor * val / 1000000000,
122 self->scaling_factor * val % 1000000000 / 1000);
126 (void) gettimeofday(&val, NULL);
128 set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
133 bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
135 result->seconds = self->stop.seconds - self->start.seconds;
136 result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
140 bench_timer_get_elapsed_time_str(struct bench_timer *self)
142 struct bench_time elapsed;
146 bench_timer_get_elapsed_time(self, &elapsed);
147 normalize_bench_time(&elapsed);
148 ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);