"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-copy-file.c" (15 Mar 2019, 3843 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-copy-file.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_copy_file_bytes(const char *opt)
   28 {
   29     uint64_t copy_file_bytes;
   30 
   31     copy_file_bytes = get_uint64_byte_filesystem(opt, 1);
   32     check_range_bytes("copy-file-bytes", copy_file_bytes,
   33         MIN_COPY_FILE_BYTES, MAX_COPY_FILE_BYTES);
   34     return set_setting("copy-file-bytes", TYPE_ID_UINT64, &copy_file_bytes);
   35 }
   36 
   37 #if defined(HAVE_COPY_FILE_RANGE)
   38 
   39 /*
   40  *  stress_copy_file
   41  *  stress reading chunks of file using copy_file_range()
   42  */
   43 static int stress_copy_file(const args_t *args)
   44 {
   45     int fd_in, fd_out, rc = EXIT_FAILURE;
   46     char filename[PATH_MAX - 5], tmp[PATH_MAX];
   47     uint64_t copy_file_bytes = DEFAULT_COPY_FILE_BYTES;
   48 
   49     if (!get_setting("copy-file-bytes", &copy_file_bytes)) {
   50         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   51             copy_file_bytes = MAX_HDD_BYTES;
   52         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   53             copy_file_bytes = MIN_HDD_BYTES;
   54     }
   55 
   56     copy_file_bytes /= args->num_instances;
   57     if (copy_file_bytes < DEFAULT_COPY_FILE_SIZE)
   58         copy_file_bytes = DEFAULT_COPY_FILE_SIZE * 2;
   59     if (copy_file_bytes < MIN_COPY_FILE_BYTES)
   60         copy_file_bytes = MIN_COPY_FILE_BYTES;
   61 
   62     if (stress_temp_dir_mk(args->name, args->pid, args->instance) < 0)
   63         goto tidy_dir;
   64     (void)stress_temp_filename_args(args,
   65             filename, sizeof(filename), mwc32());
   66     (void)snprintf(tmp, sizeof(tmp), "%s-orig", filename);
   67     if ((fd_in = open(tmp, O_CREAT | O_RDWR,  S_IRUSR | S_IWUSR)) < 0) {
   68         rc = exit_status(errno);
   69         pr_fail_err("open");
   70         goto tidy_dir;
   71     }
   72     (void)unlink(tmp);
   73     if (ftruncate(fd_in, copy_file_bytes) < 0) {
   74         rc = exit_status(errno);
   75         pr_fail_err("ftruncate");
   76         goto tidy_in;
   77     }
   78     if (shim_fsync(fd_in) < 0) {
   79         pr_fail_err("fsync");
   80         goto tidy_in;
   81     }
   82 
   83     (void)snprintf(tmp, sizeof(tmp), "%s-copy", filename);
   84     if ((fd_out = open(tmp, O_CREAT | O_WRONLY,  S_IRUSR | S_IWUSR)) < 0) {
   85         rc = exit_status(errno);
   86         pr_fail_err("open");
   87         goto tidy_in;
   88     }
   89     (void)unlink(tmp);
   90 
   91     do {
   92         ssize_t ret;
   93         shim_loff_t off_in, off_out;
   94 
   95         off_in = mwc64() % (copy_file_bytes - DEFAULT_COPY_FILE_SIZE);
   96         off_out = mwc64() % (copy_file_bytes - DEFAULT_COPY_FILE_SIZE);
   97 
   98         ret =  shim_copy_file_range(fd_in, &off_in, fd_out,
   99             &off_out, DEFAULT_COPY_FILE_SIZE, 0);
  100         if (ret < 0) {
  101             if ((errno == EAGAIN) ||
  102                 (errno == EINTR) ||
  103                 (errno == ENOSPC))
  104                 continue;
  105             pr_fail_err("copy_file_range");
  106             goto tidy_out;
  107         }
  108         (void)shim_fsync(fd_out);
  109         inc_counter(args);
  110     } while (keep_stressing());
  111     rc = EXIT_SUCCESS;
  112 
  113 tidy_out:
  114     (void)close(fd_out);
  115 tidy_in:
  116     (void)close(fd_in);
  117 tidy_dir:
  118     (void)stress_temp_dir_rm_args(args);
  119 
  120     return rc;
  121 }
  122 
  123 stressor_info_t stress_copy_file_info = {
  124     .stressor = stress_copy_file,
  125     .class = CLASS_FILESYSTEM | CLASS_OS
  126 };
  127 #else
  128 stressor_info_t stress_copy_file_info = {
  129     .stressor = stress_not_implemented,
  130     .class = CLASS_FILESYSTEM | CLASS_OS
  131 };
  132 #endif