EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
em_timer.c
1/*
2 * Copyright (c) 2017, 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#ifndef _GNU_SOURCE
31#define _GNU_SOURCE
32#endif
33
34#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#include <stdbool.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <string.h>
42
43#include <odp_api.h>
44
45#include <event_machine.h>
47
48#include "em_chaining.h"
49#include "em_error.h"
50#include "em_event.h"
51#include "em_event_inline.h"
52#include "em_event_state.h"
53#include "em_event_types.h"
54#include "em_libconfig.h"
55#include "em_mem.h"
56#include "em_queue.h"
57#include "em_queue_inline.h"
58#include "em_queue_types.h"
59#include "em_timer.h"
60#include "em_timer_types.h"
61
62static int read_config_file(void);
63
64em_status_t timer_init(timer_storage_t *const tmrs)
65{
66 for (int i = 0; i < EM_ODP_MAX_TIMERS; i++) {
67 memset(&tmrs->timer[i], 0, sizeof(event_timer_t));
68 tmrs->timer[i].idx = i; /* for fast reverse lookup */
69 tmrs->timer[i].tmo_pool = ODP_POOL_INVALID;
70 tmrs->timer[i].odp_tmr_pool = ODP_TIMER_POOL_INVALID;
71 }
72 tmrs->ring_tmo_pool = ODP_POOL_INVALID;
73 tmrs->shared_tmo_pool = ODP_POOL_INVALID;
74 tmrs->ring_reserved = 0;
75 tmrs->reserved = 0;
76 tmrs->num_rings = 0;
77 tmrs->num_timers = 0;
78 tmrs->num_ring_create_calls = 0;
79 tmrs->init_check = EM_CHECK_INIT_CALLED;
80 odp_ticketlock_init(&tmrs->timer_lock);
81
82 if (read_config_file() < 0)
83 return EM_ERR_LIB_FAILED;
84
85 return EM_OK;
86}
87
88em_status_t timer_init_local(void)
89{
90 return EM_OK;
91}
92
93em_status_t timer_term_local(void)
94{
95 return EM_OK;
96}
97
98em_status_t timer_term(timer_storage_t *const tmrs)
99{
100#if PRE_1_50_ODP_TIMER_API
101 /* ring_tmo_pool is managed by em-odp only on old ODP API.
102 * New ODP API creates/destroys it as part of periodic timer pool lifecycle.
103 */
104 if (tmrs && tmrs->ring_tmo_pool != ODP_POOL_INVALID) {
105 if (odp_pool_destroy(tmrs->ring_tmo_pool) != 0)
106 return EM_ERR_LIB_FAILED;
107 tmrs->ring_tmo_pool = ODP_POOL_INVALID;
108 }
109#else
110 (void)tmrs;
111#endif
112 return EM_OK;
113}
114
115static int read_config_file(void)
116{
117 const char *conf_str;
118 int val = 0;
119 int ret;
120 bool bval;
121
122 EM_PRINT("EM timer config:\n");
123
124 /*
125 * Option: timer.shared_tmo_pool_enable
126 */
127 conf_str = "timer.shared_tmo_pool_enable";
128 ret = em_libconfig_lookup_bool(&em_shm->libconfig, conf_str, &bval);
129 if (unlikely(!ret)) {
130 EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
131 return -1;
132 }
133 /* store & print the value */
134 em_shm->opt.timer.shared_tmo_pool_enable = bval;
135 EM_PRINT(" %s: %s\n", conf_str, bval ? "true" : "false");
136
137 /*
138 * Option: timer.shared_tmo_pool_size
139 */
140 if (em_shm->opt.timer.shared_tmo_pool_enable) {
141 conf_str = "timer.shared_tmo_pool_size";
142 ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
143 if (unlikely(!ret)) {
144 EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
145 return -1;
146 }
147
148 if (val < 1) {
149 EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
150 return -1;
151 }
152 /* store & print the value */
153 em_shm->opt.timer.shared_tmo_pool_size = val;
154 EM_PRINT(" %s: %d\n", conf_str, val);
155 }
156
157 /*
158 * Option: timer.tmo_pool_cache
159 */
160 conf_str = "timer.tmo_pool_cache";
161 ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
162 if (unlikely(!ret)) {
163 EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
164 return -1;
165 }
166
167 if (val < 0) {
168 EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
169 return -1;
170 }
171 /* store & print the value */
172 em_shm->opt.timer.tmo_pool_cache = val;
173 EM_PRINT(" %s: %d\n", conf_str, val);
174
175 /*
176 * Option: timer.ring.timer_event_pool_size
177 */
178 conf_str = "timer.ring.timer_event_pool_size";
179 ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
180 if (unlikely(!ret)) {
181 EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
182 return -1;
183 }
184
185 if (val < 0) {
186 EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
187 return -1;
188 }
189 /* store & print the value */
190 em_shm->opt.timer.ring.timer_event_pool_size = val;
191 EM_PRINT(" %s: %d\n", conf_str, val);
192
193 /*
194 * Option: timer.ring.timer_event_pool_cache
195 */
196 conf_str = "timer.ring.timer_event_pool_cache";
197 ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
198 if (unlikely(!ret)) {
199 EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
200 return -1;
201 }
202
203 if (val < 0) {
204 EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
205 return -1;
206 }
207 /* store & print the value */
208 em_shm->opt.timer.ring.timer_event_pool_cache = val;
209 EM_PRINT(" %s: %d\n", conf_str, val);
210
211 return 0;
212}
#define EM_CHECK_INIT_CALLED
em_shm_t * em_shm
#define EM_OK
uint32_t em_status_t
@ EM_ERR_LIB_FAILED
em_cfgfile_opts_t opt
Definition em_mem.h:99