"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-sendfile.c" (15 Mar 2019, 3372 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-sendfile.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_sendfile_size(const char *opt)
   28 {
   29     int64_t sendfile_size;
   30 
   31     sendfile_size = get_uint64_byte(opt);
   32     check_range_bytes("sendfile-size", sendfile_size,
   33         MIN_SENDFILE_SIZE, MAX_SENDFILE_SIZE);
   34     return set_setting("sendfile-size", TYPE_ID_UINT64, &sendfile_size);
   35 }
   36 
   37 #if defined(HAVE_SYS_SENDFILE_H) && \
   38     NEED_GLIBC(2,1,0)
   39 
   40 /*
   41  *  stress_sendfile
   42  *  stress reading of a temp file and writing to /dev/null via sendfile
   43  */
   44 static int stress_sendfile(const args_t *args)
   45 {
   46     char filename[PATH_MAX];
   47     int fdin, fdout, ret, rc = EXIT_SUCCESS;
   48     size_t sz;
   49     int64_t sendfile_size = DEFAULT_SENDFILE_SIZE;
   50 
   51     if (!get_setting("sendfile-size", &sendfile_size)) {
   52         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   53             sendfile_size = MAX_SENDFILE_SIZE;
   54         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   55             sendfile_size = MIN_SENDFILE_SIZE;
   56     }
   57     sz = (size_t)sendfile_size;
   58 
   59     ret = stress_temp_dir_mk_args(args);
   60     if (ret < 0)
   61         return exit_status(-ret);
   62 
   63     (void)stress_temp_filename_args(args,
   64         filename, sizeof(filename), mwc32());
   65 
   66     if ((fdin = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
   67         rc = exit_status(errno);
   68         pr_fail_err("open");
   69         goto dir_out;
   70     }
   71 #if defined(HAVE_POSIX_FALLOCATE)
   72     ret = posix_fallocate(fdin, (off_t)0, (off_t)sz);
   73 #else
   74     ret = shim_fallocate(fdin, 0, (off_t)0, (off_t)sz);
   75 #endif
   76     if (ret < 0) {
   77         rc = exit_status(errno);
   78         pr_fail_err("open");
   79         goto dir_out;
   80     }
   81     if ((fdout = open("/dev/null", O_WRONLY)) < 0) {
   82         pr_fail_err("open");
   83         rc = EXIT_FAILURE;
   84         goto close_in;
   85     }
   86 
   87     do {
   88         off_t offset = 0;
   89         if (sendfile(fdout, fdin, &offset, sz) < 0) {
   90             if (errno == ENOSYS) {
   91                 pr_inf("%s: skipping stressor, sendfile not implemented\n",
   92                     args->name);
   93                 rc = EXIT_NOT_IMPLEMENTED;
   94                 goto close_out;
   95             }
   96             if (errno == EINTR)
   97                 continue;
   98             pr_fail_err("sendfile");
   99             rc = EXIT_FAILURE;
  100             goto close_out;
  101         }
  102         inc_counter(args);
  103     } while (keep_stressing());
  104 
  105 close_out:
  106     (void)close(fdout);
  107 close_in:
  108     (void)close(fdin);
  109     (void)unlink(filename);
  110 dir_out:
  111     (void)stress_temp_dir_rm_args(args);
  112 
  113     return rc;
  114 }
  115 
  116 stressor_info_t stress_sendfile_info = {
  117     .stressor = stress_sendfile,
  118     .class = CLASS_PIPE_IO | CLASS_OS
  119 };
  120 #else
  121 stressor_info_t stress_sendfile_info = {
  122     .stressor = stress_not_implemented,
  123     .class = CLASS_PIPE_IO | CLASS_OS
  124 };
  125 #endif