1 // SPDX-License-Identifier: GPL-2.0+
3 * Takes page fault while writev is iterating over the vectors in the IOV
4 * Copyright (C) 2017 Red Hat, Inc. All Rights reserved.
22 void usage(char *progname)
24 fprintf(stderr, "usage: %s [-i iovcnt] filename\n", progname);
25 fprintf(stderr, "\t-i iovcnt: the count of io vectors (max is 1024), 3 by default\n");
29 int main(int argc, char *argv[])
36 char *filename = NULL;
37 int iov_cnt = DEF_IOV_CNT;
40 while ((c = getopt(argc, argv, "i:")) != -1) {
45 iov_cnt = strtol(optarg, &endp, 0);
47 fprintf(stderr, "Invalid iov count %s\n", optarg);
56 if (iov_cnt > IOV_MAX || iov_cnt <= 0)
59 if (optind == argc - 1)
60 filename = argv[optind];
64 pagesz = getpagesize();
65 data = malloc(pagesz * iov_cnt);
67 perror("malloc failed");
72 * NOTE: no pre-writing/reading on the buffer before writev, to prevent
73 * page prefault from happening, we need it happen at writev time.
76 iov = calloc(iov_cnt, sizeof(struct iovec));
78 perror("calloc failed");
82 for (i = 0; i < iov_cnt; i++) {
83 (iov + i)->iov_base = (data + pagesz * i);
84 (iov + i)->iov_len = 1;
87 if ((fd = open(filename, O_TRUNC|O_CREAT|O_RDWR, 0644)) < 0) {
88 perror("open failed");
92 ret = writev(fd, iov, iov_cnt);
94 perror("writev failed");
96 printf("wrote %zd bytes\n", ret);