mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Peter Mamonov <pmamonov@gmail.com>,
	Peter Korsgaard <peter@korsgaard.com>,
	Sascha Hauer <sha@pengutronix.de>
Subject: [PATCH v3 15/16] commands: add new bthread test command
Date: Wed, 10 Mar 2021 09:47:59 +0100	[thread overview]
Message-ID: <20210310084800.3584-16-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210310084800.3584-1-a.fatoum@pengutronix.de>

From: Ahmad Fatoum <ahmad@a3f.at>

This command is meant to show how to use the bthread API as well as
help with debugging. It's a bit overcomplicated to exercise aspects
of the API, like scheduling from both secondary and primary thread.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 commands/Kconfig   |   9 +++
 commands/Makefile  |   1 +
 commands/bthread.c | 195 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 205 insertions(+)
 create mode 100644 commands/bthread.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 520ad4b1dea3..6d84c956e576 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -253,6 +253,15 @@ config CMD_POLLER
 	  is_timeout() or one of the various delay functions. The poller command prints
 	  informations about registered pollers.
 
+config CMD_BTHREAD
+	tristate
+	prompt "bthread"
+	depends on BTHREAD
+	help
+	  barebox threads are cooperatively-scheduled (green) threads that are running in
+	  the background whenever code executes is_timeout() or one of the various delay
+	  functions. The bthread command prints informations about registered bthreads.
+
 config CMD_SLICE
 	tristate
 	prompt "slice"
diff --git a/commands/Makefile b/commands/Makefile
index 034c0e6383d3..cdf14a5e1d8d 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -129,6 +129,7 @@ obj-$(CONFIG_CMD_MMC_EXTCSD)	+= mmc_extcsd.o
 obj-$(CONFIG_CMD_NAND_BITFLIP)	+= nand-bitflip.o
 obj-$(CONFIG_CMD_SEED)		+= seed.o
 obj-$(CONFIG_CMD_IP_ROUTE_GET)  += ip-route-get.o
+obj-$(CONFIG_CMD_BTHREAD)	+= bthread.o
 obj-$(CONFIG_CMD_UBSAN)		+= ubsan.o
 
 UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/bthread.c b/commands/bthread.c
new file mode 100644
index 000000000000..05dfa68cc597
--- /dev/null
+++ b/commands/bthread.c
@@ -0,0 +1,195 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ */
+
+#include <bthread.h>
+#include <errno.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <command.h>
+#include <getopt.h>
+#include <clock.h>
+
+static int bthread_time(void)
+{
+	uint64_t start = get_time_ns();
+	int i = 0;
+
+	/*
+	 * How many background tasks can we have in one second?
+	 *
+	 * A low number here may point to problems with bthreads taking too
+	 * much time.
+	 */
+	while (!is_timeout(start, SECOND))
+		i++;
+
+	return i;
+}
+
+static int bthread_infinite(void *data)
+{
+	while (!bthread_should_stop())
+		;
+
+	return 0;
+}
+
+static int bthread_isolated_time(void)
+{
+	uint64_t start = get_time_ns();
+	struct bthread *bthread;
+	int i = 0;
+
+	bthread = bthread_run(bthread_infinite, NULL, "infinite");
+	if (!bthread)
+		return -ENOMEM;
+
+	/* main thread is the first in the run queue. Newly created bthread
+	 * is the last. So if main_thread explicitly schedules new bthread,
+	 * it will schedule back to main_thread afterwards and we won't
+	 * execute any other threads in-between.
+	 */
+
+	while (!is_timeout_non_interruptible(start, SECOND)) {
+		bthread_schedule(bthread);
+		i += 2;
+	}
+
+	bthread_stop(bthread);
+	bthread_free(bthread);
+
+	return i;
+}
+
+static int bthread_printer(void *arg)
+{
+	volatile u64 start;
+	volatile int i = 0;
+	start = get_time_ns();
+
+	while (!bthread_should_stop()) {
+		if (!is_timeout_non_interruptible(start, 225 * MSECOND))
+			continue;
+
+		if ((long)arg == i++)
+			printf("%s yield #%d\n", bthread_name(current), i);
+		start = get_time_ns();
+	}
+
+	return i;
+}
+
+static int yields;
+
+static int bthread_spawner(void *arg)
+{
+	struct bthread *bthread[4];
+	volatile u64 start;
+	volatile int i = 0;
+	int ret = 0;
+	int ecode;
+
+	start = get_time_ns();
+
+	for (i = 0; i < ARRAY_SIZE(bthread); i++) {
+		bthread[i] = bthread_run(bthread_printer, (void *)i,
+					 "%s-bthread%u", bthread_name(current), i+1);
+		if (!bthread[i]) {
+			ret = -ENOMEM;
+			goto cleanup;
+		}
+	}
+
+	while (!bthread_should_stop())
+		;
+
+cleanup:
+	while (i--) {
+		ecode = bthread_stop(bthread[i]);
+		bthread_free(bthread[i]);
+
+		if (!ret && (ecode != 4 || yields < ecode))
+			ret = -EIO;
+	}
+
+	return ret;
+}
+
+struct spawn {
+	struct bthread *bthread;
+	struct list_head list;
+};
+
+static int do_bthread(int argc, char *argv[])
+{
+	LIST_HEAD(spawners);
+	struct spawn *spawner, *tmp;
+	int ret = 0;
+	int ecode, opt, i = 0;
+	bool time = false;
+
+	while ((opt = getopt(argc, argv, "itcv")) > 0) {
+		switch (opt) {
+		case 'i':
+			bthread_info();
+			break;
+		case 'c':
+			yields = bthread_isolated_time();
+			printf("%d bthread context switches possible in 1s\n", yields);
+			break;
+		case 'v':
+			spawner = xzalloc(sizeof(*spawner));
+			spawner->bthread = bthread_run(bthread_spawner, NULL,
+						       "spawner%u", ++i);
+			if (!spawner->bthread) {
+				free(spawner);
+				ret = -ENOMEM;
+				goto cleanup;
+			}
+
+			/* We create intermediate spawning threads to test thread
+			 * creation and scheduling from non-main thread.
+			 */
+			list_add(&spawner->list, &spawners);
+
+			/* fallthrough */
+		case 't':
+			time = true;
+		}
+	}
+
+	if (time) {
+		yields = bthread_time();
+		printf("%d bthread yield calls in 1s\n", yields);
+	}
+
+cleanup:
+	list_for_each_entry_safe(spawner, tmp, &spawners, list) {
+		ecode = bthread_stop(spawner->bthread);
+		bthread_free(spawner->bthread);
+		if (!ret && ecode)
+			ret = ecode;
+		free(spawner);
+	}
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(bthread)
+	BAREBOX_CMD_HELP_TEXT("print info about registered barebox threads")
+	BAREBOX_CMD_HELP_TEXT("")
+	BAREBOX_CMD_HELP_TEXT("Options:")
+	BAREBOX_CMD_HELP_OPT ("-i", "Print information about registered bthreads")
+	BAREBOX_CMD_HELP_OPT ("-t", "measure how many bthreads we currently run in 1s")
+	BAREBOX_CMD_HELP_OPT ("-c", "count maximum context switches in 1s")
+	BAREBOX_CMD_HELP_OPT ("-v", "verify correct bthread operation")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(bthread)
+	.cmd = do_bthread,
+	BAREBOX_CMD_DESC("print info about registered bthreads")
+	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_HELP(cmd_bthread_help)
+BAREBOX_CMD_END
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-03-10  8:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-10  8:47 [PATCH v3 00/16] common: introduce bthreads, co-operative Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 01/16] common: introduce HAS_ARCH_SJLJ Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 02/16] ARM: asm: setjmp: annotate setjmp/longjmp for GCC Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 03/16] ARM: asm: setjmp: implement coroutine dependency initjmp() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 04/16] sandbox: asm: implement setjmp/longjmp/initjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 05/16] riscv: Add asm/asm.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 06/16] riscv: Add asm/linkage.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 07/16] riscv: Implement setjmp/longjmp/initjmp for RV32I Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 08/16] mips: Add linkage.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 09/16] mips: Implement setjmp/longjmp/initjmp for 32BIT Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 10/16] powerpc: Implement initjmp/setjmp/longjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 11/16] x86: implement setjmp/longjmp/initjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 12/16] common: poller: replace explicit calls to poller_call() with resched() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 13/16] console: unconditionally run resched() in ctrlc() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 14/16] common: introduce bthreads, co-operative barebox threads Ahmad Fatoum
2021-03-10  8:47 ` Ahmad Fatoum [this message]
2021-03-10  8:48 ` [PATCH v3 16/16] RFC: led: migrate from poller to bthread Ahmad Fatoum
2021-03-15  8:34 ` [PATCH v3 00/16] common: introduce bthreads, co-operative Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210310084800.3584-16-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=peter@korsgaard.com \
    --cc=pmamonov@gmail.com \
    --cc=sha@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox