"Fossies" - the Fresh Open Source Software Archive

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

    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(HAVE_SYS_EVENTFD_H) && \
   28     defined(HAVE_EVENTFD) && \
   29     NEED_GLIBC(2,8,0)
   30 
   31 /*
   32  *  stress_eventfd
   33  *  stress eventfd read/writes
   34  */
   35 static int stress_eventfd(const args_t *args)
   36 {
   37     pid_t pid;
   38     int fd1, fd2, rc;
   39 
   40     fd1 = eventfd(0, 0);
   41     if (fd1 < 0) {
   42         rc = exit_status(errno);
   43         pr_fail_dbg("eventfd");
   44         return rc;
   45     }
   46     fd2 = eventfd(0, 0);
   47     if (fd2 < 0) {
   48         rc = exit_status(errno);
   49         pr_fail_dbg("eventfd");
   50         (void)close(fd1);
   51         return rc;
   52     }
   53 
   54 again:
   55     pid = fork();
   56     if (pid < 0) {
   57         if (g_keep_stressing_flag &&
   58                     ((errno == EAGAIN) || (errno == ENOMEM)))
   59             goto again;
   60         pr_fail_dbg("fork");
   61         (void)close(fd1);
   62         (void)close(fd2);
   63         return EXIT_FAILURE;
   64     } else if (pid == 0) {
   65         (void)setpgid(0, g_pgrp);
   66         stress_parent_died_alarm();
   67 
   68         while (g_keep_stressing_flag) {
   69             uint64_t val;
   70             ssize_t ret;
   71 
   72             for (;;) {
   73                 if (!g_keep_stressing_flag)
   74                     goto exit_child;
   75                 ret = read(fd1, &val, sizeof(val));
   76                 if (ret < 0) {
   77                     if ((errno == EAGAIN) ||
   78                         (errno == EINTR))
   79                         continue;
   80                     pr_fail_dbg("child read");
   81                     goto exit_child;
   82                 }
   83                 if (ret < (ssize_t)sizeof(val)) {
   84                     pr_fail_dbg("child short read");
   85                     goto exit_child;
   86                 }
   87                 break;
   88             }
   89             val = 1;
   90 
   91             for (;;) {
   92                 if (!g_keep_stressing_flag)
   93                     goto exit_child;
   94                 ret = write(fd2, &val, sizeof(val));
   95                 if (ret < 0) {
   96                     if ((errno == EAGAIN) ||
   97                         (errno == EINTR))
   98                         continue;
   99                     pr_fail_dbg("child write");
  100                     goto exit_child;
  101                 }
  102                 if (ret  < (ssize_t)sizeof(val)) {
  103                     pr_fail_dbg("child short write");
  104                     goto exit_child;
  105                 }
  106                 break;
  107             }
  108         }
  109 exit_child:
  110         (void)close(fd1);
  111         (void)close(fd2);
  112         _exit(EXIT_SUCCESS);
  113     } else {
  114         int status;
  115 
  116         do {
  117             uint64_t val = 1;
  118             int ret;
  119 
  120             for (;;) {
  121                 if (!g_keep_stressing_flag)
  122                     goto exit_parent;
  123 
  124                 ret = write(fd1, &val, sizeof(val));
  125                 if (ret < 0) {
  126                     if ((errno == EAGAIN) ||
  127                         (errno == EINTR))
  128                         continue;
  129                     pr_fail_dbg("parent write");
  130                     goto exit_parent;
  131                 }
  132                 if (ret < (ssize_t)sizeof(val)) {
  133                     pr_fail_dbg("parent short write");
  134                     goto exit_parent;
  135                 }
  136                 break;
  137             }
  138 
  139             for (;;) {
  140                 if (!g_keep_stressing_flag)
  141                     goto exit_parent;
  142 
  143                 ret = read(fd2, &val, sizeof(val));
  144                 if (ret < 0) {
  145                     if ((errno == EAGAIN) ||
  146                         (errno == EINTR))
  147                         continue;
  148                     pr_fail_dbg("parent read");
  149                     goto exit_parent;
  150                 }
  151                 if (ret < (ssize_t)sizeof(val)) {
  152                     pr_fail_dbg("parent short read");
  153                     goto exit_parent;
  154                 }
  155                 break;
  156             }
  157             inc_counter(args);
  158         } while (keep_stressing());
  159 exit_parent:
  160         (void)kill(pid, SIGKILL);
  161         (void)waitpid(pid, &status, 0);
  162         (void)close(fd1);
  163         (void)close(fd2);
  164     }
  165     return EXIT_SUCCESS;
  166 }
  167 
  168 stressor_info_t stress_eventfd_info = {
  169     .stressor = stress_eventfd,
  170     .class = CLASS_FILESYSTEM | CLASS_SCHEDULER | CLASS_OS
  171 };
  172 #else
  173 stressor_info_t stress_eventfd_info = {
  174     .stressor = stress_not_implemented,
  175     .class = CLASS_FILESYSTEM | CLASS_SCHEDULER | CLASS_OS
  176 };
  177 #endif