aboutsummaryrefslogtreecommitdiff
path: root/example/timer/odp_timer_test.c
blob: c75469585b8d79816624c26778602848bf909ba0 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * @example  odp_timer_test.c ODP timer example application
 */

#include <string.h>
#include <stdlib.h>

#include <example_debug.h>

/* ODP main header */
#include <odp.h>

/* ODP helper for Linux apps */
#include <odph_linux.h>

/* GNU lib C */
#include <getopt.h>


#define MAX_WORKERS           32            /**< Max worker threads */
#define MSG_POOL_SIZE         (4*1024*1024) /**< Message pool size */
#define MSG_NUM_BUFS          10000         /**< Number of timers */


/** Test arguments */
typedef struct {
	int core_count;    /**< Core count*/
	int resolution_us; /**< Timeout resolution in usec*/
	int min_us;        /**< Minimum timeout in usec*/
	int max_us;        /**< Maximum timeout in usec*/
	int period_us;     /**< Timeout period in usec*/
	int tmo_count;     /**< Timeout count*/
} test_args_t;


/** @private Barrier for test synchronisation */
static odp_barrier_t test_barrier;

/** @private Timer handle*/
static odp_timer_t test_timer;


/** @private test timeout */
static void test_abs_timeouts(int thr, test_args_t *args)
{
	uint64_t tick;
	uint64_t period;
	uint64_t period_ns;
	odp_queue_t queue;
	odp_buffer_t buf;
	int num;

	EXAMPLE_DBG("  [%i] test_timeouts\n", thr);

	queue = odp_queue_lookup("timer_queue");

	period_ns = args->period_us*ODP_TIME_USEC;
	period    = odp_timer_ns_to_tick(test_timer, period_ns);

	EXAMPLE_DBG("  [%i] period %"PRIu64" ticks,  %"PRIu64" ns\n", thr,
		    period, period_ns);

	tick = odp_timer_current_tick(test_timer);

	EXAMPLE_DBG("  [%i] current tick %"PRIu64"\n", thr, tick);

	tick += period;

	if (odp_timer_absolute_tmo(test_timer, tick, queue, ODP_BUFFER_INVALID)
	    == ODP_TIMER_TMO_INVALID){
		EXAMPLE_DBG("Timeout request failed\n");
		return;
	}

	num = args->tmo_count;

	while (1) {
		odp_timeout_t tmo;

		buf = odp_schedule_one(&queue, ODP_SCHED_WAIT);

		tmo  = odp_timeout_from_buffer(buf);
		tick = odp_timeout_tick(tmo);

		EXAMPLE_DBG("  [%i] timeout, tick %"PRIu64"\n", thr, tick);

		odp_buffer_free(buf);

		num--;

		if (num == 0)
			break;

		tick += period;

		odp_timer_absolute_tmo(test_timer, tick,
				       queue, ODP_BUFFER_INVALID);
	}

	if (odp_queue_sched_type(queue) == ODP_SCHED_SYNC_ATOMIC)
		odp_schedule_release_atomic();
}


/**
 * @internal Worker thread
 *
 * @param ptr  Pointer to test arguments
 *
 * @return Pointer to exit status
 */
static void *run_thread(void *ptr)
{
	int thr;
	odp_buffer_pool_t msg_pool;
	test_args_t *args;

	args = ptr;
	thr  = odp_thread_id();

	printf("Thread %i starts on core %i\n", thr, odp_thread_core());

	/*
	 * Find the buffer pool
	 */
	msg_pool = odp_buffer_pool_lookup("msg_pool");

	if (msg_pool == ODP_BUFFER_POOL_INVALID) {
		EXAMPLE_ERR("  [%i] msg_pool not found\n", thr);
		return NULL;
	}

	odp_barrier_wait(&test_barrier);

	test_abs_timeouts(thr, args);


	printf("Thread %i exits\n", thr);
	fflush(NULL);
	return NULL;
}


/**
 * @internal Print help
 */
static void print_usage(void)
{
	printf("\n\nUsage: ./odp_example [options]\n");
	printf("Options:\n");
	printf("  -c, --count <number>    core count, core IDs start from 1\n");
	printf("  -r, --resolution <us>   timeout resolution in usec\n");
	printf("  -m, --min <us>          minimum timeout in usec\n");
	printf("  -x, --max <us>          maximum timeout in usec\n");
	printf("  -p, --period <us>       timeout period in usec\n");
	printf("  -t, --timeouts <count>  timeout repeat count\n");
	printf("  -h, --help              this help\n");
	printf("\n\n");
}


/**
 * @internal Parse arguments
 *
 * @param argc  Argument count
 * @param argv  Argument vector
 * @param args  Test arguments
 */
static void parse_args(int argc, char *argv[], test_args_t *args)
{
	int opt;
	int long_index;

	static struct option longopts[] = {
		{"count",      required_argument, NULL, 'c'},
		{"resolution", required_argument, NULL, 'r'},
		{"min",        required_argument, NULL, 'm'},
		{"max",        required_argument, NULL, 'x'},
		{"period",     required_argument, NULL, 'p'},
		{"timeouts",   required_argument, NULL, 't'},
		{"help",       no_argument,       NULL, 'h'},
		{NULL, 0, NULL, 0}
	};

	/* defaults */
	args->core_count    = 0; /* all cores */
	args->resolution_us = 10000;
	args->min_us        = args->resolution_us;
	args->max_us        = 10000000;
	args->period_us     = 1000000;
	args->tmo_count     = 30;

	while (1) {
		opt = getopt_long(argc, argv, "+c:r:m:x:p:t:h",
				 longopts, &long_index);

		if (opt == -1)
			break;	/* No more options */

		switch (opt) {
		case 'c':
			args->core_count = atoi(optarg);
			break;
		case 'r':
			args->resolution_us = atoi(optarg);
			break;
		case 'm':
			args->min_us = atoi(optarg);
			break;
		case 'x':
			args->max_us = atoi(optarg);
			break;
		case 'p':
			args->period_us = atoi(optarg);
			break;
		case 't':
			args->tmo_count = atoi(optarg);
			break;
		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);
			break;

		default:
			break;
		}
	}
}


/**
 * Test main function
 */
int main(int argc, char *argv[])
{
	odph_linux_pthread_t thread_tbl[MAX_WORKERS];
	test_args_t args;
	int num_workers;
	odp_buffer_pool_t pool;
	odp_queue_t queue;
	int first_core;
	uint64_t cycles, ns;
	odp_queue_param_t param;
	odp_shm_t shm;
	odp_buffer_pool_param_t params;

	printf("\nODP timer example starts\n");

	memset(&args, 0, sizeof(args));
	parse_args(argc, argv, &args);

	memset(thread_tbl, 0, sizeof(thread_tbl));

	if (odp_init_global(NULL, NULL)) {
		printf("ODP global init failed.\n");
		return -1;
	}

	/* Init this thread. */
	if (odp_init_local()) {
		printf("ODP local init failed.\n");
		return -1;
	}

	printf("\n");
	printf("ODP system info\n");
	printf("---------------\n");
	printf("ODP API version: %s\n",        odp_version_api_str());
	printf("CPU model:       %s\n",        odp_sys_cpu_model_str());
	printf("CPU freq (hz):   %"PRIu64"\n", odp_sys_cpu_hz());
	printf("Cache line size: %i\n",        odp_sys_cache_line_size());
	printf("Max core count:  %i\n",        odp_sys_core_count());

	printf("\n");

	/* A worker thread per core */
	num_workers = odp_sys_core_count();

	if (args.core_count)
		num_workers = args.core_count;

	/* force to max core count */
	if (num_workers > MAX_WORKERS)
		num_workers = MAX_WORKERS;

	printf("num worker threads: %i\n", num_workers);

	/*
	 * By default core #0 runs Linux kernel background tasks.
	 * Start mapping thread from core #1
	 */
	first_core = 1;

	if (odp_sys_core_count() == 1)
		first_core = 0;

	printf("first core:         %i\n", first_core);
	printf("resolution:         %i usec\n", args.resolution_us);
	printf("min timeout:        %i usec\n", args.min_us);
	printf("max timeout:        %i usec\n", args.max_us);
	printf("period:             %i usec\n", args.period_us);
	printf("timeouts:           %i\n", args.tmo_count);

	/*
	 * Create message pool
	 */
	shm = odp_shm_reserve("msg_pool",
			      MSG_POOL_SIZE, ODP_CACHE_LINE_SIZE, 0);

	params.buf_size  = 0;
	params.buf_align = 0;
	params.num_bufs  = MSG_NUM_BUFS;
	params.buf_type  = ODP_BUFFER_TYPE_TIMEOUT;

	pool = odp_buffer_pool_create("msg_pool", shm, &params);

	if (pool == ODP_BUFFER_POOL_INVALID) {
		EXAMPLE_ERR("Pool create failed.\n");
		return -1;
	}

	/*
	 * Create a queue for timer test
	 */
	memset(&param, 0, sizeof(param));
	param.sched.prio  = ODP_SCHED_PRIO_DEFAULT;
	param.sched.sync  = ODP_SCHED_SYNC_NONE;
	param.sched.group = ODP_SCHED_GROUP_DEFAULT;

	queue = odp_queue_create("timer_queue", ODP_QUEUE_TYPE_SCHED, &param);

	if (queue == ODP_QUEUE_INVALID) {
		EXAMPLE_ERR("Timer queue create failed.\n");
		return -1;
	}

	test_timer = odp_timer_create("test_timer", pool,
				      args.resolution_us*ODP_TIME_USEC,
				      args.min_us*ODP_TIME_USEC,
				      args.max_us*ODP_TIME_USEC);

	if (test_timer == ODP_TIMER_INVALID) {
		EXAMPLE_ERR("Timer create failed.\n");
		return -1;
	}


	odp_shm_print_all();

	printf("CPU freq %"PRIu64" hz\n", odp_sys_cpu_hz());
	printf("Cycles vs nanoseconds:\n");
	ns = 0;
	cycles = odp_time_ns_to_cycles(ns);

	printf("  %12"PRIu64" ns      ->  %12"PRIu64" cycles\n", ns, cycles);
	printf("  %12"PRIu64" cycles  ->  %12"PRIu64" ns\n", cycles,
	       odp_time_cycles_to_ns(cycles));

	for (ns = 1; ns <= 100*ODP_TIME_SEC; ns *= 10) {
		cycles = odp_time_ns_to_cycles(ns);

		printf("  %12"PRIu64" ns      ->  %12"PRIu64" cycles\n", ns,
		       cycles);
		printf("  %12"PRIu64" cycles  ->  %12"PRIu64" ns\n", cycles,
		       odp_time_cycles_to_ns(cycles));
	}

	printf("\n");

	/* Barrier to sync test case execution */
	odp_barrier_init(&test_barrier, num_workers);

	/* Create and launch worker threads */
	odph_linux_pthread_create(thread_tbl, num_workers, first_core,
				  run_thread, &args);

	/* Wait for worker threads to exit */
	odph_linux_pthread_join(thread_tbl, num_workers);

	printf("ODP timer test complete\n\n");

	return 0;
}