"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-full.c" (15 Mar 2019, 3192 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-full.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 #if defined(__linux__)
   28 
   29 typedef struct {
   30     const char *name;
   31     const int whence;
   32 } whences_t;
   33 
   34 static const whences_t whences[] = {
   35     { "SEEK_SET",   SEEK_SET },
   36     { "SEEK_CUR",   SEEK_CUR },
   37     { "SEEK_END",   SEEK_END }
   38 };
   39 
   40 /*
   41  *  stress_full
   42  *  stress /dev/full
   43  */
   44 static int stress_full(const args_t *args)
   45 {
   46     do {
   47         ssize_t ret;
   48         int fd, w;
   49         ssize_t i;
   50         off_t offset;
   51         char buffer[4096];
   52 
   53         if ((fd = open("/dev/full", O_RDWR)) < 0) {
   54             if (errno == ENOENT) {
   55                 pr_inf("%s: /dev/full not available, skipping stress test\n",
   56                     args->name);
   57                 return EXIT_NOT_IMPLEMENTED;
   58             }
   59             pr_fail_err("open");
   60             return EXIT_FAILURE;
   61         }
   62 
   63         /*
   64          *  Writes should always return -ENOSPC
   65          */
   66         (void)memset(buffer, 0, sizeof(buffer));
   67         ret = write(fd, buffer, sizeof(buffer));
   68         if (ret != -1) {
   69             pr_fail("%s: write to /dev/null should fail "
   70                 "with errno ENOSPC but it didn't\n",
   71                 args->name);
   72             (void)close(fd);
   73             return EXIT_FAILURE;
   74         }
   75         if ((errno == EAGAIN) || (errno == EINTR))
   76             goto try_read;
   77         if (errno != ENOSPC) {
   78             pr_fail_err("write");
   79             (void)close(fd);
   80             return EXIT_FAILURE;
   81         }
   82 
   83         /*
   84          *  Reads should always work
   85          */
   86 try_read:
   87         ret = read(fd, buffer, sizeof(buffer));
   88         if (ret < 0) {
   89             pr_fail_err("read");
   90             (void)close(fd);
   91             return EXIT_FAILURE;
   92         }
   93         for (i = 0; i < ret; i++) {
   94             if (buffer[i] != 0) {
   95                 pr_fail("%s: buffer does not "
   96                     "contain all zeros\n",
   97                     args->name);
   98                 (void)close(fd);
   99                 return EXIT_FAILURE;
  100             }
  101         }
  102 
  103         /*
  104          *  Seeks will always succeed
  105          */
  106         w = mwc32() % 3;
  107         offset = (off_t)mwc64();
  108         ret = lseek(fd, offset, whences[w].whence);
  109         if (ret < 0) {
  110             pr_fail("%s: lseek(fd, %jd, %s)\n",
  111                 args->name, (intmax_t)offset, whences[w].name);
  112             (void)close(fd);
  113             return EXIT_FAILURE;
  114         }
  115         (void)close(fd);
  116         inc_counter(args);
  117     } while (keep_stressing());
  118 
  119     return EXIT_SUCCESS;
  120 }
  121 
  122 stressor_info_t stress_full_info = {
  123     .stressor = stress_full,
  124     .class = CLASS_DEV | CLASS_MEMORY | CLASS_OS
  125 };
  126 #else
  127 stressor_info_t stress_full_info = {
  128     .stressor = stress_not_implemented,
  129     .class = CLASS_DEV | CLASS_MEMORY | CLASS_OS
  130 };
  131 #endif