-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.c
351 lines (302 loc) · 7.78 KB
/
util.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) International Business Machines Corp., 2001-2004
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <pthread.h>
#include "config.h"
#include "fh.h"
#include "util.h"
uint64_t ffsb_get_filesize(char *name)
{
#ifndef HAVE_STAT64
#define STAT(a, b) do { stat((a), (b)); } while (0)
struct stat filestat;
#else
#define STAT(a, b) do { stat64((a), (b)); } while (0)
struct stat64 filestat;
#endif
STAT(name, &filestat);
return (uint64_t)filestat.st_size;
#undef STAT
}
void *ffsb_malloc(size_t size)
{
void *ptr = malloc((size));
assert(ptr != NULL);
memset(ptr, 0, size);
return ptr;
}
void *ffsb_realloc(void *ptr, size_t size)
{
void *tmp ;
/* printf("ffsb_realloc: ptr = %p size = %ld\n",ptr,size); */
if (ptr == NULL)
return ffsb_malloc(size);
tmp = realloc(ptr, size);
assert(ptr != NULL);
ptr = tmp;
return ptr;
}
void *ffsb_align_4k(void *ptr)
{
unsigned long mask = ~(0xfff); /* 12 zeros at the end */
void *ret = (void *)((unsigned long)ptr & mask);
/* printf("align_4k got %p returning %p\n",ptr,ret); */
return ret;
}
char *ffsb_strdup(const char *str)
{
int len = strlen(str);
char *dup = ffsb_malloc(len + 1);
/* !!! am I off by one here ?? */
strncpy(dup, str, len+1);
return dup;
}
size_t ffsb_strnlen(const char *str, size_t maxlen)
{
size_t index = 0;
while (index < maxlen) {
if (str[index] == '\0')
break;
index++;
}
return index;
}
/* not perfect, in case we are somehow interrupted it's borked */
void ffsb_sleep(unsigned secs)
{
struct timeval tv = { 0 , 0 };
tv.tv_sec = secs;
select(0, NULL, NULL, NULL, &tv);
}
char *ffsb_printsize(char *buf, double size, int bufsize)
{
if (size >= 1024 * 1024 * 1024)
snprintf(buf, bufsize, "%.3gGB", size / (1024 * 1024 * 1024));
else if (size >= 1024 * 1024)
snprintf(buf, bufsize, "%.3gMB", size / (1024 * 1024));
else if (size >= 1024)
snprintf(buf, bufsize, "%.3gKB", size/1024);
else
snprintf(buf, bufsize, "%.3gB", size);
return buf;
}
void ffsb_mkdir(char *dirname)
{
if (mkdir(dirname, S_IRWXU) < 0) {
fprintf(stderr, "Error creating %s\n", dirname);
perror("mkdir");
exit(1);
}
}
struct timeval tvsub(struct timeval t1, struct timeval t0)
{
struct timeval tdiff;
tdiff.tv_sec = t1.tv_sec - t0.tv_sec;
tdiff.tv_usec = t1.tv_usec - t0.tv_usec;
if (tdiff.tv_usec < 0)
tdiff.tv_sec--, tdiff.tv_usec += 1000000;
return tdiff;
}
struct timeval tvadd(struct timeval t1, struct timeval t0)
{
struct timeval tdiff;
tdiff.tv_sec = t1.tv_sec + t0.tv_sec;
tdiff.tv_usec = t1.tv_usec + t0.tv_usec;
if (tdiff.tv_usec > 1000000)
tdiff.tv_sec++, tdiff.tv_usec -= 1000000;
return tdiff;
}
double tvtodouble(struct timeval *t)
{
return ((double)t->tv_sec*(1000000.0f) + (double)t->tv_usec) /
1000000.0f;
}
double cpu_so_far(void)
{
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return
((double) rusage.ru_utime.tv_sec) +
(((double) rusage.ru_utime.tv_usec) / 1000000.0) +
((double) rusage.ru_stime.tv_sec) +
(((double) rusage.ru_stime.tv_usec) / 1000000.0);
}
double cpu_so_far_children(void)
{
struct rusage rusage;
getrusage(RUSAGE_CHILDREN, &rusage);
return
((double) rusage.ru_utime.tv_sec) +
(((double) rusage.ru_utime.tv_usec) / 1000000.0) +
((double) rusage.ru_stime.tv_sec) +
(((double) rusage.ru_stime.tv_usec) / 1000000.0);
}
/* !!!! check portability */
float getfsutil(char *dirname)
{
struct statvfs64 fsdata;
statvfs64(dirname, &fsdata);
/* return (float)(fsdata.f_blocks-fsdata.f_bfree)/ */
/* (float)(fsdata.f_blocks-fsdata.f_bfree+fsdata.f_bavail); */
return (float) (((float)(fsdata.f_blocks - fsdata.f_bfree)) /
((float)fsdata.f_blocks));
}
uint64_t getfsutil_size(char *dirname)
{
struct statvfs64 fsdata;
statvfs64(dirname, &fsdata);
return (fsdata.f_blocks - fsdata.f_bfree) * fsdata.f_bsize;
}
int ffsb_system(char *command)
{
int pid=0, status;
extern char **environ;
if (command == NULL)
return 1;
pid = fork();
if (pid == -1)
return -1;
if (pid == 0) {
char *argv[4];
argv[0] = "sh";
argv[1] = "-c";
argv[2] = command;
argv[3] = 0;
execve("/bin/sh", argv, environ);
exit(127);
}
do {
if (waitpid(pid, &status, 0) == -1) {
if (errno != EINTR)
return -1;
}
else
return status;
} while (1);
}
void ffsb_sync()
{
struct timeval starttime, endtime, difftime;
printf("Syncing()...");
fflush(stdout);
gettimeofday(&starttime, NULL);
sync();
gettimeofday(&endtime, NULL);
timersub(&endtime, &starttime, &difftime);
printf("%ld sec\n", difftime.tv_sec);
}
void ffsb_getrusage(struct rusage *ru_self, struct rusage *ru_children)
{
int ret = 0;
/* printf("cpu_so_far is %lf\n",cpu_so_far()); */
/* printf("cpu_so_far_children is %lf\n",cpu_so_far_children()); */
ret = getrusage(RUSAGE_SELF, ru_self);
if (ret < 0)
perror("getrusage self");
/* printf("self returned %d\n",ret); */
ret = getrusage(RUSAGE_CHILDREN, ru_children);
if (ret < 0)
perror("getrusage children");
/* printf("children returned %d\n",ret); */
}
void ffsb_milli_sleep(unsigned time)
{
struct timeval tv = { 0 , 0 };
if (!time)
return;
tv.tv_usec = time * 1000;
select(0, NULL, NULL, NULL, &tv);
}
void ffsb_micro_sleep(unsigned time)
{
struct timeval tv = { 0 , 0 };
if (!time)
return;
tv.tv_usec = time ;
select(0, NULL, NULL, NULL, &tv);
}
void ffsb_barrier_init(ffsb_barrier_t *fb, unsigned count)
{
memset(fb, 0, sizeof(*fb));
pthread_mutex_init(&fb->plock, NULL);
pthread_cond_init(&fb->pcond, NULL);
fb->required_count = count;
}
void ffsb_barrier_wait(ffsb_barrier_t *fb)
{
pthread_mutex_lock(&fb->plock);
fb->current_count++;
if (fb->current_count == fb->required_count)
pthread_cond_broadcast(&fb->pcond);
else
while (fb->current_count != fb->required_count)
pthread_cond_wait(&fb->pcond, &fb->plock);
pthread_mutex_unlock(&fb->plock);
}
void ffsb_unbuffer_stdout(void)
{
#ifndef SETVBUF_REVERSED
setvbuf(stdout, NULL, _IONBF, 0);
#else
setvbuf(stdout, _IONBF, NULL, 0);
#endif
}
void ffsb_bench_gettimeofday(void)
{
unsigned long i = 0;
uint64_t total_usec;
uint64_t average = 0;
struct timeval starttime, endtime, junk, difftime;
gettimeofday(&starttime, NULL);
for (i = 0; i < 1000000; i++)
gettimeofday(&junk, NULL);
gettimeofday(&endtime, NULL);
timersub(&endtime, &starttime, &difftime);
total_usec = difftime.tv_sec * 1000000;
total_usec += difftime.tv_usec;
average = total_usec / 1000ull;
printf("average time for gettimeofday(): %"PRIu64" nsec\n", average);
}
void ffsb_bench_getpid(void)
{
unsigned long i = 0;
uint64_t total_usec;
uint64_t average = 0;
struct timeval starttime, endtime, difftime;
gettimeofday(&starttime, NULL);
for (i = 0; i < 1000000; i++)
getpid();
gettimeofday(&endtime, NULL);
timersub(&endtime, &starttime, &difftime);
total_usec = difftime.tv_sec * 1000000;
total_usec += difftime.tv_usec;
average = total_usec / 1000ull;
printf("average time for getpid(): %"PRIu64" nsec\n", average);
}