EM-ODP  3.7.0
Event Machine on ODP
em_dispatcher.c
1 /*
2  * Copyright (c) 2015, 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 #include "em_include.h"
32 
33 static int read_config_file(void)
34 {
35  const char *conf_str;
36  int val = 0;
37  int64_t val64 = 0;
38  bool val_bool = false;
39  int ret;
40 
41  /*
42  * Option: dispatch.poll_ctrl_interval
43  */
44  conf_str = "dispatch.poll_ctrl_interval";
45  ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
46  if (unlikely(!ret)) {
47  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
48  return -1;
49  }
50 
51  if (val < 0) {
52  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n",
53  conf_str, val);
54  return -1;
55  }
56  /* store & print the value */
57  em_shm->opt.dispatch.poll_ctrl_interval = val;
58  EM_PRINT(" %s: %d\n", conf_str, val);
59 
60  /*
61  * Option: dispatch.poll_ctrl_interval_ns
62  */
63  conf_str = "dispatch.poll_ctrl_interval_ns";
64  ret = em_libconfig_lookup_int64(&em_shm->libconfig, conf_str, &val64);
65  if (unlikely(!ret)) {
66  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
67  return -1;
68  }
69 
70  if (val64 < 0) {
71  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %" PRId64 "'\n",
72  conf_str, val64);
73  return -1;
74  }
75  /* store & print the value */
76  em_shm->opt.dispatch.poll_ctrl_interval_ns = val64;
77  long double sec = (long double)val64 / 1000000000.0;
78 
79  EM_PRINT(" %s: %" PRId64 "ns (%Lfs)\n", conf_str, val64, sec);
80 
81  /* Store ns value as odp_time_t */
82  em_shm->opt.dispatch.poll_ctrl_interval_time = odp_time_global_from_ns(val64);
83 
84  /*
85  * Option: dispatch.poll_drain_interval
86  */
87  conf_str = "dispatch.poll_drain_interval";
88  ret = em_libconfig_lookup_int(&em_shm->libconfig, conf_str, &val);
89  if (unlikely(!ret)) {
90  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
91  return -1;
92  }
93 
94  if (val < 0) {
95  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %d'\n",
96  conf_str, val);
97  return -1;
98  }
99  /* store & print the value */
100  em_shm->opt.dispatch.poll_drain_interval = val;
101  EM_PRINT(" %s: %d\n", conf_str, val);
102 
103  /*
104  * Option: dispatch.poll_drain_interval_ns
105  */
106  conf_str = "dispatch.poll_drain_interval_ns";
107  ret = em_libconfig_lookup_int64(&em_shm->libconfig, conf_str, &val64);
108  if (unlikely(!ret)) {
109  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
110  return -1;
111  }
112 
113  if (val64 < 0) {
114  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %" PRId64 "'\n",
115  conf_str, val64);
116  return -1;
117  }
118  /* store & print the value */
119  em_shm->opt.dispatch.poll_drain_interval_ns = val64;
120  sec = (long double)val64 / 1000000000.0;
121 
122  EM_PRINT(" %s: %" PRId64 "ns (%Lfs)\n", conf_str, val64, sec);
123 
124  /* Store ns value as odp_time_t */
125  em_shm->opt.dispatch.poll_drain_interval_time = odp_time_global_from_ns(val64);
126 
127  /*
128  * Option: dispatch.sched_wait_ns
129  */
130  if (EM_SCHED_WAIT_ENABLE) {
131  conf_str = "dispatch.sched_wait_ns";
132  ret = em_libconfig_lookup_int64(&em_shm->libconfig, conf_str, &val64);
133  if (unlikely(!ret)) {
134  EM_LOG(EM_LOG_ERR, "Config option '%s' not found.\n", conf_str);
135  return -1;
136  }
137 
138  if (val64 < -1) {
139  EM_LOG(EM_LOG_ERR, "Bad config value '%s = %" PRId64 "'\n",
140  conf_str, val64);
141  return -1;
142  }
143 
144  /* store & print the value */
145  em_shm->opt.dispatch.sched_wait_ns = val64;
146 
147  /* Store ns value as odp sched-wait-time */
148  if (val64 == 0) {
149  em_shm->opt.dispatch.sched_wait = ODP_SCHED_NO_WAIT;
150  EM_PRINT(" %s: no-wait (0 ns)\n", conf_str);
151  } else if (val64 == -1) {
152  em_shm->opt.dispatch.sched_wait = ODP_SCHED_WAIT;
153  EM_PRINT(" %s: wait indefinitely (inf. ns)\n", conf_str);
154  } else {
155  em_shm->opt.dispatch.sched_wait = odp_schedule_wait_time(val64);
156  sec = (long double)val64 / 1000000000.0;
157  EM_PRINT(" %s: wait %" PRId64 "ns (%Lfs)\n", conf_str, val64, sec);
158  }
159  } else {
160  em_shm->opt.dispatch.sched_wait_ns = 0;
161  em_shm->opt.dispatch.sched_wait = ODP_SCHED_NO_WAIT;
162  }
163 
164  /*
165  * Option: dispatch.sched_pause
166  */
167  conf_str = "dispatch.sched_pause";
168  ret = em_libconfig_lookup_bool(&em_shm->libconfig, conf_str, &val_bool);
169  if (unlikely(!ret)) {
170  EM_LOG(EM_LOG_ERR, "Config option '%s' not found\n", conf_str);
171  return -1;
172  }
173  /* store & print the value */
174  em_shm->opt.dispatch.sched_pause = val_bool;
175  EM_PRINT(" %s: %s(%d)\n", conf_str, val_bool ? "true" : "false",
176  val_bool);
177 
178  return 0;
179 }
180 
181 em_status_t dispatch_init(void)
182 {
183  if (read_config_file())
184  return EM_ERR_LIB_FAILED;
185 
186  return EM_OK;
187 }
188 
189 em_status_t dispatch_init_local(void)
190 {
191  em_locm_t *const locm = &em_locm;
192  odp_time_t poll_period = em_shm->opt.dispatch.poll_ctrl_interval_time;
193  odp_time_t now = odp_time_global();
194 
195  /*
196  * Initialize values so that the _first_ call to em_dispatch() on each
197  * core will trigger a poll of the unscheduled ctrl queues.
198  */
199  locm->dispatch_cnt = 1;
200  locm->dispatch_last_run = odp_time_diff(now, poll_period); /* wrap OK */
201 
202  /*
203  * Sanity check:
204  * Perform the same calculation as in dispatch_poll_ctrl_queue()
205  * to verify that ctrl queue polling is triggered by the first dispatch.
206  */
207  odp_time_t period = odp_time_diff(now, locm->dispatch_last_run);
208 
209  if (odp_time_cmp(period, poll_period) != 0) /* 0: periods equal */
210  return EM_ERR_TOONEAR;
211 
212  locm->poll_drain_dispatch_cnt = em_shm->opt.dispatch.poll_drain_interval;
213  locm->poll_drain_dispatch_last_run = now;
214 
216 
217  return EM_OK;
218 }
EM_OK
#define EM_OK
Definition: event_machine_types.h:329
IDLE_STATE_ACTIVE
@ IDLE_STATE_ACTIVE
Definition: em_dispatcher_types.h:53
em_locm_t::dispatch_cnt
unsigned int dispatch_cnt
Definition: em_mem.h:212
em_locm
ENV_LOCAL em_locm_t em_locm
em_locm_t::dispatch_last_run
odp_time_t dispatch_last_run
Definition: em_mem.h:214
EM_ERR_LIB_FAILED
@ EM_ERR_LIB_FAILED
Definition: event_machine_hw_types.h:291
EM_ERR_TOONEAR
@ EM_ERR_TOONEAR
Definition: event_machine_hw_types.h:305
em_shm_t::libconfig
libconfig_t libconfig
Definition: em_mem.h:146
em_locm_t::poll_drain_dispatch_last_run
odp_time_t poll_drain_dispatch_last_run
Definition: em_mem.h:219
em_locm_t::idle_state
idle_state_t idle_state
Definition: em_mem.h:193
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
em_include.h
em_locm_t::poll_drain_dispatch_cnt
unsigned int poll_drain_dispatch_cnt
Definition: em_mem.h:217
EM_SCHED_WAIT_ENABLE
#define EM_SCHED_WAIT_ENABLE
Definition: event_machine_config.h:222
em_locm_t
Definition: em_mem.h:188