"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-zero.c" (15 Mar 2019, 2744 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-zero.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 /*
   28  *  stress_zero
   29  *  stress reading of /dev/zero
   30  */
   31 static int stress_zero(const args_t *args)
   32 {
   33     int fd;
   34     const size_t page_size = args->page_size;
   35 #if defined(__minix__)
   36     const int flags = O_RDONLY;
   37 #else
   38     const int flags = O_RDWR;
   39 #endif
   40     if ((fd = open("/dev/zero", flags)) < 0) {
   41         pr_fail_err("open /dev/zero");
   42         return EXIT_FAILURE;
   43     }
   44 
   45     do {
   46         char buffer[page_size];
   47         ssize_t ret;
   48 #if defined(__linux__)
   49         int32_t *ptr;
   50 #endif
   51 
   52         ret = read(fd, buffer, sizeof(buffer));
   53         if (ret < 0) {
   54             if ((errno == EAGAIN) || (errno == EINTR))
   55                 continue;
   56             pr_fail_err("read");
   57             (void)close(fd);
   58             return EXIT_FAILURE;
   59         }
   60 
   61 #if !defined(__minix__)
   62         /* One can also write to /dev/zero w/o failure */
   63         ret = write(fd, buffer, sizeof(buffer));
   64         if (ret < 0) {
   65             if ((errno == EAGAIN) || (errno == EINTR))
   66                 continue;
   67             pr_fail_err("write");
   68             (void)close(fd);
   69             return EXIT_FAILURE;
   70         }
   71 #endif
   72 
   73 #if defined(__linux__)
   74         /*
   75          *  check if we can mmap /dev/zero
   76          */
   77         ptr = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS,
   78             fd, page_size * mwc16());
   79         if (ptr == MAP_FAILED) {
   80             if (errno == ENOMEM)
   81                 continue;
   82             pr_fail_err("mmap /dev/zero");
   83             (void)close(fd);
   84             return EXIT_FAILURE;
   85         }
   86         /* Quick sanity check if first 32 bits are zero */
   87         if (*ptr != 0) {
   88             pr_fail_err("mmap'd /dev/zero not null");
   89             (void)munmap(ptr, page_size);
   90             (void)close(fd);
   91             return EXIT_FAILURE;
   92         }
   93         (void)munmap(ptr, page_size);
   94 #endif
   95         inc_counter(args);
   96     } while (keep_stressing());
   97     (void)close(fd);
   98 
   99     return EXIT_SUCCESS;
  100 }
  101 
  102 stressor_info_t stress_zero_info = {
  103     .stressor = stress_zero,
  104     .class = CLASS_DEV | CLASS_MEMORY | CLASS_OS
  105 };