"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-splice.c" (15 Mar 2019, 2931 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-splice.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 int stress_set_splice_bytes(const char *opt)
   28 {
   29     size_t splice_bytes;
   30 
   31     splice_bytes = (size_t)get_uint64_byte_memory(opt, 1);
   32     check_range_bytes("splice-bytes", splice_bytes,
   33         MIN_SPLICE_BYTES, MAX_MEM_LIMIT);
   34     return set_setting("splice-bytes", TYPE_ID_SIZE_T, &splice_bytes);
   35 }
   36 
   37 #if defined(HAVE_SPLICE) && \
   38     defined(SPLICE_F_MOVE)
   39 
   40 /*
   41  *  stress_splice
   42  *  stress copying of /dev/zero to /dev/null
   43  */
   44 static int stress_splice(const args_t *args)
   45 {
   46     int fd_in, fd_out, fds[2];
   47     size_t splice_bytes = DEFAULT_SPLICE_BYTES;
   48 
   49     if (!get_setting("splice-bytes", &splice_bytes)) {
   50         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   51             splice_bytes = MAX_SPLICE_BYTES;
   52         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   53             splice_bytes = MIN_SPLICE_BYTES;
   54     }
   55     splice_bytes /= args->num_instances;
   56     if (splice_bytes < MIN_SPLICE_BYTES)
   57         splice_bytes = MIN_SPLICE_BYTES;
   58 
   59     if (pipe(fds) < 0) {
   60         pr_fail_err("pipe");
   61         return EXIT_FAILURE;
   62     }
   63 
   64     if ((fd_in = open("/dev/zero", O_RDONLY)) < 0) {
   65         (void)close(fds[0]);
   66         (void)close(fds[1]);
   67         pr_fail_err("open");
   68         return EXIT_FAILURE;
   69     }
   70     if ((fd_out = open("/dev/null", O_WRONLY)) < 0) {
   71         (void)close(fd_in);
   72         (void)close(fds[0]);
   73         (void)close(fds[1]);
   74         pr_fail_err("open");
   75         return EXIT_FAILURE;
   76     }
   77 
   78     do {
   79         ssize_t ret;
   80 
   81         ret = splice(fd_in, NULL, fds[1], NULL,
   82                 splice_bytes, SPLICE_F_MOVE);
   83         if (ret < 0)
   84             break;
   85 
   86         ret = splice(fds[0], NULL, fd_out, NULL,
   87                 splice_bytes, SPLICE_F_MOVE);
   88         if (ret < 0)
   89             break;
   90 
   91         inc_counter(args);
   92     } while (keep_stressing());
   93     (void)close(fd_out);
   94     (void)close(fd_in);
   95     (void)close(fds[0]);
   96     (void)close(fds[1]);
   97 
   98     return EXIT_SUCCESS;
   99 }
  100 
  101 stressor_info_t stress_splice_info = {
  102     .stressor = stress_splice,
  103     .class = CLASS_PIPE_IO | CLASS_OS
  104 };
  105 #else
  106 stressor_info_t stress_splice_info = {
  107     .stressor = stress_not_implemented,
  108     .class = CLASS_PIPE_IO | CLASS_OS
  109 };
  110 #endif