"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-msg.c" (15 Mar 2019, 4356 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-msg.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_IPC_H) &&  \
   28     defined(HAVE_SYS_MSG_H) &&  \
   29     defined(HAVE_MQ_SYSV)
   30 
   31 #define MAX_SIZE    (8)
   32 #define MSG_STOP    "STOPMSG"
   33 
   34 typedef struct {
   35     long mtype;
   36     char msg[MAX_SIZE];
   37 } msg_t;
   38 
   39 static int stress_msg_getstats(const args_t *args, const int msgq_id)
   40 {
   41     struct msqid_ds buf;
   42 
   43     if (msgctl(msgq_id, IPC_STAT, &buf) < 0) {
   44         pr_fail_err("msgctl: IPC_STAT");
   45         return -errno;
   46     }
   47 #if defined(IPC_INFO) &&    \
   48     defined(HAVE_MSG_INFO)
   49     {
   50         struct msginfo info;
   51 
   52         if (msgctl(msgq_id, IPC_INFO, (struct msqid_ds *)&info) < 0) {
   53             pr_fail_err("msgctl: IPC_INFO");
   54             return -errno;
   55         }
   56     }
   57 #endif
   58 #if defined(MSG_INFO) &&    \
   59     defined(HAVE_MSG_INFO)
   60     {
   61         struct msginfo info;
   62 
   63         if (msgctl(msgq_id, MSG_INFO, (struct msqid_ds *)&info) < 0) {
   64             pr_fail_err("msgctl: MSG_INFO");
   65             return -errno;
   66         }
   67     }
   68 #endif
   69 
   70     return 0;
   71 }
   72 
   73 /*
   74  *  stress_msg
   75  *  stress by message queues
   76  */
   77 static int stress_msg(const args_t *args)
   78 {
   79     pid_t pid;
   80     int msgq_id;
   81 
   82     msgq_id = msgget(IPC_PRIVATE, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL);
   83     if (msgq_id < 0) {
   84         pr_fail_dbg("msgget");
   85         return exit_status(errno);
   86     }
   87     pr_dbg("%s: System V message queue created, id: %d\n", args->name, msgq_id);
   88 
   89 again:
   90     pid = fork();
   91     if (pid < 0) {
   92         if (g_keep_stressing_flag &&
   93             ((errno == EAGAIN) || (errno == ENOMEM)))
   94             goto again;
   95         pr_fail_dbg("fork");
   96         return EXIT_FAILURE;
   97     } else if (pid == 0) {
   98         (void)setpgid(0, g_pgrp);
   99         stress_parent_died_alarm();
  100 
  101         while (keep_stressing()) {
  102             msg_t msg;
  103             uint64_t i;
  104 
  105             for (i = 0; keep_stressing(); i++) {
  106                 uint64_t v;
  107                 if (msgrcv(msgq_id, &msg, sizeof(msg.msg), 0, 0) < 0) {
  108                     pr_fail_dbg("msgrcv");
  109                     break;
  110                 }
  111                 if (!strcmp(msg.msg, MSG_STOP))
  112                     break;
  113                 if (g_opt_flags & OPT_FLAGS_VERIFY) {
  114                     (void)memcpy(&v, msg.msg, sizeof(v));
  115                     if (v != i)
  116                         pr_fail("%s: msgrcv: expected msg containing 0x%" PRIx64
  117                             " but received 0x%" PRIx64 " instead\n", args->name, i, v);
  118                 }
  119             }
  120             _exit(EXIT_SUCCESS);
  121         }
  122     } else {
  123         msg_t msg;
  124         uint64_t i = 0;
  125         int status;
  126 
  127         /* Parent */
  128         (void)setpgid(pid, g_pgrp);
  129 
  130         do {
  131             (void)memcpy(msg.msg, &i, sizeof(i));
  132             msg.mtype = 1;
  133             if (msgsnd(msgq_id, &msg, sizeof(i), 0) < 0) {
  134                 if (errno != EINTR)
  135                     pr_fail_dbg("msgsnd");
  136                 break;
  137             }
  138             if ((i & 0x1f) == 0)
  139                 if (stress_msg_getstats(args, msgq_id) < 0)
  140                     break;
  141             /*
  142              *  NetBSD can shove loads of messages onto
  143              *  a queue before it blocks, so force
  144              *  a scheduling yield every so often so that
  145              *  consumer can read them.
  146              */
  147             if ((i & 0xff) == 0)
  148                 (void)shim_sched_yield();
  149             i++;
  150             inc_counter(args);
  151         } while (keep_stressing());
  152 
  153         (void)shim_strlcpy(msg.msg, MSG_STOP, sizeof(msg.msg));
  154         if (msgsnd(msgq_id, &msg, sizeof(msg.msg), 0) < 0)
  155             pr_fail_dbg("termination msgsnd");
  156         (void)kill(pid, SIGKILL);
  157         (void)waitpid(pid, &status, 0);
  158 
  159         if (msgctl(msgq_id, IPC_RMID, NULL) < 0)
  160             pr_fail_dbg("msgctl");
  161         else
  162             pr_dbg("%s: System V message queue deleted, id: %d\n", args->name, msgq_id);
  163     }
  164     return EXIT_SUCCESS;
  165 }
  166 
  167 stressor_info_t stress_msg_info = {
  168     .stressor = stress_msg,
  169     .class = CLASS_SCHEDULER | CLASS_OS
  170 };
  171 #else
  172 stressor_info_t stress_msg_info = {
  173     .stressor = stress_not_implemented,
  174     .class = CLASS_SCHEDULER | CLASS_OS
  175 };
  176 #endif