open_by_handle: make -h (help) a valid option
[xfstests-dev.git] / src / writev_on_pagefault.c
1 /*
2  * Takes page fault while writev is iterating over the vectors in the IOV
3  *
4  * Copyright (C) 2017 Red Hat, Inc. All Rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/uio.h>
28 #include <limits.h>
29
30 #ifndef IOV_MAX
31 #define IOV_MAX 1024
32 #endif
33 #define DEF_IOV_CNT 3
34
35
36 void usage(char *progname)
37 {
38         fprintf(stderr, "usage: %s [-i iovcnt] filename\n", progname);
39         fprintf(stderr, "\t-i iovcnt: the count of io vectors (max is 1024), 3 by default\n");
40         exit(1);
41 }
42
43 int main(int argc, char *argv[])
44 {
45         int fd, i, c;
46         ssize_t ret;
47         struct iovec *iov;
48         int pagesz = 4096;
49         char *data = NULL;
50         char *filename = NULL;
51         int iov_cnt = DEF_IOV_CNT;
52
53
54         while ((c = getopt(argc, argv, "i:")) != -1) {
55                 char *endp;
56
57                 switch (c) {
58                 case 'i':
59                         iov_cnt = strtol(optarg, &endp, 0);
60                         if (*endp) {
61                                 fprintf(stderr, "Invalid iov count %s\n", optarg);
62                                 usage(argv[0]);
63                         }
64                         break;
65                 default:
66                         usage(argv[0]);
67                 }
68         }
69
70         if (iov_cnt > IOV_MAX || iov_cnt <= 0)
71                 usage(argv[0]);
72
73         if (optind == argc - 1)
74                 filename = argv[optind];
75         else
76                 usage(argv[0]);
77
78         pagesz = getpagesize();
79         data = malloc(pagesz * iov_cnt);
80         if (!data) {
81                 perror("malloc failed");
82                 return 1;
83         }
84
85         /*
86          * NOTE: no pre-writing/reading on the buffer before writev, to prevent
87          * page prefault from happening, we need it happen at writev time.
88          */
89
90         iov = calloc(iov_cnt, sizeof(struct iovec));
91         if (!iov) {
92                 perror("calloc failed");
93                 return 1;
94         }
95
96         for (i = 0; i < iov_cnt; i++) {
97                 (iov + i)->iov_base = (data + pagesz * i);
98                 (iov + i)->iov_len  = 1;
99         }
100
101         if ((fd = open(filename, O_TRUNC|O_CREAT|O_RDWR, 0644)) < 0) {
102                 perror("open failed");
103                 return 1;
104         }
105
106         ret = writev(fd, iov, iov_cnt);
107         if (ret < 0)
108                 perror("writev failed");
109         else
110                 printf("wrote %zd bytes\n", ret);
111
112         close(fd);
113         return 0;
114 }