aboutsummaryrefslogtreecommitdiff
path: root/test/performance/odp_atomic.c
blob: 067329bdc8981d2a378b944465b10896040f2f3d (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <string.h>
#include <sys/time.h>
#include <test_debug.h>

#include <odp.h>
#include <odp/helper/linux.h>

static void test_atomic_inc_dec_u32(void);
static void test_atomic_add_sub_u32(void);
static void test_atomic_inc_dec_64(void);
static void test_atomic_add_sub_64(void);
static void test_atomic_inc_u32(void);
static void test_atomic_dec_u32(void);
static void test_atomic_add_u32(void);
static void test_atomic_sub_u32(void);
static void test_atomic_inc_64(void);
static void test_atomic_dec_64(void);
static void test_atomic_add_64(void);
static void test_atomic_sub_64(void);
static void test_atomic_init(void);
static void test_atomic_basic(void);
static void test_atomic_store(void);
static int test_atomic_validate(void);
static int odp_test_global_init(void);
static void odp_print_system_info(void);

/**
 * Thread argument
 */
typedef struct {
	int testcase; /**< specifies which set of API's to exercise */
	int numthrds; /**< no of pthreads to create */
} pthrd_arg;

static int odp_test_thread_create(void *(*start_routine) (void *), pthrd_arg *);
static int odp_test_thread_exit(pthrd_arg *);

#define MAX_WORKERS           32            /**< Max worker threads */
/**
 * add_sub_cnt could be any valid value
 * so to exercise explicit atomic_add/sub
 * ops. For now using 5..
 */
#define ADD_SUB_CNT	5
#define	CNT 500000
#define	U32_INIT_VAL	(1UL << 10)
#define	U64_INIT_VAL	(1ULL << 33)

typedef enum {
	TEST_MIX = 1, /* Must be first test case num */
	TEST_INC_DEC_U32,
	TEST_ADD_SUB_U32,
	TEST_INC_DEC_64,
	TEST_ADD_SUB_64,
	TEST_MAX,
} odp_test_atomic_t;

static odp_atomic_u32_t a32u;
static odp_atomic_u64_t a64u;
static odp_barrier_t barrier;
static odph_linux_pthread_t thread_tbl[MAX_WORKERS]; /**< worker threads table*/
static int num_workers; /**< number of workers >----*/



static const char * const test_name[] = {
	"dummy",
	"test atomic all (add/sub/inc/dec) on 32- and 64-bit atomic ints",
	"test atomic inc/dec of 32-bit atomic int",
	"test atomic add/sub of 32-bit atomic int",
	"test atomic inc/dec of 64-bit atomic int",
	"test atomic add/sub of 64-bit atomic int"
};

static struct timeval tv0[MAX_WORKERS], tv1[MAX_WORKERS];

static void usage(void)
{
	printf("\n./odp_atomic -t <testcase> [-n <numthreads>]\n\n"
	       "\t<testcase> is\n"
	       "\t\t1 - Test all (inc/dec/add/sub on 32/64-bit atomic ints)\n"
	       "\t\t2 - Test inc/dec of 32-bit atomic int\n"
	       "\t\t3 - Test add/sub of 32-bit atomic int\n"
	       "\t\t4 - Test inc/dec of 64-bit atomic int\n"
	       "\t\t5 - Test add/sub of 64-bit atomic int\n"
	       "\t\t-n <1 - 31> - no of threads to start\n"
	       "\t\tif user doesn't specify this option, then\n"
	       "\t\tno of threads created is equivalent to no of CPU's\n"
	       "\t\tavailable in the system\n"
	       "\tExample usage:\n"
	       "\t\t./odp_atomic -t 2\n"
	       "\t\t./odp_atomic -t 3 -n 12\n");
}


void test_atomic_inc_u32(void)
{
	int i;

	for (i = 0; i < CNT; i++)
		odp_atomic_inc_u32(&a32u);
}

void test_atomic_inc_64(void)
{
	int i;

	for (i = 0; i < CNT; i++)
		odp_atomic_inc_u64(&a64u);
}

void test_atomic_dec_u32(void)
{
	int i;

	for (i = 0; i < CNT; i++)
		odp_atomic_dec_u32(&a32u);
}

void test_atomic_dec_64(void)
{
	int i;

	for (i = 0; i < CNT; i++)
		odp_atomic_dec_u64(&a64u);
}

void test_atomic_add_u32(void)
{
	int i;

	for (i = 0; i < (CNT / ADD_SUB_CNT); i++)
		odp_atomic_fetch_add_u32(&a32u, ADD_SUB_CNT);
}

void test_atomic_add_64(void)
{
	int i;

	for (i = 0; i < (CNT / ADD_SUB_CNT); i++)
		odp_atomic_fetch_add_u64(&a64u, ADD_SUB_CNT);
}

void test_atomic_sub_u32(void)
{
	int i;

	for (i = 0; i < (CNT / ADD_SUB_CNT); i++)
		odp_atomic_fetch_sub_u32(&a32u, ADD_SUB_CNT);
}

void test_atomic_sub_64(void)
{
	int i;

	for (i = 0; i < (CNT / ADD_SUB_CNT); i++)
		odp_atomic_fetch_sub_u64(&a64u, ADD_SUB_CNT);
}

void test_atomic_inc_dec_u32(void)
{
	test_atomic_inc_u32();
	test_atomic_dec_u32();
}

void test_atomic_add_sub_u32(void)
{
	test_atomic_add_u32();
	test_atomic_sub_u32();
}

void test_atomic_inc_dec_64(void)
{
	test_atomic_inc_64();
	test_atomic_dec_64();
}

void test_atomic_add_sub_64(void)
{
	test_atomic_add_64();
	test_atomic_sub_64();
}

/**
 * Test basic atomic operation like
 * add/sub/increment/decrement operation.
 */
void test_atomic_basic(void)
{
	test_atomic_inc_u32();
	test_atomic_dec_u32();
	test_atomic_add_u32();
	test_atomic_sub_u32();

	test_atomic_inc_64();
	test_atomic_dec_64();
	test_atomic_add_64();
	test_atomic_sub_64();
}

void test_atomic_init(void)
{
	odp_atomic_init_u32(&a32u, 0);
	odp_atomic_init_u64(&a64u, 0);
}

void test_atomic_store(void)
{
	odp_atomic_store_u32(&a32u, U32_INIT_VAL);
	odp_atomic_store_u64(&a64u, U64_INIT_VAL);
}

int test_atomic_validate(void)
{
	if (odp_atomic_load_u32(&a32u) != U32_INIT_VAL) {
		LOG_ERR("Atomic u32 usual functions failed\n");
		return -1;
	}

	if (odp_atomic_load_u64(&a64u) != U64_INIT_VAL) {
		LOG_ERR("Atomic u64 usual functions failed\n");
		return -1;
	}

	return 0;
}

static void *run_thread(void *arg)
{
	pthrd_arg *parg = (pthrd_arg *)arg;
	int thr;

	thr = odp_thread_id();

	LOG_DBG("Thread %i starts\n", thr);

	/* Wait here until all threads have arrived */
	/* Use multiple barriers to verify that it handles wrap around and
	 * has no race conditions which could be exposed when invoked back-
	 * to-back */
	odp_barrier_wait(&barrier);
	odp_barrier_wait(&barrier);
	odp_barrier_wait(&barrier);
	odp_barrier_wait(&barrier);

	gettimeofday(&tv0[thr], NULL);

	switch (parg->testcase) {
	case TEST_MIX:
		test_atomic_basic();
		break;
	case TEST_INC_DEC_U32:
		test_atomic_inc_dec_u32();
		break;
	case TEST_ADD_SUB_U32:
		test_atomic_add_sub_u32();
		break;
	case TEST_INC_DEC_64:
		test_atomic_inc_dec_64();
		break;
	case TEST_ADD_SUB_64:
		test_atomic_add_sub_64();
		break;
	}
	gettimeofday(&tv1[thr], NULL);
	fflush(NULL);

	printf("Time taken in thread %02d to complete op is %lld usec\n", thr,
	       (tv1[thr].tv_sec - tv0[thr].tv_sec) * 1000000ULL +
	       (tv1[thr].tv_usec - tv0[thr].tv_usec));

	return parg;
}

/** create test thread */
int odp_test_thread_create(void *func_ptr(void *), pthrd_arg *arg)
{
	odp_cpumask_t cpumask;

	/* Create and init additional threads */
	odp_cpumask_default_worker(&cpumask, arg->numthrds);
	odph_linux_pthread_create(thread_tbl, &cpumask, func_ptr,
				  (void *)arg, ODP_THREAD_WORKER);

	return 0;
}

/** exit from test thread */
int odp_test_thread_exit(pthrd_arg *arg)
{
	/* Wait for other threads to exit */
	odph_linux_pthread_join(thread_tbl, arg->numthrds);

	return 0;
}

/** test init globals and call odp_init_global() */
int odp_test_global_init(void)
{
	memset(thread_tbl, 0, sizeof(thread_tbl));

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

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

	return 0;
}

/**
 * Print system information
 */
void odp_print_system_info(void)
{
	odp_cpumask_t cpumask;
	char str[ODP_CPUMASK_STR_SIZE];

	memset(str, 1, sizeof(str));

	odp_cpumask_zero(&cpumask);

	odp_cpumask_from_str(&cpumask, "0x1");
	(void)odp_cpumask_to_str(&cpumask, str, sizeof(str));

	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_cpu_model_str());
	printf("CPU freq (hz):   %"PRIu64"\n", odp_cpu_hz_max());
	printf("Cache line size: %i\n",        odp_sys_cache_line_size());
	printf("CPU count:       %i\n",        odp_cpu_count());
	printf("CPU mask:        %s\n",        str);

	printf("\n");
}


int main(int argc, char *argv[])
{
	pthrd_arg thrdarg;
	int test_type = 1, pthrdnum = 0, i = 0, cnt = argc - 1;
	char c;
	int result;

	if (argc == 0 || argc % 2 == 0) {
		usage();
		goto err_exit;
	}

	if (odp_test_global_init() != 0)
		goto err_exit;
	odp_print_system_info();

	while (cnt != 0) {
		sscanf(argv[++i], "-%c", &c);
		switch (c) {
		case 't':
			sscanf(argv[++i], "%d", &test_type);
			break;
		case 'n':
			sscanf(argv[++i], "%d", &pthrdnum);
			break;
		default:
			LOG_ERR("Invalid option %c\n", c);
			usage();
			goto err_exit;
		}
		if (test_type < TEST_MIX || test_type > TEST_MAX ||
		    pthrdnum > odp_cpu_count() || pthrdnum < 0) {
			usage();
			goto err_exit;
		}
		cnt -= 2;
	}

	if (pthrdnum == 0)
		pthrdnum = odp_cpu_count();

	test_atomic_init();
	test_atomic_store();

	memset(&thrdarg, 0, sizeof(pthrd_arg));
	thrdarg.testcase = test_type;
	thrdarg.numthrds = pthrdnum;

	if ((test_type > 0) && (test_type < TEST_MAX)) {
		printf("%s\n", test_name[test_type]);
	} else {
		LOG_ERR("Invalid test case [%d]\n", test_type);
		usage();
		goto err_exit;
	}
	odp_barrier_init(&barrier, pthrdnum);
	odp_test_thread_create(run_thread, &thrdarg);

	odp_test_thread_exit(&thrdarg);

	result = test_atomic_validate();

	if (result == 0) {
		printf("%s_%d_%d Result:pass\n",
		       test_name[test_type], test_type, pthrdnum);
	} else {
		printf("%s_%d_%d Result:fail\n",
		       test_name[test_type], test_type, pthrdnum);
	}
	return 0;

err_exit:
	return -1;
}