bench: Modify benchmarks for a wider range of platforms
[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 <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39
40 #include "bench.h"
41
42 static void
43 set_bench_time(struct bench_time *dest, long seconds, long milliseconds)
44 {
45     dest->seconds = seconds;
46     dest->milliseconds = milliseconds;
47 }
48
49 static void
50 normalize_bench_time(struct bench_time *obj)
51 {
52     if (obj->milliseconds >= 0) {
53         return;
54     }
55     obj->milliseconds += 1000000;
56     obj->seconds--;
57 }
58
59 void
60 bench_timer_reset(struct bench_timer *self)
61 {
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;
66     }
67 #endif
68     self->start.seconds = 0L;
69     self->start.milliseconds = 0L;
70     self->stop.seconds = 0L;
71     self->stop.milliseconds = 0L;
72 }
73
74 void
75 bench_timer_start(struct bench_timer *self)
76 {
77 #if defined(USE_CLOCK_GETTIME)
78     struct timespec val;
79
80     (void)clock_gettime(CLOCK_MONOTONIC, &val);
81
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)
85     uint64_t val;
86
87     val = mach_absolute_time();
88
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);
93 #else
94     struct timeval val;
95
96     (void)gettimeofday(&val, NULL);
97
98     set_bench_time(&self->start, val.tv_sec, val.tv_usec);
99 #endif
100 }
101
102 void
103 bench_timer_stop(struct bench_timer *self)
104 {
105 #if defined(USE_CLOCK_GETTIME)
106     struct timespec val;
107
108     (void)clock_gettime(CLOCK_MONOTONIC, &val);
109
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)
113     uint64_t val;
114
115     val = mach_absolute_time();
116
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);
121 #else
122     struct timeval val;
123
124     (void)gettimeofday(&val, NULL);
125
126     set_bench_time(&self->stop, val.tv_sec, val.tv_usec);
127 #endif
128 }
129
130 void
131 bench_timer_get_elapsed_time(struct bench_timer *self, struct bench_time *result)
132 {
133     result->seconds = self->stop.seconds - self->start.seconds;
134     result->milliseconds = self->stop.milliseconds - self->start.milliseconds;
135 }
136
137 char *
138 bench_timer_get_elapsed_time_str(struct bench_timer *self)
139 {
140     struct bench_time elapsed;
141     char *buf;
142
143     bench_timer_get_elapsed_time(self, &elapsed);
144     normalize_bench_time(&elapsed);
145     asprintf(&buf, "%ld.%06ld", elapsed.seconds, elapsed.milliseconds);
146
147     return buf;
148 }