doc: whitespace changes in licenses
[dpdk.git] / lib / librte_timer / rte_timer.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <sys/queue.h>
38 #include <inttypes.h>
39
40 #include <rte_atomic.h>
41 #include <rte_common.h>
42 #include <rte_cycles.h>
43 #include <rte_per_lcore.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_launch.h>
47 #include <rte_tailq.h>
48 #include <rte_eal.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_spinlock.h>
53
54 #include "rte_timer.h"
55
56 LIST_HEAD(rte_timer_list, rte_timer);
57
58 struct priv_timer {
59         struct rte_timer_list pending;  /**< list of pending timers */
60         struct rte_timer_list expired;  /**< list of expired timers */
61         struct rte_timer_list done;     /**< list of done timers */
62         rte_spinlock_t list_lock;       /**< lock to protect list access */
63
64         /** per-core variable that true if a timer was updated on this
65          *  core since last reset of the variable */
66         int updated;
67
68         unsigned prev_lcore;              /**< used for lcore round robin */
69
70 #ifdef RTE_LIBRTE_TIMER_DEBUG
71         /** per-lcore statistics */
72         struct rte_timer_debug_stats stats;
73 #endif
74 } __rte_cache_aligned;
75
76 /** per-lcore private info for timers */
77 static struct priv_timer priv_timer[RTE_MAX_LCORE];
78
79 /* when debug is enabled, store some statistics */
80 #ifdef RTE_LIBRTE_TIMER_DEBUG
81 #define __TIMER_STAT_ADD(name, n) do {                          \
82                 unsigned __lcore_id = rte_lcore_id();           \
83                 priv_timer[__lcore_id].stats.name += (n);       \
84         } while(0)
85 #else
86 #define __TIMER_STAT_ADD(name, n) do {} while(0)
87 #endif
88
89 /* this macro allow to modify var while browsing the list */
90 #define LIST_FOREACH_SAFE(var, var2, head, field)                      \
91         for ((var) = ((head)->lh_first),                               \
92                      (var2) = ((var) ? ((var)->field.le_next) : NULL); \
93              (var);                                                    \
94              (var) = (var2),                                           \
95                      (var2) = ((var) ? ((var)->field.le_next) : NULL))
96
97
98 /* Init the timer library. */
99 void
100 rte_timer_subsystem_init(void)
101 {
102         unsigned lcore_id;
103
104         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
105                 LIST_INIT(&priv_timer[lcore_id].pending);
106                 LIST_INIT(&priv_timer[lcore_id].expired);
107                 LIST_INIT(&priv_timer[lcore_id].done);
108                 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
109                 priv_timer[lcore_id].prev_lcore = lcore_id;
110         }
111 }
112
113 /* Initialize the timer handle tim for use */
114 void
115 rte_timer_init(struct rte_timer *tim)
116 {
117         union rte_timer_status status;
118
119         status.state = RTE_TIMER_STOP;
120         status.owner = RTE_TIMER_NO_OWNER;
121         tim->status.u32 = status.u32;
122 }
123
124 /*
125  * if timer is pending or stopped (or running on the same core than
126  * us), mark timer as configuring, and on success return the previous
127  * status of the timer
128  */
129 static int
130 timer_set_config_state(struct rte_timer *tim,
131                        union rte_timer_status *ret_prev_status)
132 {
133         union rte_timer_status prev_status, status;
134         int success = 0;
135         unsigned lcore_id;
136
137         lcore_id = rte_lcore_id();
138
139         /* wait that the timer is in correct status before update,
140          * and mark it as beeing configured */
141         while (success == 0) {
142                 prev_status.u32 = tim->status.u32;
143
144                 /* timer is running on another core, exit */
145                 if (prev_status.state == RTE_TIMER_RUNNING &&
146                     (unsigned)prev_status.owner != lcore_id)
147                         return -1;
148
149                 /* timer is beeing configured on another core */
150                 if (prev_status.state == RTE_TIMER_CONFIG)
151                         return -1;
152
153                 /* here, we know that timer is stopped or pending,
154                  * mark it atomically as beeing configured */
155                 status.state = RTE_TIMER_CONFIG;
156                 status.owner = (int16_t)lcore_id;
157                 success = rte_atomic32_cmpset(&tim->status.u32,
158                                               prev_status.u32,
159                                               status.u32);
160         }
161
162         ret_prev_status->u32 = prev_status.u32;
163         return 0;
164 }
165
166 /*
167  * if timer is pending, mark timer as running
168  */
169 static int
170 timer_set_running_state(struct rte_timer *tim)
171 {
172         union rte_timer_status prev_status, status;
173         unsigned lcore_id = rte_lcore_id();
174         int success = 0;
175
176         /* wait that the timer is in correct status before update,
177          * and mark it as running */
178         while (success == 0) {
179                 prev_status.u32 = tim->status.u32;
180
181                 /* timer is not pending anymore */
182                 if (prev_status.state != RTE_TIMER_PENDING)
183                         return -1;
184
185                 /* here, we know that timer is stopped or pending,
186                  * mark it atomically as beeing configured */
187                 status.state = RTE_TIMER_RUNNING;
188                 status.owner = (int16_t)lcore_id;
189                 success = rte_atomic32_cmpset(&tim->status.u32,
190                                               prev_status.u32,
191                                               status.u32);
192         }
193
194         return 0;
195 }
196
197 /*
198  * add in list, lock if needed
199  * timer must be in config state
200  * timer must not be in a list
201  */
202 static void
203 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
204 {
205         uint64_t cur_time = rte_get_timer_cycles();
206         unsigned lcore_id = rte_lcore_id();
207         struct rte_timer *t, *t_prev;
208
209         /* if timer needs to be scheduled on another core, we need to
210          * lock the list; if it is on local core, we need to lock if
211          * we are not called from rte_timer_manage() */
212         if (tim_lcore != lcore_id || !local_is_locked)
213                 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
214
215         t = LIST_FIRST(&priv_timer[tim_lcore].pending);
216
217         /* list is empty or 'tim' will expire before 't' */
218         if (t == NULL || ((int64_t)(tim->expire - cur_time) <
219                           (int64_t)(t->expire - cur_time))) {
220                 LIST_INSERT_HEAD(&priv_timer[tim_lcore].pending, tim, next);
221         }
222         else {
223                 t_prev = t;
224
225                 /* find an element that will expire after 'tim' */
226                 LIST_FOREACH(t, &priv_timer[tim_lcore].pending, next) {
227                         if ((int64_t)(tim->expire - cur_time) <
228                             (int64_t)(t->expire - cur_time)) {
229                                 LIST_INSERT_BEFORE(t, tim, next);
230                                 break;
231                         }
232                         t_prev = t;
233                 }
234
235                 /* not found, insert at the end of the list */
236                 if (t == NULL)
237                         LIST_INSERT_AFTER(t_prev, tim, next);
238         }
239
240         if (tim_lcore != lcore_id || !local_is_locked)
241                 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
242 }
243
244 /*
245  * del from list, lock if needed
246  * timer must be in config state
247  * timer must be in a list
248  */
249 static void
250 timer_del(struct rte_timer *tim, unsigned prev_owner, int local_is_locked)
251 {
252         unsigned lcore_id = rte_lcore_id();
253
254         /* if timer needs is pending another core, we need to lock the
255          * list; if it is on local core, we need to lock if we are not
256          * called from rte_timer_manage() */
257         if (prev_owner != lcore_id || !local_is_locked)
258                 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
259
260         LIST_REMOVE(tim, next);
261
262         if (prev_owner != lcore_id || !local_is_locked)
263                 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
264 }
265
266 /* Reset and start the timer associated with the timer handle (private func) */
267 static int
268 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
269                   uint64_t period, unsigned tim_lcore,
270                   rte_timer_cb_t fct, void *arg,
271                   int local_is_locked)
272 {
273         union rte_timer_status prev_status, status;
274         int ret;
275         unsigned lcore_id = rte_lcore_id();
276
277         /* round robin for tim_lcore */
278         if (tim_lcore == (unsigned)LCORE_ID_ANY) {
279                 tim_lcore = rte_get_next_lcore(priv_timer[lcore_id].prev_lcore,
280                                                0, 1);
281                 priv_timer[lcore_id].prev_lcore = tim_lcore;
282         }
283
284         /* wait that the timer is in correct status before update,
285          * and mark it as beeing configured */
286         ret = timer_set_config_state(tim, &prev_status);
287         if (ret < 0)
288                 return -1;
289
290         __TIMER_STAT_ADD(reset, 1);
291         priv_timer[lcore_id].updated = 1;
292
293         /* remove it from list */
294         if (prev_status.state == RTE_TIMER_PENDING ||
295             prev_status.state == RTE_TIMER_RUNNING) {
296                 timer_del(tim, prev_status.owner, local_is_locked);
297                 __TIMER_STAT_ADD(pending, -1);
298         }
299
300         tim->period = period;
301         tim->expire = expire;
302         tim->f = fct;
303         tim->arg = arg;
304
305         __TIMER_STAT_ADD(pending, 1);
306         timer_add(tim, tim_lcore, local_is_locked);
307
308         /* update state: as we are in CONFIG state, only us can modify
309          * the state so we don't need to use cmpset() here */
310         rte_wmb();
311         status.state = RTE_TIMER_PENDING;
312         status.owner = (int16_t)tim_lcore;
313         tim->status.u32 = status.u32;
314
315         return 0;
316 }
317
318 /* Reset and start the timer associated with the timer handle tim */
319 int
320 rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
321                 enum rte_timer_type type, unsigned tim_lcore,
322                 rte_timer_cb_t fct, void *arg)
323 {
324         uint64_t cur_time = rte_get_timer_cycles();
325         uint64_t period;
326
327         if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
328                         !rte_lcore_is_enabled(tim_lcore)))
329                 return -1;
330
331         if (type == PERIODICAL)
332                 period = ticks;
333         else
334                 period = 0;
335
336         __rte_timer_reset(tim,  cur_time + ticks, period, tim_lcore,
337                           fct, arg, 0);
338
339         return 0;
340 }
341
342 /* loop until rte_timer_reset() succeed */
343 void
344 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
345                      enum rte_timer_type type, unsigned tim_lcore,
346                      rte_timer_cb_t fct, void *arg)
347 {
348         while (rte_timer_reset(tim, ticks, type, tim_lcore,
349                                fct, arg) != 0);
350 }
351
352 /* Stop the timer associated with the timer handle tim */
353 int
354 rte_timer_stop(struct rte_timer *tim)
355 {
356         union rte_timer_status prev_status, status;
357         unsigned lcore_id = rte_lcore_id();
358         int ret;
359
360         /* wait that the timer is in correct status before update,
361          * and mark it as beeing configured */
362         ret = timer_set_config_state(tim, &prev_status);
363         if (ret < 0)
364                 return -1;
365
366         __TIMER_STAT_ADD(stop, 1);
367         priv_timer[lcore_id].updated = 1;
368
369         /* remove it from list */
370         if (prev_status.state == RTE_TIMER_PENDING ||
371             prev_status.state == RTE_TIMER_RUNNING) {
372                 timer_del(tim, prev_status.owner, 0);
373                 __TIMER_STAT_ADD(pending, -1);
374         }
375
376         /* mark timer as stopped */
377         rte_wmb();
378         status.state = RTE_TIMER_STOP;
379         status.owner = RTE_TIMER_NO_OWNER;
380         tim->status.u32 = status.u32;
381
382         return 0;
383 }
384
385 /* loop until rte_timer_stop() succeed */
386 void
387 rte_timer_stop_sync(struct rte_timer *tim)
388 {
389         while (rte_timer_stop(tim) != 0)
390                 rte_pause();
391 }
392
393 /* Test the PENDING status of the timer handle tim */
394 int
395 rte_timer_pending(struct rte_timer *tim)
396 {
397         return tim->status.state == RTE_TIMER_PENDING;
398 }
399
400 /* must be called periodically, run all timer that expired */
401 void rte_timer_manage(void)
402 {
403         union rte_timer_status status;
404         struct rte_timer *tim, *tim2;
405         unsigned lcore_id = rte_lcore_id();
406         uint64_t cur_time;
407         int ret;
408
409         __TIMER_STAT_ADD(manage, 1);
410         /* optimize for the case where per-cpu list is empty */
411         if (LIST_EMPTY(&priv_timer[lcore_id].pending))
412                 return;
413         cur_time = rte_get_timer_cycles();
414
415         /* browse ordered list, add expired timers in 'expired' list */
416         rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
417
418         LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].pending, next) {
419                 if ((int64_t)(cur_time - tim->expire) < 0)
420                         break;
421
422                 LIST_REMOVE(tim, next);
423                 LIST_INSERT_HEAD(&priv_timer[lcore_id].expired, tim, next);
424         }
425
426
427         /* for each timer of 'expired' list, check state and execute callback */
428         while ((tim = LIST_FIRST(&priv_timer[lcore_id].expired)) != NULL) {
429                 ret = timer_set_running_state(tim);
430
431                 /* remove from expired list, and add it in done list */
432                 LIST_REMOVE(tim, next);
433                 LIST_INSERT_HEAD(&priv_timer[lcore_id].done, tim, next);
434
435                 /* this timer was not pending, continue */
436                 if (ret < 0)
437                         continue;
438
439                 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
440
441                 priv_timer[lcore_id].updated = 0;
442
443                 /* execute callback function with list unlocked */
444                 tim->f(tim, tim->arg);
445
446                 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
447
448                 /* the timer was stopped or reloaded by the callback
449                  * function, we have nothing to do here */
450                 if (priv_timer[lcore_id].updated == 1)
451                         continue;
452
453                 if (tim->period == 0) {
454                         /* remove from done list and mark timer as stopped */
455                         LIST_REMOVE(tim, next);
456                         __TIMER_STAT_ADD(pending, -1);
457                         status.state = RTE_TIMER_STOP;
458                         status.owner = RTE_TIMER_NO_OWNER;
459                         rte_wmb();
460                         tim->status.u32 = status.u32;
461                 }
462                 else {
463                         /* keep it in done list and mark timer as pending */
464                         status.state = RTE_TIMER_PENDING;
465                         status.owner = (int16_t)lcore_id;
466                         rte_wmb();
467                         tim->status.u32 = status.u32;
468                 }
469         }
470
471         /* finally, browse done list, some timer may have to be
472          * rescheduled automatically */
473         LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].done, next) {
474
475                 /* reset may fail if timer is beeing modified, in this
476                  * case the timer will remain in 'done' list until the
477                  * core that is modifying it remove it */
478                 __rte_timer_reset(tim, cur_time + tim->period,
479                                   tim->period, lcore_id, tim->f,
480                                   tim->arg, 1);
481         }
482
483         /* job finished, unlock the list lock */
484         rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
485 }
486
487 /* dump statistics about timers */
488 void rte_timer_dump_stats(void)
489 {
490 #ifdef RTE_LIBRTE_TIMER_DEBUG
491         struct rte_timer_debug_stats sum;
492         unsigned lcore_id;
493
494         memset(&sum, 0, sizeof(sum));
495         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
496                 sum.reset += priv_timer[lcore_id].stats.reset;
497                 sum.stop += priv_timer[lcore_id].stats.stop;
498                 sum.manage += priv_timer[lcore_id].stats.manage;
499                 sum.pending += priv_timer[lcore_id].stats.pending;
500         }
501         printf("Timer statistics:\n");
502         printf("  reset = %"PRIu64"\n", sum.reset);
503         printf("  stop = %"PRIu64"\n", sum.stop);
504         printf("  manage = %"PRIu64"\n", sum.manage);
505         printf("  pending = %"PRIu64"\n", sum.pending);
506 #else
507         printf("No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");
508 #endif
509 }