bench: fix compilation on hurd
[platform/upstream/libxkbcommon.git] / bench / bench.c
1 /*
2  * Copyright © 2015 Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com>
3  *                  Ran Benita <ran234@gmail.com>
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  */
24
25 #if defined(HAVE_CLOCK_GETTIME)
26 #define USE_CLOCK_GETTIME
27 #include <time.h>
28 #elif defined(__MACH__) && __MACH__ == 1
29 #define USE_MACH_ABSOLUTE_TIME
30 #include <mach/mach_time.h>
31 #else
32 /* gettimeofday() - a last resort */
33 #include <sys/time.h>
34 #endif
35
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #include "bench.h"
43
44 static void
45 set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
46 {
47     dest->seconds = seconds;
48     dest->milliseconds = milliseconds;
49 }
50
51 static void
52 normalize_bench_time(struct bench_time *obj)
53 {
54     if (obj->milliseconds >= 0) {
55         return;
56     }
57     obj->milliseconds += 1000000;
58     obj->seconds--;
59 }
60
61 void
62 bench_timer_reset(struct bench_timer *self)
63 {
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;
68     }
69 #endif
70     self->start.seconds = 0L;
71     self->start.milliseconds = 0L;
72     self->stop.seconds = 0L;
73     self->stop.milliseconds = 0L;
74 }
75
76 void
77 bench_timer_start(struct bench_timer *self)
78 {
79 #if defined(USE_CLOCK_GETTIME)
80     struct timespec val;
81
82     (void) clock_gettime(CLOCK_MONOTONIC, &val);
83
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)
87     uint64_t val;
88
89     val = mach_absolute_time();
90
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);
95 #else
96     struct timeval val;
97
98     (void) gettimeofday(&val, NULL);
99
100     set_bench_time(&self->start, val.tv_sec, val.tv_usec);
101 #endif
102 }
103
104 void
105 bench_timer_stop(struct bench_timer *self)
106 {
107 #if defined(USE_CLOCK_GETTIME)
108     struct timespec val;
109
110     (void) clock_gettime(CLOCK_MONOTONIC, &val);
111
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)
115     uint64_t val;
116
117     val = mach_absolute_time();
118
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);
123 #else
124     struct timeval val;
125
126     (void) gettimeofday(&val, NULL);
127
128     set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
129 #endif
130 }
131
132 void
133 bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
134 {
135     result->seconds = self->stop.seconds - self->start.seconds;
136     result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
137 }
138
139 char *
140 bench_timer_get_elapsed_time_str(struct bench_timer *self)
141 {
142     struct bench_time elapsed;
143     char *buf;
144     int ret;
145
146     bench_timer_get_elapsed_time(self, &elapsed);
147     normalize_bench_time(&elapsed);
148     ret = asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);
149     assert(ret >= 0);
150
151     return buf;
152 }