[PATCH] libtest: Add T_thread_switch_record()

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Aug 10 10:01:30 UTC 2020


Add support to record thread switch events.  This can be use to check
that a blocking operation results in the expected sequence of thread
switches.

Update #3199.
---
 cpukit/include/rtems/test.h           |  45 +++++++
 cpukit/libtest/t-test-thread-switch.c | 165 ++++++++++++++++++++++++++
 testsuites/libtests/ttest02/init.c    |  66 +++++++++++
 3 files changed, 276 insertions(+)
 create mode 100644 cpukit/libtest/t-test-thread-switch.c

diff --git a/cpukit/include/rtems/test.h b/cpukit/include/rtems/test.h
index a7955c735a..b850481cf3 100644
--- a/cpukit/include/rtems/test.h
+++ b/cpukit/include/rtems/test.h
@@ -83,6 +83,14 @@ typedef struct T_fixture_node {
 #define T_FILE_NAME __FILE__
 #endif
 
+#if defined(__GNUC__) || __STDC_VERSION__ >= 199409L
+#define T_ZERO_LENGH_ARRAY
+#define T_ZERO_LENGH_ARRAY_EXTENSION(n) (n)
+#else
+#define T_ZERO_LENGH_ARRAY 0
+#define T_ZERO_LENGH_ARRAY_EXTENSION(n) ((n) - 1)
+#endif
+
 /** @} */
 
 /**
@@ -2390,6 +2398,43 @@ void T_interrupt_test_busy_wait_for_interrupt(void);
 T_interrupt_test_state T_interrupt_test(const T_interrupt_test_config *config,
     void *arg);
 
+typedef struct {
+	uint32_t executing;
+	uint32_t heir;
+	uint32_t cpu;
+	T_time instant;
+} T_thread_switch_event;
+
+typedef struct {
+	size_t recorded;
+	size_t capacity;
+	uint64_t switches;
+	T_thread_switch_event events[T_ZERO_LENGH_ARRAY];
+} T_thread_switch_log;
+
+typedef struct {
+	T_thread_switch_log log;
+	T_thread_switch_event events[T_ZERO_LENGH_ARRAY_EXTENSION(2)];
+} T_thread_switch_log_2;
+
+typedef struct {
+	T_thread_switch_log log;
+	T_thread_switch_event events[T_ZERO_LENGH_ARRAY_EXTENSION(4)];
+} T_thread_switch_log_4;
+
+typedef struct {
+	T_thread_switch_log log;
+	T_thread_switch_event events[T_ZERO_LENGH_ARRAY_EXTENSION(10)];
+} T_thread_switch_log_10;
+
+T_thread_switch_log *T_thread_switch_record(T_thread_switch_log *);
+
+T_thread_switch_log *T_thread_switch_record_2(T_thread_switch_log_2 *);
+
+T_thread_switch_log *T_thread_switch_record_4(T_thread_switch_log_4 *);
+
+T_thread_switch_log *T_thread_switch_record_10(T_thread_switch_log_10 *);
+
 void T_report_hash_sha256(T_event, const char *);
 
 void T_check_heap(T_event, const char *);
diff --git a/cpukit/libtest/t-test-thread-switch.c b/cpukit/libtest/t-test-thread-switch.c
new file mode 100644
index 0000000000..6457bbfd06
--- /dev/null
+++ b/cpukit/libtest/t-test-thread-switch.c
@@ -0,0 +1,165 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup RTEMSTestFrameworkImpl
+ *
+ * @brief Implementation of T_thread_switch_record().
+ */
+
+/*
+ * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/test.h>
+
+#include <rtems.h>
+#include <rtems/score/percpu.h>
+#include <rtems/score/smp.h>
+#include <rtems/score/thread.h>
+#include <rtems/score/userextimpl.h>
+
+typedef struct {
+	RTEMS_INTERRUPT_LOCK_MEMBER(lock)
+	T_thread_switch_log *active;
+	User_extensions_Control ext;
+	T_fixture_node node;
+} T_thread_switch_context;
+
+static void T_thread_switch_recorder(Thread_Control *, Thread_Control *);
+
+static T_thread_switch_context T_thread_switch_instance = {
+#ifdef RTEMS_SMP
+	.lock = RTEMS_INTERRUPT_LOCK_INITIALIZER("Test Thread Switches"),
+#endif
+	.ext = {
+		.Callouts = {
+			.thread_switch = T_thread_switch_recorder
+		}
+	}
+};
+
+static void
+T_thread_switch_teardown(void *arg)
+{
+	T_thread_switch_context *ctx;
+
+	ctx = arg;
+	_User_extensions_Remove_set(&ctx->ext);
+}
+
+static const T_fixture T_thread_switch_fixture = {
+	.teardown = T_thread_switch_teardown,
+	.initial_context = &T_thread_switch_instance
+};
+
+static void
+T_thread_switch_recorder(Thread_Control *executing, Thread_Control *heir)
+{
+	rtems_interrupt_lock_context lock_context;
+	T_thread_switch_context *ctx;
+	T_thread_switch_log *log;
+
+	ctx = &T_thread_switch_instance;
+
+#ifdef RTEMS_SMP
+	if (ctx->active == NULL) {
+		return;
+	}
+#endif
+
+	rtems_interrupt_lock_acquire_isr(&ctx->lock, &lock_context);
+	log = ctx->active;
+
+	if (log != NULL) {
+		size_t recorded;
+
+		++log->switches;
+		recorded = log->recorded;
+
+		if (recorded < log->capacity) {
+			log->recorded = recorded + 1;
+			log->events[recorded].executing = executing->Object.id;
+			log->events[recorded].heir = heir->Object.id;
+			log->events[recorded].cpu =
+				_SMP_Get_current_processor();
+			log->events[recorded].instant = T_now();
+		}
+	}
+
+	rtems_interrupt_lock_release_isr(&ctx->lock, &lock_context);
+}
+
+T_thread_switch_log *
+T_thread_switch_record(T_thread_switch_log *log)
+{
+	rtems_interrupt_lock_context lock_context;
+	T_thread_switch_log *previous;
+	T_thread_switch_context *ctx;
+
+	ctx = &T_thread_switch_instance;
+
+	if (ctx->node.fixture == NULL) {
+		_User_extensions_Add_set(&ctx->ext);
+		T_push_fixture(&ctx->node, &T_thread_switch_fixture);
+	}
+
+	if (log != NULL) {
+		log->recorded = 0;
+		log->switches = 0;
+	}
+
+	rtems_interrupt_lock_acquire(&ctx->lock, &lock_context);
+	previous = ctx->active;
+	ctx->active = log;
+	rtems_interrupt_lock_release(&ctx->lock, &lock_context);
+
+	return previous;
+}
+
+T_thread_switch_log *
+T_thread_switch_record_2(T_thread_switch_log_2 *log)
+{
+	log->log.capacity = 2;
+	return T_thread_switch_record(&log->log);
+}
+
+T_thread_switch_log *
+T_thread_switch_record_4(T_thread_switch_log_4 *log)
+{
+	log->log.capacity = 4;
+	return T_thread_switch_record(&log->log);
+}
+
+T_thread_switch_log *
+T_thread_switch_record_10(T_thread_switch_log_10 *log)
+{
+	log->log.capacity = 10;
+	return T_thread_switch_record(&log->log);
+}
diff --git a/testsuites/libtests/ttest02/init.c b/testsuites/libtests/ttest02/init.c
index 78c874faf7..846a778536 100644
--- a/testsuites/libtests/ttest02/init.c
+++ b/testsuites/libtests/ttest02/init.c
@@ -32,6 +32,8 @@
 #include <rtems/test.h>
 #include <rtems/test-info.h>
 
+#include <string.h>
+
 #include <rtems.h>
 
 static void
@@ -206,6 +208,70 @@ T_TEST_CASE(TestInterruptBlocked)
 	T_step_eq_int(2, state, T_INTERRUPT_TEST_DONE);
 }
 
+T_TEST_CASE(TestThreadSwitch)
+{
+	T_thread_switch_log_2 log_2;
+	T_thread_switch_log_4 log_4;
+	T_thread_switch_log_10 log_10;
+	T_thread_switch_log *log;
+	uint32_t executing;
+	uint32_t heir;
+	size_t i;
+
+	memset(&log_2, 0xff, sizeof(log_2));
+	log = T_thread_switch_record_2(&log_2);
+	T_null(log);
+	T_eq_sz(log_2.log.recorded, 0);
+	T_eq_sz(log_2.log.capacity, 2);
+	T_eq_u64(log_2.log.switches, 0);
+
+	memset(&log_4, 0xff, sizeof(log_4));
+	log = T_thread_switch_record_4(&log_4);
+	T_eq_ptr(log, &log_2.log);
+	T_eq_sz(log_4.log.recorded, 0);
+	T_eq_sz(log_4.log.capacity, 4);
+	T_eq_u64(log_4.log.switches, 0);
+
+	memset(&log_10, 0xff, sizeof(log_10));
+	log = T_thread_switch_record_10(&log_10);
+	T_eq_ptr(log, &log_4.log);
+	T_eq_sz(log_10.log.recorded, 0);
+	T_eq_sz(log_10.log.capacity, 10);
+	T_eq_u64(log_10.log.switches, 0);
+
+	for (i = 0; i < 6; ++i) {
+		rtems_status_code sc;
+
+		sc = rtems_task_wake_after(2);
+		T_rsc_success(sc);
+	}
+
+	log = T_thread_switch_record(NULL);
+	T_eq_ptr(log, &log_10.log);
+	T_eq_sz(log->recorded, 10);
+	T_eq_sz(log->capacity, 10);
+	T_eq_u64(log->switches, 12);
+	executing = rtems_task_self();
+	T_eq_u32(log->events[0].executing, executing);
+	T_ne_u32(log->events[0].heir, 0);
+	heir = log->events[0].heir;
+	T_eq_u32(log->events[0].cpu, 0);
+	T_ne_u64(log->events[0].instant, 0);
+
+	for (i = 1; i < 10; ++i) {
+		uint32_t tmp;
+
+		tmp = executing;
+		executing = heir;
+		heir = tmp;
+
+		T_eq_u32(log->events[i].executing, executing);
+		T_eq_u32(log->events[i].heir, heir);
+		T_eq_u32(log->events[i].cpu, 0);
+		T_gt_u64(log->events[i].instant, log->events[i - 1].instant);
+	}
+}
+
 const char rtems_test_name[] = "TTEST 2";
 
 static void
-- 
2.26.2



More information about the devel mailing list