Debian Codegraph @

testsuite/systemtap.examples/io/traceaio.c

raw
1 // Test program for traceaio.stp example.
2 // Copyright (C) 2001 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8 //
9 // Example usage:
10 //
11 // $ gcc -D_GNU_SOURCE -o traceaio traceaio.c -laio
12 //
13 // $ sudo stap traceaio.stp -c "./traceaio /var/tmp/traceaio.data"
14 // Tracing started
15 // [ 0 traceaio(756217):] io_submit(140589416931328, 4, 0x7ffc6d6bb8a0)
16 // iocb[ 0]=0x7ffc6d6bb8c0, fd=3, opcode=1, offset=0, nbytes=4096, buf=0x1764000
17 // iocb[ 1]=0x7ffc6d6bb900, fd=3, opcode=0, offset=4096, nbytes=4096, buf=0x1765000
18 // iocb[ 2]=0x7ffc6d6bb940, fd=3, opcode=8, offset=8192, nbytes=1, buf=0x7ffc6d6bb880
19 // iovec[ 0]=0x7ffc6d6bb880, base=0x1766000, len=4096
20 // iocb[ 3]=0x7ffc6d6bb980, fd=3, opcode=7, offset=12288, nbytes=1, buf=0x7ffc6d6bb890
21 // iovec[ 0]=0x7ffc6d6bb890, base=0x1767000, len=4096
22 // Tracing stopped
23 //
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libaio.h>
34
35 #define IO_SIZE 4096
36
37 int main(int argc, char *argv[])
38 {
39 int fd;
40 void *buffer;
41 io_context_t ioctx = {0};
42 struct iocb iocbs[4];
43 struct iocb *iocbp[] = {&iocbs[0], &iocbs[1], &iocbs[2], &iocbs[3]};
44 struct iovec iovs[2];
45 int err;
46
47 if (argc != 2) {
48 fprintf(stderr, "Usage: traceaio-test FILENAME\n");
49 exit(1);
50 }
51
52 fd = open(argv[1], O_RDWR | O_CREAT | O_DIRECT, 0644);
53 assert(fd != -1);
54
55 err = fallocate(fd, 0, 0, 4 * IO_SIZE);
56 assert(err == 0);
57
58 err = posix_memalign(&buffer, IO_SIZE, 4 * IO_SIZE);
59 assert(err == 0);
60
61 memset(buffer, 0, 4 * IO_SIZE);
62
63 err = io_setup(128, &ioctx);
64 assert(err == 0);
65
66 /* PWRITE */
67
68 io_prep_pwrite(&iocbs[0], fd, buffer + 0 * IO_SIZE, IO_SIZE, 0 * IO_SIZE);
69
70 /* PREAD */
71
72 io_prep_pread(&iocbs[1], fd, buffer + 1 * IO_SIZE, IO_SIZE, 1 * IO_SIZE);
73
74 /* PWRITEV */
75
76 iovs[0].iov_base = buffer + 2 * IO_SIZE;
77 iovs[0].iov_len = IO_SIZE;
78 io_prep_pwritev(&iocbs[2], fd, &iovs[0], 1, 2 * IO_SIZE);
79
80 /* PREADV */
81
82 iovs[1].iov_base = buffer + 3 * IO_SIZE;
83 iovs[1].iov_len = IO_SIZE;
84 io_prep_preadv(&iocbs[3], fd, &iovs[1], 1, 3 * IO_SIZE);
85
86 io_submit(ioctx, 4, iocbp);
87
88 io_getevents(ioctx, 4, 4, NULL, NULL);
89 io_destroy(ioctx);
90 free(buffer);
91 close(fd);
92
93 return 0;
94 }