"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-null.c" (15 Mar 2019, 2179 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-null.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_null
   29  *  stress writing to /dev/null
   30  */
   31 static int stress_null(const args_t *args)
   32 {
   33     int fd;
   34     char buffer[4096];
   35 
   36     if ((fd = open("/dev/null", O_RDWR)) < 0) {
   37         pr_fail_err("open");
   38         return EXIT_FAILURE;
   39     }
   40 
   41     (void)memset(buffer, 0xff, sizeof(buffer));
   42     do {
   43         ssize_t ret;
   44 #if defined(__linux__)
   45         void *ptr;
   46         const size_t page_size = args->page_size;
   47 #endif
   48 
   49         ret = write(fd, buffer, sizeof(buffer));
   50         if (ret <= 0) {
   51             if ((errno == EAGAIN) || (errno == EINTR))
   52                 continue;
   53             if (errno) {
   54                 pr_fail_err("write");
   55                 (void)close(fd);
   56                 return EXIT_FAILURE;
   57             }
   58             continue;
   59         }
   60 
   61 #if defined(__linux__)
   62         ptr = mmap(NULL, page_size, PROT_WRITE,
   63             MAP_PRIVATE | MAP_ANONYMOUS, fd, mwc64() & ~(page_size - 1));
   64         if (ptr != MAP_FAILED) {
   65             (void)memset(ptr, mwc8(), page_size);
   66             (void)shim_msync(ptr, page_size, MS_SYNC);
   67             (void)munmap(ptr, page_size);
   68         }
   69 #endif
   70 
   71         inc_counter(args);
   72     } while (keep_stressing());
   73     (void)close(fd);
   74 
   75     return EXIT_SUCCESS;
   76 }
   77 
   78 stressor_info_t stress_null_info = {
   79     .stressor = stress_null,
   80     .class = CLASS_DEV | CLASS_MEMORY | CLASS_OS
   81 };