From patchwork Tue Jun 16 09:01:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1310139 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49mMdM1nf7z9sSS for ; Tue, 16 Jun 2020 19:01:42 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 2EC503C2D06 for ; Tue, 16 Jun 2020 11:01:40 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id E12ED3C2CE8 for ; Tue, 16 Jun 2020 11:01:36 +0200 (CEST) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-4.smtp.seeweb.it (Postfix) with ESMTP id 1AA7A101589B for ; Tue, 16 Jun 2020 11:01:34 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.73,518,1583164800"; d="scan'208";a="94570331" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 16 Jun 2020 17:01:31 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id 872964CE2E8A for ; Tue, 16 Jun 2020 17:01:25 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 16 Jun 2020 17:01:24 +0800 From: Yang Xu To: Date: Tue, 16 Jun 2020 17:01:21 +0800 Message-ID: <1592298082-21792-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD04.g08.fujitsu.local (10.167.33.200) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: 872964CE2E8A.AE15B X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH v1 1/2] libs/libltpnewipc: Add missing libmsgctl.c X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Add libmsgctl.c into ltp new ipc libs, so the upcoming msgstress cleanup cases can use its functions. Signed-off-by: Yang Xu --- libs/libltpnewipc/libmsgctl.c | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 libs/libltpnewipc/libmsgctl.c diff --git a/libs/libltpnewipc/libmsgctl.c b/libs/libltpnewipc/libmsgctl.c new file mode 100644 index 000000000..6de512d29 --- /dev/null +++ b/libs/libltpnewipc/libmsgctl.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) International Business Machines Corp., 2002 + * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "libmsgctl.h" + +int doreader(long key, int tid, long type, int child, int nreps) +{ + int i, size; + int id; + struct mbuffer buffer; + + id = msgget(key, 0); + if (id < 0) { + printf("msgget() error in the reader of child group %d: %s\n", + child, strerror(errno)); + + return FAIL; + } + if (id != tid) { + printf("Message queue mismatch in the reader of child group %d for message queue id %d\n", + child, id); + + return FAIL; + } + for (i = 0; i < nreps; i++) { + memset(&buffer, 0, sizeof(buffer)); + + size = msgrcv(id, &buffer, 100, type, 0); + if (size < 0) { + printf("msgrcv() error in child %d, read # = %d: %s\n", + child, (i + 1), strerror(errno)); + + return FAIL; + } + if (buffer.type != type) { + printf("Type mismatch in child %d, read #d = %d: ", + child, (i + 1)); + printf("for message got %ld, expected - %ld\n", + buffer.type, type); + + return FAIL; + } + if (buffer.data.len + 1 != size) { + printf("Size mismatch in child %d, read # = %d: ", + child, (i + 1)); + printf("for message got %d, expected - %d\n", + buffer.data.len + 1, size); + + return FAIL; + } + if (verify(buffer.data.pbytes, (key % 255), size - 1, child)) { + printf("Verify failed in child %d read # = %d, key = %lx\n", + child, (i + 1), key); + + return FAIL; + } + key++; + } + return PASS; +} + +int dowriter(long key, int tid, long type, int child, int nreps) +{ + int i, size; + int id; + struct mbuffer buffer; + + id = msgget(key, 0); + if (id < 0) { + printf("msgget() error in the writer of child group %d: %s\n", + child, strerror(errno)); + + return FAIL; + } + if (id != tid) { + printf("Message queue mismatch in the reader of child group %d for message queue id %d\n", + child, id); + + return FAIL; + } + + for (i = 0; i < nreps; i++) { + memset(&buffer, 0, sizeof(buffer)); + + do { + size = (lrand48() % 99); + } while (size == 0); + fill_buffer(buffer.data.pbytes, (key % 255), size); + buffer.data.len = size; + buffer.type = type; + if (msgsnd(id, &buffer, size + 1, 0) < 0) { + printf("msgsnd() error in child %d, write # = %d, key = %lx: %s\n", + child, nreps, key, strerror(errno)); + + return FAIL; + } + key++; + } + return PASS; +} + +int fill_buffer(char *buf, char val, int size) +{ + int i; + + for (i = 0; i < size; i++) + buf[i] = val; + return 0; +} + +/* Check a buffer for correct values */ +int verify(char *buf, char val, int size, int child) +{ + while (size-- > 0) { + if (*buf++ != val) { + printf("Verify error in child %d, *buf = %x, val = %x, size = %d\n", + child, *buf, val, size); + + return FAIL; + } + } + return PASS; +} From patchwork Tue Jun 16 09:01:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Xu X-Patchwork-Id: 1310140 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49mMdW5f21z9sSS for ; Tue, 16 Jun 2020 19:01:51 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id E665D3C2D05 for ; Tue, 16 Jun 2020 11:01:48 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id E63243C2D07 for ; Tue, 16 Jun 2020 11:01:41 +0200 (CEST) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-4.smtp.seeweb.it (Postfix) with ESMTP id 140611034E53 for ; Tue, 16 Jun 2020 11:01:37 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.73,518,1583164800"; d="scan'208";a="94570333" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 16 Jun 2020 17:01:37 +0800 Received: from G08CNEXMBPEKD04.g08.fujitsu.local (unknown [10.167.33.201]) by cn.fujitsu.com (Postfix) with ESMTP id 7436E4CE2E9B for ; Tue, 16 Jun 2020 17:01:33 +0800 (CST) Received: from localhost.localdomain (10.167.220.84) by G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 16 Jun 2020 17:01:31 +0800 From: Yang Xu To: Date: Tue, 16 Jun 2020 17:01:22 +0800 Message-ID: <1592298082-21792-2-git-send-email-xuyang2018.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1592298082-21792-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> References: <1592298082-21792-1-git-send-email-xuyang2018.jy@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.84] X-ClientProxiedBy: G08CNEXCHPEKD04.g08.fujitsu.local (10.167.33.200) To G08CNEXMBPEKD04.g08.fujitsu.local (10.167.33.201) X-yoursite-MailScanner-ID: 7436E4CE2E9B.A2705 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang2018.jy@cn.fujitsu.com X-Spam-Status: No, score=0.4 required=7.0 tests=KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH v1 2/2] syscalls/msgstress: scale number of processes accodingly to available RAM X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" In these tests, fork may fail because of EAGAIN error in the following situations: 1)Reaching the RLIMIT_NPROC soft resource limit 2)Reaching /proc/sys/kernel/threads-max limit 3)Reaching /proc/sys/kernel/pid_max limit 4)the PID limit (pids.max) imposed by the cgroup "process number" (PIDs) controller was reached According to kernel code kernel/fork.c, for thread_max it has the following calculation formula: THREAD_SIZE = PAGE_SIZE << THREAD_SIZE_ORDER max_threads = totalram_pages / (8 * THREAD_SIZE / PAGE_SIZE); The default maximum number of threads is set to a safe value: the thread structures can take up at most one eighth of the memory. the pid limit is equal to max_threads/2. For example(on x86_64), 4G memory, page_size is 4k,THREAD_SIZE_ORDER is 1 max_threads = 2^32/(8 * 8 * 1024) = 65536 RLIMIT_NPROC = 32768 // 0.5 Also, pids cgroup and systemd also has this limit. On old systemd version, ths limit is 0.8 *thread_num. On newer systemd version, the limit is 0.33 *thread_num. In here, we use 0.3. Aparting from this changes, use new api for these cases and use some SAFE MACRO. Merge msgstress03.c into msgstress01.c and use -r option to read actual msgmni value on procfs instead. Fixes #509 Signed-off-by: Yang Xu --- runtest/syscalls | 4 +- runtest/syscalls-ipc | 4 +- .../kernel/syscalls/ipc/msgstress/.gitignore | 1 - .../kernel/syscalls/ipc/msgstress/Makefile | 4 +- .../syscalls/ipc/msgstress/msgstress01.c | 303 ++++++--------- .../syscalls/ipc/msgstress/msgstress02.c | 301 +++++--------- .../syscalls/ipc/msgstress/msgstress03.c | 290 -------------- .../syscalls/ipc/msgstress/msgstress04.c | 367 ++++++------------ 8 files changed, 311 insertions(+), 963 deletions(-) delete mode 100644 testcases/kernel/syscalls/ipc/msgstress/msgstress03.c diff --git a/runtest/syscalls b/runtest/syscalls index bee03e93f..18e5fa5b5 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -797,9 +797,9 @@ msgctl01 msgctl01 msgctl02 msgctl02 msgctl03 msgctl03 msgctl04 msgctl04 -msgstress01 msgstress01 +msgstress01 msgstress01 -l 100000 msgstress02 msgstress02 -msgstress03 msgstress03 +msgstress03 msgstress01 -r y msgstress04 msgstress04 msgctl12 msgctl12 diff --git a/runtest/syscalls-ipc b/runtest/syscalls-ipc index 54d8622d4..b2688e25b 100644 --- a/runtest/syscalls-ipc +++ b/runtest/syscalls-ipc @@ -2,9 +2,9 @@ msgctl01 msgctl01 msgctl02 msgctl02 msgctl03 msgctl03 msgctl04 msgctl04 -msgstress01 msgstress01 +msgstress01 msgstress01 -l 100000 msgstress02 msgstress02 -msgstress03 msgstress03 +msgstress03 msgstress03 -r y msgstress04 msgstress04 msgctl12 msgctl12 diff --git a/testcases/kernel/syscalls/ipc/msgstress/.gitignore b/testcases/kernel/syscalls/ipc/msgstress/.gitignore index a8f675399..e1df4cda8 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/.gitignore +++ b/testcases/kernel/syscalls/ipc/msgstress/.gitignore @@ -1,4 +1,3 @@ /msgstress01 /msgstress02 -/msgstress03 /msgstress04 diff --git a/testcases/kernel/syscalls/ipc/msgstress/Makefile b/testcases/kernel/syscalls/ipc/msgstress/Makefile index f62cd1f48..a11cbcf2e 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/Makefile +++ b/testcases/kernel/syscalls/ipc/msgstress/Makefile @@ -3,10 +3,10 @@ top_srcdir ?= ../../../../.. -LTPLIBS = ltpipc +LTPLIBS = ltpnewipc include $(top_srcdir)/include/mk/testcases.mk -LDLIBS += -lltpipc +LDLIBS += -lltpnewipc include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c index 0a660c042..bf132d4dc 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c +++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress01.c @@ -1,29 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) International Business Machines Corp., 2002 * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * * 06/30/2001 Port to Linux nsharoff@us.ibm.com * 11/06/2002 Port to LTP dbarrera@us.ibm.com - */ - -/* + * 06/15/2020 Convert to new api xuyang2018.jy@cn.fujitsu.com + * * Get and manipulate a message queue. */ -#define _XOPEN_SOURCE 500 #include #include #include @@ -31,101 +16,66 @@ #include #include #include -#include #include #include #include #include #include -#include "test.h" -#include "ipcmsg.h" +#include "tst_test.h" +#include "libnewipc.h" +#include "tst_safe_sysv_ipc.h" #include "libmsgctl.h" -char *TCID = "msgstress01"; -int TST_TOTAL = 1; - -#ifndef CONFIG_COLDFIRE -#define MAXNPROCS 1000000 /* This value is set to an arbitrary high limit. */ -#else -#define MAXNPROCS 100000 /* Coldfire can't deal with 1000000 */ -#endif +#define MAXNPROCS 1000000 #define MAXNREPS 100000 static key_t keyarray[MAXNPROCS]; static int pidarray[MAXNPROCS]; static int tid; static int MSGMNI, nprocs, nreps; -static int procstat; -static int mykid; - -void setup(void); -void cleanup(void); - -static int dotest(key_t key, int child_process); -static void sig_handler(); - static char *opt_nprocs; static char *opt_nreps; - -static option_t options[] = { - {"n:", NULL, &opt_nprocs}, - {"l:", NULL, &opt_nreps}, - {NULL, NULL, NULL}, +static char *opt_msgmnifile; +static void cleanup(void); + +static struct tst_option options[] = { + {"n:", &opt_nprocs, "-n N Number of processes"}, + {"l:", &opt_nreps, "-l N Number of iterations"}, + {"r:", &opt_msgmnifile, "-r Y/N use actual msgmni value under procfs"}, + {NULL, NULL, NULL} }; -static void usage(void) -{ - printf(" -n Number of processes\n"); - printf(" -l Number of iterations\n"); -} - -int main(int argc, char **argv) +static int dotest(key_t key, int child_process) { - int i, j, ok, pid; - int count, status; - struct sigaction act; + int pid; + int ret, status; - tst_parse_opts(argc, argv, options, usage); + tid = SAFE_MSGGET(key, IPC_CREAT | S_IRUSR | S_IWUSR); - setup(); + pid = SAFE_FORK(); + if (pid == 0) + exit(doreader(key, tid, 1, child_process, nreps)); + ret = dowriter(key, tid, 1, child_process, nreps); + SAFE_WAIT(&status); - nreps = MAXNREPS; - nprocs = MSGMNI; + if (ret != PASS) + exit(FAIL); + if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS))) + exit(FAIL); - if (opt_nreps) { - nreps = atoi(opt_nreps); - if (nreps > MAXNREPS) { - tst_resm(TINFO, - "Requested number of iterations too large, " - "setting to Max. of %d", MAXNREPS); - nreps = MAXNREPS; - } - } + SAFE_MSGCTL(tid, IPC_RMID, NULL); + return PASS; +} - if (opt_nprocs) { - nprocs = atoi(opt_nprocs); - if (nprocs > MSGMNI) { - tst_resm(TINFO, - "Requested number of processes too large, " - "setting to Max. of %d", MSGMNI); - nprocs = MSGMNI; - } - } +static void verify_msgstress(void) +{ + int i, j, ok, pid; + int count, status; srand(getpid()); tid = -1; - /* Setup signal handling routine */ - memset(&act, 0, sizeof(act)); - act.sa_handler = sig_handler; - sigemptyset(&act.sa_mask); - sigaddset(&act.sa_mask, SIGTERM); - if (sigaction(SIGTERM, &act, NULL) < 0) { - tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed"); - } - /* Set up array of unique keys for use in allocating message - * queues - */ + /* Set up array of unique keys for use in allocating message queues */ for (i = 0; i < nprocs; i++) { ok = 1; do { @@ -146,24 +96,17 @@ int main(int argc, char **argv) } while (ok == 0); } - /* Fork a number of processes, each of which will + /* + * Fork a number of processes, each of which will * create a message queue with one reader/writer * pair which will read and write a number (iterations) * of random length messages with specific values. */ for (i = 0; i < nprocs; i++) { - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - tst_brkm(TFAIL, - NULL, - "\tFork failed (may be OK if under stress)"); - } - /* Child does this */ - if (pid == 0) { - procstat = 1; + pid = SAFE_FORK(); + if (pid == 0) exit(dotest(keyarray[i], i)); - } pidarray[i] = pid; } @@ -171,131 +114,105 @@ int main(int argc, char **argv) while (1) { if ((wait(&status)) > 0) { if (status >> 8 != 0) { - tst_brkm(TFAIL, NULL, - "Child exit status = %d", - status >> 8); + tst_brk(TFAIL, "Child exit status = %d", status >> 8); } count++; } else { if (errno != EINTR) { break; } -#ifdef DEBUG - tst_resm(TINFO, "Signal detected during wait"); -#endif + tst_res(TINFO, "Signal detected during wait"); + } } /* Make sure proper number of children exited */ if (count != nprocs) { - tst_brkm(TFAIL, - NULL, + tst_brk(TFAIL, "Wrong number of children exited, Saw %d, Expected %d", count, nprocs); } - tst_resm(TPASS, "Test ran successfully!"); - - cleanup(); - tst_exit(); + tst_res(TPASS, "Test ran successfully!"); } -static int dotest(key_t key, int child_process) -{ - int id, pid; - int ret, status; - - sighold(SIGTERM); - TEST(msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR)); - if (TEST_RETURN < 0) { - printf("msgget() error in child %d: %s\n", - child_process, strerror(TEST_ERRNO)); - - return FAIL; - } - tid = id = TEST_RETURN; - sigrelse(SIGTERM); - - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - printf("\tFork failed (may be OK if under stress)\n"); - TEST(msgctl(tid, IPC_RMID, 0)); - if (TEST_RETURN < 0) { - printf("mscgtl() error in cleanup: %s\n", - strerror(TEST_ERRNO)); - } - return FAIL; - } - /* Child does this */ - if (pid == 0) - exit(doreader(key, id, 1, child_process, nreps)); - /* Parent does this */ - mykid = pid; - procstat = 2; - ret = dowriter(key, id, 1, child_process, nreps); - wait(&status); - - if (ret != PASS) - exit(FAIL); - - if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS))) - exit(FAIL); - - TEST(msgctl(id, IPC_RMID, 0)); - if (TEST_RETURN < 0) { - printf("msgctl() errno %d: %s\n", - TEST_ERRNO, strerror(TEST_ERRNO)); - - return FAIL; - } - return PASS; -} - -static void sig_handler(void) -{ -} - -void setup(void) +static void setup(void) { int nr_msgqs; + int threads_max; + int free_pids; - tst_tmpdir(); + SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%d", &nr_msgqs); + SAFE_FILE_SCANF("/proc/sys/kernel/threads-max", "%d", &threads_max); - tst_sig(FORK, DEF_HANDLER, cleanup); + nr_msgqs -= GET_USED_QUEUES(); + if (nr_msgqs <= 0) + tst_brk(TCONF, "Max number of message queues already used, cannot create more."); - TEST_PAUSE; + if (opt_msgmnifile) + MSGMNI = nr_msgqs; + else + MSGMNI = min(nr_msgqs, NR_MSGQUEUES); - nr_msgqs = get_max_msgqueues(); - if (nr_msgqs < 0) - cleanup(); + if (opt_nreps) { + nreps = SAFE_STRTOL(opt_nreps, 1, INT_MAX); + nreps = min(nreps, MAXNREPS); + } else { + nreps = 10000; + } - nr_msgqs -= get_used_msgqueues(); - if (nr_msgqs <= 0) { - tst_resm(TBROK, - "Max number of message queues already used, cannot create more."); - cleanup(); + if (opt_nprocs) { + nprocs = SAFE_STRTOL(opt_nprocs, 1, INT_MAX); + nprocs = min(nprocs, MAXNPROCS); + nprocs = min(nprocs, MSGMNI); + } else { + nprocs = MSGMNI; } /* - * Since msgmni scales to the memory size, it may reach huge values - * that are not necessary for this test. - * That's why we define NR_MSGQUEUES as a high boundary for it. + * fork may fail because of EAGAIN error in the following situations: + * 1)Reaching the RLIMIT_NPROC soft resource limit + * 2)Reaching /proc/sys/kernel/threads-max limit + * 3)Reaching /proc/sys/kernel/pid_max limit + * 4)the PID limit (pids.max) imposed by the cgroup "process number" (PIDs) controller was reached + * + * According to kernel code kernel/fork.c, for thread_max it has the following + * calculation formula: + * THREAD_SIZE = PAGE_SIZE << THREAD_SIZE_ORDER + * max_threads = totalram_pages / (8 * THREAD_SIZE / PAGE_SIZE); + * The default maximum number of threads is set to a safe value: the thread + * structures can take up at most one eighth of the memory. + * the pid limit is equal to max_threads/2. + * For example(on x86_64), 4G memory, page_size is 4k,THREAD_SIZE_ORDER is 1 + * max_threads = 2^32/(8 * 8 * 1024) = 65536 + * RLIMIT_NPROC = 32768 // 0.5 + * + * Also, pids cgroup and systemd also has this limit. On old systemd version, + * ths limit is 0.8 *thread_num. On newer systemd version, the limit is 0.33 * + * thread_num. In here, we use 0.3. */ - MSGMNI = min(nr_msgqs, NR_MSGQUEUES); + nprocs = min(nprocs, 0.3 * threads_max); + free_pids = tst_get_free_pids(); + if (free_pids < 0) + tst_brk(TBROK, "Can't obtain free_pid count"); + if (!free_pids) + tst_brk(TBROK, "No free pids"); + + nprocs = min(nprocs, free_pids * 0.8); + + tst_res(TINFO, "Number of message queues is %d, process is %d, iterations is %d", MSGMNI, nprocs, nreps); } void cleanup(void) { - int status; - -#ifdef DEBUG - tst_resm(TINFO, "Removing the message queue"); -#endif - (void)msgctl(tid, IPC_RMID, NULL); - if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) { - (void)msgctl(tid, IPC_RMID, NULL); - tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed"); - - } - - tst_rmdir(); + if (tid >= 0) + SAFE_MSGCTL(tid, IPC_RMID, NULL); } + +static struct tst_test test = { + .needs_tmpdir = 1, + .options = options, + .setup = setup, + .cleanup = cleanup, + .forks_child = 1, + .test_all = verify_msgstress, +}; diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress02.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress02.c index e15131043..622f955dd 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress02.c +++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress02.c @@ -1,31 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) International Business Machines Corp., 2002 * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * * 06/30/2001 Port to Linux nsharoff@us.ibm.com * 11/11/2002 Port to LTP dbarrera@us.ibm.com - */ - -/* + * 06/15/2020 Convert to new api xuyang2018.jy@cn.fujitsu.com + * * Get and manipulate a message queue. */ - #define _XOPEN_SOURCE 500 #include -#include #include #include #include @@ -35,19 +19,13 @@ #include #include #include -#include "test.h" -#include "ipcmsg.h" +#include "tst_test.h" +#include "libnewipc.h" +#include "tst_safe_sysv_ipc.h" #include "libmsgctl.h" -char *TCID = "msgstress02"; -int TST_TOTAL = 1; - #define MAXNREPS 1000 -#ifndef CONFIG_COLDFIRE -#define MAXNPROCS 1000000 /* This value is set to an arbitrary high limit. */ -#else -#define MAXNPROCS 100000 /* Coldfire can't deal with 1000000 */ -#endif +#define MAXNPROCS 1000000 #define MAXNKIDS 10 static key_t keyarray[MAXNPROCS]; @@ -58,81 +36,35 @@ static int tid; static int nprocs, nreps, nkids, MSGMNI; static int procstat; -void setup(void); -void cleanup(void); +static void cleanup(void); static void term(int); static int dotest(key_t, int); -static void cleanup_msgqueue(int i, int tid); static char *opt_nprocs; static char *opt_nkids; static char *opt_nreps; -static option_t options[] = { - {"n:", NULL, &opt_nprocs}, - {"c:", NULL, &opt_nkids}, - {"l:", NULL, &opt_nreps}, - {NULL, NULL, NULL}, +static struct tst_option options[] = { + {"n:", &opt_nprocs, "-n N Number of processes"}, + {"c:", &opt_nkids, "-c N Number of read/write child pairs"}, + {"l:", &opt_nreps, "-l N Number of iterations"}, + {NULL, NULL, NULL} }; -static void usage(void) -{ - printf(" -n Number of processes\n"); - printf(" -c Number of read/write child pairs\n"); - printf(" -l Number of iterations\n"); -} - -int main(int argc, char **argv) +static void verify_msgstress(void) { int i, j, ok, pid; int count, status; - tst_parse_opts(argc, argv, options, usage); - - setup(); - - nreps = MAXNREPS; - nprocs = MSGMNI; - nkids = MAXNKIDS; - - if (opt_nreps) { - nreps = atoi(opt_nreps); - if (nreps > MAXNREPS) { - tst_resm(TINFO, - "Requested number of iterations too large, " - "setting to Max. of %d", MAXNREPS); - nreps = MAXNREPS; - } - } - - if (opt_nprocs) { - nprocs = atoi(opt_nprocs); - if (nprocs > MSGMNI) { - tst_resm(TINFO, - "Requested number of processes too large, " - "setting to Max. of %d", MSGMNI); - nprocs = MSGMNI; - } - } - - if (opt_nkids) { - nkids = atoi(opt_nkids); - if (nkids > MAXNKIDS) { - tst_resm(TINFO, - "Requested number of read/write pairs too " - "large, setting to Max. of %d", MAXNKIDS); - nkids = MAXNKIDS; - } - } - procstat = 0; srand48((unsigned)getpid() + (unsigned)(getppid() << 16)); tid = -1; /* Setup signal handleing routine */ + if (sigset(SIGTERM, term) == SIG_ERR) { - tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed"); + tst_brk(TFAIL, "Sigset SIGTERM failed"); } /* Set up array of unique keys for use in allocating message * queues @@ -161,15 +93,8 @@ int main(int argc, char **argv) * pairs which will read and write a number (iterations) * of random length messages with specific values (keys). */ - for (i = 0; i < nprocs; i++) { - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - tst_brkm(TFAIL, - NULL, - "\tFork failed (may be OK if under stress)"); - } - /* Child does this */ + pid = SAFE_FORK(); if (pid == 0) { procstat = 1; exit(dotest(keyarray[i], i)); @@ -180,81 +105,40 @@ int main(int argc, char **argv) count = 0; while (1) { if ((wait(&status)) > 0) { - if (status >> 8 != PASS) { - tst_brkm(TFAIL, NULL, - "Child exit status = %d", - status >> 8); - } + if (status >> 8 != PASS) + tst_brk(TFAIL, "Child exit status = %d", + status >> 8); count++; } else { - if (errno != EINTR) { + if (errno != EINTR) break; - } -#ifdef DEBUG - tst_resm(TINFO, "Signal detected during wait"); -#endif + tst_res(TINFO, "Signal detected during wait"); } } /* Make sure proper number of children exited */ if (count != nprocs) { - tst_brkm(TFAIL, - NULL, - "Wrong number of children exited, Saw %d, Expected %d", - count, nprocs); + tst_brk(TFAIL, + "Wrong number of children exited, Saw %d, Expected %d", + count, nprocs); } - tst_resm(TPASS, "Test ran successfully!"); - + tst_res(TPASS, "Test ran successfully!"); cleanup(); - tst_exit(); -} - -static void cleanup_msgqueue(int i, int tid) -{ - /* - * Decrease the value of i by 1 because it - * is getting incremented even if the fork - * is failing. - */ - - i--; - /* - * Kill all children & free message queue. - */ - for (; i >= 0; i--) { - (void)kill(rkidarray[i], SIGKILL); - (void)kill(wkidarray[i], SIGKILL); - } - - if (msgctl(tid, IPC_RMID, 0) < 0) { - tst_brkm(TFAIL | TERRNO, NULL, "Msgctl error in cleanup"); - } } static int dotest(key_t key, int child_process) { - int id, pid; + int pid; int i, count, status, exit_status; sighold(SIGTERM); - if ((id = msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0) { - printf("msgget() error in child %d: %s\n", - child_process, strerror(errno)); - return FAIL; - } - tid = id; + tid = SAFE_MSGGET(key, IPC_CREAT | S_IRUSR | S_IWUSR); sigrelse(SIGTERM); exit_status = PASS; for (i = 0; i < nkids; i++) { - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - printf("Fork failure in the first child of child group %d\n", - child_process); - cleanup_msgqueue(i, tid); - return FAIL; - } + pid = SAFE_FORK(); /* First child does this */ if (pid == 0) { procstat = 2; @@ -262,18 +146,7 @@ static int dotest(key_t key, int child_process) child_process, nreps)); } rkidarray[i] = pid; - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - printf("Fork failure in the second child of child group %d\n", - child_process); - /* - * Kill the reader child process - */ - (void)kill(rkidarray[i], SIGKILL); - - cleanup_msgqueue(i, tid); - return FAIL; - } + pid = SAFE_FORK(); /* Second child does this */ if (pid == 0) { procstat = 2; @@ -293,10 +166,7 @@ static int dotest(key_t key, int child_process) kill(rkidarray[i], SIGTERM); kill(wkidarray[i], SIGTERM); } - if (msgctl(tid, IPC_RMID, 0) < 0) { - printf("msgctl() error: %s\n", - strerror(errno)); - } + SAFE_MSGCTL(tid, IPC_RMID, NULL); return FAIL; } count++; @@ -310,16 +180,10 @@ static int dotest(key_t key, int child_process) if (count != (nkids * 2)) { printf("Wrong number of children exited in child group %d, saw %d, expected %d\n", child_process, count, (nkids * 2)); - if (msgctl(tid, IPC_RMID, 0) < 0) { - printf("msgctl() error: %s\n", strerror(errno)); - } - return FAIL; - } - if (msgctl(id, IPC_RMID, 0) < 0) { - printf("msgctl() failure in child group %d: %s\n", - child_process, strerror(errno)); + SAFE_MSGCTL(tid, IPC_RMID, NULL); return FAIL; } + SAFE_MSGCTL(tid, IPC_RMID, NULL); return exit_status; } @@ -328,17 +192,9 @@ static void term(int sig LTP_ATTRIBUTE_UNUSED) int i; if (procstat == 0) { -#ifdef DEBUG - tst_resm(TINFO, "SIGTERM signal received, test killing kids"); -#endif for (i = 0; i < nprocs; i++) { - if (pidarray[i] > 0) { - if (kill(pidarray[i], SIGTERM) < 0) { - printf("Kill failed to kill child %d", - i); - exit(FAIL); - } - } + if (pidarray[i] > 0) + SAFE_KILL(pidarray[i], SIGTERM); } return; } @@ -353,56 +209,73 @@ static void term(int sig LTP_ATTRIBUTE_UNUSED) } for (i = 0; i < nkids; i++) { if (rkidarray[i] > 0) - kill(rkidarray[i], SIGTERM); + SAFE_KILL(rkidarray[i], SIGTERM); if (wkidarray[i] > 0) - kill(wkidarray[i], SIGTERM); + SAFE_KILL(wkidarray[i], SIGTERM); } } -void setup(void) +static void setup(void) { int nr_msgqs; + int threads_max; + int free_pids; - tst_tmpdir(); + SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%d", &nr_msgqs); + SAFE_FILE_SCANF("/proc/sys/kernel/threads-max", "%d", &threads_max); - tst_sig(FORK, DEF_HANDLER, cleanup); + MSGMNI = nr_msgqs - GET_USED_QUEUES(); + if (MSGMNI <= 0) + tst_brk(TCONF, "Max number of message queues already used, cannot create more."); - TEST_PAUSE; + tst_res(TINFO, "Found %d available message queues", MSGMNI); - nr_msgqs = get_max_msgqueues(); - if (nr_msgqs < 0) - cleanup(); + if (opt_nreps) { + nreps = SAFE_STRTOL(opt_nreps, 1, INT_MAX); + nreps = min(nreps, MAXNREPS); + } else { + nreps = 1000; + } - nr_msgqs -= get_used_msgqueues(); - if (nr_msgqs <= 0) { - tst_resm(TBROK, - "Max number of message queues already used, cannot create more."); - cleanup(); + if (opt_nkids) { + nkids = SAFE_STRTOL(opt_nkids, 1, INT_MAX); + nkids = min(nkids, MAXNKIDS); + } else { + nkids = MAXNKIDS; } - /* - * Since msgmni scales to the memory size, it may reach huge values - * that are not necessary for this test. - * That's why we define NR_MSGQUEUES as a high boundary for it. - */ - MSGMNI = min(nr_msgqs, NR_MSGQUEUES); + if (opt_nprocs) { + nprocs = SAFE_STRTOL(opt_nprocs, 1, INT_MAX); + nprocs = min(nprocs, MAXNPROCS); + nprocs = min(nprocs, nr_msgqs); + } else { + nprocs = NR_MSGQUEUES; + } + + free_pids = tst_get_free_pids(); + if (free_pids < 0) + tst_brk(TBROK, "Can't obtain free_pid count"); + if (!free_pids) + tst_brk(TBROK, "No free pids"); + + free_pids = min(free_pids, 0.3 * threads_max); + nprocs = min(nprocs, (free_pids / 2) / (1 + 2 * nkids)); + if (!nprocs) + tst_brk(TBROK, "Not enough free pids"); + tst_res(TINFO, "%d processes %d read/write pais, %d repeats", nprocs, nkids, nreps); } void cleanup(void) { - int status; - -#ifdef DEBUG - tst_resm(TINFO, "Removing the message queue"); -#endif - fflush(stdout); - (void)msgctl(tid, IPC_RMID, NULL); - if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) { - (void)msgctl(tid, IPC_RMID, NULL); - tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed"); - - } - - fflush(stdout); - tst_rmdir(); + if (tid >= 0) + SAFE_MSGCTL(tid, IPC_RMID, NULL); } + +static struct tst_test test = { + .needs_tmpdir = 1, + .options = options, + .setup = setup, + .cleanup = cleanup, + .forks_child = 1, + .test_all = verify_msgstress, +}; diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c deleted file mode 100644 index 294b401b1..000000000 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Copyright (c) International Business Machines Corp., 2002 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - * 06/30/2001 Port to Linux nsharoff@us.ibm.com - * 11/06/2002 Port to LTP dbarrera@us.ibm.com - */ - -/* - * Get and manipulate a message queue. - * Same as msgstress01 but gets the actual msgmni value under procfs. - */ - -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "test.h" -#include "ipcmsg.h" -#include "libmsgctl.h" - -char *TCID = "msgstress03"; -int TST_TOTAL = 1; - -#define MAXNPROCS 10000 /*These should be sufficient */ -#define MAXNREPS 10000 /*Else they srewup the system un-necessarily */ - -static key_t keyarray[MAXNPROCS]; -static int pidarray[MAXNPROCS]; -static int tid; -static int MSGMNI, nprocs, nreps; -static int procstat; -static int mykid; - -void setup(void); -void cleanup(void); - -static int dotest(key_t key, int child_process); -static void sig_handler(int signo); - -static char *opt_nprocs; -static char *opt_nreps; - -static option_t options[] = { - {"n:", NULL, &opt_nprocs}, - {"l:", NULL, &opt_nreps}, - {NULL, NULL, NULL}, -}; - -static void usage(void) -{ - printf(" -n Number of processes\n"); - printf(" -l Number of iterations\n"); -} - -int main(int argc, char **argv) -{ - int i, j, ok, pid; - int count, status; - struct sigaction act; - - tst_parse_opts(argc, argv, options, usage); - - setup(); - - nreps = MAXNREPS; - nprocs = MSGMNI; - - if (opt_nreps) { - nreps = atoi(opt_nreps); - if (nreps > MAXNREPS) { - tst_resm(TINFO, - "Requested number of iterations too large, " - "setting to Max. of %d", MAXNREPS); - nreps = MAXNREPS; - } - } - - if (opt_nprocs) { - nprocs = atoi(opt_nprocs); - if (nprocs > MSGMNI) { - tst_resm(TINFO, - "Requested number of processes too large, " - "setting to Max. of %d", MSGMNI); - nprocs = MSGMNI; - } - } - - srand(getpid()); - tid = -1; - - /* Setup signal handling routine */ - memset(&act, 0, sizeof(act)); - act.sa_handler = sig_handler; - sigemptyset(&act.sa_mask); - sigaddset(&act.sa_mask, SIGTERM); - if (sigaction(SIGTERM, &act, NULL) < 0) { - tst_brkm(TFAIL, NULL, "Sigset SIGTERM failed"); - } - /* Set up array of unique keys for use in allocating message - * queues - */ - for (i = 0; i < nprocs; i++) { - ok = 1; - do { - /* Get random key */ - keyarray[i] = (key_t) rand(); - /* Make sure key is unique and not private */ - if (keyarray[i] == IPC_PRIVATE) { - ok = 0; - continue; - } - for (j = 0; j < i; j++) { - if (keyarray[j] == keyarray[i]) { - ok = 0; - break; - } - ok = 1; - } - } while (ok == 0); - } - - /* Fork a number of processes, each of which will - * create a message queue with one reader/writer - * pair which will read and write a number (iterations) - * of random length messages with specific values. - */ - - for (i = 0; i < nprocs; i++) { - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - tst_brkm(TFAIL, - NULL, - "\tFork failed (may be OK if under stress)"); - } - /* Child does this */ - if (pid == 0) { - procstat = 1; - exit(dotest(keyarray[i], i)); - } - pidarray[i] = pid; - } - - count = 0; - while (1) { - if ((wait(&status)) > 0) { - if (status >> 8 != 0) { - tst_brkm(TFAIL, NULL, - "Child exit status = %d", - status >> 8); - } - count++; - } else { - if (errno != EINTR) { - break; - } -#ifdef DEBUG - tst_resm(TINFO, "Signal detected during wait"); -#endif - } - } - /* Make sure proper number of children exited */ - if (count != nprocs) { - tst_brkm(TFAIL, - NULL, - "Wrong number of children exited, Saw %d, Expected %d", - count, nprocs); - } - - tst_resm(TPASS, "Test ran successfully!"); - - cleanup(); - tst_exit(); -} - -static int dotest(key_t key, int child_process) -{ - int id, pid; - int ret, status; - - sighold(SIGTERM); - TEST(msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR)); - if (TEST_RETURN < 0) { - printf("msgget() error in child %d: %s\n", - child_process, strerror(TEST_ERRNO)); - return FAIL; - } - tid = id = TEST_RETURN; - sigrelse(SIGTERM); - - fflush(stdout); - if ((pid = FORK_OR_VFORK()) < 0) { - printf("Fork failed (may be OK if under stress)\n"); - TEST(msgctl(tid, IPC_RMID, 0)); - if (TEST_RETURN < 0) { - printf("msgctl() error in cleanup: %s\n", - strerror(TEST_ERRNO)); - } - return FAIL; - } - if (pid == 0) - exit(doreader(key, id, 1, child_process, nreps)); - - mykid = pid; - procstat = 2; - ret = dowriter(key, id, 1, child_process, nreps); - wait(&status); - - if (ret != PASS) - exit(FAIL); - - if ((!WIFEXITED(status) || (WEXITSTATUS(status) != PASS))) - exit(FAIL); - - TEST(msgctl(id, IPC_RMID, 0)); - if (TEST_RETURN < 0) { - printf("msgctl() failed: %s\n", - strerror(TEST_ERRNO)); - return FAIL; - } - return PASS; -} - -static void sig_handler(int signo LTP_ATTRIBUTE_UNUSED) -{ -} - -void setup(void) -{ - int nr_msgqs; - - tst_tmpdir(); - - tst_sig(FORK, DEF_HANDLER, cleanup); - - TEST_PAUSE; - - nr_msgqs = get_max_msgqueues(); - if (nr_msgqs < 0) - cleanup(); - - MSGMNI = nr_msgqs - get_used_msgqueues(); - if (MSGMNI > MAXNPROCS) - MSGMNI = MAXNPROCS; - if (MSGMNI <= 0) { - tst_resm(TBROK, - "Max number of message queues already used, cannot create more."); - cleanup(); - } -} - -void cleanup(void) -{ - int status; - -#ifdef DEBUG - tst_resm(TINFO, "Removing the message queue"); -#endif - (void)msgctl(tid, IPC_RMID, NULL); - if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) { - (void)msgctl(tid, IPC_RMID, NULL); - tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed"); - - } - - tst_rmdir(); -} diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c index f1c124990..6d175cd58 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c +++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c @@ -1,32 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) International Business Machines Corp., 2002 * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * * 06/30/2001 Port to Linux nsharoff@us.ibm.com * 11/11/2002 Port to LTP dbarrera@us.ibm.com - */ - -/* + * 06/15/2020 Convert to new api xuyang2018.jy@cn.fujitsu.com + * * Get and manipulate a message queue. * Same as msgstress02 but gets the actual msgmni value under procfs. */ - #define _XOPEN_SOURCE 500 #include -#include #include #include #include @@ -36,23 +20,15 @@ #include #include #include -#include "test.h" -#include "ipcmsg.h" +#include "tst_test.h" +#include "libnewipc.h" +#include "tst_safe_sysv_ipc.h" #include "libmsgctl.h" -char *TCID = "msgstress04"; -int TST_TOTAL = 1; - #define MAXNREPS 1000 -#ifndef CONFIG_COLDFIRE -#define MAXNPROCS 1000000 /* This value is set to an arbitrary high limit. */ -#else -#define MAXNPROCS 100000 /* Coldfire can't deal with 1000000 */ -#endif +#define MAXNPROCS 1000000 #define MAXNKIDS 10 -#define DEFNKIDS 2 -static int maxnkids = MAXNKIDS; /* Used if pid_max is exceeded */ static key_t keyarray[MAXNPROCS]; static int pidarray[MAXNPROCS]; static int rkidarray[MAXNKIDS]; @@ -62,83 +38,71 @@ static int nprocs, nreps, nkids, MSGMNI; static int maxnprocs; static int procstat; -void setup(void); -void cleanup(void); +static void cleanup(void); static void term(int); static int dotest(key_t, int); -static void dotest_iteration(int off); -static void cleanup_msgqueue(int i, int tid); static char *opt_maxnprocs; static char *opt_nkids; static char *opt_nreps; -static option_t options[] = { - {"n:", NULL, &opt_maxnprocs}, - {"c:", NULL, &opt_nkids}, - {"l:", NULL, &opt_nreps}, - {NULL, NULL, NULL}, +static struct tst_option options[] = { + {"n:", &opt_maxnprocs, "-n N Number of processes"}, + {"c:", &opt_nkids, "-c N Number of read/write child pairs"}, + {"l:", &opt_nreps, "-l N Number of iterations"}, + {NULL, NULL, NULL} }; -static void usage(void) -{ - printf(" -n Number of processes\n"); - printf(" -c Number of read/write child pairs\n"); - printf(" -l Number of iterations\n"); -} - - -int main(int argc, char **argv) +static void dotest_iteration(int off) { - int i, j, ok; - - tst_parse_opts(argc, argv, options, usage); - - setup(); - - nreps = MAXNREPS; - nkids = MAXNKIDS; - - if (opt_nreps) { - nreps = atoi(opt_nreps); - if (nreps > MAXNREPS) { - tst_resm(TINFO, - "Requested number of iterations too large, " - "setting to Max. of %d", MAXNREPS); - nreps = MAXNREPS; - } - } + key_t key; + int i, count, status; + pid_t pid; + memset(pidarray, 0, sizeof(pidarray)); - if (opt_nkids) { - nkids = atoi(opt_nkids); - if (nkids > MAXNKIDS) { - tst_resm(TINFO, - "Requested number of read/write pairs too " - "large, setting to Max. of %d", MAXNKIDS); - nkids = MAXNKIDS; + for (i = 0; i < nprocs; i++) { + key = keyarray[off + i]; + pid = SAFE_FORK(); + if (pid == 0) { + procstat = 1; + exit(dotest(key, i)); } + pidarray[i] = pid; } - - if (opt_maxnprocs) { - if (atoi(opt_maxnprocs) > maxnprocs) { - tst_resm(TINFO, - "Requested number of processes too large, " - "setting to Max. of %d", MSGMNI); + count = 0; + while (1) { + if ((wait(&status)) > 0) { + if (status >> 8 != PASS) + tst_brk(TFAIL, + "Child exit status = %d", status >> 8); + count++; } else { - maxnprocs = atoi(opt_maxnprocs); + if (errno != EINTR) + break; + tst_res(TINFO, "Signal detected during wait"); } } + /* Make sure proper number of children exited */ + if (count != nprocs) + tst_brk(TFAIL, + "Wrong number of children exited, Saw %d, Expected %d", + count, nprocs); +} + +static void verify_msgstress(void) +{ + int i, j, ok; procstat = 0; srand48((unsigned)getpid() + (unsigned)(getppid() << 16)); tid = -1; - /* Setup signal handling routine */ - if (sigset(SIGTERM, term) == SIG_ERR) - tst_brkm(TFAIL, cleanup, "Sigset SIGTERM failed"); - + /* Setup signal handleing routine */ + if (sigset(SIGTERM, term) == SIG_ERR) { + tst_brk(TFAIL, "Sigset SIGTERM failed"); + } /* Set up array of unique keys for use in allocating message * queues */ @@ -161,15 +125,11 @@ int main(int argc, char **argv) } } while (ok == 0); } - /* Fork a number of processes, each of which will + /* Fork a number of processes (nprocs), each of which will * create a message queue with several (nkids) reader/writer * pairs which will read and write a number (iterations) * of random length messages with specific values (keys). - * - * We do not fork more than maxnprocs at a time and - * we fork until all the message queues get used. */ - if (MSGMNI <= maxnprocs) { nprocs = MSGMNI; dotest_iteration(0); @@ -178,84 +138,12 @@ int main(int argc, char **argv) nprocs = maxnprocs; dotest_iteration(i * maxnprocs); } - nprocs = MSGMNI % maxnprocs; dotest_iteration(i * maxnprocs); } - tst_resm(TPASS, "Test ran successfully!"); - + tst_res(TPASS, "Test ran successfully!"); cleanup(); - tst_exit(); -} - -static void dotest_iteration(int off) -{ - key_t key; - int i, count, status; - pid_t pid; - - memset(pidarray, 0, sizeof(pidarray)); - - for (i = 0; i < nprocs; i++) { - key = keyarray[off + i]; - - if ((pid = FORK_OR_VFORK()) < 0) - tst_brkm(TFAIL, cleanup, - "Fork failed (may be OK if under stress)"); - - /* Child does this */ - if (pid == 0) { - procstat = 1; - exit(dotest(key, i)); - } - pidarray[i] = pid; - } - - count = 0; - while (1) { - if ((wait(&status)) > 0) { - if (status >> 8 != PASS) - tst_brkm(TFAIL, cleanup, - "Child exit status = %d", status >> 8); - count++; - } else { - if (errno != EINTR) { - break; - } -#ifdef DEBUG - tst_resm(TINFO, "Signal detected during wait"); -#endif - } - } - /* Make sure proper number of children exited */ - if (count != nprocs) - tst_brkm(TFAIL, cleanup, - "Wrong number of children exited, Saw %d, Expected %d", - count, nprocs); -} - -static void cleanup_msgqueue(int i, int tid) -{ - /* - * Decrease the value of i by 1 because it - * is getting incremented even if the fork - * is failing. - */ - - i--; - /* - * Kill all children & free message queue. - */ - for (; i >= 0; i--) { - (void)kill(rkidarray[i], SIGKILL); - (void)kill(wkidarray[i], SIGKILL); - } - - if (msgctl(tid, IPC_RMID, 0) < 0) { - printf("Msgctl error in cleanup_msgqueue %d\n", errno); - exit(FAIL); - } } static int dotest(key_t key, int child_process) @@ -264,42 +152,21 @@ static int dotest(key_t key, int child_process) int i, count, status, exit_status; sighold(SIGTERM); - if ((id = msgget(key, IPC_CREAT | S_IRUSR | S_IWUSR)) < 0) { - printf("msgget() error in child %d: %s\n", - child_process, strerror(errno)); - return FAIL; - } + id = SAFE_MSGGET(key, IPC_CREAT | S_IRUSR | S_IWUSR); tid = id; sigrelse(SIGTERM); exit_status = PASS; for (i = 0; i < nkids; i++) { - if ((pid = FORK_OR_VFORK()) < 0) { - printf("Fork failure in the first child of child group %d\n", - child_process); - cleanup_msgqueue(i, tid); - return FAIL; - } - /* First child does this */ + pid = SAFE_FORK(); if (pid == 0) { procstat = 2; exit(doreader(key, tid, getpid(), child_process, nreps)); } rkidarray[i] = pid; - if ((pid = FORK_OR_VFORK()) < 0) { - printf("Fork failure in the second child of child group %d\n", - child_process); - /* - * Kill the reader child process - */ - (void)kill(rkidarray[i], SIGKILL); - - cleanup_msgqueue(i, tid); - return FAIL; - } - /* Second child does this */ + pid = SAFE_FORK(); if (pid == 0) { procstat = 2; exit(dowriter(key, tid, rkidarray[i], @@ -318,10 +185,7 @@ static int dotest(key_t key, int child_process) kill(rkidarray[i], SIGTERM); kill(wkidarray[i], SIGTERM); } - if (msgctl(tid, IPC_RMID, 0) < 0) { - printf("msgctl() error: %s\n", - strerror(errno)); - } + SAFE_MSGCTL(tid, IPC_RMID, NULL); return FAIL; } count++; @@ -335,37 +199,21 @@ static int dotest(key_t key, int child_process) if (count != (nkids * 2)) { printf("Wrong number of children exited in child group %d, saw %d, expected %d\n", child_process, count, (nkids * 2)); - if (msgctl(tid, IPC_RMID, 0) < 0) { - printf("msgctl() error: %s\n", strerror(errno)); - } - return FAIL; - } - if (msgctl(id, IPC_RMID, 0) < 0) { - printf("msgctl() failure in child group %d: %s\n", - child_process, strerror(errno)); + SAFE_MSGCTL(tid, IPC_RMID, NULL); return FAIL; } + SAFE_MSGCTL(id, IPC_RMID, NULL); return exit_status; } -/* ARGSUSED */ static void term(int sig LTP_ATTRIBUTE_UNUSED) { int i; if (procstat == 0) { -#ifdef DEBUG - tst_resm(TINFO, "SIGTERM signal received, test killing kids"); -#endif for (i = 0; i < nprocs; i++) { - if (pidarray[i] > 0) { - if (kill(pidarray[i], SIGTERM) < 0) { - tst_resm(TBROK, - "Kill failed to kill child %d", - i); - exit(FAIL); - } - } + if (pidarray[i] > 0) + SAFE_KILL(pidarray[i], SIGTERM); } return; } @@ -379,72 +227,73 @@ static void term(int sig LTP_ATTRIBUTE_UNUSED) } for (i = 0; i < nkids; i++) { if (rkidarray[i] > 0) - kill(rkidarray[i], SIGTERM); + SAFE_KILL(rkidarray[i], SIGTERM); if (wkidarray[i] > 0) - kill(wkidarray[i], SIGTERM); + SAFE_KILL(wkidarray[i], SIGTERM); } } -void setup(void) +static void setup(void) { - int nr_msgqs, free_pids; + int nr_msgqs; + int threads_max; + int free_pids; - tst_tmpdir(); - /* You will want to enable some signal handling so you can capture - * unexpected signals like SIGSEGV. - */ - tst_sig(FORK, DEF_HANDLER, cleanup); + SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%d", &nr_msgqs); + SAFE_FILE_SCANF("/proc/sys/kernel/threads-max", "%d", &threads_max); - /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to - * fork the test with the -c option. You want to make sure you do this - * before you create your temporary directory. - */ - TEST_PAUSE; + MSGMNI = nr_msgqs - GET_USED_QUEUES(); + if (MSGMNI <= 0) + tst_brk(TCONF, "Max number of message queues already used, cannot create more."); - nr_msgqs = get_max_msgqueues(); - if (nr_msgqs < 0) - tst_brkm(TBROK, cleanup, "get_max_msgqueues() failed"); + tst_res(TINFO, "Found %d available message queues", MSGMNI); - MSGMNI = nr_msgqs - get_used_msgqueues(); - if (MSGMNI <= 0) - tst_brkm(TBROK, cleanup, - "Max number of message queues already used, cannot create more."); + nreps = 1000; + nkids = MAXNKIDS; - tst_resm(TINFO, "Found %d available message queues", MSGMNI); + if (opt_nreps) { + nreps = SAFE_STRTOL(opt_nreps, 1, INT_MAX); + nreps = min(nreps, MAXNREPS); + } - free_pids = tst_get_free_pids(cleanup); - if (free_pids < 0) { - tst_brkm(TBROK, cleanup, "Can't obtain free_pid count"); - } else if (!free_pids) { - tst_brkm(TBROK, cleanup, "No free pids"); + if (opt_nkids) { + nkids = SAFE_STRTOL(opt_nkids, 1, INT_MAX); + nkids = min(nkids, MAXNKIDS); } - /* We don't use more than a half of available pids. - * For each child we fork up to 2*maxnkids grandchildren. */ - maxnprocs = (free_pids / 2) / (1 + 2 * maxnkids); + free_pids = tst_get_free_pids(); + if (free_pids < 0) + tst_brk(TBROK, "Can't obtain free_pid count"); + if (!free_pids) + tst_brk(TBROK, "No free pids"); + /* + * We don't use more than a half of available pids. + * For each child we fork up to 2*maxnkids grandchildren. + */ + free_pids = min(free_pids, 0.3 * threads_max); + maxnprocs = (free_pids / 2) / (1 + 2 * nkids); if (!maxnprocs) - tst_brkm(TBROK, cleanup, "Not enough free pids"); + tst_brk(TBROK, "Not enough free pids"); + + if (opt_maxnprocs) { + maxnprocs = SAFE_STRTOL(opt_maxnprocs, 1, maxnprocs); + } - tst_resm(TINFO, "Using upto %d pids", free_pids / 2); + tst_res(TINFO, "Using upto %d pids, %d processes %d read/write pais, %d repeats", free_pids/2, maxnprocs, nkids, nreps); } void cleanup(void) { - int status; - - /* - * Remove the message queue from the system - */ -#ifdef DEBUG - tst_resm(TINFO, "Removing the message queue"); -#endif - (void)msgctl(tid, IPC_RMID, NULL); - if ((status = msgctl(tid, IPC_STAT, NULL)) != -1) { - (void)msgctl(tid, IPC_RMID, NULL); - tst_resm(TFAIL, "msgctl(tid, IPC_RMID) failed"); - - } - - tst_rmdir(); + if (tid >= 0) + SAFE_MSGCTL(tid, IPC_RMID, NULL); } + +static struct tst_test test = { + .needs_tmpdir = 1, + .options = options, + .setup = setup, + .cleanup = cleanup, + .forks_child = 1, + .test_all = verify_msgstress, +};