EM-ODP  3.7.0
Event Machine on ODP
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 #include "em_include.h"
31 
32 static int read_config_file(void);
33 
34 em_status_t timer_init(timer_storage_t *const tmrs)
35 {
36  for (int i = 0; i < EM_ODP_MAX_TIMERS; i++) {
37  memset(&tmrs->timer[i], 0, sizeof(event_timer_t));
38  tmrs->timer[i].idx = i; /* for fast reverse lookup */
39  tmrs->timer[i].tmo_pool = ODP_POOL_INVALID;
40  tmrs->timer[i].odp_tmr_pool = ODP_TIMER_POOL_INVALID;
41  }
42  tmrs->ring_tmo_pool = ODP_POOL_INVALID;
43  tmrs->shared_tmo_pool = ODP_POOL_INVALID;
44  tmrs->ring_reserved = 0;
45  tmrs->reserved = 0;
46  tmrs->num_rings = 0;
47  tmrs->num_timers = 0;
48  tmrs->num_ring_create_calls = 0;
49  tmrs->init_check = EM_CHECK_INIT_CALLED;
50  odp_ticketlock_init(&tmrs->timer_lock);
51 
52  if (read_config_file() < 0)
53  return EM_ERR_LIB_FAILED;
54 
55  return EM_OK;
56 }
57 
58 em_status_t timer_init_local(void)
59 {
60  return EM_OK;
61 }
62 
63 em_status_t timer_term_local(void)
64 {
65  return EM_OK;
66 }
67 
68 em_status_t timer_term(timer_storage_t *const tmrs)
69 {
70  if (tmrs && tmrs->ring_tmo_pool != ODP_POOL_INVALID) {
71  if (odp_pool_destroy(tmrs->ring_tmo_pool) != 0)
72  return EM_ERR_LIB_FAILED;
73  tmrs->ring_tmo_pool = ODP_POOL_INVALID;
74  }
75 
76  return EM_OK;
77 }
78 
79 static int read_config_file(void)
80 {
81  const char *conf_str;
82  int val = 0;
83  int ret;
84  bool bval;
85 
86  EM_PRINT("EM-timer config:\n");
87 
88  /*
89  * Option: timer.shared_tmo_pool_enable
90  */
91  conf_str = "timer.shared_tmo_pool_enable";
92  ret = em_libconfig_lookup_bool(&em_shm->libconfig, conf_str, &bval);
93  if (unlikely(!ret)) {
94  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
95  return -1;
96  }
97  /* store & print the value */
98  em_shm->opt.timer.shared_tmo_pool_enable = bval;
99  EM_PRINT(" %s: %s\n", conf_str, bval ? "true" : "false");
100 
101  /*
102  * Option: timer.shared_tmo_pool_size
103  */
104  if (em_shm->opt.timer.shared_tmo_pool_enable) {
105  conf_str = "timer.shared_tmo_pool_size";
106  ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
107  if (unlikely(!ret)) {
108  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
109  return -1;
110  }
111 
112  if (val < 1) {
113  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
114  return -1;
115  }
116  /* store & print the value */
117  em_shm->opt.timer.shared_tmo_pool_size = val;
118  EM_PRINT(" %s: %d\n", conf_str, val);
119  }
120 
121  /*
122  * Option: timer.tmo_pool_cache
123  */
124  conf_str = "timer.tmo_pool_cache";
125  ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
126  if (unlikely(!ret)) {
127  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
128  return -1;
129  }
130 
131  if (val < 0) {
132  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
133  return -1;
134  }
135  /* store & print the value */
136  em_shm->opt.timer.tmo_pool_cache = val;
137  EM_PRINT(" %s: %d\n", conf_str, val);
138 
139  /*
140  * Option: timer.ring.timer_event_pool_size
141  */
142  conf_str = "timer.ring.timer_event_pool_size";
143  ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
144  if (unlikely(!ret)) {
145  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
146  return -1;
147  }
148 
149  if (val < 0) {
150  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n", conf_str, val);
151  return -1;
152  }
153  /* store & print the value */
154  em_shm->opt.timer.ring.timer_event_pool_size = val;
155  EM_PRINT(" %s: %d\n", conf_str, val);
156 
157  /*
158  * Option: timer.ring.timer_event_pool_cache
159  */
160  conf_str = "timer.ring.timer_event_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.ring.timer_event_pool_cache = val;
173  EM_PRINT(" %s: %d\n", conf_str, val);
174 
175  return 0;
176 }
EM_OK
#define EM_OK
Definition: event_machine_types.h:329
EM_ERR_LIB_FAILED
@ EM_ERR_LIB_FAILED
Definition: event_machine_hw_types.h:291
timer_storage_t
Definition: em_timer_types.h:50
em_shm_t::libconfig
libconfig_t libconfig
Definition: em_mem.h:146
em_status_t
uint32_t em_status_t
Definition: event_machine_types.h:321
em_shm
em_shm_t * em_shm
Definition: event_machine_init.c:41
event_timer_t
Definition: em_timer_types.h:38
em_include.h
EM_CHECK_INIT_CALLED
#define EM_CHECK_INIT_CALLED
Definition: em_include.h:69