-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.c
236 lines (201 loc) · 4.78 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2017, Mellanox Technologies. All rights reserved.
* Copyright (C) 2011-2015 Konstantin Khlebnikov <koct9i@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <hugetlbfs.h>
#include <sys/time.h>
#include <malloc.h>
#include <inttypes.h>
#define APP_PREFIX "rdma_mr_lat: "
#define NSEC_PER_SEC 1000000000ll
void err(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, APP_PREFIX);
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
exit(eval);
}
void errx(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, APP_PREFIX);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(eval);
}
#ifdef HAVE_CLOCK_GETTIME
long long current_time(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
err(3, "clock_gettime failed");
return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
}
#else
long long current_time(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
err(3, "gettimeofday failed");
return tv.tv_sec * NSEC_PER_SEC + tv.tv_usec * 1000ll;
}
#endif /* HAVE_CLOCK_GETTIME */
struct suffix {
const char *txt;
long long mul;
};
static struct suffix int_suffix[] = {
{ "T", 1000000000000ll },
{ "G", 1000000000ll },
{ "M", 1000000ll },
{ "k", 1000ll },
{ "", 1ll },
{ "da", 10ll },
{ "P", 1000000000000000ll },
{ "E", 1000000000000000000ll },
{ NULL, 0ll },
};
static struct suffix size_suffix[] = {
/* These are first match for printing */
{ "PiB", 1ll<<50 },
{ "TiB", 1ll<<40 },
{ "GiB", 1ll<<30 },
{ "MiB", 1ll<<20 },
{ "KiB", 1ll<<10 },
{ "B", 1 },
{ "", 1 },
/* Should be decimal, keep binary for compatibility */
{ "k", 1ll<<10 },
{ "kb", 1ll<<10 },
{ "m", 1ll<<20 },
{ "mb", 1ll<<20 },
{ "g", 1ll<<30 },
{ "gb", 1ll<<30 },
{ "t", 1ll<<40 },
{ "tb", 1ll<<40 },
{ "pb", 1ll<<50 },
{ "eb", 1ll<<60 },
{ "sector", 512 },
{ "page", 4096 },
{ NULL, 0ll },
};
static struct suffix time_suffix[] = {
{ "hour", NSEC_PER_SEC * 60 * 60 },
{ "min", NSEC_PER_SEC * 60 },
{ "s", NSEC_PER_SEC },
{ "ms", 1000000ll },
{ "us", 1000ll },
{ "ns", 1ll },
{ "nsec", 1ll },
{ "usec", 1000ll },
{ "msec", 1000000ll },
{ "", NSEC_PER_SEC },
{ "sec", NSEC_PER_SEC },
{ "m", NSEC_PER_SEC * 60 },
{ "h", NSEC_PER_SEC * 60 * 60 },
{ NULL, 0ll },
};
long long parse_suffix(const char *str, struct suffix *sfx,
long long min, long long max)
{
char *end;
double val, den;
val = strtod(str, &end);
if (*end == '/') {
if (end == str)
val = 1;
den = strtod(end + 1, &end);
if (!den)
errx(1, "division by zero in parsing argument: %s", str);
val /= den;
}
for ( ; sfx->txt ; sfx++ ) {
if (strcasecmp(end, sfx->txt))
continue;
val *= sfx->mul;
if (val < min || val > max)
errx(1, "integer overflow at parsing argument: %s", str);
return val;
}
errx(1, "invalid suffix: \"%s\"", end);
return 0;
}
int parse_int(const char *str)
{
return parse_suffix(str, int_suffix, 0, INT_MAX);
}
ssize_t parse_size(const char *str)
{
return parse_suffix(str, size_suffix, 0, LONG_MAX);
}
off_t parse_offset(const char *str)
{
return parse_suffix(str, size_suffix, 0, LLONG_MAX);
}
long long parse_time(const char *str)
{
return parse_suffix(str, time_suffix, 0, LLONG_MAX);
}
long long parse_time_seconds(const char *str)
{
return parse_suffix(str, time_suffix, 0, LLONG_MAX) / NSEC_PER_SEC;
}
void print_suffix(long long val, struct suffix *sfx)
{
int precision;
while (val < sfx->mul && sfx->mul > 1)
sfx++;
if (val % sfx->mul == 0)
precision = 0;
else if (val >= sfx->mul * 10)
precision = 1;
else
precision = 2;
printf("%.*f", precision, val * 1.0 / sfx->mul);
if (*sfx->txt)
printf(" %s", sfx->txt);
}
void print_int(long long val)
{
print_suffix(val, int_suffix);
}
void print_size(long long val)
{
print_suffix(val, size_suffix);
}
void print_time(long long val)
{
print_suffix(val, time_suffix);
}