Whamcloud - gitweb
New testing program exercising sendfile(2) (Jay).
[fs/lustre-release.git] / lustre / tests / sendfile.c
1
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <sys/sendfile.h>
9 #include <sys/stat.h>
10 #include <sys/socket.h>
11
12 #include <liblustre.h>
13 #include <lnet/lnetctl.h>
14 #include <obd.h>
15 #include <lustre_lib.h>
16 #include <obd_lov.h>
17 #include <lustre/liblustreapi.h>
18
19 #define syserr(str) { perror(str); exit(-1); }
20
21 int main(int argc, char *argv[])
22 {
23         char *sfile, *tfile;
24         struct stat stbuf;
25         int size;
26         int infd, outfd;
27         int sd[2];
28         int rc;
29         char *buf;
30         char cmd[1024];
31         int page_size = sysconf(_SC_PAGESIZE);
32         loff_t pos;
33
34         if (argc < 3) {
35                 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
36                 exit(-1);
37         }
38
39         sfile = argv[1];
40         tfile = argv[2];
41
42         if (stat(sfile, &stbuf) < 0) {
43                 if (errno == ENOENT) {
44                         /* assume doing non-object file testing */
45                         infd = open(sfile, O_LOV_DELAY_CREATE|O_CREAT|O_RDWR,
46                                     0644);
47                         if (infd < 0)
48                                 syserr("open source file:");
49
50                         size = random() % (1 * 1024 * 1024) + 1024;
51                         if (ftruncate(infd, (off_t)size) < 0)
52                                 syserr("truncate file error:");
53                 } else {
54                         syserr("stat file: ");
55                 }
56         } else if (S_ISREG(stbuf.st_mode)) {
57                 size = (int)stbuf.st_size;
58                 infd = open(sfile, O_RDONLY, 0644);
59                 if (infd < 0)
60                         syserr("Open an existing file error:");
61         } else {
62                 fprintf(stderr, "%s is not a regular file\n", sfile);
63                 exit(-1);
64         }
65
66         outfd = open(tfile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
67         if (outfd < 0)
68                 syserr("open dest file:");
69
70         rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
71         if (rc < 0)
72                 syserr("socketpair");
73
74         pos = 0;
75         while (size > 0) {
76                 int rc2;
77                 size_t seg_size;
78
79                 seg_size = (size < page_size) ? size : (random() % size + 1);
80                 if (seg_size > 4 * page_size)
81                         seg_size = 4 * page_size;
82                 rc = sendfile(sd[0], infd, &pos, seg_size);
83                 if (rc < 0)
84                         syserr("sendfile:");
85
86                 size -= seg_size;
87                 if (size == 0)
88                         close(sd[0]);
89
90                 buf = malloc(seg_size);
91                 rc = read(sd[1], buf, seg_size);
92                 if (rc != seg_size)
93                         syserr("read from socket:");
94
95                 rc2 = write(outfd, buf, rc);
96                 if (rc2 != rc)
97                         syserr("write dest file error:");
98                 free(buf);
99         }
100         close(sd[1]), close(infd), close(outfd);
101
102         sprintf(cmd, "cmp %s %s\n", sfile, tfile);
103         return system(cmd);
104 }