2 * Takes page fault while writev is iterating over the vectors in the IOV
4 * Copyright (C) 2017 Red Hat, Inc. All Rights reserved.
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.
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.
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.
22 #include <sys/types.h>
36 void usage(char *progname)
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");
43 int main(int argc, char *argv[])
50 char *filename = NULL;
51 int iov_cnt = DEF_IOV_CNT;
54 while ((c = getopt(argc, argv, "i:")) != -1) {
59 iov_cnt = strtol(optarg, &endp, 0);
61 fprintf(stderr, "Invalid iov count %s\n", optarg);
70 if (iov_cnt > IOV_MAX || iov_cnt <= 0)
73 if (optind == argc - 1)
74 filename = argv[optind];
78 pagesz = getpagesize();
79 data = malloc(pagesz * iov_cnt);
81 perror("malloc failed");
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.
90 iov = calloc(iov_cnt, sizeof(struct iovec));
92 perror("calloc failed");
96 for (i = 0; i < iov_cnt; i++) {
97 (iov + i)->iov_base = (data + pagesz * i);
98 (iov + i)->iov_len = 1;
101 if ((fd = open(filename, O_TRUNC|O_CREAT|O_RDWR, 0644)) < 0) {
102 perror("open failed");
106 ret = writev(fd, iov, iov_cnt);
108 perror("writev failed");
110 printf("wrote %d bytes\n", (int)ret);