forked from jblomer/iotools
/
iotrace.c
709 lines (599 loc) · 19.7 KB
/
iotrace.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/**
* Copyright CERN; jblomer@cern.ch
*/
#define _LARGEFILE64_SOURCE
#if __STDC_VERSION__ >= 199901L
// POSIX 2008
#define _XOPEN_SOURCE 700
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */
#include <aio.h>
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#include "wire_format.h"
// GNU extension
#ifndef RTLD_NEXT
#define RTLD_NEXT ((void *) -1l)
#endif
#ifndef O_TMPFILE
#define O_TMPFILE (020000000)
#endif
#define MAX_FILES_TRACED 64
#define IOTRACE_CLOCK_ID CLOCK_MONOTONIC
struct iotrace_state_t {
int (*ptr_open)(const char *pathname, int flags, ...);
int (*ptr_open64)(const char *pathname, int flags, ...);
int (*ptr_openat)(int fd, const char *pathname, int flags, ...);
int (*ptr_openat64)(int fd, const char *pathname, int flags, ...);
int (*ptr_fcntl)(int fd, int cmd, ...);
int (*ptr_fcntl64)(int fd, int cmd, ...);
ssize_t (*ptr_read)(int fd, void *buf, size_t count);
ssize_t (*ptr_readv)(int fd, const struct iovec *iov, int iovcnt);
int (*ptr_close)(int fd);
off_t (*ptr_lseek)(int fd, off_t offset, int whence);
off64_t (*ptr_lseek64)(int fd, off64_t offset, int whence);
ssize_t (*ptr_pread)(int fd, void *buf, size_t nbyte, off_t offset);
ssize_t (*ptr_pread64)(int fd, void *buf, size_t nbyte, off64_t offset);
ssize_t (*ptr_preadv)(int fd, const struct iovec *iov, int iovcnt, off_t off);
ssize_t (*ptr_preadv64)(int fd, const struct iovec *iov, int c, off64_t off);
ssize_t (*ptr_preadv2)(int fd, const struct iovec *iov, int c, off_t off,
int flags);
void *(*ptr_mmap)(void *a, size_t l, int prot, int flags, int fd, off_t off);
void *(*ptr_mmap64)(void *a, size_t l, int p, int flags, int fd, off64_t off);
int (*ptr_dup)(int fd);
int (*ptr_dup2)(int fd, int fd2);
int (*ptr_aio_read)(struct aiocb *aiocbp);
// TODO: aio_read, aio_read64
int trace_fds[MAX_FILES_TRACED];
int sz_trace_fds;
pthread_mutex_t lock_trace_fds;
char *pattern;
int fd_fanout;
};
struct iotrace_state_t *iotrace_state = NULL;
static void iotrace_init_state() {
if (iotrace_state != NULL)
return;
iotrace_state = malloc(sizeof(struct iotrace_state_t));
memset(iotrace_state, 0, sizeof(struct iotrace_state_t));
iotrace_state->ptr_open = dlsym(RTLD_NEXT, "open");
if (iotrace_state->ptr_open == NULL) { abort(); }
iotrace_state->ptr_open64 = dlsym(RTLD_NEXT, "open64");
if (iotrace_state->ptr_open64 == NULL) { abort(); }
iotrace_state->ptr_openat = dlsym(RTLD_NEXT, "openat");
if (iotrace_state->ptr_openat == NULL) { abort(); }
iotrace_state->ptr_openat64 = dlsym(RTLD_NEXT, "openat64");
if (iotrace_state->ptr_openat64 == NULL) { abort(); }
iotrace_state->ptr_fcntl = dlsym(RTLD_NEXT, "fcntl");
if (iotrace_state->ptr_fcntl == NULL) { abort(); }
//iotrace_state->ptr_fcntl64 = dlsym(RTLD_NEXT, "fcntl64");
//if (iotrace_state->ptr_fcntl64 == NULL) { abort(); }
iotrace_state->ptr_read = dlsym(RTLD_NEXT, "read");
if (iotrace_state->ptr_read == NULL) { abort(); }
iotrace_state->ptr_readv = dlsym(RTLD_NEXT, "readv");
if (iotrace_state->ptr_readv == NULL) { abort(); }
iotrace_state->ptr_close = dlsym(RTLD_NEXT, "close");
if (iotrace_state->ptr_close == NULL) { abort(); }
iotrace_state->ptr_lseek = dlsym(RTLD_NEXT, "lseek");
if (iotrace_state->ptr_lseek == NULL) { abort(); }
iotrace_state->ptr_lseek64 = dlsym(RTLD_NEXT, "lseek64");
if (iotrace_state->ptr_lseek64 == NULL) { abort(); }
iotrace_state->ptr_pread = dlsym(RTLD_NEXT, "pread");
if (iotrace_state->ptr_pread == NULL) { abort(); }
iotrace_state->ptr_pread64 = dlsym(RTLD_NEXT, "pread64");
if (iotrace_state->ptr_pread64 == NULL) { abort(); }
iotrace_state->ptr_preadv = dlsym(RTLD_NEXT, "preadv");
if (iotrace_state->ptr_preadv == NULL) { abort(); }
iotrace_state->ptr_preadv64 = dlsym(RTLD_NEXT, "preadv64");
if (iotrace_state->ptr_preadv64 == NULL) { abort(); }
//iotrace_state->ptr_preadv2 = dlsym(RTLD_NEXT, "preadv2");
//if (iotrace_state->ptr_preadv2 == NULL) { abort(); }
iotrace_state->ptr_mmap = dlsym(RTLD_NEXT, "mmap");
if (iotrace_state->ptr_mmap == NULL) { abort(); }
iotrace_state->ptr_mmap64 = dlsym(RTLD_NEXT, "mmap64");
if (iotrace_state->ptr_mmap64 == NULL) { abort(); }
iotrace_state->ptr_dup = dlsym(RTLD_NEXT, "dup");
if (iotrace_state->ptr_dup == NULL) { abort(); }
iotrace_state->ptr_dup2 = dlsym(RTLD_NEXT, "dup2");
if (iotrace_state->ptr_dup2 == NULL) { abort(); }
iotrace_state->ptr_aio_read = dlsym(RTLD_NEXT, "aio_read");
if (iotrace_state->ptr_aio_read == NULL) { abort(); }
pthread_mutex_init(&iotrace_state->lock_trace_fds, NULL);
iotrace_state->pattern = getenv("IOTRACE_FILENAME");
if (iotrace_state->pattern != NULL)
printf("*** IOTRACE: tracing %s\n", iotrace_state->pattern);
char *fanout = getenv("IOTRACE_FANOUT");
if (fanout == NULL) { fanout = "iotrace.fanout"; }
iotrace_state->fd_fanout = iotrace_state->ptr_open(fanout, O_WRONLY);
if (iotrace_state->fd_fanout < 0) {
fprintf(stderr, "cannot open pipe %s\n", fanout);
abort();
}
struct timespec tp_res;
int retval = clock_getres(IOTRACE_CLOCK_ID, &tp_res);
assert(retval == 0);
printf("*** IOTRACE: clock resolution %lds, %ldns\n",
tp_res.tv_sec, tp_res.tv_nsec);
}
static int iotrace_should_trace(const char *pathname) {
if ((pathname == NULL) || (pathname[0] == '\0'))
return 0;
if (iotrace_state->pattern == NULL)
return 1;
int len = strlen(pathname);
int i = len - 1;
for (; i >= 0; --i) {
if (pathname[i] == '/')
break;
}
++i;
return strcmp(pathname + i, iotrace_state->pattern) == 0;
}
static int iotrace_get_idx_trace_fds(int fd) {
for (int i = 0; i < iotrace_state->sz_trace_fds; ++i) {
if (iotrace_state->trace_fds[i] == fd)
return i;
}
return -1;
}
static void iotrace_lock() {
pthread_mutex_lock(&iotrace_state->lock_trace_fds);
}
static void iotrace_unlock() {
pthread_mutex_unlock(&iotrace_state->lock_trace_fds);
}
static void iotrace_send(struct iotrace_frame *frame) {
ssize_t written = write(iotrace_state->fd_fanout, frame,
sizeof(struct iotrace_frame));
assert(written == sizeof(struct iotrace_frame));
}
static inline struct timespec iotrace_stopwatch() {
struct timespec ts_start;
int retval = clock_gettime(IOTRACE_CLOCK_ID, &ts_start);
assert(retval == 0);
return ts_start;
}
static inline int64_t iotrace_meter_ns(struct timespec *ts_start) {
struct timespec ts_end;
int retval = clock_gettime(IOTRACE_CLOCK_ID, &ts_end);
assert(retval == 0);
return ((ts_end.tv_sec - ts_start->tv_sec) * 1000000000 +
(ts_end.tv_nsec - ts_start->tv_nsec));
}
int fcntl(int fd, int cmd, ...) {
iotrace_init_state();
int arg_int = 0;
int use_arg_int = 0;
void *arg_ptr = NULL;
int use_arg_ptr = 0;
if ((cmd == F_DUPFD) || (cmd == F_SETFD) || (cmd == F_SETFL) ||
(cmd == F_SETOWN))
{
va_list ap;
va_start(ap, cmd);
arg_int = va_arg(ap, int);
va_end(ap);
use_arg_int = 1;
}
if ((cmd == F_GETLK) || (cmd == F_SETLK)) {
va_list ap;
va_start(ap, cmd);
arg_ptr = va_arg(ap, void *);
va_end(ap);
use_arg_ptr = 1;
}
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if ((idx_trace_fds < 0) || (cmd == F_GETLK) || (cmd == F_SETLK)) {
if (use_arg_int)
return iotrace_state->ptr_fcntl(fd, cmd, arg_int);
if (use_arg_ptr)
return iotrace_state->ptr_fcntl(fd, cmd, arg_ptr);
return iotrace_state->ptr_fcntl(fd, cmd);
}
printf("strange fcntl\n");
abort();
}
/*int fcntl64(int fd, int cmd, ...) {
iotrace_init_state();
int arg_int = 0;
int use_arg_int = 0;
void *arg_ptr = NULL;
int use_arg_ptr = 0;
if ((cmd == F_DUPFD) || (cmd == F_SETFD) || (cmd == F_SETFL) ||
(cmd == F_SETOWN))
{
va_list ap;
va_start(ap, cmd);
arg_int = va_arg(ap, int);
va_end(ap);
use_arg_int = 1;
}
if ((cmd == F_GETLK) || (cmd == F_SETLK)) {
va_list ap;
va_start(ap, cmd);
arg_ptr = va_arg(ap, void *);
va_end(ap);
use_arg_ptr = 1;
}
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0) {
if (use_arg_int)
return iotrace_state->ptr_fcntl64(fd, cmd, arg_int);
if (use_arg_ptr)
return iotrace_state->ptr_fcntl64(fd, cmd, arg_ptr);
return iotrace_state->ptr_fcntl64(fd, cmd);
}
abort();
}*/
int open(const char *pathname, int flags, ...) {
iotrace_init_state();
mode_t mode = 0;
if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
int do_trace = iotrace_should_trace(pathname);
if (!do_trace)
return iotrace_state->ptr_open(pathname, flags, mode);
printf("*** IOTRACE: following %s\n", pathname);
struct timespec ts_start = iotrace_stopwatch();
int result = iotrace_state->ptr_open(pathname, flags, mode);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
if (result >= 0) {
iotrace_lock();
if (iotrace_state->sz_trace_fds >= MAX_FILES_TRACED) { abort(); }
iotrace_state->trace_fds[iotrace_state->sz_trace_fds++] = result;
iotrace_unlock();
struct iotrace_frame frame;
frame.op = IOO_OPEN;
frame.fd = result;
frame.duration_ns = duration_ns;
iotrace_send(&frame);
}
return result;
}
int open64(const char *pathname, int flags, ...) {
iotrace_init_state();
mode_t mode = 0;
if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
int do_trace = iotrace_should_trace(pathname);
if (!do_trace)
return iotrace_state->ptr_open64(pathname, flags, mode);
printf("*** IOTRACE: following %s\n", pathname);
struct timespec ts_start = iotrace_stopwatch();
int result = iotrace_state->ptr_open64(pathname, flags, mode);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
if (result >= 0) {
iotrace_lock();
if (iotrace_state->sz_trace_fds >= MAX_FILES_TRACED) { abort(); }
iotrace_state->trace_fds[iotrace_state->sz_trace_fds++] = result;
iotrace_unlock();
struct iotrace_frame frame;
frame.op = IOO_OPEN;
frame.fd = result;
frame.duration_ns = duration_ns;
iotrace_send(&frame);
}
return result;
}
int openat(int fd, const char *pathname, int flags, ...) {
iotrace_init_state();
mode_t mode = 0;
if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
int do_trace = iotrace_should_trace(pathname);
if (!do_trace)
return iotrace_state->ptr_openat(fd, pathname, flags, mode);
printf("*** IOTRACE: following %s\n", pathname);
struct timespec ts_start = iotrace_stopwatch();
int result = iotrace_state->ptr_openat(fd, pathname, flags, mode);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
if (result >= 0) {
iotrace_lock();
if (iotrace_state->sz_trace_fds >= MAX_FILES_TRACED) { abort(); }
iotrace_state->trace_fds[iotrace_state->sz_trace_fds++] = result;
iotrace_unlock();
struct iotrace_frame frame;
frame.op = IOO_OPEN;
frame.fd = result;
frame.duration_ns = duration_ns;
iotrace_send(&frame);
}
return result;
}
int openat64(int fd, const char *pathname, int flags, ...) {
iotrace_init_state();
mode_t mode = 0;
if ((flags & O_CREAT) || (flags & O_TMPFILE)) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
}
int do_trace = iotrace_should_trace(pathname);
if (!do_trace)
return iotrace_state->ptr_openat64(fd, pathname, flags, mode);
printf("*** IOTRACE: following %s\n", pathname);
struct timespec ts_start = iotrace_stopwatch();
int result = iotrace_state->ptr_openat64(fd, pathname, flags, mode);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
if (result >= 0) {
iotrace_lock();
if (iotrace_state->sz_trace_fds >= MAX_FILES_TRACED) { abort(); }
iotrace_state->trace_fds[iotrace_state->sz_trace_fds++] = result;
iotrace_unlock();
struct iotrace_frame frame;
frame.op = IOO_OPEN;
frame.fd = result;
frame.duration_ns = duration_ns;
iotrace_send(&frame);
}
return result;
}
int aio_read(struct aiocb *aiocbp) {
iotrace_init_state();
int fd = aiocbp->aio_fildes;
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_aio_read(aiocbp);
printf("aio_read unsupported\n");
abort();
}
ssize_t read(int fd, void *buf, size_t count) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_read(fd, buf, count);
struct timespec ts_start = iotrace_stopwatch();
ssize_t result = iotrace_state->ptr_read(fd, buf, count);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_READ;
frame.fd = fd;
frame.duration_ns = duration_ns;
if (result >= 0)
frame.info.read.size = result;
else
frame.info.read.size = 0;
iotrace_send(&frame);
return result;
}
ssize_t readv(int fd, const struct iovec *iov, int iovcnt) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_readv(fd, iov, iovcnt);
struct timespec ts_start = iotrace_stopwatch();
ssize_t result = iotrace_state->ptr_readv(fd, iov, iovcnt);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_READ;
frame.fd = fd;
frame.duration_ns = duration_ns;
if (result >= 0)
frame.info.read.size = result;
else
frame.info.read.size = 0;
iotrace_send(&frame);
return result;
}
ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t off) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_preadv(fd, iov, iovcnt, off);
printf("preadv unsupported\n");
abort();
}
ssize_t preadv64(int fd, const struct iovec *iov, int iovcnt, off64_t off) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_preadv64(fd, iov, iovcnt, off);
printf("preadv64 unsupported\n");
abort();
}
/*ssize_t preadv2(int fd, const struct iovec *iov, int c, off_t off, int flags) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_preadv2(fd, iov, c, off, flags);
abort();
}*/
off_t lseek(int fd, off_t offset, int whence) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_lseek(fd, offset, whence);
off_t pos_cur = iotrace_state->ptr_lseek(fd, 0, SEEK_CUR);
struct timespec ts_start = iotrace_stopwatch();
off_t result = iotrace_state->ptr_lseek(fd, offset, whence);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_SEEK;
frame.fd = fd;
frame.duration_ns = duration_ns;
if ((result >= 0) && (pos_cur >= 0))
frame.info.seek.offset = result - pos_cur;
else
frame.info.seek.offset = -1;
iotrace_send(&frame);
return result;
}
off64_t lseek64(int fd, off64_t offset, int whence) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_lseek64(fd, offset, whence);
off64_t pos_cur = iotrace_state->ptr_lseek64(fd, 0, SEEK_CUR);
struct timespec ts_start = iotrace_stopwatch();
off64_t result = iotrace_state->ptr_lseek64(fd, offset, whence);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_SEEK;
frame.fd = fd;
frame.duration_ns = duration_ns;
if ((result >= 0) && (pos_cur >= 0))
frame.info.seek.offset = result - pos_cur;
else
frame.info.seek.offset = -1;
iotrace_send(&frame);
return result;
}
ssize_t pread(int fd, void *buf, size_t nbyte, off_t offset) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_pread(fd, buf, nbyte, offset);
//printf("*** IOTRACE: pread\n");
off64_t pos_cur = iotrace_state->ptr_lseek(fd, 0, SEEK_CUR);
struct timespec ts_start = iotrace_stopwatch();
ssize_t result = iotrace_state->ptr_pread(fd, buf, nbyte, offset);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_SEEK;
frame.fd = fd;
frame.duration_ns = 0;
if (pos_cur >= 0)
frame.info.seek.offset = offset - pos_cur;
else
frame.info.seek.offset = -1;
iotrace_send(&frame);
frame.op = IOO_READ;
frame.duration_ns = duration_ns;
if (result >= 0)
frame.info.read.size = result;
else
frame.info.read.size = 0;
iotrace_send(&frame);
return result;
}
ssize_t pread64(int fd, void *buf, size_t nbyte, off64_t offset) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_pread64(fd, buf, nbyte, offset);
//printf("*** IOTRACE: pread64\n");
off64_t pos_cur = iotrace_state->ptr_lseek64(fd, 0, SEEK_CUR);
struct timespec ts_start = iotrace_stopwatch();
ssize_t result = iotrace_state->ptr_pread64(fd, buf, nbyte, offset);
int64_t duration_ns = iotrace_meter_ns(&ts_start);
struct iotrace_frame frame;
frame.op = IOO_SEEK;
frame.fd = fd;
frame.duration_ns = 0;
if (pos_cur >= 0)
frame.info.seek.offset = offset - pos_cur;
else
frame.info.seek.offset = -1;
iotrace_send(&frame);
frame.op = IOO_READ;
frame.duration_ns = duration_ns;
if (result >= 0)
frame.info.read.size = result;
else
frame.info.read.size = 0;
iotrace_send(&frame);
return result;
}
int close(int fd) {
iotrace_init_state();
int result = iotrace_state->ptr_close(fd);
if (result != 0)
return result;
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
if (idx_trace_fds >= 0) {
iotrace_state->trace_fds[idx_trace_fds] =
iotrace_state->trace_fds[iotrace_state->sz_trace_fds - 1];
iotrace_state->sz_trace_fds--;
}
iotrace_unlock();
return result;
}
void *mmap(void *a, size_t l, int prot, int flags, int fd, off_t off) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_mmap(a, l, prot, flags, fd, off);
printf("mmap unsupported\n");
abort();
}
void *mmap64(void *a, size_t l, int prot, int flags, int fd, off64_t off) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_mmap64(a, l, prot, flags, fd, off);
printf("mmap64 unsupported\n");
abort();
}
int dup(int fd) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_dup(fd);
printf("dup unsupported\n");
abort();
}
int dup2(int fd, int fd2) {
iotrace_init_state();
iotrace_lock();
int idx_trace_fds = iotrace_get_idx_trace_fds(fd);
iotrace_unlock();
if (idx_trace_fds < 0)
return iotrace_state->ptr_dup2(fd, fd2);
printf("dup2 unsupported\n");
abort();
}