Skip to contentSkip to conversation

libs/libltpnewipc: Add missing libmsgctl.c

PATCH New Yang Xu 2 participants 3 messages Jun 16, 2020

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/2 libs/libltpnewipc: Add missing libmsgctl.c

Add libmsgctl.c into ltp new ipc libs, so the upcoming msgstress cleanup cases can use its functions.

---
 libs/libltpnewipc/libmsgctl.c | 134 ++++++++++++++++++++++++++++++++++
 1 file changed, 134 insertions(+)
 create mode 100644 libs/libltpnewipc/libmsgctl.c
libs/libltpnewipc/libmsgctl.c new file +134 -0
new file mode 100644
index 000000000..6de512d29
@@ -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 <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#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;
+}
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/2 syscalls/msgstress: scale number of processes accodingly to available RAM

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

---
 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
runtest/syscalls +2 -2
index bee03e93f..18e5fa5b5 100644
@@ -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
 
runtest/syscalls-ipc +2 -2
index 54d8622d4..b2688e25b 100644
@@ -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
 
testcases/kernel/syscalls/ipc/msgstress/.gitignore +0 -1
index a8f675399..e1df4cda8 100644
@@ -1,4 +1,3 @@
 /msgstress01
 /msgstress02
-/msgstress03
 /msgstress04
testcases/kernel/syscalls/ipc/msgstress/Makefile +2 -2
index f62cd1f48..a11cbcf2e 100644
@@ -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
testcases/kernel/syscalls/ipc/msgstress/msgstress01.c +110 -193

395 lines not on this page · raw message

testcases/kernel/syscalls/ipc/msgstress/msgstress02.c +87 -214

431 lines not on this page · raw message

testcases/kernel/syscalls/ipc/msgstress/msgstress03.c deleted +0 -290

290 lines not on this page · raw message

testcases/kernel/syscalls/ipc/msgstress/msgstress04.c +108 -259

506 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

PATCH 1/2 libs/libltpnewipc: Add missing libmsgctl.c

In reply to Yang Xu

Hi!

Quoted 2 lines

Add libmsgctl.c into ltp new ipc libs, so the upcoming msgstress cleanup cases can use its functions.

First of all the code should be ported into new library, which among other things, means that the printf() should be replaced with tst_res(), and there would no longer be a reason to return the PASS/FAIL since that would be handled by the test library from that point on.

Signature · 2 lines
Cyril Hrubis
[email protected]