aboutsummaryrefslogtreecommitdiff
path: root/example/isolation/odp_isolation.c
blob: 1ade4c7f2bbf239f84d542d22c786944e3c9946b (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
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/**
 * @file
 *
 * @example  odp_isol_test.c ODP isolation example application
 */

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

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

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

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

#define MAX_WORKERS	32	/**< Max worker threads */

/**
 * Parsed command line application arguments
 */
typedef struct {
	int core_count; /**< Core count*/
	char **core_name;	/**< Array of pointers to core name */
} appl_args_t;

/**
 * @internal Print help
 */
static void print_usage(void)
{
	printf("\n\nUsage: ./odp_isolation [options]\n");
	printf("Options:\n");
	printf("  -l, --cpulist <cpu number in comma separated fashion>\n");
	printf("  -h, --help              this help\n");
	printf("\n\n");
}

/**
 * @internal Worker thread
 *
 * @param arg  Arguments
 *
 * @return NULL on failure
 */
static void *run_thread(void *arg)
{
	int thr;
	unsigned long long tick = 0;
	int counter = 0;

	thr = odp_thread_id();

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

	/* loop for some duration then exit */
	do {
		tick++;

		if (tick > 10000000) {

			tick = 0;
			counter++;
		}

		if (counter == 1000)
			break;
	} while (1);

	return arg;
}

/**
 * @internal Parse arguments
 *
 * @param argc  Argument count
 * @param argv  Argument vector
 * @param args  Test arguments
 */
static void parse_args(int argc, char *argv[], appl_args_t *args)
{
	int opt;
	int long_index;
	char *names, *str, *token, *save;
	size_t len;
	int i;

	static struct option longopts[] = {
		{"cpulists", required_argument, NULL, 'l'}, /* return 'l' */
		{"help", no_argument, NULL, 'h'}, /* return 'h' */
		{NULL, 0, NULL, 0}
	};

	while (1) {
		opt = getopt_long(argc, argv, "+l:h", longopts, &long_index);

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

		switch (opt) {
		case 'l':
			len = strlen(optarg);
			if (len == 0) {
				print_usage();
				exit(EXIT_FAILURE);
			}
			len += 1;	/* add room for '\0' */

			names = malloc(len);
			if (names == NULL) {
				print_usage();
				exit(EXIT_FAILURE);
			}

			/* count the number of tokens separated by ',' */
			strcpy(names, optarg);
			for (str = names, i = 0;; str = NULL, i++) {
				token = strtok_r(str, ",", &save);
				if (token == NULL)
					break;
			}
			args->core_count = i;

			if (args->core_count == 0) {
				print_usage();
				exit(EXIT_FAILURE);
			}
			/* allocate storage for the if names */
			args->core_name =
				calloc(args->core_count, sizeof(char *));

			/* store the if names (reset names string) */
			strcpy(names, optarg);
			for (str = names, i = 0;; str = NULL, i++) {
				token = strtok_r(str, ",", &save);
				if (token == NULL)
					break;
				args->core_name[i] = token;
			}
			break;

		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);
			break;

		default:
			break;
		}
	}
}

/**
 * Isolation test application
 */
int main(int argc, char *argv[])
{
	odp_linux_pthread_t thread_tbl[MAX_WORKERS];
	appl_args_t args;
	int thr_id;
	int num_workers;
	int first_core;
	int i;

	printf("\nODP isolation test application\n");

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

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

	/*
	 * Don't need timer init for isolation, so mask them.
	 */
	if (odp_init_global(ODP_INIT_F_ALL & ~ODP_INIT_F_TIMER)) {
		printf("ODP global 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);

	/*
	 * Init this thread.
	 * */
	thr_id = odp_thread_create(0);
	odp_init_local(thr_id);

	/*
	 * create dummy dp thread and don't pin them on any core,
	 * use odp-on-isolated-cpu.sh script to define dplane cpuset
	 * and to pin, run app on them, Refer readme.
	 */
	for (i=0; i<num_workers; i++) {

		first_core = atoi(args.core_name[i]);
		/* Create and launch worker threads */
		odp_linux_pthread_create(&thread_tbl[i], 1, first_core,
					 run_thread, NULL);
	}

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

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

	return 0;
}