xref: /dpdk/doc/guides/prog_guide/timer_lib.rst (revision 41dd9a6bc2d9c6e20e139ad713cc9d172572dd43)
1*5630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
2*5630257fSFerruh Yigit    Copyright(c) 2010-2014 Intel Corporation.
3fc1f2750SBernard Iremonger
4fc1f2750SBernard IremongerTimer Library
5fc1f2750SBernard Iremonger=============
6fc1f2750SBernard Iremonger
748624fd9SSiobhan ButlerThe Timer library provides a timer service to DPDK execution units to enable execution of callback functions asynchronously.
8fc1f2750SBernard IremongerFeatures of the library are:
9fc1f2750SBernard Iremonger
10fc1f2750SBernard Iremonger*   Timers can be periodic (multi-shot) or single (one-shot).
11fc1f2750SBernard Iremonger
12fc1f2750SBernard Iremonger*   Timers can be loaded from one core and executed on another. It has to be specified in the call to rte_timer_reset().
13fc1f2750SBernard Iremonger
14fc1f2750SBernard Iremonger*   Timers provide high precision (depends on the call frequency to rte_timer_manage() that checks timer expiration for the local core).
15fc1f2750SBernard Iremonger
16fc1f2750SBernard Iremonger*   If not required in the application, timers can be disabled at compilation time by not calling the rte_timer_manage() to increase performance.
17fc1f2750SBernard Iremonger
18fc1f2750SBernard IremongerThe timer library uses the rte_get_timer_cycles() function that uses the High Precision Event Timer (HPET)
19fc1f2750SBernard Iremongeror the CPUs Time Stamp Counter (TSC) to provide a reliable time reference.
20fc1f2750SBernard Iremonger
21fc1f2750SBernard IremongerThis library provides an interface to add, delete and restart a timer. The API is based on BSD callout() with a few differences.
22fc1f2750SBernard IremongerRefer to the `callout manual <http://www.daemon-systems.org/man/callout.9.html>`_.
23fc1f2750SBernard Iremonger
24fc1f2750SBernard IremongerImplementation Details
25fc1f2750SBernard Iremonger----------------------
26fc1f2750SBernard Iremonger
27fc1f2750SBernard IremongerTimers are tracked on a per-lcore basis,
28fc1f2750SBernard Iremongerwith all pending timers for a core being maintained in order of timer expiry in a skiplist data structure.
29fc1f2750SBernard IremongerThe skiplist used has ten levels and each entry in the table appears in each level with probability ¼^level.
30fc1f2750SBernard IremongerThis means that all entries are present in level 0, 1 in every 4 entries is present at level 1,
31fc1f2750SBernard Iremongerone in every 16 at level 2 and so on up to level 9.
32fc1f2750SBernard IremongerThis means that adding and removing entries from the timer list for a core can be done in log(n) time,
33fc1f2750SBernard Iremongerup to 4^10 entries, that is, approximately 1,000,000 timers per lcore.
34fc1f2750SBernard Iremonger
35fc1f2750SBernard IremongerA timer structure contains a special field called status,
36fc1f2750SBernard Iremongerwhich is a union of a timer state (stopped, pending, running, config) and an owner (lcore id).
37fc1f2750SBernard IremongerDepending on the timer state, we know if a timer is present in a list or not:
38fc1f2750SBernard Iremonger
39fc1f2750SBernard Iremonger*   STOPPED: no owner, not in a list
40fc1f2750SBernard Iremonger
41fc1f2750SBernard Iremonger*   CONFIG: owned by a core, must not be modified by another core, maybe in a list or not, depending on previous state
42fc1f2750SBernard Iremonger
43fc1f2750SBernard Iremonger*   PENDING: owned by a core, present in a list
44fc1f2750SBernard Iremonger
45fc1f2750SBernard Iremonger*   RUNNING: owned by a core, must not be modified by another core, present in a list
46fc1f2750SBernard Iremonger
47fc1f2750SBernard IremongerResetting or stopping a timer while it is in a CONFIG or RUNNING state is not allowed.
48fc1f2750SBernard IremongerWhen modifying the state of a timer,
49fc1f2750SBernard Iremongera Compare And Swap instruction should be used to guarantee that the status (state+owner) is modified atomically.
50fc1f2750SBernard Iremonger
51fc1f2750SBernard IremongerInside the rte_timer_manage() function,
52fc1f2750SBernard Iremongerthe skiplist is used as a regular list by iterating along the level 0 list, which contains all timer entries,
53fc1f2750SBernard Iremongeruntil an entry which has not yet expired has been encountered.
54fc1f2750SBernard IremongerTo improve performance in the case where there are entries in the timer list but none of those timers have yet expired,
55fc1f2750SBernard Iremongerthe expiry time of the first list entry is maintained within the per-core timer list structure itself.
56fc1f2750SBernard IremongerOn 64-bit platforms, this value can be checked without the need to take a lock on the overall structure.
57fc1f2750SBernard Iremonger(Since expiry times are maintained as 64-bit values,
58fc1f2750SBernard Iremongera check on the value cannot be done on 32-bit platforms without using either a compare-and-swap (CAS) instruction or using a lock,
59fea1d908SJohn McNamaraso this additional check is skipped in favor of checking as normal once the lock has been taken.)
60fc1f2750SBernard IremongerOn both 64-bit and 32-bit platforms,
61fc1f2750SBernard Iremongera call to rte_timer_manage() returns without taking a lock in the case where the timer list for the calling core is empty.
62fc1f2750SBernard Iremonger
63fc1f2750SBernard IremongerUse Cases
64fc1f2750SBernard Iremonger---------
65fc1f2750SBernard Iremonger
66fc1f2750SBernard IremongerThe timer library is used for periodic calls, such as garbage collectors, or some state machines (ARP, bridging, and so on).
67fc1f2750SBernard Iremonger
68fc1f2750SBernard IremongerReferences
69fc1f2750SBernard Iremonger----------
70fc1f2750SBernard Iremonger
71fc1f2750SBernard Iremonger*   `callout manual <http://www.daemon-systems.org/man/callout.9.html>`_
72fc1f2750SBernard Iremonger    - The callout facility that provides timers with a mechanism to execute a function at a given time.
73fc1f2750SBernard Iremonger
74fc1f2750SBernard Iremonger*   `HPET <http://en.wikipedia.org/wiki/HPET>`_
75fc1f2750SBernard Iremonger    - Information about the High Precision Event Timer (HPET).
76