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