EM-ODP  3.7.0
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 } em_conf_t;
227 
228 /**
229  * Initialize configuration parameters for em_init()
230  *
231  * Initialize em_conf_t to default values for all fields.
232  * After initialization, the user further needs to set the mandatory fields of
233  * 'em_conf_t' before calling em_init().
234  * Always initialize 'conf' first with em_conf_init(&conf) to
235  * ensure backwards compatibility with potentially added new options.
236  *
237  * @param conf Address of the em_conf_t to be initialized
238  *
239  * @see em_init()
240  */
241 void em_conf_init(em_conf_t *conf);
242 
243 /**
244  * Initialize the Event Machine.
245  *
246  * Must be called once at startup. Additionally each EM-core needs to call the
247  * em_init_core() function before using any further EM API functions/resources.
248  *
249  * @param conf EM runtime config options,
250  * HW/platform specific: see event_machine_hw_types.h
251  *
252  * @return EM_OK if successful.
253  *
254  * @see em_init_core() for EM-core specific init after em_init().
255  */
256 em_status_t em_init(const em_conf_t *conf);
257 
258 /**
259  * Initialize an EM-core.
260  *
261  * Must be called once by each EM-core (= process, thread or bare metal core).
262  * EM queues, EOs, queue groups etc. can be created after a successful return
263  * from this function.
264  * Note, the thread or process must be pinned to a unique physical core before
265  * running em_init_core().
266  *
267  * @return EM_OK if successful.
268  *
269  * @see em_init()
270  */
272 
273 /**
274  * Terminate the Event Machine.
275  *
276  * Called once at exit. Additionally, before the one call to em_term(),
277  * each EM-core needs to call the em_term_core() function to free up local
278  * resources.
279  *
280  * @param conf EM runtime config options
281  *
282  * @return EM_OK if successful.
283  *
284  * @see em_term_core() for EM-core specific termination before em_term().
285  */
286 em_status_t em_term(const em_conf_t *conf);
287 
288 /**
289  * Terminate an EM-core.
290  *
291  * Called by each EM-core (= process, thread or bare metal core)
292  * before one call to em_term().
293  *
294  * @return EM_OK if successful.
295  *
296  * @see em_term()
297  */
299 
300 /**
301  * Return the EM device-id for this instance.
302  *
303  * This is a convenience function that returns the EM device-id given by the
304  * user to em_init() via the em_conf_t::device_id field.
305  *
306  * The function should only be called after a successful EM initialization.
307  *
308  * @return the device-id of this EM instance.
309  */
310 uint16_t em_device_id(void);
311 
312 /**
313  * @}
314  */
315 #ifdef __cplusplus
316 }
317 #endif
318 
319 #pragma GCC visibility pop
320 #endif /* EVENT_MACHINE_INIT_H_ */
em_device_id
uint16_t em_device_id(void)
Definition: event_machine_init.c:468
em_conf_t::vlog_fn
em_vlog_func_t vlog_fn
Definition: event_machine_init.h:171
em_conf_t::event_timer
int event_timer
Definition: event_machine_init.h:105
em_input_poll_func_t
int(* em_input_poll_func_t)(void)
Definition: event_machine_hw_types.h:349
em_conf_t::thread_per_core
int thread_per_core
Definition: event_machine_init.h:115
em_api_hooks_t
Definition: event_machine_hooks.h:214
em_log_func_t
int(* em_log_func_t)(em_log_level_t level, const char *fmt,...) __attribute__((format(printf
Definition: event_machine_hw_types.h:329
em_idle_hooks_t
Definition: event_machine_hooks.h:254
em_conf_t::device_id
uint16_t device_id
Definition: event_machine_init.h:99
em_conf_t::default_pool_cfg
em_pool_cfg_t default_pool_cfg
Definition: event_machine_init.h:160
em_core_mask_t
Definition: event_machine_hw_types.h:242
em_conf_t::output_drain_fn
em_output_drain_func_t output_drain_fn
Definition: event_machine_init.h:202
em_conf_t::log_fn
em_log_func_t log_fn
Definition: event_machine_init.h:169
em_pool_cfg_t
Definition: event_machine_pool.h:141
em_term_core
em_status_t em_term_core(void)
Definition: event_machine_init.c:416
em_term
em_status_t em_term(const em_conf_t *conf)
Definition: event_machine_init.c:351
em_init
em_status_t em_init(const em_conf_t *conf)
Definition: event_machine_init.c:65
em_conf_t::process_per_core
int process_per_core
Definition: event_machine_init.h:123
em_conf_t::idle_hooks
em_idle_hooks_t idle_hooks
Definition: event_machine_init.h:225
em_conf_t::input_poll_fn
em_input_poll_func_t input_poll_fn
Definition: event_machine_init.h:182
em_conf_t::input_poll_mask
em_core_mask_t input_poll_mask
Definition: event_machine_init.h:190
em_status_t
uint32_t em_status_t
Definition: event_machine_types.h:321
em_conf_init
void em_conf_init(em_conf_t *conf)
Definition: event_machine_init.c:54
em_conf_t::api_hooks
em_api_hooks_t api_hooks
Definition: event_machine_init.h:218
em_output_drain_func_t
int(* em_output_drain_func_t)(void)
Definition: event_machine_hw_types.h:367
em_vlog_func_t
int(*) typedef int(* em_vlog_func_t)(em_log_level_t level, const char *fmt, va_list args)
Definition: event_machine_hw_types.h:337
em_conf_t::core_count
int core_count
Definition: event_machine_init.h:132
em_conf_t::phys_mask
em_core_mask_t phys_mask
Definition: event_machine_init.h:149
em_conf_t::output_drain_mask
em_core_mask_t output_drain_mask
Definition: event_machine_init.h:210
em_init_core
em_status_t em_init_core(void)
Definition: event_machine_init.c:247
em_conf_t
Definition: event_machine_init.h:93