first public release
[dpdk.git] / lib / librte_timer / rte_timer.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <sys/queue.h>
40 #include <inttypes.h>
41
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>
50 #include <rte_eal.h>
51 #include <rte_per_lcore.h>
52 #include <rte_lcore.h>
53 #include <rte_branch_prediction.h>
54 #include <rte_spinlock.h>
55
56 #include "rte_timer.h"
57
58 LIST_HEAD(rte_timer_list, rte_timer);
59
60 struct priv_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 */
65
66         /** per-core variable that true if a timer was updated on this
67          *  core since last reset of the variable */
68         int updated;
69
70         unsigned prev_lcore;              /**< used for lcore round robin */
71
72 #ifdef RTE_LIBRTE_TIMER_DEBUG
73         /** per-lcore statistics */
74         struct rte_timer_debug_stats stats;
75 #endif
76 } __rte_cache_aligned;
77
78 /** per-lcore private info for timers */
79 static struct priv_timer priv_timer[RTE_MAX_LCORE];
80
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);       \
86         } while(0)
87 #else
88 #define __TIMER_STAT_ADD(name, n) do {} while(0)
89 #endif
90
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); \
95              (var);                                                    \
96              (var) = (var2),                                           \
97                      (var2) = ((var) ? ((var)->field.le_next) : NULL))
98
99
100 /* Init the timer library. */
101 void
102 rte_timer_subsystem_init(void)
103 {
104         unsigned lcore_id;
105
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;
112         }
113 }
114
115 /* Initialize the timer handle tim for use */
116 void
117 rte_timer_init(struct rte_timer *tim)
118 {
119         union rte_timer_status status;
120
121         status.state = RTE_TIMER_STOP;
122         status.owner = RTE_TIMER_NO_OWNER;
123         tim->status.u32 = status.u32;
124 }
125
126 /*
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
130  */
131 static int
132 timer_set_config_state(struct rte_timer *tim,
133                        union rte_timer_status *ret_prev_status)
134 {
135         union rte_timer_status prev_status, status;
136         int success = 0;
137         unsigned lcore_id;
138
139         lcore_id = rte_lcore_id();
140
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;
145
146                 /* timer is running on another core, exit */
147                 if (prev_status.state == RTE_TIMER_RUNNING &&
148                     (unsigned)prev_status.owner != lcore_id)
149                         return -1;
150
151                 /* timer is beeing configured on another core */
152                 if (prev_status.state == RTE_TIMER_CONFIG)
153                         return -1;
154
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,
160                                               prev_status.u32,
161                                               status.u32);
162         }
163
164         ret_prev_status->u32 = prev_status.u32;
165         return 0;
166 }
167
168 /*
169  * if timer is pending, mark timer as running
170  */
171 static int
172 timer_set_running_state(struct rte_timer *tim)
173 {
174         union rte_timer_status prev_status, status;
175         unsigned lcore_id = rte_lcore_id();
176         int success = 0;
177
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;
182
183                 /* timer is not pending anymore */
184                 if (prev_status.state != RTE_TIMER_PENDING)
185                         return -1;
186
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,
192                                               prev_status.u32,
193                                               status.u32);
194         }
195
196         return 0;
197 }
198
199 /*
200  * add in list, lock if needed
201  * timer must be in config state
202  * timer must not be in a list
203  */
204 static void
205 timer_add(struct rte_timer *tim, unsigned tim_lcore, int local_is_locked)
206 {
207         uint64_t cur_time = rte_get_hpet_cycles();
208         unsigned lcore_id = rte_lcore_id();
209         struct rte_timer *t, *t_prev;
210
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);
216
217         t = LIST_FIRST(&priv_timer[tim_lcore].pending);
218
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);
223         }
224         else {
225                 t_prev = t;
226
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);
232                                 break;
233                         }
234                         t_prev = t;
235                 }
236
237                 /* not found, insert at the end of the list */
238                 if (t == NULL)
239                         LIST_INSERT_AFTER(t_prev, tim, next);
240         }
241
242         if (tim_lcore != lcore_id || !local_is_locked)
243                 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
244 }
245
246 /*
247  * del from list, lock if needed
248  * timer must be in config state
249  * timer must be in a list
250  */
251 static void
252 timer_del(struct rte_timer *tim, unsigned prev_owner, int local_is_locked)
253 {
254         unsigned lcore_id = rte_lcore_id();
255
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);
261
262         LIST_REMOVE(tim, next);
263
264         if (prev_owner != lcore_id || !local_is_locked)
265                 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
266 }
267
268 /* Reset and start the timer associated with the timer handle (private func) */
269 static int
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,
273                   int local_is_locked)
274 {
275         union rte_timer_status prev_status, status;
276         int ret;
277         unsigned lcore_id = rte_lcore_id();
278
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,
282                                                0, 1);
283                 priv_timer[lcore_id].prev_lcore = tim_lcore;
284         }
285
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);
289         if (ret < 0)
290                 return -1;
291
292         __TIMER_STAT_ADD(reset, 1);
293         priv_timer[lcore_id].updated = 1;
294
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);
300         }
301
302         tim->period = period;
303         tim->expire = expire;
304         tim->f = fct;
305         tim->arg = arg;
306
307         __TIMER_STAT_ADD(pending, 1);
308         timer_add(tim, tim_lcore, local_is_locked);
309
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 */
312         rte_wmb();
313         status.state = RTE_TIMER_PENDING;
314         status.owner = (int16_t)tim_lcore;
315         tim->status.u32 = status.u32;
316
317         return 0;
318 }
319
320 /* Reset and start the timer associated with the timer handle tim */
321 int
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)
325 {
326         uint64_t cur_time = rte_get_hpet_cycles();
327         uint64_t period;
328
329         if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
330                         !rte_lcore_is_enabled(tim_lcore)))
331                 return -1;
332
333         if (type == PERIODICAL)
334                 period = ticks;
335         else
336                 period = 0;
337
338         __rte_timer_reset(tim,  cur_time + ticks, period, tim_lcore,
339                           fct, arg, 0);
340
341         return 0;
342 }
343
344 /* loop until rte_timer_reset() succeed */
345 void
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)
349 {
350         while (rte_timer_reset(tim, ticks, type, tim_lcore,
351                                fct, arg) != 0);
352 }
353
354 /* Stop the timer associated with the timer handle tim */
355 int
356 rte_timer_stop(struct rte_timer *tim)
357 {
358         union rte_timer_status prev_status, status;
359         unsigned lcore_id = rte_lcore_id();
360         int ret;
361
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);
365         if (ret < 0)
366                 return -1;
367
368         __TIMER_STAT_ADD(stop, 1);
369         priv_timer[lcore_id].updated = 1;
370
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);
376         }
377
378         /* mark timer as stopped */
379         rte_wmb();
380         status.state = RTE_TIMER_STOP;
381         status.owner = RTE_TIMER_NO_OWNER;
382         tim->status.u32 = status.u32;
383
384         return 0;
385 }
386
387 /* loop until rte_timer_stop() succeed */
388 void
389 rte_timer_stop_sync(struct rte_timer *tim)
390 {
391         while (rte_timer_stop(tim) != 0);
392 }
393
394 /* Test the PENDING status of the timer handle tim */
395 int
396 rte_timer_pending(struct rte_timer *tim)
397 {
398         return tim->status.state == RTE_TIMER_PENDING;
399 }
400
401 /* must be called periodically, run all timer that expired */
402 void rte_timer_manage(void)
403 {
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();
408         int ret;
409
410         __TIMER_STAT_ADD(manage, 1);
411
412         /* browse ordered list, add expired timers in 'expired' list */
413         rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
414
415         LIST_FOREACH_SAFE(tim, tim2, &priv_timer[lcore_id].pending, next) {
416                 if ((int64_t)(cur_time - tim->expire) < 0)
417                         break;
418
419                 LIST_REMOVE(tim, next);
420                 LIST_INSERT_HEAD(&priv_timer[lcore_id].expired, tim, next);
421         }
422
423
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);
427
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);
431
432                 /* this timer was not pending, continue */
433                 if (ret < 0)
434                         continue;
435
436                 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
437
438                 priv_timer[lcore_id].updated = 0;
439
440                 /* execute callback function with list unlocked */
441                 tim->f(tim, tim->arg);
442
443                 rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
444
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)
448                         continue;
449
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;
456                         rte_wmb();
457                         tim->status.u32 = status.u32;
458                 }
459                 else {
460                         /* keep it in done list and mark timer as pending */
461                         status.state = RTE_TIMER_PENDING;
462                         status.owner = (int16_t)lcore_id;
463                         rte_wmb();
464                         tim->status.u32 = status.u32;
465                 }
466         }
467
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) {
471
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,
477                                   tim->arg, 1);
478         }
479
480         /* job finished, unlock the list lock */
481         rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
482 }
483
484 /* dump statistics about timers */
485 void rte_timer_dump_stats(void)
486 {
487 #ifdef RTE_LIBRTE_TIMER_DEBUG
488         struct rte_timer_debug_stats sum;
489         unsigned lcore_id;
490
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;
497         }
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);
503 #else
504         printf("No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");
505 #endif
506 }