EM-ODP  3.8.0-1
Event Machine on ODP
event_machine_init.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef EVENT_MACHINE_INIT_H_
32 #define EVENT_MACHINE_INIT_H_
33 
34 #pragma GCC visibility push(default)
35 
36 /**
37  * @file
38  * @defgroup init Initialization and termination
39  * Event Machine initialization and termination
40  * @{
41  *
42  * The Event Machine must be initialized before use. One core that will be part
43  * of EM calls em_init(). Additionally, after the user has set up the threads,
44  * or processes and pinned those to HW-cores, each participating core, i.e.
45  * EM-core, needs to run em_init_core(). Only now is an EM-core ready to use the
46  * other EM API functions and can finally enter the dispatch-loop via
47  * em_dispath() on each core that should handle events.
48  *
49  * The EM termination sequence runs in the opposite order: each core needs to
50  * call em_term_core() before one last call to em_term().
51  *
52  * The 'em_conf_t' type given to em_init() and em_term() is HW/platform specific
53  * and is defined in event_machine_hw_types.h
54  *
55  * Do not include this from the application, event_machine.h will
56  * do it for you.
57  */
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 /**
64  * Event Machine run-time configuration options given at startup to em_init()
65  *
66  * The 'em_conf_t' struct should be initialized with em_conf_init() before use.
67  * This initialization provides better backwards compatibility since all options
68  * will be set to default values.
69  * The user must further set the needed configuration and call em_init():
70  *
71  * @code
72  * em_conf_t conf;
73  * em_conf_init(&conf); // init with default values
74  * conf.thread_per_core = 1;
75  * ...
76  * conf.core_count = N;
77  * conf.phys_mask = set N bits; // use em_core_mask_...() functions to set
78  * ...
79  * ret = em_init(&conf); // on one core
80  * ...
81  * ret = em_init_core(); // on each of the 'conf.core_count' cores
82  * @endcode
83  *
84  * Content is copied into EM by em_init().
85  *
86  * @note Several EM options are configured through compile-time defines.
87  * Run-time options allow using the same EM-lib with different configs.
88  * Also see the overridable EM runtime config file values,
89  * default file: config/em-odp.config
90  *
91  * @see em_conf_init(), em_init()
92  */
93 typedef struct {
94  /**
95  * EM device id - use different device ids for each EM instance or
96  * remote EM device that need to communicate with each other.
97  * Default value is 0.
98  */
99  uint16_t device_id;
100 
101  /**
102  * Event Timer: enable=1, disable=0.
103  * Default value is 0 (disable).
104  */
106 
107  /**
108  * RunMode: EM run with one thread per core.
109  * Set 'true' to select thread-per-core mode.
110  * This is the recommended mode, but the user must explicitly set it to
111  * enable. Default value is 0.
112  * @note The user must set either 'thread_per_core' or
113  * 'process_per_core' but not both.
114  */
116 
117  /**
118  * RunMode: EM run with one process per core.
119  * Set 'true' to select process-per-core mode. Default value is 0.
120  * @note The user must set either 'thread_per_core' or
121  * 'process_per_core' but not both.
122  */
124 
125  /**
126  * Number of EM-cores (== number of EM-threads or EM-processes).
127  * The 'core_count' must match the number of bits set in 'phys_mask'.
128  * EM-cores will be enumerated from 0 to 'core_count-1' regardless of
129  * the actual physical core ids.
130  * Default value is 0 and needs to be changed by the user.
131  */
133 
134  /**
135  * Physical core mask, exactly listing the physical CPU cores to be used
136  * by EM (this is a physical core mask even though the 'em_core_mask_t'
137  * type is used).
138  * Default value is all-0 and needs to be changed by the user.
139  * @note EM otherwise operates on logical cores, i.e. enumerated
140  * contiguously from 0 to 'core_count-1' and a logical
141  * EM core mask has 'core_count' consecutively set bits.
142  * Example - physical mask vs. corresponding EM core mask:
143  * .core_count = 8
144  * .physmask: 0xf0f0 (binary: 1111 0000 1111 0000 - 8 set bits)
145  * = 8 phys-cores (phys-cores 4-7,12-15)
146  * ==> EM-mask: 0x00ff (0000 0000 1111 1111 binary) - 8 EM cores
147  * = 8 EM-cores (EM-cores 0-7)
148  */
150 
151  /**
152  * Pool configuration for the EM default pool (EM_POOL_DEFAULT).
153  * Default value is set by em_pool_cfg_init() and needs to be changed
154  * by the user.
155  *
156  * Note that if the default pool configuration is also given in the
157  * config file through option 'startup_pools', it will override this
158  * default pool configuration.
159  */
161 
162  /**
163  * EM log functions.
164  * Default values are NULL and causes EM to use internal default
165  * log-functions.
166  */
167  struct {
168  /** EM log function, user overridable, variable number of args*/
170  /** EM log function, user overridable, va_list */
172  } log;
173 
174  /** EM event/pkt input related functions and config */
175  struct {
176  /**
177  * User provided function, called from within the EM-dispatch
178  * loop, mainly for polling various input sources for events or
179  * pkts and then enqueue them into EM.
180  * Set to 'NULL' if not needed (default).
181  */
183  /**
184  * EM core mask to control which EM-cores (0 to 'core_count-1')
185  * input_poll_fn() will be called on.
186  * The provided mask has to be equal or a subset of the
187  * EM core mask with all 'core_count' bits set.
188  * A zero mask means execution on _all_ EM cores (default).
189  */
191  } input;
192 
193  /** EM event/pkt output related functions and config */
194  struct {
195  /**
196  * User provided function, called from within the EM-dispatch
197  * loop, mainly for 'periodical' draining of buffered output to
198  * make sure events/pkts are eventually sent out even if the
199  * rate is low or stops for a while.
200  * Set to 'NULL' if not needed (default).
201  */
203  /**
204  * EM core mask to control which EM-cores (0 to 'core_count-1')
205  * output_drain_fn() will be called on.
206  * The provided mask has to be equal or a subset of the
207  * EM core mask with all 'core_count' bits set.
208  * A zero mask means execution on _all_ EM cores (default).
209  */
211  } output;
212 
213  /**
214  * User provided API callback hooks.
215  * Set only the needed hooks to avoid performance degradation.
216  * Only used if EM_API_HOOKS_ENABLE != 0
217  */
219 
220  /**
221  * User provided idle callback hooks.
222  * Set only the needed hooks to avoid performance degradation.
223  * Only used if EM_IDLE_HOOKS_ENABLE != 0
224  */
226 
227  /**
228  * Internal check - don't touch!
229  *
230  * EM will verify that em_conf_init() has been called
231  * before calling em_init().
232  */
234 } em_conf_t;
235 
236 /**
237  * Initialize configuration parameters for em_init()
238  *
239  * Initialize em_conf_t to default values for all fields.
240  * After initialization, the user further needs to set the mandatory fields of
241  * 'em_conf_t' before calling em_init().
242  * Always initialize 'conf' first with em_conf_init(&conf) to
243  * ensure backwards compatibility with potentially added new options.
244  *
245  * @param conf Address of the em_conf_t to be initialized
246  *
247  * @see em_init()
248  */
249 void em_conf_init(em_conf_t *conf);
250 
251 /**
252  * Initialize the Event Machine.
253  *
254  * Must be called once at startup. Additionally each EM-core needs to call the
255  * em_init_core() function before using any further EM API functions/resources.
256  *
257  * @param conf EM runtime config options,
258  * HW/platform specific: see event_machine_hw_types.h
259  *
260  * @return EM_OK if successful.
261  *
262  * @see em_init_core() for EM-core specific init after em_init().
263  */
264 em_status_t em_init(const em_conf_t *conf);
265 
266 /**
267  * Initialize an EM-core.
268  *
269  * Must be called once by each EM-core (= process, thread or bare metal core).
270  * EM queues, EOs, queue groups etc. can be created after a successful return
271  * from this function.
272  * Note, the thread or process must be pinned to a unique physical core before
273  * running em_init_core().
274  *
275  * @return EM_OK if successful.
276  *
277  * @see em_init()
278  */
280 
281 /**
282  * Terminate the Event Machine.
283  *
284  * Called once at exit. Additionally, before the one call to em_term(),
285  * each EM-core needs to call the em_term_core() function to free up local
286  * resources.
287  *
288  * @param conf EM runtime config options
289  *
290  * @return EM_OK if successful.
291  *
292  * @see em_term_core() for EM-core specific termination before em_term().
293  */
294 em_status_t em_term(const em_conf_t *conf);
295 
296 /**
297  * Terminate an EM-core.
298  *
299  * Called by each EM-core (= process, thread or bare metal core)
300  * before one call to em_term().
301  *
302  * @return EM_OK if successful.
303  *
304  * @see em_term()
305  */
307 
308 /**
309  * Return the EM device-id for this instance.
310  *
311  * This is a convenience function that returns the EM device-id given by the
312  * user to em_init() via the em_conf_t::device_id field.
313  *
314  * The function should only be called after a successful EM initialization.
315  *
316  * @return the device-id of this EM instance.
317  */
318 uint16_t em_device_id(void);
319 
320 /**
321  * @}
322  */
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #pragma GCC visibility pop
328 #endif /* EVENT_MACHINE_INIT_H_ */
int(* em_input_poll_func_t)(void)
int(* em_log_func_t)(em_log_level_t level, const char *fmt,...) __attribute__((format(printf
int(*) typedef int(* em_vlog_func_t)(em_log_level_t level, const char *fmt, va_list args)
int(* em_output_drain_func_t)(void)
uint32_t em_status_t
em_status_t em_term(const em_conf_t *conf)
em_status_t em_term_core(void)
void em_conf_init(em_conf_t *conf)
em_status_t em_init(const em_conf_t *conf)
uint16_t em_device_id(void)
em_status_t em_init_core(void)
em_log_func_t log_fn
em_core_mask_t input_poll_mask
uint16_t device_id
em_core_mask_t output_drain_mask
em_api_hooks_t api_hooks
em_vlog_func_t vlog_fn
em_pool_cfg_t default_pool_cfg
em_output_drain_func_t output_drain_fn
em_core_mask_t phys_mask
em_input_poll_func_t input_poll_fn
uint32_t __internal_check
em_idle_hooks_t idle_hooks