aboutsummaryrefslogtreecommitdiff
path: root/helper/include/odp/helper/threads.h
blob: c18a46e8a27f62ff209acd9d84186b1bcb9867d9 (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2013-2018 Linaro Limited
 * Copyright (c) 2019-2021 Nokia
 */


/**
 * @file
 *
 * ODP Linux helper API
 *
 * This file is an optional helper to ODP APIs. These functions are provided
 * to ease common setups in a Linux system. User is free to implement the same
 * setups in other ways (not via this API).
 */

#ifndef ODPH_LINUX_H_
#define ODPH_LINUX_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <odp_api.h>

#include <pthread.h>
#include <getopt.h>
#include <sys/types.h>

/**
 * @defgroup odph_thread ODPH THREAD
 * Setup threads/processes
 *
 * @{
 */

/** Thread parameter for Linux pthreads and processes */
typedef struct {
	void *(*start)(void *);    /**< Thread entry point function */
	void *arg;                  /**< Argument for the function */
	odp_thread_type_t thr_type; /**< ODP thread type */
	odp_instance_t instance;    /**< ODP instance handle */
} odph_linux_thr_params_t;

/** Linux pthread state information */
typedef struct {
	pthread_t      thread; /**< Pthread ID */
	pthread_attr_t attr;   /**< Pthread attributes */
	int            cpu;    /**< CPU ID */
	/** Copy of thread params */
	odph_linux_thr_params_t thr_params;
} odph_linux_pthread_t;

/** Linux process state information */
typedef struct {
	pid_t pid;      /**< Process ID */
	int   cpu;      /**< CPU ID */
	int   status;   /**< Process state change status */
} odph_linux_process_t;

/** Thread parameters (pthreads and processes) */
typedef struct {
	/** Thread entry point function */
	int (*start)(void *arg);

	/** Argument for the function */
	void *arg;

	/** ODP thread type */
	odp_thread_type_t thr_type;

	/** Minimum stack size in bytes. 0 = use default. */
	uint64_t stack_size;

} odph_thread_param_t;

/** Helper internal thread start arguments. Used both in process and thread
 *  mode */
typedef struct {
	/** Thread status */
	uint32_t status;

	/** Thread initialization status */
	odp_atomic_u32_t *init_status;

	/** Process or thread */
	odp_mem_model_t mem_model;

	/** ODP instance handle */
	odp_instance_t instance;

	/** Thread parameters */
	odph_thread_param_t thr_params;

} odph_thread_start_args_t;

/** Thread state information. Used both in process and thread mode */
typedef struct {
	/** Start arguments */
	odph_thread_start_args_t start_args;

	/** CPU ID */
	int cpu;

	/** 1: last table entry */
	uint8_t last;

	/** Variant field mappings for thread/process modes */
	union {
		/** For thread implementation */
		struct {
			pthread_t	thread_id; /**< Pthread ID */
			pthread_attr_t	attr;	/**< Pthread attributes */
		} thread;

		/** For process implementation */
		struct {
			pid_t		pid;	/**< Process ID */
			int		status;	/**< Process state chge status*/
		} proc;
	};

} odph_thread_t;

/** Linux helper options */
typedef struct {
	odp_mem_model_t mem_model; /**< Process or thread */
} odph_helper_options_t;

/** Common parameters for odph_thread_create() call */
typedef struct {
	/**
	 * ODP instance handle
	 *
	 * This is used for all threads, instead of 'instance' field of per
	 * thread parameters (odph_thread_param_t).
	 */
	odp_instance_t instance;

	/**
	 * CPU mask for thread pinning
	 */
	const odp_cpumask_t *cpumask;

	/**
	 * Select between Linux pthreads and processes
	 *
	 * 0: Use pthreads
	 * 1: Use processes
	 *
	 * Default value is 0.
	 */
	int thread_model;

	/**
	 * Synchronized thread creation
	 *
	 * 0: Don't synchronize thread creation
	 * 1: Create threads in series so that the next thread is created
	 *    only after the previous thread have signaled that it has passed
	 *    ODP local initialization.
	 *
	 * Default value is 0.
	 */
	int sync;

	/**
	 * Synchronized thread creation timeout in nanoseconds
	 *
	 * When synchronized thread creation has been requested, waiting for the
	 * synchronization signal times out once the time indicated by this
	 * parameter has passed.
	 *
	 * If this parameter is 0, the default value is used.
	 *
	 * Default value is ODP_TIME_SEC_IN_NS.
	 */
	uint64_t sync_timeout;

	/**
	 * Thread parameter sharing
	 *
	 * 0: Thread parameters are not shared. The thread parameter table
	 *    contains 'num' elements.
	 * 1: The thread parameter table contains a single element, which is
	 *    used for creating all 'num' threads.
	 *
	 * Default value is 0.
	 */
	int share_param;

} odph_thread_common_param_t;

/**
 * Initialize thread params
 *
 * Initialize an odph_thread_param_t to its default values for all fields.
 *
 * @param[out] param Pointer to parameter structure
 */
void odph_thread_param_init(odph_thread_param_t *param);

/**
 * Initialize thread common params
 *
 * Initialize an odph_thread_common_param_t to its default values for all
 * fields.
 *
 * @param[out] param Pointer to parameter structure
 */
void odph_thread_common_param_init(odph_thread_common_param_t *param);

/**
 * Create and pin threads (as Linux pthreads or processes)
 *
 * Function may be called multiple times to create threads in steps. Each call
 * launches 'num' threads and pins those to separate CPUs based on the cpumask.
 * Use 'thread_model' parameter to select if Linux pthreads or processes are
 * used. This selection may be overridden with ODP helper options. See e.g.
 * --odph_proc under odph_options() documentation.
 *
 * Thread creation may be synchronized by setting 'sync' parameter. It
 * serializes thread start up (odp_init_local() calls), which helps to
 * stabilize application start up sequence.
 *
 * By default, the thread parameter table contains 'num' elements, one for
 * each thread to be created. However, all threads may be created
 * with a single thread parameter table element by setting 'share_param'
 * parameter.
 *
 * Use odph_thread_common_param_init() and odph_thread_param_init() to
 * initialize parameters with default values.
 *
 * Thread table must be large enough to hold 'num' elements. Also the cpumask
 * must contain 'num' CPUs. Threads are pinned to CPUs in order - the first
 * thread goes to the smallest CPU number of the mask, etc.
 *
 * Launched threads may be waited for exit with odph_thread_join(), or with
 * direct Linux system calls.
 *
 * @param[out] thread        Thread table for output
 * @param      param         Common parameters for all threads to be created
 * @param      thr_param     Table of thread parameters
 * @param      num           Number of threads to create
 *
 * @return Number of threads created
 * @retval -1  On failure
 *
 * @see odph_thread_join()
 */
int odph_thread_create(odph_thread_t thread[],
		       const odph_thread_common_param_t *param,
		       const odph_thread_param_t thr_param[],
		       int num);

/**
 * Wait previously launched threads to exit
 *
 * Function waits for threads launched with odph_thread_create() to exit.
 * Threads may be waited to exit in a different order than those were created.
 * A function call may be used to wait any number of launched threads to exit.
 * A particular thread may be waited only once.
 *
 * @param thread        Table of threads to exit
 * @param num           Number of threads to exit
 *
 * @return Number of threads exited
 * @retval -1  On failure
 *
 * @see odph_thread_create()
 */
int odph_thread_join(odph_thread_t thread[], int num);

/**
 * Set CPU affinity of the current odp thread
 *
 * CPU affinity determines the CPU core on which the thread is
 * eligible to run.
 *
 * @param cpu           The affinity CPU core
 *
 * @return 0 on success, -1 on failure
 */
int odph_odpthread_setaffinity(const int cpu);

/**
 * Get CPU affinity of the current odp thread
 *
 * CPU affinity determines the CPU core on which the thread is
 * eligible to run.
 *
 * @return positive cpu ID on success, -1 on failure
 */
int odph_odpthread_getaffinity(void);

/**
 * Parse linux helper options
 *
 * Parse the command line options. Pick up (--odph_ prefixed) options meant for
 * the helper itself. When helper options are found, those are removed from
 * argv[] and remaining options are packed to the beginning of the array.
 *
 * <table> <caption> Currently supported options </caption>
 *
 * <tr><th>Command line <th>Environment variable <th>Description
 * <tr><td>--odph_proc  <td>ODPH_PROC_MODE       <td>When defined, threads are
 *                                                   Linux processes. Otherwise,
 *                                                   pthreads are used instead.
 * </table>
 *
 * @param argc   Argument count
 * @param argv   Argument vector
 *
 * @return New argument count. Original argument count decremented by
 *         the number of removed helper options.
 */
int odph_parse_options(int argc, char *argv[]);

/**
 * Get linux helper options
 *
 * Return used ODP helper options. odph_parse_options() must be called before
 * using this function.
 *
 * @param[out] options  ODP helper options
 *
 * @return 0 on success, -1 on failure
 */
int odph_options(odph_helper_options_t *options);

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif