summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/app/test/test_timer_secondary.c
blob: 7a3bc873b35972981a023b0d35075a8ebb77b61a (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2019 Intel Corporation
 */

#include <stdio.h>
#include <string.h>

#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_debug.h>
#include <rte_memzone.h>
#include <rte_atomic.h>
#include <rte_timer.h>
#include <rte_cycles.h>
#include <rte_mempool.h>
#include <rte_random.h>

#include "test.h"
#include "process.h"

#define NUM_TIMERS		(1 << 20) /* ~1M timers */
#define NUM_LCORES_NEEDED	3
#define TEST_INFO_MZ_NAME	"test_timer_info_mz"
#define MSECPERSEC		1E3

#define launch_proc(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__)

struct test_info {
	unsigned int mstr_lcore;
	unsigned int mgr_lcore;
	unsigned int sec_lcore;
	uint32_t timer_data_id;
	volatile int expected_count;
	volatile int expired_count;
	struct rte_mempool *tim_mempool;
	struct rte_timer *expired_timers[NUM_TIMERS];
	int expired_timers_idx;
	volatile int exit_flag;
};

static int
timer_secondary_spawn_wait(unsigned int lcore)
{
	char coremask[10];
#ifdef RTE_EXEC_ENV_LINUXAPP
	char tmp[PATH_MAX] = {0};
	char prefix[PATH_MAX] = {0};

	get_current_prefix(tmp, sizeof(tmp));

	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
#else
	const char *prefix = "";
#endif
	char const *argv[] = {
		prgname,
		"-c", coremask,
		"--proc-type=secondary",
		prefix
	};

	snprintf(coremask, sizeof(coremask), "%x", (1 << lcore));

	return launch_proc(argv);
}

static void
handle_expired_timer(struct rte_timer *tim)
{
	struct test_info *test_info = tim->arg;

	test_info->expired_count++;
	test_info->expired_timers[test_info->expired_timers_idx++] = tim;
}

static int
timer_manage_loop(void *arg)
{
#define TICK_MSECS 1
	uint64_t tick_cycles = TICK_MSECS * rte_get_timer_hz() / MSECPERSEC;
	uint64_t prev_tsc = 0, cur_tsc, diff_tsc;
	struct test_info *test_info = arg;

	while (!test_info->exit_flag) {
		cur_tsc = rte_rdtsc();
		diff_tsc = cur_tsc - prev_tsc;

		if (diff_tsc > tick_cycles) {
			/* Scan timer list for expired timers */
			rte_timer_alt_manage(test_info->timer_data_id,
					     NULL,
					     0,
					     handle_expired_timer);

			/* Return expired timer objects back to mempool */
			rte_mempool_put_bulk(test_info->tim_mempool,
					     (void **)test_info->expired_timers,
					     test_info->expired_timers_idx);

			test_info->expired_timers_idx = 0;

			prev_tsc = cur_tsc;
		}

		rte_pause();
	}

	return 0;
}

int
test_timer_secondary(void)
{
	int proc_type = rte_eal_process_type();
	const struct rte_memzone *mz;
	struct test_info *test_info;
	int ret;

	if (proc_type == RTE_PROC_PRIMARY) {
		if (rte_lcore_count() < NUM_LCORES_NEEDED) {
			printf("Not enough cores for test_timer_secondary, expecting at least %u\n",
			       NUM_LCORES_NEEDED);
			return TEST_SKIPPED;
		}

		mz = rte_memzone_reserve(TEST_INFO_MZ_NAME, sizeof(*test_info),
					 SOCKET_ID_ANY, 0);
		test_info = mz->addr;
		TEST_ASSERT_NOT_NULL(test_info, "Couldn't allocate memory for "
				     "test data");

		test_info->tim_mempool = rte_mempool_create("test_timer_mp",
				NUM_TIMERS, sizeof(struct rte_timer), 0, 0,
				NULL, NULL, NULL, NULL, rte_socket_id(), 0);

		ret = rte_timer_data_alloc(&test_info->timer_data_id);
		TEST_ASSERT_SUCCESS(ret, "Failed to allocate timer data "
				    "instance");

		unsigned int *mstr_lcorep = &test_info->mstr_lcore;
		unsigned int *mgr_lcorep = &test_info->mgr_lcore;
		unsigned int *sec_lcorep = &test_info->sec_lcore;

		*mstr_lcorep = rte_get_master_lcore();
		*mgr_lcorep = rte_get_next_lcore(*mstr_lcorep, 1, 1);
		*sec_lcorep = rte_get_next_lcore(*mgr_lcorep, 1, 1);

		ret = rte_eal_remote_launch(timer_manage_loop,
					    (void *)test_info,
					    *mgr_lcorep);
		TEST_ASSERT_SUCCESS(ret, "Failed to launch timer manage loop");

		ret = timer_secondary_spawn_wait(*sec_lcorep);
		TEST_ASSERT_SUCCESS(ret, "Secondary process execution failed");

		rte_delay_ms(2000);

		test_info->exit_flag = 1;
		rte_eal_wait_lcore(*mgr_lcorep);

#ifdef RTE_LIBRTE_TIMER_DEBUG
		rte_timer_alt_dump_stats(test_info->timer_data_id, stdout);
#endif

		return test_info->expected_count == test_info->expired_count ?
			TEST_SUCCESS : TEST_FAILED;

	} else if (proc_type == RTE_PROC_SECONDARY) {
		uint64_t ticks, timeout_ms;
		struct rte_timer *tim;
		int i;

		mz = rte_memzone_lookup(TEST_INFO_MZ_NAME);
		test_info = mz->addr;
		TEST_ASSERT_NOT_NULL(test_info, "Couldn't lookup memzone for "
				     "test info");

		for (i = 0; i < NUM_TIMERS; i++) {
			rte_mempool_get(test_info->tim_mempool, (void **)&tim);

			rte_timer_init(tim);

			/* generate timeouts between 10 and 160 ms */
			timeout_ms = ((rte_rand() & 0xF) + 1) * 10;
			ticks = timeout_ms * rte_get_timer_hz() / MSECPERSEC;

			ret = rte_timer_alt_reset(test_info->timer_data_id,
						  tim, ticks, SINGLE,
						  test_info->mgr_lcore, NULL,
						  test_info);
			if (ret < 0)
				return TEST_FAILED;

			test_info->expected_count++;

			/* randomly leave timer running or stop it */
			if (rte_rand() & 1)
				continue;

			ret = rte_timer_alt_stop(test_info->timer_data_id,
						 tim);
			if (ret == 0) {
				test_info->expected_count--;
				rte_mempool_put(test_info->tim_mempool,
						(void *)tim);
			}

		}

		return TEST_SUCCESS;
	}

	return TEST_FAILED;
}

REGISTER_TEST_COMMAND(timer_secondary_autotest, test_timer_secondary);