"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-vm-splice.c" (15 Mar 2019, 3289 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-vm-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_vm_splice_bytes(const char *opt)
   28 {
   29     size_t vm_splice_bytes;
   30 
   31     vm_splice_bytes = (size_t)get_uint64_byte_memory(opt, 1);
   32     check_range_bytes("vm-splice-bytes", vm_splice_bytes,
   33         MIN_VM_SPLICE_BYTES, MAX_MEM_LIMIT);
   34     return set_setting("vm-splice-bytes", TYPE_ID_SIZE_T, &vm_splice_bytes);
   35 }
   36 
   37 #if defined(HAVE_VMSPLICE) &&   \
   38     defined(SPLICE_F_MOVE)
   39 
   40 /*
   41  *  stress_splice
   42  *  stress copying of /dev/zero to /dev/null
   43  */
   44 static int stress_vm_splice(const args_t *args)
   45 {
   46     int fd, fds[2];
   47     uint8_t *buf;
   48     const size_t page_size = args->page_size;
   49     size_t sz, vm_splice_bytes = DEFAULT_VM_SPLICE_BYTES;
   50 
   51     if (!get_setting("vm-splice-bytes", &vm_splice_bytes)) {
   52         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   53             vm_splice_bytes = MAX_VM_SPLICE_BYTES;
   54         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   55             vm_splice_bytes = MIN_VM_SPLICE_BYTES;
   56     }
   57     vm_splice_bytes /= args->num_instances;
   58     if (vm_splice_bytes < MIN_VM_SPLICE_BYTES)
   59         vm_splice_bytes = MIN_VM_SPLICE_BYTES;
   60     if (vm_splice_bytes < page_size)
   61         vm_splice_bytes = page_size;
   62     sz = vm_splice_bytes & ~(page_size - 1);
   63 
   64     buf = mmap(NULL, sz, PROT_READ | PROT_WRITE,
   65         MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   66     if (buf == MAP_FAILED) {
   67         int rc = exit_status(errno);
   68 
   69         pr_fail_dbg("mmap");
   70         return rc;
   71     }
   72 
   73     if (pipe(fds) < 0) {
   74         (void)munmap(buf, sz);
   75         pr_fail_err("pipe");
   76         return EXIT_FAILURE;
   77     }
   78 
   79     if ((fd = open("/dev/null", O_WRONLY)) < 0) {
   80         (void)munmap(buf, sz);
   81         (void)close(fds[0]);
   82         (void)close(fds[1]);
   83         pr_fail_err("open");
   84         return EXIT_FAILURE;
   85     }
   86 
   87     do {
   88         int ret;
   89         ssize_t bytes;
   90         struct iovec iov;
   91 
   92         iov.iov_base = buf;
   93         iov.iov_len = sz;
   94 
   95         bytes = vmsplice(fds[1], &iov, 1, 0);
   96         if (bytes < 0)
   97             break;
   98         ret = splice(fds[0], NULL, fd, NULL,
   99             vm_splice_bytes, SPLICE_F_MOVE);
  100         if (ret < 0)
  101             break;
  102 
  103         inc_counter(args);
  104     } while (keep_stressing());
  105 
  106     (void)munmap(buf, sz);
  107     (void)close(fd);
  108     (void)close(fds[0]);
  109     (void)close(fds[1]);
  110 
  111     return EXIT_SUCCESS;
  112 }
  113 
  114 stressor_info_t stress_vm_splice_info = {
  115     .stressor = stress_vm_splice,
  116     .class = CLASS_VM | CLASS_PIPE_IO | CLASS_OS
  117 };
  118 #else
  119 stressor_info_t stress_vm_splice_info = {
  120     .stressor = stress_not_implemented,
  121     .class = CLASS_VM | CLASS_PIPE_IO | CLASS_OS
  122 };
  123 #endif