summaryrefslogtreecommitdiffstats
path: root/cpukit/libtest/t-test-thread-switch.c
blob: 6457bbfd065008dac34019838cedc873e9fa2f45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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);
}