9f2e921a8ae489b2ffa8830f55d15d88130bf98b
[dpdk.git] / lib / librte_timer / rte_timer.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <inttypes.h>
10 #include <assert.h>
11 #include <sys/queue.h>
12
13 #include <rte_atomic.h>
14 #include <rte_common.h>
15 #include <rte_cycles.h>
16 #include <rte_per_lcore.h>
17 #include <rte_memory.h>
18 #include <rte_launch.h>
19 #include <rte_eal.h>
20 #include <rte_lcore.h>
21 #include <rte_branch_prediction.h>
22 #include <rte_spinlock.h>
23 #include <rte_random.h>
24 #include <rte_pause.h>
25 #include <rte_memzone.h>
26 #include <rte_malloc.h>
27 #include <rte_compat.h>
28
29 #include "rte_timer.h"
30
31 /**
32  * Per-lcore info for timers.
33  */
34 struct priv_timer {
35         struct rte_timer pending_head;  /**< dummy timer instance to head up list */
36         rte_spinlock_t list_lock;       /**< lock to protect list access */
37
38         /** per-core variable that true if a timer was updated on this
39          *  core since last reset of the variable */
40         int updated;
41
42         /** track the current depth of the skiplist */
43         unsigned curr_skiplist_depth;
44
45         unsigned prev_lcore;              /**< used for lcore round robin */
46
47         /** running timer on this lcore now */
48         struct rte_timer *running_tim;
49
50 #ifdef RTE_LIBRTE_TIMER_DEBUG
51         /** per-lcore statistics */
52         struct rte_timer_debug_stats stats;
53 #endif
54 } __rte_cache_aligned;
55
56 #define FL_ALLOCATED    (1 << 0)
57 struct rte_timer_data {
58         struct priv_timer priv_timer[RTE_MAX_LCORE];
59         uint8_t internal_flags;
60 };
61
62 #define RTE_MAX_DATA_ELS 64
63 static struct rte_timer_data *rte_timer_data_arr;
64 static const uint32_t default_data_id;
65 static uint32_t rte_timer_subsystem_initialized;
66
67 /* For maintaining older interfaces for a period */
68 static struct rte_timer_data default_timer_data;
69
70 /* when debug is enabled, store some statistics */
71 #ifdef RTE_LIBRTE_TIMER_DEBUG
72 #define __TIMER_STAT_ADD(priv_timer, name, n) do {                      \
73                 unsigned __lcore_id = rte_lcore_id();                   \
74                 if (__lcore_id < RTE_MAX_LCORE)                         \
75                         priv_timer[__lcore_id].stats.name += (n);       \
76         } while(0)
77 #else
78 #define __TIMER_STAT_ADD(priv_timer, name, n) do {} while (0)
79 #endif
80
81 static inline int
82 timer_data_valid(uint32_t id)
83 {
84         return !!(rte_timer_data_arr[id].internal_flags & FL_ALLOCATED);
85 }
86
87 /* validate ID and retrieve timer data pointer, or return error value */
88 #define TIMER_DATA_VALID_GET_OR_ERR_RET(id, timer_data, retval) do {    \
89         if (id >= RTE_MAX_DATA_ELS || !timer_data_valid(id))            \
90                 return retval;                                          \
91         timer_data = &rte_timer_data_arr[id];                           \
92 } while (0)
93
94 int __rte_experimental
95 rte_timer_data_alloc(uint32_t *id_ptr)
96 {
97         int i;
98         struct rte_timer_data *data;
99
100         if (!rte_timer_subsystem_initialized)
101                 return -ENOMEM;
102
103         for (i = 0; i < RTE_MAX_DATA_ELS; i++) {
104                 data = &rte_timer_data_arr[i];
105                 if (!(data->internal_flags & FL_ALLOCATED)) {
106                         data->internal_flags |= FL_ALLOCATED;
107
108                         if (id_ptr)
109                                 *id_ptr = i;
110
111                         return 0;
112                 }
113         }
114
115         return -ENOSPC;
116 }
117
118 int __rte_experimental
119 rte_timer_data_dealloc(uint32_t id)
120 {
121         struct rte_timer_data *timer_data;
122         TIMER_DATA_VALID_GET_OR_ERR_RET(id, timer_data, -EINVAL);
123
124         timer_data->internal_flags &= ~(FL_ALLOCATED);
125
126         return 0;
127 }
128
129 void
130 rte_timer_subsystem_init_v20(void)
131 {
132         unsigned lcore_id;
133         struct priv_timer *priv_timer = default_timer_data.priv_timer;
134
135         /* since priv_timer is static, it's zeroed by default, so only init some
136          * fields.
137          */
138         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++) {
139                 rte_spinlock_init(&priv_timer[lcore_id].list_lock);
140                 priv_timer[lcore_id].prev_lcore = lcore_id;
141         }
142 }
143 VERSION_SYMBOL(rte_timer_subsystem_init, _v20, 2.0);
144
145 /* Init the timer library. Allocate an array of timer data structs in shared
146  * memory, and allocate the zeroth entry for use with original timer
147  * APIs. Since the intersection of the sets of lcore ids in primary and
148  * secondary processes should be empty, the zeroth entry can be shared by
149  * multiple processes.
150  */
151 int
152 rte_timer_subsystem_init_v1905(void)
153 {
154         const struct rte_memzone *mz;
155         struct rte_timer_data *data;
156         int i, lcore_id;
157         static const char *mz_name = "rte_timer_mz";
158
159         if (rte_timer_subsystem_initialized)
160                 return -EALREADY;
161
162         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
163                 mz = rte_memzone_lookup(mz_name);
164                 if (mz == NULL)
165                         return -EEXIST;
166
167                 rte_timer_data_arr = mz->addr;
168
169                 rte_timer_data_arr[default_data_id].internal_flags |=
170                         FL_ALLOCATED;
171
172                 rte_timer_subsystem_initialized = 1;
173
174                 return 0;
175         }
176
177         mz = rte_memzone_reserve_aligned(mz_name,
178                         RTE_MAX_DATA_ELS * sizeof(*rte_timer_data_arr),
179                         SOCKET_ID_ANY, 0, RTE_CACHE_LINE_SIZE);
180         if (mz == NULL)
181                 return -ENOMEM;
182
183         rte_timer_data_arr = mz->addr;
184
185         for (i = 0; i < RTE_MAX_DATA_ELS; i++) {
186                 data = &rte_timer_data_arr[i];
187
188                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
189                         rte_spinlock_init(
190                                 &data->priv_timer[lcore_id].list_lock);
191                         data->priv_timer[lcore_id].prev_lcore = lcore_id;
192                 }
193         }
194
195         rte_timer_data_arr[default_data_id].internal_flags |= FL_ALLOCATED;
196
197         rte_timer_subsystem_initialized = 1;
198
199         return 0;
200 }
201 MAP_STATIC_SYMBOL(int rte_timer_subsystem_init(void),
202                   rte_timer_subsystem_init_v1905);
203 BIND_DEFAULT_SYMBOL(rte_timer_subsystem_init, _v1905, 19.05);
204
205 void __rte_experimental
206 rte_timer_subsystem_finalize(void)
207 {
208         if (rte_timer_data_arr)
209                 rte_free(rte_timer_data_arr);
210
211         rte_timer_subsystem_initialized = 0;
212 }
213
214 /* Initialize the timer handle tim for use */
215 void
216 rte_timer_init(struct rte_timer *tim)
217 {
218         union rte_timer_status status;
219
220         status.state = RTE_TIMER_STOP;
221         status.owner = RTE_TIMER_NO_OWNER;
222         tim->status.u32 = status.u32;
223 }
224
225 /*
226  * if timer is pending or stopped (or running on the same core than
227  * us), mark timer as configuring, and on success return the previous
228  * status of the timer
229  */
230 static int
231 timer_set_config_state(struct rte_timer *tim,
232                        union rte_timer_status *ret_prev_status,
233                        struct priv_timer *priv_timer)
234 {
235         union rte_timer_status prev_status, status;
236         int success = 0;
237         unsigned lcore_id;
238
239         lcore_id = rte_lcore_id();
240
241         /* wait that the timer is in correct status before update,
242          * and mark it as being configured */
243         while (success == 0) {
244                 prev_status.u32 = tim->status.u32;
245
246                 /* timer is running on another core
247                  * or ready to run on local core, exit
248                  */
249                 if (prev_status.state == RTE_TIMER_RUNNING &&
250                     (prev_status.owner != (uint16_t)lcore_id ||
251                      tim != priv_timer[lcore_id].running_tim))
252                         return -1;
253
254                 /* timer is being configured on another core */
255                 if (prev_status.state == RTE_TIMER_CONFIG)
256                         return -1;
257
258                 /* here, we know that timer is stopped or pending,
259                  * mark it atomically as being configured */
260                 status.state = RTE_TIMER_CONFIG;
261                 status.owner = (int16_t)lcore_id;
262                 success = rte_atomic32_cmpset(&tim->status.u32,
263                                               prev_status.u32,
264                                               status.u32);
265         }
266
267         ret_prev_status->u32 = prev_status.u32;
268         return 0;
269 }
270
271 /*
272  * if timer is pending, mark timer as running
273  */
274 static int
275 timer_set_running_state(struct rte_timer *tim)
276 {
277         union rte_timer_status prev_status, status;
278         unsigned lcore_id = rte_lcore_id();
279         int success = 0;
280
281         /* wait that the timer is in correct status before update,
282          * and mark it as running */
283         while (success == 0) {
284                 prev_status.u32 = tim->status.u32;
285
286                 /* timer is not pending anymore */
287                 if (prev_status.state != RTE_TIMER_PENDING)
288                         return -1;
289
290                 /* here, we know that timer is stopped or pending,
291                  * mark it atomically as being configured */
292                 status.state = RTE_TIMER_RUNNING;
293                 status.owner = (int16_t)lcore_id;
294                 success = rte_atomic32_cmpset(&tim->status.u32,
295                                               prev_status.u32,
296                                               status.u32);
297         }
298
299         return 0;
300 }
301
302 /*
303  * Return a skiplist level for a new entry.
304  * This probabilistically gives a level with p=1/4 that an entry at level n
305  * will also appear at level n+1.
306  */
307 static uint32_t
308 timer_get_skiplist_level(unsigned curr_depth)
309 {
310 #ifdef RTE_LIBRTE_TIMER_DEBUG
311         static uint32_t i, count = 0;
312         static uint32_t levels[MAX_SKIPLIST_DEPTH] = {0};
313 #endif
314
315         /* probability value is 1/4, i.e. all at level 0, 1 in 4 is at level 1,
316          * 1 in 16 at level 2, 1 in 64 at level 3, etc. Calculated using lowest
317          * bit position of a (pseudo)random number.
318          */
319         uint32_t rand = rte_rand() & (UINT32_MAX - 1);
320         uint32_t level = rand == 0 ? MAX_SKIPLIST_DEPTH : (rte_bsf32(rand)-1) / 2;
321
322         /* limit the levels used to one above our current level, so we don't,
323          * for instance, have a level 0 and a level 7 without anything between
324          */
325         if (level > curr_depth)
326                 level = curr_depth;
327         if (level >= MAX_SKIPLIST_DEPTH)
328                 level = MAX_SKIPLIST_DEPTH-1;
329 #ifdef RTE_LIBRTE_TIMER_DEBUG
330         count ++;
331         levels[level]++;
332         if (count % 10000 == 0)
333                 for (i = 0; i < MAX_SKIPLIST_DEPTH; i++)
334                         printf("Level %u: %u\n", (unsigned)i, (unsigned)levels[i]);
335 #endif
336         return level;
337 }
338
339 /*
340  * For a given time value, get the entries at each level which
341  * are <= that time value.
342  */
343 static void
344 timer_get_prev_entries(uint64_t time_val, unsigned tim_lcore,
345                        struct rte_timer **prev, struct priv_timer *priv_timer)
346 {
347         unsigned lvl = priv_timer[tim_lcore].curr_skiplist_depth;
348         prev[lvl] = &priv_timer[tim_lcore].pending_head;
349         while(lvl != 0) {
350                 lvl--;
351                 prev[lvl] = prev[lvl+1];
352                 while (prev[lvl]->sl_next[lvl] &&
353                                 prev[lvl]->sl_next[lvl]->expire <= time_val)
354                         prev[lvl] = prev[lvl]->sl_next[lvl];
355         }
356 }
357
358 /*
359  * Given a timer node in the skiplist, find the previous entries for it at
360  * all skiplist levels.
361  */
362 static void
363 timer_get_prev_entries_for_node(struct rte_timer *tim, unsigned tim_lcore,
364                                 struct rte_timer **prev,
365                                 struct priv_timer *priv_timer)
366 {
367         int i;
368
369         /* to get a specific entry in the list, look for just lower than the time
370          * values, and then increment on each level individually if necessary
371          */
372         timer_get_prev_entries(tim->expire - 1, tim_lcore, prev, priv_timer);
373         for (i = priv_timer[tim_lcore].curr_skiplist_depth - 1; i >= 0; i--) {
374                 while (prev[i]->sl_next[i] != NULL &&
375                                 prev[i]->sl_next[i] != tim &&
376                                 prev[i]->sl_next[i]->expire <= tim->expire)
377                         prev[i] = prev[i]->sl_next[i];
378         }
379 }
380
381 /* call with lock held as necessary
382  * add in list
383  * timer must be in config state
384  * timer must not be in a list
385  */
386 static void
387 timer_add(struct rte_timer *tim, unsigned int tim_lcore,
388           struct priv_timer *priv_timer)
389 {
390         unsigned lvl;
391         struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
392
393         /* find where exactly this element goes in the list of elements
394          * for each depth. */
395         timer_get_prev_entries(tim->expire, tim_lcore, prev, priv_timer);
396
397         /* now assign it a new level and add at that level */
398         const unsigned tim_level = timer_get_skiplist_level(
399                         priv_timer[tim_lcore].curr_skiplist_depth);
400         if (tim_level == priv_timer[tim_lcore].curr_skiplist_depth)
401                 priv_timer[tim_lcore].curr_skiplist_depth++;
402
403         lvl = tim_level;
404         while (lvl > 0) {
405                 tim->sl_next[lvl] = prev[lvl]->sl_next[lvl];
406                 prev[lvl]->sl_next[lvl] = tim;
407                 lvl--;
408         }
409         tim->sl_next[0] = prev[0]->sl_next[0];
410         prev[0]->sl_next[0] = tim;
411
412         /* save the lowest list entry into the expire field of the dummy hdr
413          * NOTE: this is not atomic on 32-bit*/
414         priv_timer[tim_lcore].pending_head.expire = priv_timer[tim_lcore].\
415                         pending_head.sl_next[0]->expire;
416 }
417
418 /*
419  * del from list, lock if needed
420  * timer must be in config state
421  * timer must be in a list
422  */
423 static void
424 timer_del(struct rte_timer *tim, union rte_timer_status prev_status,
425           int local_is_locked, struct priv_timer *priv_timer)
426 {
427         unsigned lcore_id = rte_lcore_id();
428         unsigned prev_owner = prev_status.owner;
429         int i;
430         struct rte_timer *prev[MAX_SKIPLIST_DEPTH+1];
431
432         /* if timer needs is pending another core, we need to lock the
433          * list; if it is on local core, we need to lock if we are not
434          * called from rte_timer_manage() */
435         if (prev_owner != lcore_id || !local_is_locked)
436                 rte_spinlock_lock(&priv_timer[prev_owner].list_lock);
437
438         /* save the lowest list entry into the expire field of the dummy hdr.
439          * NOTE: this is not atomic on 32-bit */
440         if (tim == priv_timer[prev_owner].pending_head.sl_next[0])
441                 priv_timer[prev_owner].pending_head.expire =
442                                 ((tim->sl_next[0] == NULL) ? 0 : tim->sl_next[0]->expire);
443
444         /* adjust pointers from previous entries to point past this */
445         timer_get_prev_entries_for_node(tim, prev_owner, prev, priv_timer);
446         for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--) {
447                 if (prev[i]->sl_next[i] == tim)
448                         prev[i]->sl_next[i] = tim->sl_next[i];
449         }
450
451         /* in case we deleted last entry at a level, adjust down max level */
452         for (i = priv_timer[prev_owner].curr_skiplist_depth - 1; i >= 0; i--)
453                 if (priv_timer[prev_owner].pending_head.sl_next[i] == NULL)
454                         priv_timer[prev_owner].curr_skiplist_depth --;
455                 else
456                         break;
457
458         if (prev_owner != lcore_id || !local_is_locked)
459                 rte_spinlock_unlock(&priv_timer[prev_owner].list_lock);
460 }
461
462 /* Reset and start the timer associated with the timer handle (private func) */
463 static int
464 __rte_timer_reset(struct rte_timer *tim, uint64_t expire,
465                   uint64_t period, unsigned tim_lcore,
466                   rte_timer_cb_t fct, void *arg,
467                   int local_is_locked,
468                   struct rte_timer_data *timer_data)
469 {
470         union rte_timer_status prev_status, status;
471         int ret;
472         unsigned lcore_id = rte_lcore_id();
473         struct priv_timer *priv_timer = timer_data->priv_timer;
474
475         /* round robin for tim_lcore */
476         if (tim_lcore == (unsigned)LCORE_ID_ANY) {
477                 if (lcore_id < RTE_MAX_LCORE) {
478                         /* EAL thread with valid lcore_id */
479                         tim_lcore = rte_get_next_lcore(
480                                 priv_timer[lcore_id].prev_lcore,
481                                 0, 1);
482                         priv_timer[lcore_id].prev_lcore = tim_lcore;
483                 } else
484                         /* non-EAL thread do not run rte_timer_manage(),
485                          * so schedule the timer on the first enabled lcore. */
486                         tim_lcore = rte_get_next_lcore(LCORE_ID_ANY, 0, 1);
487         }
488
489         /* wait that the timer is in correct status before update,
490          * and mark it as being configured */
491         ret = timer_set_config_state(tim, &prev_status, priv_timer);
492         if (ret < 0)
493                 return -1;
494
495         __TIMER_STAT_ADD(priv_timer, reset, 1);
496         if (prev_status.state == RTE_TIMER_RUNNING &&
497             lcore_id < RTE_MAX_LCORE) {
498                 priv_timer[lcore_id].updated = 1;
499         }
500
501         /* remove it from list */
502         if (prev_status.state == RTE_TIMER_PENDING) {
503                 timer_del(tim, prev_status, local_is_locked, priv_timer);
504                 __TIMER_STAT_ADD(priv_timer, pending, -1);
505         }
506
507         tim->period = period;
508         tim->expire = expire;
509         tim->f = fct;
510         tim->arg = arg;
511
512         /* if timer needs to be scheduled on another core, we need to
513          * lock the destination list; if it is on local core, we need to lock if
514          * we are not called from rte_timer_manage()
515          */
516         if (tim_lcore != lcore_id || !local_is_locked)
517                 rte_spinlock_lock(&priv_timer[tim_lcore].list_lock);
518
519         __TIMER_STAT_ADD(priv_timer, pending, 1);
520         timer_add(tim, tim_lcore, priv_timer);
521
522         /* update state: as we are in CONFIG state, only us can modify
523          * the state so we don't need to use cmpset() here */
524         rte_wmb();
525         status.state = RTE_TIMER_PENDING;
526         status.owner = (int16_t)tim_lcore;
527         tim->status.u32 = status.u32;
528
529         if (tim_lcore != lcore_id || !local_is_locked)
530                 rte_spinlock_unlock(&priv_timer[tim_lcore].list_lock);
531
532         return 0;
533 }
534
535 /* Reset and start the timer associated with the timer handle tim */
536 int
537 rte_timer_reset_v20(struct rte_timer *tim, uint64_t ticks,
538                     enum rte_timer_type type, unsigned int tim_lcore,
539                     rte_timer_cb_t fct, void *arg)
540 {
541         uint64_t cur_time = rte_get_timer_cycles();
542         uint64_t period;
543
544         if (unlikely((tim_lcore != (unsigned)LCORE_ID_ANY) &&
545                         !(rte_lcore_is_enabled(tim_lcore) ||
546                           rte_lcore_has_role(tim_lcore, ROLE_SERVICE))))
547                 return -1;
548
549         if (type == PERIODICAL)
550                 period = ticks;
551         else
552                 period = 0;
553
554         return __rte_timer_reset(tim,  cur_time + ticks, period, tim_lcore,
555                           fct, arg, 0, &default_timer_data);
556 }
557 VERSION_SYMBOL(rte_timer_reset, _v20, 2.0);
558
559 int
560 rte_timer_reset_v1905(struct rte_timer *tim, uint64_t ticks,
561                       enum rte_timer_type type, unsigned int tim_lcore,
562                       rte_timer_cb_t fct, void *arg)
563 {
564         return rte_timer_alt_reset(default_data_id, tim, ticks, type,
565                                    tim_lcore, fct, arg);
566 }
567 MAP_STATIC_SYMBOL(int rte_timer_reset(struct rte_timer *tim, uint64_t ticks,
568                                       enum rte_timer_type type,
569                                       unsigned int tim_lcore,
570                                       rte_timer_cb_t fct, void *arg),
571                   rte_timer_reset_v1905);
572 BIND_DEFAULT_SYMBOL(rte_timer_reset, _v1905, 19.05);
573
574 int __rte_experimental
575 rte_timer_alt_reset(uint32_t timer_data_id, struct rte_timer *tim,
576                     uint64_t ticks, enum rte_timer_type type,
577                     unsigned int tim_lcore, rte_timer_cb_t fct, void *arg)
578 {
579         uint64_t cur_time = rte_get_timer_cycles();
580         uint64_t period;
581         struct rte_timer_data *timer_data;
582
583         TIMER_DATA_VALID_GET_OR_ERR_RET(timer_data_id, timer_data, -EINVAL);
584
585         if (type == PERIODICAL)
586                 period = ticks;
587         else
588                 period = 0;
589
590         return __rte_timer_reset(tim,  cur_time + ticks, period, tim_lcore,
591                                  fct, arg, 0, timer_data);
592 }
593
594 /* loop until rte_timer_reset() succeed */
595 void
596 rte_timer_reset_sync(struct rte_timer *tim, uint64_t ticks,
597                      enum rte_timer_type type, unsigned tim_lcore,
598                      rte_timer_cb_t fct, void *arg)
599 {
600         while (rte_timer_reset(tim, ticks, type, tim_lcore,
601                                fct, arg) != 0)
602                 rte_pause();
603 }
604
605 static int
606 __rte_timer_stop(struct rte_timer *tim, int local_is_locked,
607                  struct rte_timer_data *timer_data)
608 {
609         union rte_timer_status prev_status, status;
610         unsigned lcore_id = rte_lcore_id();
611         int ret;
612         struct priv_timer *priv_timer = timer_data->priv_timer;
613
614         /* wait that the timer is in correct status before update,
615          * and mark it as being configured */
616         ret = timer_set_config_state(tim, &prev_status, priv_timer);
617         if (ret < 0)
618                 return -1;
619
620         __TIMER_STAT_ADD(priv_timer, stop, 1);
621         if (prev_status.state == RTE_TIMER_RUNNING &&
622             lcore_id < RTE_MAX_LCORE) {
623                 priv_timer[lcore_id].updated = 1;
624         }
625
626         /* remove it from list */
627         if (prev_status.state == RTE_TIMER_PENDING) {
628                 timer_del(tim, prev_status, local_is_locked, priv_timer);
629                 __TIMER_STAT_ADD(priv_timer, pending, -1);
630         }
631
632         /* mark timer as stopped */
633         rte_wmb();
634         status.state = RTE_TIMER_STOP;
635         status.owner = RTE_TIMER_NO_OWNER;
636         tim->status.u32 = status.u32;
637
638         return 0;
639 }
640
641 /* Stop the timer associated with the timer handle tim */
642 int
643 rte_timer_stop_v20(struct rte_timer *tim)
644 {
645         return __rte_timer_stop(tim, 0, &default_timer_data);
646 }
647 VERSION_SYMBOL(rte_timer_stop, _v20, 2.0);
648
649 int
650 rte_timer_stop_v1905(struct rte_timer *tim)
651 {
652         return rte_timer_alt_stop(default_data_id, tim);
653 }
654 MAP_STATIC_SYMBOL(int rte_timer_stop(struct rte_timer *tim),
655                   rte_timer_stop_v1905);
656 BIND_DEFAULT_SYMBOL(rte_timer_stop, _v1905, 19.05);
657
658 int __rte_experimental
659 rte_timer_alt_stop(uint32_t timer_data_id, struct rte_timer *tim)
660 {
661         struct rte_timer_data *timer_data;
662
663         TIMER_DATA_VALID_GET_OR_ERR_RET(timer_data_id, timer_data, -EINVAL);
664
665         return __rte_timer_stop(tim, 0, timer_data);
666 }
667
668 /* loop until rte_timer_stop() succeed */
669 void
670 rte_timer_stop_sync(struct rte_timer *tim)
671 {
672         while (rte_timer_stop(tim) != 0)
673                 rte_pause();
674 }
675
676 /* Test the PENDING status of the timer handle tim */
677 int
678 rte_timer_pending(struct rte_timer *tim)
679 {
680         return tim->status.state == RTE_TIMER_PENDING;
681 }
682
683 /* must be called periodically, run all timer that expired */
684 static void
685 __rte_timer_manage(struct rte_timer_data *timer_data)
686 {
687         union rte_timer_status status;
688         struct rte_timer *tim, *next_tim;
689         struct rte_timer *run_first_tim, **pprev;
690         unsigned lcore_id = rte_lcore_id();
691         struct rte_timer *prev[MAX_SKIPLIST_DEPTH + 1];
692         uint64_t cur_time;
693         int i, ret;
694         struct priv_timer *priv_timer = timer_data->priv_timer;
695
696         /* timer manager only runs on EAL thread with valid lcore_id */
697         assert(lcore_id < RTE_MAX_LCORE);
698
699         __TIMER_STAT_ADD(priv_timer, manage, 1);
700         /* optimize for the case where per-cpu list is empty */
701         if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL)
702                 return;
703         cur_time = rte_get_timer_cycles();
704
705 #ifdef RTE_ARCH_64
706         /* on 64-bit the value cached in the pending_head.expired will be
707          * updated atomically, so we can consult that for a quick check here
708          * outside the lock */
709         if (likely(priv_timer[lcore_id].pending_head.expire > cur_time))
710                 return;
711 #endif
712
713         /* browse ordered list, add expired timers in 'expired' list */
714         rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
715
716         /* if nothing to do just unlock and return */
717         if (priv_timer[lcore_id].pending_head.sl_next[0] == NULL ||
718             priv_timer[lcore_id].pending_head.sl_next[0]->expire > cur_time) {
719                 rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
720                 return;
721         }
722
723         /* save start of list of expired timers */
724         tim = priv_timer[lcore_id].pending_head.sl_next[0];
725
726         /* break the existing list at current time point */
727         timer_get_prev_entries(cur_time, lcore_id, prev, priv_timer);
728         for (i = priv_timer[lcore_id].curr_skiplist_depth -1; i >= 0; i--) {
729                 if (prev[i] == &priv_timer[lcore_id].pending_head)
730                         continue;
731                 priv_timer[lcore_id].pending_head.sl_next[i] =
732                     prev[i]->sl_next[i];
733                 if (prev[i]->sl_next[i] == NULL)
734                         priv_timer[lcore_id].curr_skiplist_depth--;
735                 prev[i] ->sl_next[i] = NULL;
736         }
737
738         /* transition run-list from PENDING to RUNNING */
739         run_first_tim = tim;
740         pprev = &run_first_tim;
741
742         for ( ; tim != NULL; tim = next_tim) {
743                 next_tim = tim->sl_next[0];
744
745                 ret = timer_set_running_state(tim);
746                 if (likely(ret == 0)) {
747                         pprev = &tim->sl_next[0];
748                 } else {
749                         /* another core is trying to re-config this one,
750                          * remove it from local expired list
751                          */
752                         *pprev = next_tim;
753                 }
754         }
755
756         /* update the next to expire timer value */
757         priv_timer[lcore_id].pending_head.expire =
758             (priv_timer[lcore_id].pending_head.sl_next[0] == NULL) ? 0 :
759                 priv_timer[lcore_id].pending_head.sl_next[0]->expire;
760
761         rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
762
763         /* now scan expired list and call callbacks */
764         for (tim = run_first_tim; tim != NULL; tim = next_tim) {
765                 next_tim = tim->sl_next[0];
766                 priv_timer[lcore_id].updated = 0;
767                 priv_timer[lcore_id].running_tim = tim;
768
769                 /* execute callback function with list unlocked */
770                 tim->f(tim, tim->arg);
771
772                 __TIMER_STAT_ADD(priv_timer, pending, -1);
773                 /* the timer was stopped or reloaded by the callback
774                  * function, we have nothing to do here */
775                 if (priv_timer[lcore_id].updated == 1)
776                         continue;
777
778                 if (tim->period == 0) {
779                         /* remove from done list and mark timer as stopped */
780                         status.state = RTE_TIMER_STOP;
781                         status.owner = RTE_TIMER_NO_OWNER;
782                         rte_wmb();
783                         tim->status.u32 = status.u32;
784                 }
785                 else {
786                         /* keep it in list and mark timer as pending */
787                         rte_spinlock_lock(&priv_timer[lcore_id].list_lock);
788                         status.state = RTE_TIMER_PENDING;
789                         __TIMER_STAT_ADD(priv_timer, pending, 1);
790                         status.owner = (int16_t)lcore_id;
791                         rte_wmb();
792                         tim->status.u32 = status.u32;
793                         __rte_timer_reset(tim, tim->expire + tim->period,
794                                 tim->period, lcore_id, tim->f, tim->arg, 1,
795                                 timer_data);
796                         rte_spinlock_unlock(&priv_timer[lcore_id].list_lock);
797                 }
798         }
799         priv_timer[lcore_id].running_tim = NULL;
800 }
801
802 void
803 rte_timer_manage_v20(void)
804 {
805         __rte_timer_manage(&default_timer_data);
806 }
807 VERSION_SYMBOL(rte_timer_manage, _v20, 2.0);
808
809 int
810 rte_timer_manage_v1905(void)
811 {
812         struct rte_timer_data *timer_data;
813
814         TIMER_DATA_VALID_GET_OR_ERR_RET(default_data_id, timer_data, -EINVAL);
815
816         __rte_timer_manage(timer_data);
817
818         return 0;
819 }
820 MAP_STATIC_SYMBOL(int rte_timer_manage(void), rte_timer_manage_v1905);
821 BIND_DEFAULT_SYMBOL(rte_timer_manage, _v1905, 19.05);
822
823 int __rte_experimental
824 rte_timer_alt_manage(uint32_t timer_data_id,
825                      unsigned int *poll_lcores,
826                      int nb_poll_lcores,
827                      rte_timer_alt_manage_cb_t f)
828 {
829         unsigned int default_poll_lcores[] = {rte_lcore_id()};
830         union rte_timer_status status;
831         struct rte_timer *tim, *next_tim, **pprev;
832         struct rte_timer *run_first_tims[RTE_MAX_LCORE];
833         unsigned int this_lcore = rte_lcore_id();
834         struct rte_timer *prev[MAX_SKIPLIST_DEPTH + 1];
835         uint64_t cur_time;
836         int i, j, ret;
837         int nb_runlists = 0;
838         struct rte_timer_data *data;
839         struct priv_timer *privp;
840         uint32_t poll_lcore;
841
842         TIMER_DATA_VALID_GET_OR_ERR_RET(timer_data_id, data, -EINVAL);
843
844         /* timer manager only runs on EAL thread with valid lcore_id */
845         assert(this_lcore < RTE_MAX_LCORE);
846
847         __TIMER_STAT_ADD(data->priv_timer, manage, 1);
848
849         if (poll_lcores == NULL) {
850                 poll_lcores = default_poll_lcores;
851                 nb_poll_lcores = RTE_DIM(default_poll_lcores);
852         }
853
854         for (i = 0; i < nb_poll_lcores; i++) {
855                 poll_lcore = poll_lcores[i];
856                 privp = &data->priv_timer[poll_lcore];
857
858                 /* optimize for the case where per-cpu list is empty */
859                 if (privp->pending_head.sl_next[0] == NULL)
860                         continue;
861                 cur_time = rte_get_timer_cycles();
862
863 #ifdef RTE_ARCH_64
864                 /* on 64-bit the value cached in the pending_head.expired will
865                  * be updated atomically, so we can consult that for a quick
866                  * check here outside the lock
867                  */
868                 if (likely(privp->pending_head.expire > cur_time))
869                         continue;
870 #endif
871
872                 /* browse ordered list, add expired timers in 'expired' list */
873                 rte_spinlock_lock(&privp->list_lock);
874
875                 /* if nothing to do just unlock and return */
876                 if (privp->pending_head.sl_next[0] == NULL ||
877                     privp->pending_head.sl_next[0]->expire > cur_time) {
878                         rte_spinlock_unlock(&privp->list_lock);
879                         continue;
880                 }
881
882                 /* save start of list of expired timers */
883                 tim = privp->pending_head.sl_next[0];
884
885                 /* break the existing list at current time point */
886                 timer_get_prev_entries(cur_time, poll_lcore, prev,
887                                        data->priv_timer);
888                 for (j = privp->curr_skiplist_depth - 1; j >= 0; j--) {
889                         if (prev[j] == &privp->pending_head)
890                                 continue;
891                         privp->pending_head.sl_next[j] =
892                                 prev[j]->sl_next[j];
893                         if (prev[j]->sl_next[j] == NULL)
894                                 privp->curr_skiplist_depth--;
895
896                         prev[j]->sl_next[j] = NULL;
897                 }
898
899                 /* transition run-list from PENDING to RUNNING */
900                 run_first_tims[nb_runlists] = tim;
901                 pprev = &run_first_tims[nb_runlists];
902                 nb_runlists++;
903
904                 for ( ; tim != NULL; tim = next_tim) {
905                         next_tim = tim->sl_next[0];
906
907                         ret = timer_set_running_state(tim);
908                         if (likely(ret == 0)) {
909                                 pprev = &tim->sl_next[0];
910                         } else {
911                                 /* another core is trying to re-config this one,
912                                  * remove it from local expired list
913                                  */
914                                 *pprev = next_tim;
915                         }
916                 }
917
918                 /* update the next to expire timer value */
919                 privp->pending_head.expire =
920                     (privp->pending_head.sl_next[0] == NULL) ? 0 :
921                         privp->pending_head.sl_next[0]->expire;
922
923                 rte_spinlock_unlock(&privp->list_lock);
924         }
925
926         /* Now process the run lists */
927         while (1) {
928                 bool done = true;
929                 uint64_t min_expire = UINT64_MAX;
930                 int min_idx = 0;
931
932                 /* Find the next oldest timer to process */
933                 for (i = 0; i < nb_runlists; i++) {
934                         tim = run_first_tims[i];
935
936                         if (tim != NULL && tim->expire < min_expire) {
937                                 min_expire = tim->expire;
938                                 min_idx = i;
939                                 done = false;
940                         }
941                 }
942
943                 if (done)
944                         break;
945
946                 tim = run_first_tims[min_idx];
947
948                 /* Move down the runlist from which we picked a timer to
949                  * execute
950                  */
951                 run_first_tims[min_idx] = run_first_tims[min_idx]->sl_next[0];
952
953                 data->priv_timer[this_lcore].updated = 0;
954                 data->priv_timer[this_lcore].running_tim = tim;
955
956                 /* Call the provided callback function */
957                 f(tim);
958
959                 __TIMER_STAT_ADD(data->priv_timer, pending, -1);
960
961                 /* the timer was stopped or reloaded by the callback
962                  * function, we have nothing to do here
963                  */
964                 if (data->priv_timer[this_lcore].updated == 1)
965                         continue;
966
967                 if (tim->period == 0) {
968                         /* remove from done list and mark timer as stopped */
969                         status.state = RTE_TIMER_STOP;
970                         status.owner = RTE_TIMER_NO_OWNER;
971                         rte_wmb();
972                         tim->status.u32 = status.u32;
973                 } else {
974                         /* keep it in list and mark timer as pending */
975                         rte_spinlock_lock(
976                                 &data->priv_timer[this_lcore].list_lock);
977                         status.state = RTE_TIMER_PENDING;
978                         __TIMER_STAT_ADD(data->priv_timer, pending, 1);
979                         status.owner = (int16_t)this_lcore;
980                         rte_wmb();
981                         tim->status.u32 = status.u32;
982                         __rte_timer_reset(tim, tim->expire + tim->period,
983                                 tim->period, this_lcore, tim->f, tim->arg, 1,
984                                 data);
985                         rte_spinlock_unlock(
986                                 &data->priv_timer[this_lcore].list_lock);
987                 }
988
989                 data->priv_timer[this_lcore].running_tim = NULL;
990         }
991
992         return 0;
993 }
994
995 /* Walk pending lists, stopping timers and calling user-specified function */
996 int __rte_experimental
997 rte_timer_stop_all(uint32_t timer_data_id, unsigned int *walk_lcores,
998                    int nb_walk_lcores,
999                    rte_timer_stop_all_cb_t f, void *f_arg)
1000 {
1001         int i;
1002         struct priv_timer *priv_timer;
1003         uint32_t walk_lcore;
1004         struct rte_timer *tim, *next_tim;
1005         struct rte_timer_data *timer_data;
1006
1007         TIMER_DATA_VALID_GET_OR_ERR_RET(timer_data_id, timer_data, -EINVAL);
1008
1009         for (i = 0; i < nb_walk_lcores; i++) {
1010                 walk_lcore = walk_lcores[i];
1011                 priv_timer = &timer_data->priv_timer[walk_lcore];
1012
1013                 rte_spinlock_lock(&priv_timer->list_lock);
1014
1015                 for (tim = priv_timer->pending_head.sl_next[0];
1016                      tim != NULL;
1017                      tim = next_tim) {
1018                         next_tim = tim->sl_next[0];
1019
1020                         /* Call timer_stop with lock held */
1021                         __rte_timer_stop(tim, 1, timer_data);
1022
1023                         if (f)
1024                                 f(tim, f_arg);
1025                 }
1026
1027                 rte_spinlock_unlock(&priv_timer->list_lock);
1028         }
1029
1030         return 0;
1031 }
1032
1033 /* dump statistics about timers */
1034 static void
1035 __rte_timer_dump_stats(struct rte_timer_data *timer_data __rte_unused, FILE *f)
1036 {
1037 #ifdef RTE_LIBRTE_TIMER_DEBUG
1038         struct rte_timer_debug_stats sum;
1039         unsigned lcore_id;
1040         struct priv_timer *priv_timer = timer_data->priv_timer;
1041
1042         memset(&sum, 0, sizeof(sum));
1043         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1044                 sum.reset += priv_timer[lcore_id].stats.reset;
1045                 sum.stop += priv_timer[lcore_id].stats.stop;
1046                 sum.manage += priv_timer[lcore_id].stats.manage;
1047                 sum.pending += priv_timer[lcore_id].stats.pending;
1048         }
1049         fprintf(f, "Timer statistics:\n");
1050         fprintf(f, "  reset = %"PRIu64"\n", sum.reset);
1051         fprintf(f, "  stop = %"PRIu64"\n", sum.stop);
1052         fprintf(f, "  manage = %"PRIu64"\n", sum.manage);
1053         fprintf(f, "  pending = %"PRIu64"\n", sum.pending);
1054 #else
1055         fprintf(f, "No timer statistics, RTE_LIBRTE_TIMER_DEBUG is disabled\n");
1056 #endif
1057 }
1058
1059 void
1060 rte_timer_dump_stats_v20(FILE *f)
1061 {
1062         __rte_timer_dump_stats(&default_timer_data, f);
1063 }
1064 VERSION_SYMBOL(rte_timer_dump_stats, _v20, 2.0);
1065
1066 int
1067 rte_timer_dump_stats_v1905(FILE *f)
1068 {
1069         return rte_timer_alt_dump_stats(default_data_id, f);
1070 }
1071 MAP_STATIC_SYMBOL(int rte_timer_dump_stats(FILE *f),
1072                   rte_timer_dump_stats_v1905);
1073 BIND_DEFAULT_SYMBOL(rte_timer_dump_stats, _v1905, 19.05);
1074
1075 int __rte_experimental
1076 rte_timer_alt_dump_stats(uint32_t timer_data_id __rte_unused, FILE *f)
1077 {
1078         struct rte_timer_data *timer_data;
1079
1080         TIMER_DATA_VALID_GET_OR_ERR_RET(timer_data_id, timer_data, -EINVAL);
1081
1082         __rte_timer_dump_stats(timer_data, f);
1083
1084         return 0;
1085 }