"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.10.00/stress-utime.c" (8 Jul 2019, 4973 Bytes) of package /linux/privat/stress-ng-0.10.00.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-utime.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.59.1_vs_0.09.60.

    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 static const help_t help[] = {
   28     { NULL, "utime N",  "start N workers updating file timestamps" },
   29     { NULL, "utime-ops N",  "stop after N utime bogo operations" },
   30     { NULL, "utime-fsync",  "force utime meta data sync to the file system" },
   31     { NULL, NULL,       NULL }
   32 };
   33 
   34 static int stress_set_utime_fsync(const char *opt)
   35 {
   36     bool utime_fsync = true;
   37 
   38     (void)opt;
   39     return set_setting("utime-fsync", TYPE_ID_BOOL, &utime_fsync);
   40 }
   41 
   42 /*
   43  *  stress_utime()
   44  *  stress system by setting file utime
   45  */
   46 static int stress_utime(const args_t *args)
   47 {
   48     char filename[PATH_MAX];
   49     int ret, fd;
   50     bool utime_fsync = false;
   51 
   52     (void)get_setting("utime-fsync", &utime_fsync);
   53 
   54     ret = stress_temp_dir_mk_args(args);
   55     if (ret < 0)
   56         return exit_status(-ret);
   57 
   58     (void)stress_temp_filename_args(args,
   59         filename, sizeof(filename), mwc32());
   60     if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
   61         ret = exit_status(errno);
   62         pr_err("%s: open failed: errno=%d: (%s)\n",
   63             args->name, errno, strerror(errno));
   64         (void)stress_temp_dir_rm_args(args);
   65         return ret;
   66     }
   67 
   68     do {
   69         struct timeval timevals[2];
   70 #if (defined(HAVE_FUTIMENS) || defined(HAVE_UTIMENSAT)) && \
   71     (defined(UTIME_NOW) || defined(UTIME_OMIT))
   72         struct timespec ts[2];
   73 #endif
   74 
   75         if (gettimeofday(&timevals[0], NULL) == 0) {
   76             timevals[1] = timevals[0];
   77             if (utimes(filename, timevals) < 0) {
   78                 pr_dbg("%s: utimes failed: errno=%d: (%s)\n",
   79                     args->name, errno, strerror(errno));
   80                 break;
   81             }
   82         }
   83         if (utimes(filename, NULL) < 0) {
   84             pr_dbg("%s: utimes failed: errno=%d: (%s)\n",
   85                 args->name, errno, strerror(errno));
   86             break;
   87         }
   88 #if defined(HAVE_FUTIMENS)
   89         if (futimens(fd, NULL) < 0) {
   90             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
   91                 args->name, errno, strerror(errno));
   92             break;
   93         }
   94 
   95 #if defined(UTIME_NOW)
   96         ts[0].tv_sec = UTIME_NOW;
   97         ts[0].tv_nsec = UTIME_NOW;
   98         ts[1].tv_sec = UTIME_NOW;
   99         ts[1].tv_nsec = UTIME_NOW;
  100         if (futimens(fd, &ts[0]) < 0) {
  101             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
  102                 args->name, errno, strerror(errno));
  103             break;
  104         }
  105 #endif
  106 
  107 #if defined(UTIME_OMIT)
  108         ts[0].tv_sec = UTIME_OMIT;
  109         ts[0].tv_nsec = UTIME_OMIT;
  110         if (futimens(fd, &ts[0]) < 0) {
  111             pr_dbg("%s: futimens failed: errno=%d: (%s)\n",
  112                 args->name, errno, strerror(errno));
  113             break;
  114         }
  115 #endif
  116 #endif
  117 
  118 #if defined(HAVE_UTIMENSAT)
  119 
  120 #if defined(UTIME_NOW)
  121         ts[0].tv_sec = UTIME_NOW;
  122         ts[0].tv_nsec = UTIME_NOW;
  123 
  124         ts[1].tv_sec = UTIME_NOW;
  125         ts[1].tv_nsec = UTIME_NOW;
  126 
  127         (void)utimensat(AT_FDCWD, filename, ts, 0);
  128 #endif
  129 
  130 #if defined(UTIME_OMIT)
  131         ts[1].tv_nsec = UTIME_OMIT;
  132         (void)utimensat(AT_FDCWD, filename, ts, 0);
  133 #endif
  134 
  135 #if defined(AT_SYMLINK_NOFOLLOW)
  136 #if defined(UTIME_NOW)
  137         ts[1].tv_nsec = UTIME_NOW;
  138         (void)utimensat(AT_FDCWD, filename, ts, AT_SYMLINK_NOFOLLOW);
  139 #endif
  140 #if defined(UTIME_OMIT)
  141         ts[1].tv_nsec = UTIME_OMIT;
  142         (void)utimensat(AT_FDCWD, filename, ts, AT_SYMLINK_NOFOLLOW);
  143 #endif
  144 #endif
  145         if (utime_fsync)
  146             (void)shim_fsync(fd);
  147 #endif
  148 
  149 #if defined(HAVE_UTIME_H)
  150         {
  151             struct utimbuf utbuf;
  152 
  153             utbuf.actime = (time_t)time_now();
  154             utbuf.modtime = utbuf.actime;
  155 
  156             if (utime(filename, &utbuf) < 0) {
  157                 pr_dbg("%s: utime failed: errno=%d: (%s)\n",
  158                     args->name, errno, strerror(errno));
  159                 break;
  160             }
  161             if (utime(filename, NULL) < 0) {
  162                 pr_dbg("%s: utime failed: errno=%d: (%s)\n",
  163                     args->name, errno, strerror(errno));
  164                 break;
  165             }
  166         }
  167 #endif
  168         /* forces metadata writeback */
  169         if (utime_fsync)
  170             (void)shim_fsync(fd);
  171         inc_counter(args);
  172     } while (keep_stressing());
  173 
  174     (void)close(fd);
  175     (void)unlink(filename);
  176     (void)stress_temp_dir_rm_args(args);
  177 
  178     return EXIT_SUCCESS;
  179 }
  180 
  181 static const opt_set_func_t opt_set_funcs[] = {
  182         { OPT_utime_fsync,  stress_set_utime_fsync },
  183     { 0,            NULL }
  184 };
  185 
  186 stressor_info_t stress_utime_info = {
  187     .stressor = stress_utime,
  188     .class = CLASS_FILESYSTEM | CLASS_OS,
  189     .opt_set_funcs = opt_set_funcs,
  190     .help = help
  191 };