]> rtime.felk.cvut.cz Git - frescor/demo.git/blob - build/test_o_direct/direct.c
Add test for O_DIRECT
[frescor/demo.git] / build / test_o_direct / direct.c
1 #define _GNU_SOURCE
2 #define _XOPEN_SOURCE 600
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <malloc.h>
12 #include <error.h>
13
14 int trace_fd = -1;
15 int marker_fd = -1;
16
17 #define MAX_PATH 256
18 #define _STR(x) #x
19 #define STR(x) _STR(x)
20
21 #define MARKER(str) write(marker_fd, str, strlen(str));
22
23 static const char *find_debugfs(void)
24 {
25         static char debugfs[MAX_PATH+1];
26         static int debugfs_found;
27         char type[100];
28         FILE *fp;
29
30         if (debugfs_found)
31                 return debugfs;
32
33         if ((fp = fopen("/proc/mounts","r")) == NULL)
34                 return NULL;
35
36         while (fscanf(fp, "%*s %"
37                       STR(MAX_PATH)
38                       "s %99s %*s %*d %*d\n",
39                       debugfs, type) == 2) {
40                 if (strcmp(type, "debugfs") == 0)
41                         break;
42         }
43         fclose(fp);
44
45         if (strcmp(type, "debugfs") != 0)
46                 return NULL;
47
48         debugfs_found = 1;
49
50         return debugfs;
51 }
52
53 void init_ftrace()
54 {
55         const char *debugfs;
56         char path[256], pid[10];
57         int pid_fd;
58         
59         debugfs = find_debugfs();
60         if (debugfs) {
61                 strcpy(path, debugfs);
62                 strcat(path,"/tracing/trace_marker");
63                 marker_fd = open(path, O_WRONLY);
64
65                 strcpy(path, debugfs);
66                 strcat(path,"/tracing/set_ftrace_pid");
67                 pid_fd = open(path, O_WRONLY);
68                 sprintf(pid, "%d", getpid());
69                 if (pid_fd >= 0)
70                         write(pid_fd, pid, strlen(pid));
71
72                 strcpy(path, debugfs);
73                 strcat(path,"/tracing/tracing_on");
74                 trace_fd = open(path, O_WRONLY);
75                 if (trace_fd >= 0)
76                         write(trace_fd, "1", 1);
77         }
78 }
79
80 int main()
81 {
82         int fd, ret;
83         char *buf;
84         size_t pagesize = sysconf(_SC_PAGESIZE);
85
86         buf = memalign(pagesize, pagesize);
87         if (!buf)
88                 error(1, errno, "memalign");
89
90         init_ftrace();
91
92         if (marker_fd >= 0)
93                 MARKER("Without O_DIRECT");
94
95         fd = open("file", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
96         if (fd < 0)
97                 error(1, errno, "open");
98         ret = write(fd, buf, 512);
99         if (ret < 0)
100                 error(1, errno, "write");
101         close(fd);
102
103         if (marker_fd)
104                 MARKER("With O_DIRECT");
105
106         fd = open("file", O_CREAT | O_TRUNC | O_WRONLY | O_DIRECT, S_IRUSR | S_IWUSR);
107         if (fd < 0)
108                 error(1, errno, "open");
109         ret = write(fd, buf, 512);
110         if (ret < 0)
111                 error(1, errno, "write");
112         close(fd);
113
114         if (marker_fd)
115                 MARKER("Without O_DIRECT and with posix_fadvise");
116
117         fd = open("file", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
118         if (fd < 0)
119                 error(1, errno, "open");
120         ret = write(fd, buf, 512);
121         if (ret < 0)
122                 error(1, errno, "write");
123         ret = posix_fadvise(fd, 0, 512, POSIX_FADV_DONTNEED);
124         if (ret != 0)
125                 error(1, ret, "posix_fadvise");
126         close(fd);
127
128         if (marker_fd)
129                 MARKER("End");
130
131         if (trace_fd >= 0)
132                 write(trace_fd, "0", 1);
133         return 0;
134 }