4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * version: DPDK.L.1.2.3-3
39 #include <sys/queue.h>
42 #include <rte_atomic.h>
43 #include <rte_common.h>
44 #include <rte_cycles.h>
45 #include <rte_per_lcore.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_launch.h>
49 #include <rte_tailq.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_spinlock.h>
56 #include "rte_timer.h"
58 LIST_HEAD(rte_timer_list, rte_timer);
61 struct rte_timer_list pending; /**< list of pending timers */
62 struct rte_timer_list expired; /**< list of expired timers */
63 struct rte_timer_list done; /**< list of done timers */
64 rte_spinlock_t list_lock; /**< lock to protect list access */
66 /** per-core variable that true if a timer was updated on this
67 * core since last reset of the variable */
70 unsigned prev_lcore; /**< used for lcore round robin */
72 #ifdef RTE_LIBRTE_TIMER_DEBUG
73 /** per-lcore statistics */
74 struct rte_timer_debug_stats stats;
76 } __rte_cache_aligned;
78 /** per-lcore private info for timers */
79 static struct priv_timer priv_timer[RTE_MAX_LCORE];
81 /* when debug is enabled, store some statistics */
82 #ifdef RTE_LIBRTE_TIMER_DEBUG
83 #define __TIMER_STAT_ADD(name, n) do { \
84 unsigned __lcore_id = rte_lcore_id(); \
85 priv_timer[__lcore_id].stats.name += (n); \
88 #define __TIMER_STAT_ADD(name, n) do {} while(0)
91 /* this macro allow to modify var while browsing the list */
92 #define LIST_FOREACH_SAFE(var, var2, head, field) \
93 for ((var) = ((head)->lh_first), \
94 (var2) = ((var) ? ((var)->field.le_next) : NULL); \
97 (var2) = ((var) ? ((var)->field.le_next) : NULL))
100 /* Init the timer library. */
102 rte_timer_subsystem_init(void)
106 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
107 LIST_INIT(&priv_timer[lcore_id].pending);
108 LIST_INIT(&priv_timer[lcore_id].expired);
109 LIST_INIT(&priv_timer[lcore_id].done);
110 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
111 priv_timer[lcore_id].prev_lcore = lcore_id;
115 /* Initialize the timer handle tim for use */
117 rte_timer_init(struct rte_timer *tim)
119 union rte_timer_status status;
121 status.state = RTE_TIMER_STOP;
122 status.owner = RTE_TIMER_NO_OWNER;
123 tim->status.u32 = status.u32;
127 * if timer is pending or stopped (or running on the same core than
128 * us), mark timer as configuring, and on success return the previous
129 * status of the timer
132 timer_set_config_state(struct rte_timer *tim,
133 union rte_timer_status *ret_prev_status)
135 union rte_timer_status prev_status, status;
139 lcore_id = rte_lcore_id();
141 /* wait that the timer is in correct status before update,
142 * and mark it as beeing configured */
143 while (success == 0) {
144 prev_status.u32 = tim->status.u32;
146 /* timer is running on another core, exit */
147 if (prev_status.state == RTE_TIMER_RUNNING &&
148 (unsigned)prev_status.owner != lcore_id)
151 /* timer is beeing configured on another core */
152 if (prev_status.state == RTE_TIMER_CONFIG)
155 /* here, we know that timer is stopped or pending,
156 * mark it atomically as beeing configured */
157 status.state = RTE_TIMER_CONFIG;
158 status.owner = (int16_t)lcore_id;
159 success = rte_atomic32_cmpset(&tim->status.u32,
164 ret_prev_status->u32 = prev_status.u32;
169 * if timer is pending, mark timer as running
172 timer_set_running_state(struct rte_timer *tim)
174 union rte_timer_status prev_status, status;
175 unsigned lcore_id = rte_lcore_id();
178 /* wait that the timer is in correct status before update,
179 * and mark it as running */
180 while (success == 0) {
181 prev_status.u32 = tim->status.u32;
183 /* timer is not pending anymore */
184 if (prev_status.state != RTE_TIMER_PENDING)
187 /* here, we know that timer is stopped or pending,
188 * mark it atomically as beeing configured */
189 status.state = RTE_TIMER_RUNNING;
190 status.owner = (int16_t)lcore_id;
191 success = rte_atomic32_cmpset(&tim->status.u32,
200 * add in list, lock if needed
201 * timer must be in config state
202 * timer must not be in a list
205 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
207 uint64_t cur_time = rte_get_hpet_cycles();
208 unsigned lcore_id = rte_lcore_id();
209 struct rte_timer *t, *t_prev;
211 /* if timer needs to be scheduled on another core, we need to
212 * lock the list; if it is on local core, we need to lock if
213 * we are not called from rte_timer_manage() */
214 if (tim_lcore != lcore_id || !local_is_locked)
215 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
217 t = LIST_FIRST(&priv_timer[tim_lcore].pending);
219 /* list is empty or 'tim' will expire before 't' */
220 if (t == NULL || ((int64_t)(tim->expire - cur_time) <
221 (int64_t)(t->expire - cur_time))) {
222 LIST_INSERT_HEAD(&priv_timer[tim_lcore].pending, tim, next);
227 /* find an element that will expire after 'tim' */
228 LIST_FOREACH(t, &priv_timer[tim_lcore].pending, next) {
229 if ((int64_t)(tim->expire - cur_time) <
230 (int64_t)(t->expire - cur_time)) {
231 LIST_INSERT_BEFORE(t, tim, next);
237 /* not found, insert at the end of the list */
239 LIST_INSERT_AFTER(t_prev, tim, next);
242 if (tim_lcore != lcore_id || !local_is_locked)
243 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
247 * del from list, lock if needed
248 * timer must be in config state
249 * timer must be in a list
252 timer_del(struct rte_timer *tim, unsigned prev_owner, int local_is_locked)
254 unsigned lcore_id = rte_lcore_id();
256 /* if timer needs is pending another core, we need to lock the
257 * list; if it is on local core, we need to lock if we are not
258 * called from rte_timer_manage() */
259 if (prev_owner != lcore_id || !local_is_locked)
260 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
262 LIST_REMOVE(tim, next);
264 if (prev_owner != lcore_id || !local_is_locked)
265 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
268 /* Reset and start the timer associated with the timer handle (private func) */
270 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
271 uint64_t period, unsigned tim_lcore,
272 rte_timer_cb_t fct, void *arg,
275 union rte_timer_status prev_status, status;
277 unsigned lcore_id = rte_lcore_id();
279 /* round robin for tim_lcore */
280 if (tim_lcore == (unsigned)LCORE_ID_ANY) {
281 tim_lcore = rte_get_next_lcore(priv_timer[lcore_id].prev_lcore,
283 priv_timer[lcore_id].prev_lcore = tim_lcore;
286 /* wait that the timer is in correct status before update,
287 * and mark it as beeing configured */
288 ret = timer_set_config_state(tim, &prev_status);
292 __TIMER_STAT_ADD(reset, 1);
293 priv_timer[lcore_id].updated = 1;
295 /* remove it from list */
296 if (prev_status.state == RTE_TIMER_PENDING ||
297 prev_status.state == RTE_TIMER_RUNNING) {
298 timer_del(tim, prev_status.owner, local_is_locked);
299 __TIMER_STAT_ADD(pending, -1);
302 tim->period = period;
303 tim->expire = expire;
307 __TIMER_STAT_ADD(pending, 1);
308 timer_add(tim, tim_lcore, local_is_locked);
310 /* update state: as we are in CONFIG state, only us can modify
311 * the state so we don't need to use cmpset() here */
313 status.state = RTE_TIMER_PENDING;
314 status.owner = (int16_t)tim_lcore;
315 tim->status.u32 = status.u32;
320 /* Reset and start the timer associated with the timer handle tim */
322 rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
323 enum rte_timer_type type, unsigned tim_lcore,
324 rte_timer_cb_t fct, void *arg)
326 uint64_t cur_time = rte_get_hpet_cycles();
329 if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
330 !rte_lcore_is_enabled(tim_lcore)))
333 if (type == PERIODICAL)
338 __rte_timer_reset(tim, cur_time + ticks, period, tim_lcore,
344 /* loop until rte_timer_reset() succeed */
346 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
347 enum rte_timer_type type, unsigned tim_lcore,
348 rte_timer_cb_t fct, void *arg)
350 while (rte_timer_reset(tim, ticks, type, tim_lcore,
354 /* Stop the timer associated with the timer handle tim */
356 rte_timer_stop(struct rte_timer *tim)
358 union rte_timer_status prev_status, status;
359 unsigned lcore_id = rte_lcore_id();
362 /* wait that the timer is in correct status before update,
363 * and mark it as beeing configured */
364 ret = timer_set_config_state(tim, &prev_status);
368 __TIMER_STAT_ADD(stop, 1);
369 priv_timer[lcore_id].updated = 1;
371 /* remove it from list */
372 if (prev_status.state == RTE_TIMER_PENDING ||
373 prev_status.state == RTE_TIMER_RUNNING) {
374 timer_del(tim, prev_status.owner, 0);
375 __TIMER_STAT_ADD(pending, -1);
378 /* mark timer as stopped */
380 status.state = RTE_TIMER_STOP;
381 status.owner = RTE_TIMER_NO_OWNER;
382 tim->status.u32 = status.u32;
387 /* loop until rte_timer_stop() succeed */
389 rte_timer_stop_sync(struct rte_timer *tim)
391 while (rte_timer_stop(tim) != 0);
394 /* Test the PENDING status of the timer handle tim */
396 rte_timer_pending(struct rte_timer *tim)
398 return tim->status.state == RTE_TIMER_PENDING;
401 /* must be called periodically, run all timer that expired */
402 void rte_timer_manage(void)
404 union rte_timer_status status;
405 struct rte_timer *tim, *tim2;
406 unsigned lcore_id = rte_lcore_id();
407 uint64_t cur_time = rte_get_hpet_cycles();
410 __TIMER_STAT_ADD(manage, 1);
412 /* browse ordered list, add expired timers in 'expired' list */
413 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
415 LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].pending, next) {
416 if ((int64_t)(cur_time - tim->expire) < 0)
419 LIST_REMOVE(tim, next);
420 LIST_INSERT_HEAD(&priv_timer[lcore_id].expired, tim, next);
424 /* for each timer of 'expired' list, check state and execute callback */
425 while ((tim = LIST_FIRST(&priv_timer[lcore_id].expired)) != NULL) {
426 ret = timer_set_running_state(tim);
428 /* remove from expired list, and add it in done list */
429 LIST_REMOVE(tim, next);
430 LIST_INSERT_HEAD(&priv_timer[lcore_id].done, tim, next);
432 /* this timer was not pending, continue */
436 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
438 priv_timer[lcore_id].updated = 0;
440 /* execute callback function with list unlocked */
441 tim->f(tim, tim->arg);
443 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
445 /* the timer was stopped or reloaded by the callback
446 * function, we have nothing to do here */
447 if (priv_timer[lcore_id].updated == 1)
450 if (tim->period == 0) {
451 /* remove from done list and mark timer as stopped */
452 LIST_REMOVE(tim, next);
453 __TIMER_STAT_ADD(pending, -1);
454 status.state = RTE_TIMER_STOP;
455 status.owner = RTE_TIMER_NO_OWNER;
457 tim->status.u32 = status.u32;
460 /* keep it in done list and mark timer as pending */
461 status.state = RTE_TIMER_PENDING;
462 status.owner = (int16_t)lcore_id;
464 tim->status.u32 = status.u32;
468 /* finally, browse done list, some timer may have to be
469 * rescheduled automatically */
470 LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].done, next) {
472 /* reset may fail if timer is beeing modified, in this
473 * case the timer will remain in 'done' list until the
474 * core that is modifying it remove it */
475 __rte_timer_reset(tim, cur_time + tim->period,
476 tim->period, lcore_id, tim->f,
480 /* job finished, unlock the list lock */
481 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
484 /* dump statistics about timers */
485 void rte_timer_dump_stats(void)
487 #ifdef RTE_LIBRTE_TIMER_DEBUG
488 struct rte_timer_debug_stats sum;
491 memset(&sum, 0, sizeof(sum));
492 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
493 sum.reset += priv_timer[lcore_id].stats.reset;
494 sum.stop += priv_timer[lcore_id].stats.stop;
495 sum.manage += priv_timer[lcore_id].stats.manage;
496 sum.pending += priv_timer[lcore_id].stats.pending;
498 printf("Timer statistics:\n");
499 printf(" reset = %"PRIu64"\n", sum.reset);
500 printf(" stop = %"PRIu64"\n", sum.stop);
501 printf(" manage = %"PRIu64"\n", sum.manage);
502 printf(" pending = %"PRIu64"\n", sum.pending);
504 printf("No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");