A linked list-based timer management method and device
Patent Information
- Application Number
- CN202211664217.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-22
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2042-12-22
AI Technical Summary
[0008]本发明旨在解决现有的定时器机制容易出现事件调度出错以及设备挂机的问题,提出一种基于链表的定时器管理方法及装置
[0034] The beneficial effects of this invention are: the linked list-based timer management method and apparatus of this invention, by adding virtual nodes to the timer linked list and constructing a timer mechanism based on the virtual nodes, can avoid event scheduling errors and device downtime issues under certain special operations, even for some business modules with complex logic and high performance requirements, while ensuring timer performance.
Smart Images

Figure CN115981854B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of timer technology, and more specifically to a timer management method and apparatus based on linked lists. Background Technology
[0002] Each business module needs to use timers to handle various events. The following example uses the IKE (Internet KeyExchange) business module to illustrate the operating principle of the timer mechanism within the module. Currently, IKE uses the epoll function from the Linux character device driver to implement the timer. The epoll function adds the current file pointer to the wait queue. In the IKE main thread, various events are written to the timer linked list. The epoll timer schedules events every second, processing all expired events each time.
[0003] Currently, there are two types of IKE timer linked lists, which are used in some more complex operation scenarios. The first type is a simple ordered linked list, where nodes at the beginning of the list expire earlier. Whenever a node corresponding to a timer event is inserted into the list, the list needs to be traversed in ascending order of event expiration time to insert the node at the specified position. When the epoll timer is scheduled, the list is traversed and the expired events are executed. The second type is a tick timer linked list (its design is derived from the Linux kernel's timer scheduling mechanism). Compared to the first type, this linked list is more efficient in adding and deleting event nodes. This timer linked list maintains a current time `cur_time`. Every second, a timer is scheduled, executing all nodes in the timer linked list that are equal to `cur_time&255`. The timer framework has two arrays, each array node being a linked list head. The linked list of each node in the first array stores all events expiring within one second, and the linked list of each node in the second array stores events expiring within 255 seconds. See [link to relevant documentation] for details. Figure 1 When inserting a timer event, the timeout period is used to calculate which node in which array the data should be inserted into. Whenever a timer is scheduled, IKE calculates cur_time & 255. If it equals 0, it means the first array has finished executing, and all timer events for a specific item in the second array need to be inserted into the first array.
[0004] These two timers have at least the following drawbacks:
[0005] IKE and other business modules have high requirements for negotiation performance, and many timer event addition and deletion operations are performed during IKE negotiation. Therefore, the timer mechanism must ensure the ability to quickly add and delete event nodes. For IKE, many timer events are reusable, but allocating and releasing memory is a very time-consuming operation. Therefore, in actual use, IKE does not allocate memory every time an event node is inserted, nor does it delete memory every time an event node is deleted. In other words, the timer mechanism itself cannot allocate or delete memory for event nodes; only the IKE and other business modules can determine whether to allocate or release memory. This means that the timer mechanism itself cannot determine whether the memory of a node has been released. Because the operations of adding, deleting, modifying, and querying timer nodes by IKE and other business modules are quite complex, it is possible that the memory of some nodes in the timer linked list has been released, but the nodes are still attached to the timer linked list. Therefore, if the timer mechanism encounters such an invalid node, it will lead to serious problems such as system crashes. The timer mechanism must ensure that IKE services can still operate normally in these complex scenarios.
[0006] For example, please see Figure 2 In existing timer linked lists, each node stores pointers to the previous and next nodes. If a node is deleted while its event is being executed, its memory is released, but the data in that memory is poisoned, causing IKE to access incorrect memory. To avoid this problem, the next node is usually retrieved early when timeout processing for a node. However, this approach still has the following issues:
[0007] by Figure 2 For example, if the memory of node 2 is deleted while executing node 1, it will cause a hangup-like problem. If the timeout of node 3 is modified while executing node 2, it may cause serious problems for the device in the tick timer. This is because the pointer of node 3 has been retrieved in advance in the tick timer, and when node 3 is executed, node 3 may have already been moved to the linked list of other array nodes. This will cause many events that have not yet timed out to be executed prematurely. For business modules such as IKE, this is likely to cause hangup-like problems for the device. If there are only three nodes in the current linked list of IKE, when IKE is processing node 3, if a new node is inserted after node 3, since IKE finds that node 3 is the last node, it will not process the newly added node event. Summary of the Invention
[0008] This invention aims to solve the problems of event scheduling errors and device hang-up that are prone to occur in existing timer mechanisms, and proposes a timer management method and device based on linked lists.
[0009] The technical solution adopted by the present invention to solve the above-mentioned technical problems is as follows:
[0010] On the one hand, a timer management method based on a linked list is provided, the method comprising:
[0011] Create a linked list to manage the timers;
[0012] When a timed event needs to be added, a timer node is requested and configured, and the timer node is inserted into the corresponding position in the linked list. The timer node stores a pointer to the previous node and a pointer to the next node.
[0013] Add a corresponding virtual node after the timer node. The virtual node stores pointers to the previous node and pointers to the next node.
[0014] After the business module performs timeout processing on the timer node, it deletes the timer node and its corresponding virtual node.
[0015] Furthermore, the method also includes:
[0016] When a business module performs timeout processing on a timer node, it obtains in advance the pointer to the next node in that timer node.
[0017] Furthermore, the method also includes:
[0018] When a timed event needs to be deleted, the corresponding timer node is deleted, but the virtual node corresponding to the timer node is retained.
[0019] Furthermore, the method also includes:
[0020] When a timed event needs to be modified, the timeout time of the corresponding timer node is modified, and the timer node is adjusted to the corresponding position in the linked list according to the order of the modified timeout times.
[0021] Adjust the virtual node corresponding to the timer node to be placed after the timer node.
[0022] Furthermore, the linked list is a sorted linked list or a tick timer linked list.
[0023] On the other hand, a linked list-based timer management device is provided, comprising:
[0024] Create a unit to create a linked list for managing timers;
[0025] The management unit is used to request and configure a timer node when a timed event needs to be added, and insert the timer node into the corresponding position of the linked list. The timer node stores a pointer to the previous node and a pointer to the next node. The unit also adds a corresponding virtual node after the timer node. The virtual node stores a pointer to the previous node and a pointer to the next node.
[0026] The deletion unit is used to delete the timer node and its corresponding virtual node after the business module performs timeout processing on the timer node.
[0027] Furthermore, the business module is used to obtain in advance the pointer to the next node in the timer node when performing timeout processing on the timer node.
[0028] Furthermore, the management unit is also used for:
[0029] When a timed event needs to be deleted, the corresponding timer node is deleted, but the virtual node corresponding to the timer node is retained.
[0030] Furthermore, the management unit is also used for:
[0031] When a timed event needs to be modified, the timeout time of the corresponding timer node is modified, and the timer node is adjusted to the corresponding position in the linked list according to the order of the modified timeout times.
[0032] Adjust the virtual node corresponding to the timer node to be placed after the timer node.
[0033] Furthermore, the linked list is a sorted linked list or a tick timer linked list.
[0034] The beneficial effects of this invention are: the linked list-based timer management method and apparatus of this invention, by adding virtual nodes to the timer linked list and constructing a timer mechanism based on the virtual nodes, can avoid event scheduling errors and device downtime issues under certain special operations, even for some business modules with complex logic and high performance requirements, while ensuring timer performance. Attached Figure Description
[0035] Figure 1 This is a schematic diagram of the structure of a scale timer linked list in the prior art;
[0036] Figure 2 This is a schematic diagram of the structure of a linked list node in the prior art;
[0037] Figure 3 This is a schematic diagram of the structure of a linked list node according to an embodiment of the present invention;
[0038] Figure 4 This is a schematic diagram of the linked list structure after adding a timer node according to an embodiment of the present invention;
[0039] Figure 5 This is a schematic diagram of the linked list structure after deleting the timer node according to an embodiment of the present invention;
[0040] Figure 6 This is a schematic diagram of the scale timer linked list according to an embodiment of the present invention;
[0041] Figure 7 This is a schematic diagram of the scale timer linked list after modifying the timeout period according to an embodiment of the present invention. Detailed Implementation
[0042] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0043] This invention aims to provide a linked list-based timer management method and apparatus to avoid event scheduling errors and device downtime, and to improve timer performance. The main technical solution includes: creating a linked list for managing timers; when a timed event needs to be added, requesting and configuring a timer node, and inserting the timer node into the corresponding position in the linked list, wherein the timer node stores pointers to the previous node and the next node; adding a corresponding virtual node after the timer node, wherein the virtual node stores pointers to the previous node and the next node; and deleting the timer node and its corresponding virtual node after the business module performs timeout processing on the timer node.
[0044] This invention adds virtual nodes to a linked list and constructs a new timer mechanism based on these virtual nodes. Specifically, when a timed event needs to be added, a timer node is requested and configured, inserted into the corresponding position in the linked list, and a corresponding virtual node is added after the timer node. After the event corresponding to the timer node is executed, the timer node and its corresponding virtual node are deleted. Since the virtual nodes are only used for traversing the linked list and can only be created and deleted by the timer, they are opaque to the business modules. To reduce memory consumption, these virtual nodes only store pointers to the previous and next nodes. By adding virtual nodes and constructing a timer mechanism based on them, event scheduling errors and device hang-ups under certain special operations are avoided.
[0045] For example, when executing a timer node, if the current timer node or a subsequent timer node is deleted, the corresponding virtual node is preserved during deletion. This allows the pointer to the next timer node to still be accurately found through the corresponding virtual node, thus avoiding access to incorrect memory and hang-up issues. As another example, if a new timer node is inserted during timer node execution, the business module obtains the pointer to the virtual node in advance. Therefore, even if a new timer node is added after the last timer node, it will still be processed sequentially. Furthermore, if the timeout of the next timer node is modified during timer node execution, the position of the corresponding virtual node is adjusted synchronously. Therefore, when the business module performs timeout processing, it can avoid premature execution of non-timeout events due to obtaining the pointer to the next node in advance.
[0046] Example
[0047] The timer management method based on linked lists described in this embodiment of the invention includes:
[0048] Step 1: Create a linked list to manage the timers;
[0049] Step 2: When a timed event needs to be added, apply for and configure a timer node, and insert the timer node into the corresponding position in the linked list. The timer node stores a pointer to the previous node and a pointer to the next node.
[0050] Step 3: Add a corresponding virtual node after the timer node. The virtual node stores a pointer to the previous node and a pointer to the next node.
[0051] Step 4: After the business module performs timeout processing on the timer node, delete the timer node and its corresponding virtual node.
[0052] In this embodiment, the linked list can be a sorted linked list or a tick timer linked list. A sorted linked list is a simple ordered chain where nodes at the beginning of the list expire earlier, meaning their timeout periods are shorter. The tick timer linked list has two arrays, each array node being a linked list head. The linked list of each node in the first array stores all events expiring within one second, while the linked list of each node in the second array stores events expiring within 255 seconds.
[0053] Please see Figure 3 In this embodiment, virtual nodes are added to the linked list. The method for adding and deleting event nodes in the timer mechanism around these virtual nodes is as follows:
[0054] 1. Add timed events
[0055] In this embodiment, when a timed event needs to be added, a timer node is requested and configured, inserted into the corresponding position in the linked list, and a corresponding virtual node is added after the timer node. Please refer to [link to relevant documentation]. Figure 3 If a new timed event needs to be added, a timer node 4 is requested and configured, and then inserted into the corresponding position in the linked list. For a sorted linked list, timer node 4 is inserted into the linked list according to the order of its timeout times. For a tick-based timer linked list, timer node 4 is inserted into the corresponding position of the corresponding tick in the linked list according to its timeout time. Assuming that timer node 4 is after timer node 3, the new linked list is shown in [reference needed]. Figure 4 .
[0056] 2. Delete timed events
[0057] In this embodiment, if it is necessary to delete a timed event, the corresponding timer node is deleted, while the virtual node corresponding to that timer node is retained. For example... Figure 3 As shown, if timer node 2 is deleted, virtual node b cannot be deleted simultaneously. This is because the pointer to virtual node b may have already been retrieved by the timer scheduling mechanism and cannot be modified. After deleting node 2, the new linked list will appear as shown in the diagram. Figure 5 .
[0058] 3. Modify timed events
[0059] In this embodiment, when it is necessary to modify a timed event, the timeout time of the corresponding timer node is modified, and the timer node is adjusted to the corresponding position in the linked list according to the order of the modified timeout times; the virtual node corresponding to the timer node is adjusted to the position after the timer node.
[0060] Please see Figure 6 Taking a tick timer as an example (the logic of timers in an ordered linked list is largely the same, so it will not be elaborated here), assuming that the timeout time for timer nodes 1, 2, and 3 is 0 seconds, and now the timeout time for timer node 3 is changed to 6 seconds, then when modifying the position of timer node 3, the virtual node c needs to be moved after timer node 3 again. After the position of node 3 is changed, please refer to the new linked list. Figure 7 .
[0061] 4. Timer scheduling
[0062] In this embodiment, after the business module performs timeout processing on the timer node, it deletes the corresponding timer node and its corresponding virtual node. Please refer to [link to relevant documentation]. Figure 3 When the event is scheduled to timer node 2 and is completed, the corresponding virtual node b must be deleted at the same time to avoid memory leaks.
[0063] Based on the aforementioned timer mechanism, this embodiment can avoid event scheduling errors and device hang-up issues under certain special operations. These special operations include at least the following four scenarios:
[0064] 1. The current timer node was deleted when the timer node was executed.
[0065] In this embodiment, when the business module performs timeout processing on a timer node, it obtains in advance the pointer to the next node within that timer node. Please refer to [link to relevant documentation]. Figure 3 When the business module is processing the event of timer node 1, it simultaneously retrieves the pointer of virtual node a in advance. If node 1 is deleted when the event of timer node 1 is executed, the pointer of timer node 2 can still be accurately found through virtual node a.
[0066] 2. When executing a timer node, subsequent timer nodes were deleted.
[0067] Please see Figure 5 If timer node 2 is deleted while timer node 1 is being executed, then the next pointer of virtual node a will point to virtual node b. Through virtual node b, timer node 3 can be accurately located and the events of timer node 3 can be processed.
[0068] 3. A new timer node was inserted during the execution of the timer node.
[0069] If a new timer node is added while the current timer node is being executed, the newly added timer node can be accessed and executed directly after the current timer node has finished executing.
[0070] 4. The next timer node was modified during the execution of the timer node.
[0071] Please see Figure 7 If the timeout of timer node 3 is modified while timer node 2 is being executed, timer node 3 will be moved to another linked list. After timer node 2 finishes execution, the information of virtual node b has been updated, so the wrong node will not be accessed.
[0072] As can be seen, through the timer mechanism in this embodiment, even for some business modules with more complex logic and higher performance requirements, it can ensure the performance of the timer while avoiding event scheduling errors and device hang-ups under some special operations.
[0073] Based on the above technical solution, this embodiment also provides a timer management device based on a linked list, including:
[0074] Create a unit to create a linked list for managing timers;
[0075] The management unit is used to request and configure a timer node when a timed event needs to be added, and insert the timer node into the corresponding position of the linked list. The timer node stores a pointer to the previous node and a pointer to the next node. The unit also adds a corresponding virtual node after the timer node. The virtual node stores a pointer to the previous node and a pointer to the next node.
[0076] The deletion unit is used to delete the timer node and its corresponding virtual node after the business module performs timeout processing on the timer node.
[0077] It is understood that since the linked list-based timer management device described in the embodiments of the present invention is a device for implementing the linked list-based timer management method described in the embodiments, the device disclosed in the embodiments is relatively simple to describe because it corresponds to the method disclosed in the embodiments. For relevant parts, please refer to the description of the method.
Claims
1. A timer management method based on linked lists, characterized in that, The method includes: Create a linked list to manage the timers; When a timed event needs to be added, a timer node is requested and configured, and the timer node is inserted into the corresponding position in the linked list. The timer node stores a pointer to the previous node and a pointer to the next node. Add a corresponding virtual node after the timer node. The virtual node stores pointers to the previous node and pointers to the next node. After the business module performs timeout processing on the timer node, it deletes the timer node and its corresponding virtual node. When a timed event needs to be modified, the timeout time of the corresponding timer node is modified, and the timer node is adjusted to the corresponding position in the linked list according to the order of the modified timeout times. Adjust the virtual node corresponding to the timer node to be placed after the timer node; When a timed event needs to be deleted, the corresponding timer node is deleted, but the virtual node corresponding to the timer node is retained.
2. The timer management method based on a linked list as described in claim 1, characterized in that, The method further includes: When a business module performs timeout processing on a timer node, it obtains in advance the pointer to the next node in that timer node.
3. The timer management method based on a linked list as described in claim 1 or 2, characterized in that, The linked list is either a sorted linked list or a tick timer linked list.
4. A timer management device based on a linked list, characterized in that, include: Create a unit to create a linked list for managing timers; The management unit is used to request and configure a timer node when a timed event needs to be added, and to insert the timer node into the corresponding position in the linked list. The timer node stores a pointer to the previous node and a pointer to the next node. And add a corresponding virtual node after the timer node, wherein the virtual node stores a pointer to the previous node and a pointer to the next node; The deletion unit is used to delete the timer node and its corresponding virtual node after the business module performs timeout processing on the timer node; The management unit is also used to: when it is necessary to modify a timed event, modify the timeout time of the corresponding timer node, and adjust the timer node to the corresponding position in the linked list according to the order of the modified timeout times; Adjust the virtual node corresponding to the timer node to be placed after the timer node; The management unit is also used to: when a timed event needs to be deleted, delete the corresponding timer node, and retain the virtual node corresponding to the timer node.
5. The linked list-based timer management device as described in claim 4, characterized in that, The business module is used to obtain the pointer to the next node in advance when performing timeout processing on the timer node.
6. The linked list-based timer management device as described in claim 4 or 5, characterized in that, The linked list is either a sorted linked list or a tick timer linked list.
Citation Information
Patent Citations
Scheduling method of timer chain meter
CN1936765A
Method and apparatus for implementing a fully dynamic lock-free hash table
US7287131B1