X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Ftests%2Fsendfile.c;h=3f5115536183dba0122ce0bace37dc84437e1e93;hb=e9e4690b86c8a0aa18ec24841bc9e9e7b25b3658;hp=21ae58ad88ab94da64ada32789aa124a22dc7165;hpb=3caa9b7adee210f36fb74e192fcd1f989d1b11e5;p=fs%2Flustre-release.git diff --git a/lustre/tests/sendfile.c b/lustre/tests/sendfile.c index 21ae58a..3f51155 100644 --- a/lustre/tests/sendfile.c +++ b/lustre/tests/sendfile.c @@ -1,20 +1,46 @@ +/* + * GPL HEADER START + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 only, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License version 2 for more details (a copy is included + * in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; If not, see + * http://www.gnu.org/licenses/gpl-2.0.html + * + * GPL HEADER END + */ +/* + * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. + * Use is subject to license terms. + */ +/* + * This file is part of Lustre, http://www.lustre.org/ + * Lustre is a trademark of Sun Microsystems, Inc. + */ #include #include #include #include +#include +#include #include #include #include #include #include -#include -#include -#include -#include -#include -#include +#include #define syserr(str) { perror(str); exit(-1); } @@ -23,12 +49,12 @@ int main(int argc, char *argv[]) char *sfile, *tfile; struct stat stbuf; int size; + unsigned long bufsize = 1024 * 1024; int infd, outfd; int sd[2]; int rc; char *buf; char cmd[1024]; - int page_size = sysconf(_SC_PAGESIZE); loff_t pos; if (argc < 3) { @@ -42,7 +68,8 @@ int main(int argc, char *argv[]) if (stat(sfile, &stbuf) < 0) { if (errno == ENOENT) { /* assume doing non-object file testing */ - infd = open(sfile, O_LOV_DELAY_CREATE|O_CREAT|O_RDWR, + infd = open(sfile, + O_LOV_DELAY_CREATE | O_CREAT | O_RDWR, 0644); if (infd < 0) syserr("open source file:"); @@ -63,7 +90,7 @@ int main(int argc, char *argv[]) exit(-1); } - outfd = open(tfile, O_WRONLY|O_TRUNC|O_CREAT, 0666); + outfd = open(tfile, O_WRONLY | O_TRUNC | O_CREAT, 0666); if (outfd < 0) syserr("open dest file:"); @@ -71,34 +98,48 @@ int main(int argc, char *argv[]) if (rc < 0) syserr("socketpair"); + rc = fcntl(sd[0], F_SETFL, O_NONBLOCK); + if (rc < 0) + syserr("fcntl"); + + rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF, + &bufsize, sizeof(bufsize)); + if (rc) + syserr("setsockopt"); + + srandom(time(NULL)); + pos = 0; while (size > 0) { int rc2; size_t seg_size; - seg_size = (size < page_size) ? size : (random() % size + 1); - if (seg_size > 4 * page_size) - seg_size = 4 * page_size; - rc = sendfile(sd[0], infd, &pos, seg_size); - if (rc < 0) - syserr("sendfile:"); - - size -= seg_size; - if (size == 0) - close(sd[0]); - - buf = malloc(seg_size); - rc = read(sd[1], buf, seg_size); - if (rc != seg_size) - syserr("read from socket:"); - - rc2 = write(outfd, buf, rc); - if (rc2 != rc) - syserr("write dest file error:"); - free(buf); + seg_size = random() % bufsize + 1; + if (seg_size > size) + seg_size = size; + + while (seg_size) { + rc = sendfile(sd[0], infd, &pos, seg_size); + if (rc < 0) + syserr("sendfile:"); + + seg_size -= rc; + size -= rc; + if (size == 0) + close(sd[0]); + + buf = malloc(rc); + if (read(sd[1], buf, rc) < 0) + syserr("read from socket:"); + + rc2 = write(outfd, buf, rc); + if (rc2 != rc) + syserr("write dest file error:"); + free(buf); + } } close(sd[1]), close(infd), close(outfd); - sprintf(cmd, "cmp %s %s\n", sfile, tfile); + snprintf(cmd, sizeof(cmd), "cmp %s %s\n", sfile, tfile); return system(cmd); }