"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-seek.c" (15 Mar 2019, 4526 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-seek.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_seek_size(const char *opt)
   28 {
   29     uint64_t seek_size;
   30 
   31     seek_size = get_uint64_byte(opt);
   32     check_range_bytes("seek-size", seek_size,
   33         MIN_SEEK_SIZE, MAX_SEEK_SIZE);
   34     return set_setting("seek-size", TYPE_ID_UINT64, &seek_size);
   35 }
   36 
   37 /*
   38  *  stress_seek
   39  *  stress I/O via random seeks and read/writes
   40  */
   41 static int stress_seek(const args_t *args)
   42 {
   43     uint64_t len;
   44     uint64_t seek_size = DEFAULT_SEEK_SIZE;
   45     int ret, fd, rc = EXIT_FAILURE;
   46     char filename[PATH_MAX];
   47     uint8_t buf[512];
   48 #if defined(OPT_SEEK_PUNCH)
   49     bool punch_hole = true;
   50 #endif
   51 
   52     if (!get_setting("seek-size", &seek_size)) {
   53         if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
   54             seek_size = MAX_SEEK_SIZE;
   55         if (g_opt_flags & OPT_FLAGS_MINIMIZE)
   56             seek_size = MIN_SEEK_SIZE;
   57     }
   58     len = seek_size - sizeof(buf);
   59 
   60     ret = stress_temp_dir_mk_args(args);
   61     if (ret < 0)
   62         return exit_status(-ret);
   63 
   64     stress_strnrnd((char *)buf, sizeof(buf));
   65 
   66     (void)stress_temp_filename_args(args,
   67         filename, sizeof(filename), mwc32());
   68     if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
   69         rc = exit_status(errno);
   70         pr_fail_err("open");
   71         goto finish;
   72     }
   73     (void)unlink(filename);
   74     /* Generate file with hole at the end */
   75     if (lseek(fd, (off_t)len, SEEK_SET) < 0) {
   76         rc = exit_status(errno);
   77         pr_fail_err("lseek");
   78         goto close_finish;
   79     }
   80     if (write(fd, buf, sizeof(buf)) < 0) {
   81         rc = exit_status(errno);
   82         pr_fail_err("write");
   83         goto close_finish;
   84     }
   85 
   86     do {
   87         off_t offset;
   88         uint8_t tmp[512];
   89         ssize_t rwret;
   90 
   91         offset = mwc64() % len;
   92         if (lseek(fd, (off_t)offset, SEEK_SET) < 0) {
   93             pr_fail_err("lseek");
   94             goto close_finish;
   95         }
   96 re_write:
   97         if (!g_keep_stressing_flag)
   98             break;
   99         rwret = write(fd, buf, sizeof(buf));
  100         if (rwret <= 0) {
  101             if ((errno == EAGAIN) || (errno == EINTR))
  102                 goto re_write;
  103             if (errno) {
  104                 pr_fail_err("write");
  105                 goto close_finish;
  106             }
  107         }
  108 
  109         offset = mwc64() % len;
  110         if (lseek(fd, (off_t)offset, SEEK_SET) < 0) {
  111             pr_fail_err("lseek SEEK_SET");
  112             goto close_finish;
  113         }
  114 re_read:
  115         if (!g_keep_stressing_flag)
  116             break;
  117         rwret = read(fd, tmp, sizeof(tmp));
  118         if (rwret <= 0) {
  119             if ((errno == EAGAIN) || (errno == EINTR))
  120                 goto re_read;
  121             if (errno) {
  122                 pr_fail_err("read");
  123                 goto close_finish;
  124             }
  125         }
  126         if ((rwret != sizeof(tmp)) &&
  127             (g_opt_flags & OPT_FLAGS_VERIFY)) {
  128             pr_fail("%s: incorrect read size, expecting 512 bytes", args->name);
  129         }
  130 #if defined(SEEK_END)
  131         if (lseek(fd, 0, SEEK_END) < 0) {
  132             if (errno != EINVAL)
  133                 pr_fail_err("lseek SEEK_END");
  134         }
  135 #endif
  136 #if defined(SEEK_CUR)
  137         if (lseek(fd, 0, SEEK_CUR) < 0) {
  138             if (errno != EINVAL)
  139                 pr_fail_err("lseek SEEK_CUR");
  140         }
  141 #endif
  142 #if defined(SEEK_HOLE) && !defined(__APPLE__)
  143         if (lseek(fd, 0, SEEK_HOLE) < 0) {
  144             if (errno != EINVAL)
  145                 pr_fail_err("lseek SEEK_HOLE");
  146         }
  147 #endif
  148 #if defined(SEEK_DATA) && !defined(__APPLE__)
  149         if (lseek(fd, 0, SEEK_DATA) < 0) {
  150             if (errno != EINVAL)
  151                 pr_fail_err("lseek SEEK_DATA");
  152         }
  153 #endif
  154 
  155 #if defined(OPT_SEEK_PUNCH)
  156         if (!punch_hole)
  157             continue;
  158 
  159         offset = mwc64() % len;
  160         if (shim_fallocate(fd, FALLOC_FL_PUNCH_HOLE |
  161                   FALLOC_FL_KEEP_SIZE, offset, 8192) < 0) {
  162             if (errno == EOPNOTSUPP)
  163                 punch_hole = false;
  164         }
  165 #endif
  166         inc_counter(args);
  167     } while (keep_stressing());
  168 
  169     rc = EXIT_SUCCESS;
  170 close_finish:
  171     (void)close(fd);
  172 finish:
  173     (void)stress_temp_dir_rm_args(args);
  174     return rc;
  175 }
  176 
  177 stressor_info_t stress_seek_info = {
  178     .stressor = stress_seek,
  179     .class = CLASS_IO | CLASS_OS
  180 };