"Fossies" - the Fresh Open Source Software Archive

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

    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_TEE) &&    \
   28     defined(SPLICE_F_NONBLOCK)
   29 
   30 #define TEE_IO_SIZE (65536)
   31 
   32 /*
   33  *  stress_tee_spawn()
   34  *  spawn off tee I/O processes
   35  */
   36 static pid_t stress_tee_spawn(
   37     const args_t *args,
   38     void (*func)(int fds[2]),
   39     int fds[2])
   40 {
   41     pid_t pid;
   42 
   43     if (pipe(fds) < 0) {
   44         pr_err("%s: pipe failed: %d (%s)\n",
   45             args->name, errno, strerror(errno));
   46         return -1;
   47     }
   48 
   49 again:
   50     pid = fork();
   51     if (pid < 0) {
   52         if (g_keep_stressing_flag && (errno == EAGAIN))
   53             goto again;
   54 
   55         (void)close(fds[0]);
   56         (void)close(fds[1]);
   57         pr_err("%s: fork failed: %d (%s)\n",
   58             args->name, errno, strerror(errno));
   59         return -1;
   60     }
   61     if (pid == 0) {
   62         (void)setpgid(0, g_pgrp);
   63         stress_parent_died_alarm();
   64 
   65         func(fds);
   66         _exit(EXIT_SUCCESS);
   67     }
   68     (void)setpgid(pid, g_pgrp);
   69     return pid;
   70 }
   71 
   72 /*
   73  *  stress_tee_pipe_write()
   74  *  write data down a pipe
   75  */
   76 static void stress_tee_pipe_write(int fds[2])
   77 {
   78     char buffer[TEE_IO_SIZE];
   79 
   80     (void)close(fds[0]);
   81 
   82     (void)memset(buffer, 0, sizeof(buffer));
   83     while (g_keep_stressing_flag) {
   84         ssize_t ret;
   85 
   86         ret = write(fds[1], buffer, sizeof(buffer));
   87         if (ret < 0) {
   88             if (errno != EAGAIN)
   89                 break;
   90         }
   91     }
   92     (void)close(fds[1]);
   93 }
   94 
   95 /*
   96  *  stress_tee_pipe_read()
   97  *  read data from a pipe
   98  */
   99 static void stress_tee_pipe_read(int fds[2])
  100 {
  101     char buffer[TEE_IO_SIZE];
  102 
  103     (void)close(fds[1]);
  104 
  105     while (g_keep_stressing_flag) {
  106         ssize_t ret;
  107 
  108         ret = read(fds[0], buffer, sizeof(buffer));
  109         if (ret < 0)
  110             if (errno != EAGAIN)
  111                 break;
  112     }
  113     (void)close(fds[1]);
  114 }
  115 
  116 /*
  117  *  stress_tee()
  118  *  stress the Linux tee syscall
  119  */
  120 static int stress_tee(const args_t *args)
  121 {
  122     ssize_t len, slen;
  123     int fd, pipe_in[2], pipe_out[2];
  124     pid_t pids[2];
  125     int ret = EXIT_FAILURE, status;
  126 
  127     fd = open("/dev/null", O_WRONLY);
  128     if (fd < 0) {
  129         pr_err("%s: open /dev/null failed: errno=%d (%s)\n",
  130             args->name, errno, strerror(errno));
  131         return EXIT_FAILURE;
  132     }
  133 
  134     pids[0] = stress_tee_spawn(args, stress_tee_pipe_write, pipe_in);
  135     if (pids[0] < 0) {
  136         (void)close(fd);
  137         return EXIT_FAILURE;
  138     }
  139     (void)close(pipe_in[1]);
  140 
  141     pids[1] = stress_tee_spawn(args, stress_tee_pipe_read, pipe_out);
  142     if (pids[0] < 0)
  143         goto tidy_child1;
  144     (void)close(pipe_out[0]);
  145 
  146     do {
  147         len = tee(pipe_in[0], pipe_out[1],
  148             INT_MAX, 0 & SPLICE_F_NONBLOCK);
  149 
  150         if (len < 0) {
  151             if (errno == EAGAIN)
  152                 continue;
  153             if (errno == EINTR)
  154                 break;
  155             if (errno == ENOMEM) {
  156                 pr_inf("%s: skipping stressor, out of memory\n",
  157                     args->name);
  158                 ret = EXIT_NO_RESOURCE;
  159                 goto tidy_child2;
  160             }
  161             pr_err("%s: tee failed: errno=%d (%s)\n",
  162                 args->name, errno, strerror(errno));
  163             goto tidy_child2;
  164         } else {
  165             if (len == 0)
  166                 break;
  167         }
  168 
  169         while (len > 0) {
  170             slen = splice(pipe_in[0], NULL, fd, NULL,
  171                 len, SPLICE_F_MOVE);
  172             if (errno == EINTR)
  173                 break;
  174             if (slen < 0) {
  175                 pr_err("%s: splice failed: errno=%d (%s)\n",
  176                     args->name, errno, strerror(errno));
  177                 goto tidy_child2;
  178             }
  179             len -= slen;
  180         }
  181         inc_counter(args);
  182     } while (keep_stressing());
  183 
  184     ret = EXIT_SUCCESS;
  185 
  186 tidy_child2:
  187     (void)close(pipe_out[1]);
  188     (void)kill(pids[1], SIGKILL);
  189     (void)waitpid(pids[1], &status, 0);
  190 
  191 tidy_child1:
  192     (void)close(pipe_in[0]);
  193     (void)kill(pids[0], SIGKILL);
  194     (void)waitpid(pids[0], &status, 0);
  195 
  196     (void)close(fd);
  197 
  198     return ret;
  199 }
  200 
  201 stressor_info_t stress_tee_info = {
  202     .stressor = stress_tee,
  203     .class = CLASS_PIPE_IO | CLASS_OS | CLASS_SCHEDULER
  204 };
  205 #else
  206 stressor_info_t stress_tee_info = {
  207     .stressor = stress_not_implemented,
  208     .class = CLASS_PIPE_IO | CLASS_OS | CLASS_SCHEDULER
  209 };
  210 #endif