"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-open.c" (15 Mar 2019, 4069 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-open.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.49_vs_0.09.50.

    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 typedef int (*open_func_t)(void);
   28 
   29 static int open_dev_zero_rd(void)
   30 {
   31     int flags = 0;
   32 #if defined(O_ASYNC)
   33     flags |= (mwc32() & O_ASYNC);
   34 #endif
   35 #if defined(O_CLOEXEC)
   36     flags |= (mwc32() & O_CLOEXEC);
   37 #endif
   38 #if defined(O_LARGEFILE)
   39     flags |= (mwc32() & O_LARGEFILE);
   40 #endif
   41 #if defined(O_NOFOLLOW)
   42     flags |= (mwc32() & O_NOFOLLOW);
   43 #endif
   44 #if defined(O_NONBLOCK)
   45     flags |= (mwc32() & O_NONBLOCK);
   46 #endif
   47 
   48     return open("/dev/zero", O_RDONLY | flags);
   49 }
   50 
   51 static int open_dev_null_wr(void)
   52 {
   53     int flags = 0;
   54 #if defined(O_ASYNC)
   55     flags |= (mwc32() & O_ASYNC);
   56 #endif
   57 #if defined(O_CLOEXEC)
   58     flags |= (mwc32() & O_CLOEXEC);
   59 #endif
   60 #if defined(O_LARGEFILE)
   61     flags |= (mwc32() & O_LARGEFILE);
   62 #endif
   63 #if defined(O_NOFOLLOW)
   64     flags |= (mwc32() & O_NOFOLLOW);
   65 #endif
   66 #if defined(O_NONBLOCK)
   67     flags |= (mwc32() & O_NONBLOCK);
   68 #endif
   69 #if defined(O_DSYNC)
   70     flags |= (mwc32() & O_DSYNC);
   71 #endif
   72 #if defined(O_SYNC)
   73     flags |= (mwc32() & O_SYNC);
   74 #endif
   75 
   76     return open("/dev/null", O_WRONLY | flags);
   77 }
   78 
   79 #if defined(O_TMPFILE)
   80 static int open_tmp_rdwr(void)
   81 {
   82     int flags = 0;
   83 #if defined(O_TRUNC)
   84     flags |= (mwc32() & O_TRUNC);
   85 #endif
   86 #if defined(O_APPEND)
   87     flags |= (mwc32() & O_APPEND);
   88 #endif
   89 #if defined(O_NOATIME)
   90     flags |= (mwc32() & O_NOATIME);
   91 #endif
   92 #if defined(O_DIRECT)
   93     flags |= (mwc32() & O_DIRECT);
   94 #endif
   95     return open("/tmp", O_TMPFILE | flags | O_RDWR, S_IRUSR | S_IWUSR);
   96 }
   97 #endif
   98 
   99 #if defined(HAVE_POSIX_OPENPT) && defined(O_RDWR) && defined(N_NOCTTY)
  100 static int open_pt(void)
  101 {
  102     return posix_openpt(O_RDWR | O_NOCTTY);
  103 }
  104 #endif
  105 
  106 #if defined(O_TMPFILE) && defined(O_EXCL)
  107 static int open_tmp_rdwr_excl(void)
  108 {
  109     return open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
  110 }
  111 #endif
  112 
  113 #if defined(O_DIRECTORY)
  114 static int open_dir(void)
  115 {
  116     return open(".", O_DIRECTORY | O_RDONLY);
  117 }
  118 #endif
  119 
  120 #if defined(O_PATH)
  121 static int open_path(void)
  122 {
  123     return open(".", O_DIRECTORY | O_PATH);
  124 }
  125 #endif
  126 
  127 
  128 static open_func_t open_funcs[] = {
  129     open_dev_zero_rd,
  130     open_dev_null_wr,
  131 #if defined(O_TMPFILE)
  132     open_tmp_rdwr,
  133 #endif
  134 #if defined(O_TMPFILE) && defined(O_EXCL)
  135     open_tmp_rdwr_excl,
  136 #endif
  137 #if defined(O_DIRECTORY)
  138     open_dir,
  139 #endif
  140 #if defined(O_PATH)
  141     open_path,
  142 #endif
  143 #if defined(HAVE_POSIX_OPENPT) && defined(O_RDWR) && defined(N_NOCTTY)
  144     open_pt
  145 #endif
  146 };
  147 
  148 /*
  149  *  stress_open()
  150  *  stress system by rapid open/close calls
  151  */
  152 static int stress_open(const args_t *args)
  153 {
  154     int fds[STRESS_FD_MAX];
  155     size_t max_fd = stress_get_file_limit();
  156 
  157     if (max_fd > SIZEOF_ARRAY(fds))
  158         max_fd = SIZEOF_ARRAY(fds);
  159 
  160     do {
  161         size_t i, n;
  162 
  163         for (i = 0; i < max_fd; i++) {
  164             int idx = mwc32() % SIZEOF_ARRAY(open_funcs);
  165 
  166             fds[i] = open_funcs[idx]();
  167 
  168             if (fds[i] < 0)
  169                 break;
  170             if (!keep_stressing())
  171                 break;
  172             inc_counter(args);
  173         }
  174         n = i;
  175 
  176         for (i = 0; i < n; i++) {
  177             if (fds[i] < 0)
  178                 break;
  179             (void)close(fds[i]);
  180         }
  181     } while (keep_stressing());
  182 
  183     return EXIT_SUCCESS;
  184 }
  185 
  186 stressor_info_t stress_open_info = {
  187     .stressor = stress_open,
  188     .class = CLASS_FILESYSTEM | CLASS_OS
  189 };