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 */
44 set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
46 dest->seconds = seconds;
47 dest->milliseconds = milliseconds;
51 normalize_bench_time(struct bench_time *obj)
53 if (obj->milliseconds >= 0) {
56 obj->milliseconds += 1000000;
61 bench_timer_reset(struct bench_timer *self)
63 #if defined(USE_MACH_ABSOLUTE_TIME)
64 mach_timebase_info_data_t info;
65 if (mach_timebase_info(&info) == 0) {
66 self->scaling_factor = info.numer / info.denom;
69 self->start.seconds = 0L;
70 self->start.milliseconds = 0L;
71 self->stop.seconds = 0L;
72 self->stop.milliseconds = 0L;
76 bench_timer_start(struct bench_timer *self)
78 #if defined(USE_CLOCK_GETTIME)
81 (void) clock_gettime(CLOCK_MONOTONIC, &val);
83 /* With conversion from nanosecond to millisecond */
84 set_bench_time(&self->start, val.tv_sec, val.tv_nsec / 1000);
85 #elif defined(USE_MACH_ABSOLUTE_TIME)
88 val = mach_absolute_time();
90 /* With conversion from nanosecond to millisecond */
91 set_bench_time(&self->start,
92 self->scaling_factor * val / 1000000000,
93 self->scaling_factor * val % 1000000000 / 1000);
97 (void) gettimeofday(&val, NULL);
99 set_bench_time(&self->start, val.tv_sec, val.tv_usec);
104 bench_timer_stop(struct bench_timer *self)
106 #if defined(USE_CLOCK_GETTIME)
109 (void) clock_gettime(CLOCK_MONOTONIC, &val);
111 /* With conversion from nanosecond to millisecond */
112 set_bench_time(&self->stop, val.tv_sec, val.tv_nsec / 1000);
113 #elif defined(USE_MACH_ABSOLUTE_TIME)
116 val = mach_absolute_time();
118 /* With conversion from nanosecond to millisecond */
119 set_bench_time(&self->stop,
120 self->scaling_factor * val / 1000000000,
121 self->scaling_factor * val % 1000000000 / 1000);
125 (void) gettimeofday(&val, NULL);
127 set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
132 bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
134 result->seconds = self->stop.seconds - self->start.seconds;
135 result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
139 bench_timer_get_elapsed_time_str(struct bench_timer *self)
141 struct bench_time elapsed;
145 bench_timer_get_elapsed_time(self, &elapsed);
146 normalize_bench_time(&elapsed);
147 ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);