4 * Copyright(c) 2010-2014 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.
39 #include <rte_atomic.h>
40 #include <rte_common.h>
41 #include <rte_cycles.h>
42 #include <rte_per_lcore.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_launch.h>
46 #include <rte_tailq.h>
48 #include <rte_per_lcore.h>
49 #include <rte_lcore.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_spinlock.h>
52 #include <rte_random.h>
54 #include "rte_timer.h"
56 LIST_HEAD(rte_timer_list, rte_timer);
59 struct rte_timer pending_head; /**< dummy timer instance to head up list */
60 rte_spinlock_t list_lock; /**< lock to protect list access */
62 /** per-core variable that true if a timer was updated on this
63 * core since last reset of the variable */
66 /** track the current depth of the skiplist */
67 unsigned curr_skiplist_depth;
69 unsigned prev_lcore; /**< used for lcore round robin */
71 #ifdef RTE_LIBRTE_TIMER_DEBUG
72 /** per-lcore statistics */
73 struct rte_timer_debug_stats stats;
75 } __rte_cache_aligned;
77 /** per-lcore private info for timers */
78 static struct priv_timer priv_timer[RTE_MAX_LCORE];
80 /* when debug is enabled, store some statistics */
81 #ifdef RTE_LIBRTE_TIMER_DEBUG
82 #define __TIMER_STAT_ADD(name, n) do { \
83 unsigned __lcore_id = rte_lcore_id(); \
84 priv_timer[__lcore_id].stats.name += (n); \
87 #define __TIMER_STAT_ADD(name, n) do {} while(0)
90 /* Init the timer library. */
92 rte_timer_subsystem_init(void)
96 /* since priv_timer is static, it's zeroed by default, so only init some
99 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
100 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
101 priv_timer[lcore_id].prev_lcore = lcore_id;
105 /* Initialize the timer handle tim for use */
107 rte_timer_init(struct rte_timer *tim)
109 union rte_timer_status status;
111 status.state = RTE_TIMER_STOP;
112 status.owner = RTE_TIMER_NO_OWNER;
113 tim->status.u32 = status.u32;
117 * if timer is pending or stopped (or running on the same core than
118 * us), mark timer as configuring, and on success return the previous
119 * status of the timer
122 timer_set_config_state(struct rte_timer *tim,
123 union rte_timer_status *ret_prev_status)
125 union rte_timer_status prev_status, status;
129 lcore_id = rte_lcore_id();
131 /* wait that the timer is in correct status before update,
132 * and mark it as being configured */
133 while (success == 0) {
134 prev_status.u32 = tim->status.u32;
136 /* timer is running on another core, exit */
137 if (prev_status.state == RTE_TIMER_RUNNING &&
138 (unsigned)prev_status.owner != lcore_id)
141 /* timer is being configured on another core */
142 if (prev_status.state == RTE_TIMER_CONFIG)
145 /* here, we know that timer is stopped or pending,
146 * mark it atomically as being configured */
147 status.state = RTE_TIMER_CONFIG;
148 status.owner = (int16_t)lcore_id;
149 success = rte_atomic32_cmpset(&tim->status.u32,
154 ret_prev_status->u32 = prev_status.u32;
159 * if timer is pending, mark timer as running
162 timer_set_running_state(struct rte_timer *tim)
164 union rte_timer_status prev_status, status;
165 unsigned lcore_id = rte_lcore_id();
168 /* wait that the timer is in correct status before update,
169 * and mark it as running */
170 while (success == 0) {
171 prev_status.u32 = tim->status.u32;
173 /* timer is not pending anymore */
174 if (prev_status.state != RTE_TIMER_PENDING)
177 /* here, we know that timer is stopped or pending,
178 * mark it atomically as beeing configured */
179 status.state = RTE_TIMER_RUNNING;
180 status.owner = (int16_t)lcore_id;
181 success = rte_atomic32_cmpset(&tim->status.u32,
190 * Return a skiplist level for a new entry.
191 * This probabalistically gives a level with p=1/4 that an entry at level n
192 * will also appear at level n+1.
195 timer_get_skiplist_level(unsigned curr_depth)
197 #ifdef RTE_LIBRTE_TIMER_DEBUG
198 static uint32_t i, count = 0;
199 static uint32_t levels[MAX_SKIPLIST_DEPTH] = {0};
202 /* probability value is 1/4, i.e. all at level 0, 1 in 4 is at level 1,
203 * 1 in 16 at level 2, 1 in 64 at level 3, etc. Calculated using lowest
204 * bit position of a (pseudo)random number.
206 uint32_t rand = rte_rand() & (UINT32_MAX - 1);
207 uint32_t level = rand == 0 ? MAX_SKIPLIST_DEPTH : (rte_bsf32(rand)-1) / 2;
209 /* limit the levels used to one above our current level, so we don't,
210 * for instance, have a level 0 and a level 7 without anything between
212 if (level > curr_depth)
214 if (level >= MAX_SKIPLIST_DEPTH)
215 level = MAX_SKIPLIST_DEPTH-1;
216 #ifdef RTE_LIBRTE_TIMER_DEBUG
219 if (count % 10000 == 0)
220 for (i = 0; i < MAX_SKIPLIST_DEPTH; i++)
221 printf("Level %u: %u\n", (unsigned)i, (unsigned)levels[i]);
227 * For a given time value, get the entries at each level which
228 * are <= that time value.
231 timer_get_prev_entries(uint64_t time_val, unsigned tim_lcore,
232 struct rte_timer **prev)
234 unsigned lvl = priv_timer[tim_lcore].curr_skiplist_depth;
235 prev[lvl] = &priv_timer[tim_lcore].pending_head;
238 prev[lvl] = prev[lvl+1];
239 while (prev[lvl]->sl_next[lvl] &&
240 prev[lvl]->sl_next[lvl]->expire <= time_val)
241 prev[lvl] = prev[lvl]->sl_next[lvl];
246 * Given a timer node in the skiplist, find the previous entries for it at
247 * all skiplist levels.
250 timer_get_prev_entries_for_node(struct rte_timer *tim, unsigned tim_lcore,
251 struct rte_timer **prev)
254 /* to get a specific entry in the list, look for just lower than the time
255 * values, and then increment on each level individually if necessary
257 timer_get_prev_entries(tim->expire - 1, tim_lcore, prev);
258 for (i = priv_timer[tim_lcore].curr_skiplist_depth - 1; i >= 0; i--) {
259 while (prev[i]->sl_next[i] != NULL &&
260 prev[i]->sl_next[i] != tim &&
261 prev[i]->sl_next[i]->expire <= tim->expire)
262 prev[i] = prev[i]->sl_next[i];
267 * add in list, lock if needed
268 * timer must be in config state
269 * timer must not be in a list
272 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
274 unsigned lcore_id = rte_lcore_id();
276 struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
278 /* if timer needs to be scheduled on another core, we need to
279 * lock the list; if it is on local core, we need to lock if
280 * we are not called from rte_timer_manage() */
281 if (tim_lcore != lcore_id || !local_is_locked)
282 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
284 /* find where exactly this element goes in the list of elements
286 timer_get_prev_entries(tim->expire, tim_lcore, prev);
288 /* now assign it a new level and add at that level */
289 const unsigned tim_level = timer_get_skiplist_level(
290 priv_timer[tim_lcore].curr_skiplist_depth);
291 if (tim_level == priv_timer[tim_lcore].curr_skiplist_depth)
292 priv_timer[tim_lcore].curr_skiplist_depth++;
296 tim->sl_next[lvl] = prev[lvl]->sl_next[lvl];
297 prev[lvl]->sl_next[lvl] = tim;
300 tim->sl_next[0] = prev[0]->sl_next[0];
301 prev[0]->sl_next[0] = tim;
303 /* save the lowest list entry into the expire field of the dummy hdr
304 * NOTE: this is not atomic on 32-bit*/
305 priv_timer[tim_lcore].pending_head.expire = priv_timer[tim_lcore].\
306 pending_head.sl_next[0]->expire;
308 if (tim_lcore != lcore_id || !local_is_locked)
309 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
313 * del from list, lock if needed
314 * timer must be in config state
315 * timer must be in a list
318 timer_del(struct rte_timer *tim, union rte_timer_status prev_status,
321 unsigned lcore_id = rte_lcore_id();
322 unsigned prev_owner = prev_status.owner;
324 struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
326 /* if timer needs is pending another core, we need to lock the
327 * list; if it is on local core, we need to lock if we are not
328 * called from rte_timer_manage() */
329 if (prev_owner != lcore_id || !local_is_locked)
330 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
332 /* save the lowest list entry into the expire field of the dummy hdr.
333 * NOTE: this is not atomic on 32-bit */
334 if (tim == priv_timer[prev_owner].pending_head.sl_next[0])
335 priv_timer[prev_owner].pending_head.expire =
336 ((tim->sl_next[0] == NULL) ? 0 : tim->sl_next[0]->expire);
338 /* adjust pointers from previous entries to point past this */
339 timer_get_prev_entries_for_node(tim, prev_owner, prev);
340 for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--) {
341 if (prev[i]->sl_next[i] == tim)
342 prev[i]->sl_next[i] = tim->sl_next[i];
345 /* in case we deleted last entry at a level, adjust down max level */
346 for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--)
347 if (priv_timer[prev_owner].pending_head.sl_next[i] == NULL)
348 priv_timer[prev_owner].curr_skiplist_depth --;
352 if (prev_owner != lcore_id || !local_is_locked)
353 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
356 /* Reset and start the timer associated with the timer handle (private func) */
358 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
359 uint64_t period, unsigned tim_lcore,
360 rte_timer_cb_t fct, void *arg,
363 union rte_timer_status prev_status, status;
365 unsigned lcore_id = rte_lcore_id();
367 /* round robin for tim_lcore */
368 if (tim_lcore == (unsigned)LCORE_ID_ANY) {
369 tim_lcore = rte_get_next_lcore(priv_timer[lcore_id].prev_lcore,
371 priv_timer[lcore_id].prev_lcore = tim_lcore;
374 /* wait that the timer is in correct status before update,
375 * and mark it as being configured */
376 ret = timer_set_config_state(tim, &prev_status);
380 __TIMER_STAT_ADD(reset, 1);
381 if (prev_status.state == RTE_TIMER_RUNNING) {
382 priv_timer[lcore_id].updated = 1;
385 /* remove it from list */
386 if (prev_status.state == RTE_TIMER_PENDING) {
387 timer_del(tim, prev_status, local_is_locked);
388 __TIMER_STAT_ADD(pending, -1);
391 tim->period = period;
392 tim->expire = expire;
396 __TIMER_STAT_ADD(pending, 1);
397 timer_add(tim, tim_lcore, local_is_locked);
399 /* update state: as we are in CONFIG state, only us can modify
400 * the state so we don't need to use cmpset() here */
402 status.state = RTE_TIMER_PENDING;
403 status.owner = (int16_t)tim_lcore;
404 tim->status.u32 = status.u32;
409 /* Reset and start the timer associated with the timer handle tim */
411 rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
412 enum rte_timer_type type, unsigned tim_lcore,
413 rte_timer_cb_t fct, void *arg)
415 uint64_t cur_time = rte_get_timer_cycles();
418 if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
419 !rte_lcore_is_enabled(tim_lcore)))
422 if (type == PERIODICAL)
427 __rte_timer_reset(tim, cur_time + ticks, period, tim_lcore,
433 /* loop until rte_timer_reset() succeed */
435 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
436 enum rte_timer_type type, unsigned tim_lcore,
437 rte_timer_cb_t fct, void *arg)
439 while (rte_timer_reset(tim, ticks, type, tim_lcore,
443 /* Stop the timer associated with the timer handle tim */
445 rte_timer_stop(struct rte_timer *tim)
447 union rte_timer_status prev_status, status;
448 unsigned lcore_id = rte_lcore_id();
451 /* wait that the timer is in correct status before update,
452 * and mark it as being configured */
453 ret = timer_set_config_state(tim, &prev_status);
457 __TIMER_STAT_ADD(stop, 1);
458 if (prev_status.state == RTE_TIMER_RUNNING) {
459 priv_timer[lcore_id].updated = 1;
462 /* remove it from list */
463 if (prev_status.state == RTE_TIMER_PENDING) {
464 timer_del(tim, prev_status, 0);
465 __TIMER_STAT_ADD(pending, -1);
468 /* mark timer as stopped */
470 status.state = RTE_TIMER_STOP;
471 status.owner = RTE_TIMER_NO_OWNER;
472 tim->status.u32 = status.u32;
477 /* loop until rte_timer_stop() succeed */
479 rte_timer_stop_sync(struct rte_timer *tim)
481 while (rte_timer_stop(tim) != 0)
485 /* Test the PENDING status of the timer handle tim */
487 rte_timer_pending(struct rte_timer *tim)
489 return tim->status.state == RTE_TIMER_PENDING;
492 /* must be called periodically, run all timer that expired */
493 void rte_timer_manage(void)
495 union rte_timer_status status;
496 struct rte_timer *tim, *next_tim;
497 unsigned lcore_id = rte_lcore_id();
498 struct rte_timer *prev[MAX_SKIPLIST_DEPTH + 1];
502 __TIMER_STAT_ADD(manage, 1);
503 /* optimize for the case where per-cpu list is empty */
504 if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL)
506 cur_time = rte_get_timer_cycles();
508 #ifdef RTE_ARCH_X86_64
509 /* on 64-bit the value cached in the pending_head.expired will be updated
510 * atomically, so we can consult that for a quick check here outside the
512 if (likely(priv_timer[lcore_id].pending_head.expire > cur_time))
516 /* browse ordered list, add expired timers in 'expired' list */
517 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
519 /* if nothing to do just unlock and return */
520 if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL ||
521 priv_timer[lcore_id].pending_head.sl_next[0]->expire > cur_time)
524 /* save start of list of expired timers */
525 tim = priv_timer[lcore_id].pending_head.sl_next[0];
527 /* break the existing list at current time point */
528 timer_get_prev_entries(cur_time, lcore_id, prev);
529 for (i = priv_timer[lcore_id].curr_skiplist_depth -1; i >= 0; i--) {
530 priv_timer[lcore_id].pending_head.sl_next[i] = prev[i]->sl_next[i];
531 if (prev[i]->sl_next[i] == NULL)
532 priv_timer[lcore_id].curr_skiplist_depth--;
533 prev[i] ->sl_next[i] = NULL;
536 /* now scan expired list and call callbacks */
537 for ( ; tim != NULL; tim = next_tim) {
538 next_tim = tim->sl_next[0];
540 ret = timer_set_running_state(tim);
542 /* this timer was not pending, continue */
546 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
548 priv_timer[lcore_id].updated = 0;
550 /* execute callback function with list unlocked */
551 tim->f(tim, tim->arg);
553 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
554 __TIMER_STAT_ADD(pending, -1);
555 /* the timer was stopped or reloaded by the callback
556 * function, we have nothing to do here */
557 if (priv_timer[lcore_id].updated == 1)
560 if (tim->period == 0) {
561 /* remove from done list and mark timer as stopped */
562 status.state = RTE_TIMER_STOP;
563 status.owner = RTE_TIMER_NO_OWNER;
565 tim->status.u32 = status.u32;
568 /* keep it in list and mark timer as pending */
569 status.state = RTE_TIMER_PENDING;
570 __TIMER_STAT_ADD(pending, 1);
571 status.owner = (int16_t)lcore_id;
573 tim->status.u32 = status.u32;
574 __rte_timer_reset(tim, cur_time + tim->period,
575 tim->period, lcore_id, tim->f, tim->arg, 1);
579 /* update the next to expire timer value */
580 priv_timer[lcore_id].pending_head.expire =
581 (priv_timer[lcore_id].pending_head.sl_next[0] == NULL) ? 0 :
582 priv_timer[lcore_id].pending_head.sl_next[0]->expire;
584 /* job finished, unlock the list lock */
585 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
588 /* dump statistics about timers */
589 void rte_timer_dump_stats(FILE *f)
591 #ifdef RTE_LIBRTE_TIMER_DEBUG
592 struct rte_timer_debug_stats sum;
595 memset(&sum, 0, sizeof(sum));
596 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
597 sum.reset += priv_timer[lcore_id].stats.reset;
598 sum.stop += priv_timer[lcore_id].stats.stop;
599 sum.manage += priv_timer[lcore_id].stats.manage;
600 sum.pending += priv_timer[lcore_id].stats.pending;
602 fprintf(f, "Timer statistics:\n");
603 fprintf(f, " reset = %"PRIu64"\n", sum.reset);
604 fprintf(f, " stop = %"PRIu64"\n", sum.stop);
605 fprintf(f, " manage = %"PRIu64"\n", sum.manage);
606 fprintf(f, " pending = %"PRIu64"\n", sum.pending);
608 fprintf(f, "No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");