-
Notifications
You must be signed in to change notification settings - Fork 0
/
cprt.c
306 lines (255 loc) · 7.24 KB
/
cprt.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* cprt.c - Module for portable functions that can't be done
* with macros in the cprt.h file.
* See https://github.com/fordsfords/cprt
*/
/*
# This code and its documentation is Copyright 2002-2021 Steven Ford
# and licensed "public domain" style under Creative Commons "CC0":
# https://creativecommons.org/publicdomain/zero/1.0/
# To the extent possible under law, the contributors to this project have
# waived all copyright and related or neighboring rights to this work.
# In other words, you can use this code for any purpose without any
# restrictions. This work is published from: United States. The project home
# is https://github.com/fordsfords/cprt
*/
#if ! defined(_WIN32)
/* Unix */
#define _GNU_SOURCE
#endif
#include "cprt.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#if defined(_WIN32)
LARGE_INTEGER cprt_frequency;
LARGE_INTEGER cprt_start_time;
#endif
#if defined(_WIN32)
int cprt_timeofday(struct cprt_timeval *tv, void *unused_tz)
{
FILETIME tfile;
uint64_t time;
/* Static constant to remove time skew between FILETIME and POSIX
* time. POSIX and Win32 use different epochs (Jan. 1, 1970 v.s.
* Jan. 1, 1601). The following constant defines the difference
* in 100ns ticks. */
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
GetSystemTimeAsFileTime(&tfile);
time = (uint64_t)tfile.dwLowDateTime;
time += ((uint64_t)tfile.dwHighDateTime) << 32;
tv->tv_sec = (long)((time - EPOCH) / 10000000L);
tv->tv_usec = (long)(((time - EPOCH) / 10) % 1000000L);
return 0;
} /* cprt_timeofday */
void cprt_inittime()
{
QueryPerformanceFrequency(&cprt_frequency);
QueryPerformanceCounter(&cprt_start_time);
} /* cprt_inittime */
void cprt_gettime(struct cprt_timespec *ts)
{
LARGE_INTEGER ticks;
uint64_t ns;
QueryPerformanceCounter(&ticks);
ns = ticks.QuadPart - cprt_start_time.QuadPart;
ns *= (1000000000/cprt_frequency.QuadPart);
ts->tv_sec = (time_t)(ns / 1000000000);
ts->tv_nsec = (long)(ns % 1000000000);
} /* cprt_gettime */
#elif defined(__APPLE__)
void cprt_inittime()
{
} /* cprt_inittime */
#else /* Non-Apple Unixes */
void cprt_inittime()
{
} /* cprt_inittime */
#endif
void cprt_localtime_r(time_t *timep, struct tm *result)
{
#if defined(_WIN32)
int i = localtime_s(result, timep);
#else /* Unixes */
localtime_r(timep, result);
#endif
} /* cprt_localtime_r */
char *cprt_strerror(int errnum, char *buffer, size_t buf_sz)
{
#if defined(_WIN32)
strerror_s(buffer, buf_sz, errnum);
#elif defined(__linux__)
char work_buffer[1024];
char *err_str;
/* Note that this uses the GNU-variant of strerror_r. */
err_str = strerror_r(errnum, work_buffer, sizeof(work_buffer));
if (err_str != NULL) {
strncpy(buffer, err_str, buf_sz);
buffer[buf_sz-1] = '\0'; /* make sure it has a null term. */
}
#else /* Non-Linux Unix. */
strerror_r(errnum, buffer, buf_sz);
#endif
return buffer;
} /* cprt_strerror */
void cprt_set_affinity(uint64_t in_mask)
{
#if defined(_WIN32)
DWORD_PTR rc;
rc = SetThreadAffinityMask(GetCurrentThread(), in_mask);
if (rc == 0) {
errno = GetLastError();
CPRT_PERRNO("SetThreadAffinityMask");
}
#elif defined(__linux__)
cpu_set_t cpuset;
int i;
uint64_t bit = 1;
CPU_ZERO(&cpuset);
for (i = 0; i < 64; i++) {
if ((in_mask & bit) == bit) {
CPU_SET(i, &cpuset);
}
bit = bit << 1;
}
CPRT_EOK0(errno = pthread_setaffinity_np(
pthread_self(), sizeof(cpuset), &cpuset));
#else /* Non-Linux Unix. */
#endif
} /* cprt_set_affinity */
/* Return 0 on success, -1 on error (sets errno). */
int cprt_try_affinity(uint64_t in_mask)
{
#if defined(_WIN32)
DWORD_PTR rc;
rc = SetThreadAffinityMask(GetCurrentThread(), in_mask);
if (rc == 0) {
errno = GetLastError();
return -1;
}
#elif defined(__linux__)
cpu_set_t cpuset;
int i;
uint64_t bit = 1;
CPU_ZERO(&cpuset);
for (i = 0; i < 64; i++) {
if ((in_mask & bit) == bit) {
CPU_SET(i, &cpuset);
}
bit = bit << 1;
}
errno = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
if (errno != 0) {
return -1;
}
#else /* Non-Linux Unix. */
#endif
return 0;
} /* cprt_try_affinity */
#define CPRT_MAX_EVENTS 1024
int cprt_num_events = 0;
int cprt_events[CPRT_MAX_EVENTS];
void cprt_event(int event)
{
cprt_events[(CPRT_ATOMIC_INC_VAL(&cprt_num_events) - 1) % CPRT_MAX_EVENTS] =
event;
} /* cprt_event */
void cprt_dump_events(FILE *fd)
{
int i, n;
n = cprt_num_events;
printf("cprt_num_events=%d\n", n);
for (i = 1; i <= CPRT_MAX_EVENTS; i++) {
fprintf(fd, " cprt_event[%d] = %09d\n",
(n - i), cprt_events[(n - i) % CPRT_MAX_EVENTS]);
if (n == i) {
break; /* There were less than CPRT_MAX_EVENTS events. */
}
}
} /* cprt_dump_events */
/* Portable getopt(). */
char* cprt_optarg;
int cprt_optopt;
int cprt_optind = 1; /* Next argv to process. */
int cprt_opterr = 1; /* Print errors to stderr. */
int cprt_getopt(int argc, char *const argv[], const char *optstring)
{
char *argv_str;
char *opt_spec; /* Option specifier in optstring. */
int argv_len;
cprt_optarg = NULL;
cprt_optopt = 0;
if (cprt_optind >= argc) {
/* End of command line. */
return EOF;
};
argv_str = argv[cprt_optind];
argv_len = (int)strlen(argv_str);
if (argv_len < 2) {
/* A single character, even if a '-', is not an option. Its an arg. */
return EOF;
}
if (argv_str[0] != '-') {
/* No more options, this argv is the first arg. */
return EOF;
}
if (strcmp(argv_str, "--") == 0) {
/* Special "--" option; next argv is arg. */
cprt_optind++;
return EOF;
}
cprt_optopt = argv_str[1];
if (! isgraph(cprt_optopt)) {
if (cprt_opterr != 0) {
fprintf(stderr, "Error, argv[%d] has non-printable character.\n", cprt_optind);
}
return '?';
}
opt_spec = strchr("-:;", cprt_optopt);
if (strchr("-:;", cprt_optopt) != NULL) {
if (cprt_opterr != 0) {
fprintf(stderr, "Error, '%c' not a valid option character.\n", cprt_optopt);
}
return '?';
}
opt_spec = strchr(optstring, cprt_optopt);
if (opt_spec == NULL) {
if (cprt_opterr != 0) {
fprintf(stderr, "Error, '%c' not a defined option.\n", cprt_optopt);
}
return '?';
}
/* User specified a valid option. See if needs value. */
if (opt_spec[1] == ':') {
/* Value is needed. See if it's part of the same argv. */
if (argv_len > 2) {
cprt_optarg = &argv_str[2];
cprt_optind++; /* Next option. */
return cprt_optopt;
}
cprt_optind++; /* Value must be next argv. */
if (cprt_optind >= argc) {
/* Ran out of argvs, value not supplied. */
if (cprt_opterr != 0) {
fprintf(stderr, "Error, '-%c' requires value.\n", cprt_optopt);
}
return '?';
}
cprt_optarg = argv[cprt_optind];
cprt_optind++; /* Next option. */
return cprt_optopt;
}
else { /* Option has no value. */
if (argv_len > 2) {
/* This getopt() implementation does not support multiple options
* in one argv. */
if (cprt_opterr != 0) {
fprintf(stderr, "Error, '-%c' must have no value.\n", cprt_optopt);
}
return '?';
}
cprt_optind++; /* Next option. */
return cprt_optopt;
}
} /* cprt_getopt */