CN115981854A - Timer management method and device based on linked list - Google Patents

Timer management method and device based on linked list Download PDF

Info

Publication number
CN115981854A
CN115981854A CN202211664217.3A CN202211664217A CN115981854A CN 115981854 A CN115981854 A CN 115981854A CN 202211664217 A CN202211664217 A CN 202211664217A CN 115981854 A CN115981854 A CN 115981854A
Authority
CN
China
Prior art keywords
node
timer
linked list
pointer pointing
virtual
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211664217.3A
Other languages
Chinese (zh)
Inventor
李海威
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Maipu Communication Technology Co Ltd
Original Assignee
Maipu Communication Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Maipu Communication Technology Co Ltd filed Critical Maipu Communication Technology Co Ltd
Priority to CN202211664217.3A priority Critical patent/CN115981854A/en
Publication of CN115981854A publication Critical patent/CN115981854A/en
Pending legal-status Critical Current

Links

Images

Landscapes

  • Data Exchanges In Wide-Area Networks (AREA)

Abstract

The invention relates to the technical field of timers, discloses a timer management method and a device based on a linked list, and aims to solve the problems that an existing timer mechanism is easy to cause event scheduling errors and equipment hangs up, and the scheme mainly comprises the following steps: creating a linked list for managing timers; when a timing event needs to be added, applying for and configuring a timer node, and inserting the timer node to a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node; adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node; and when the service module executes overtime processing on the timer node, deleting the timer node and the corresponding virtual node. The invention avoids the problems of event scheduling error and equipment hang-up under some special operations, and is suitable for the service module.

Description

Timer management method and device based on linked list
Technical Field
The invention relates to the technical field of timers, in particular to a timer management method and device based on a linked list.
Background
Each service module needs to use a timer to process various events, and the following takes a service module IKE (Internet Key Exchange) as an example to explain the operation principle of the timer mechanism in the service module. The current IKE uses an epoll function in a character device driver in Linux to realize a timer, and the epoll function is used for hanging a current file pointer to a waiting queue. In the IKE main thread, various events are written into a timer linked list, the timer epoll is scheduled once every second, and all expired events are processed each time.
There are two types of current IKE timer chains, which are in some more complex operational scenarios. The first type of timer linked list is a simple ordered linked list that expires earlier the node in the linked list is. When a node corresponding to a timer event is inserted into the linked list, the nodes are inserted into the specified positions by traversing the linked list according to the sequence of the expiration time of the event from early to late. And when the timer epoll is scheduled, traversing the linked list and executing an expired event. The second kind of timer linked list is a scale timer linked list (the design scheme of the linked list is based on the timer scheduling mechanism of linux kernel), and compared with the first kind of linked list, the linked list is more efficient in adding and deleting event nodes. The timer linked list maintains a current time cur _ time, and the timer is scheduled every second to execute all nodes in the timer linked list of (cur _ time & 255). The timer framework has two arrays, each array node is a chain table header, the chain table of each node of the first array stores all events that expire in one second, and the chain table of each node of the second array stores events that expire in 255 seconds, as detailed with reference to fig. 1. When a timer event is inserted, the node of which array the data is inserted into is calculated according to the timeout time. Each time the timer is scheduled, IKE calculates cur _ time &255, and if 0, it indicates that the first array is finished executing, and it needs to insert all timer events of one item in the second array into the first array.
These two timers have at least the following disadvantages:
the service modules such as IKE have high requirements on negotiation performance, and many addition and deletion operations of the timer event are performed during IKE negotiation, so the timer mechanism must ensure that the event nodes can be added and deleted quickly. For IKE, many timer events can be multiplexed, and on the other hand, applying for and releasing memory is a time-consuming operation, so that IKE does not apply for memory every time an event node is inserted, or delete memory every time an event node is deleted in actual use. That is to say, the timer mechanism cannot apply for a memory or delete a memory for an event node, and only the service modules such as IKE determine whether to apply for or release a memory by themselves. This may cause the timer mechanism itself to be unable to determine whether the memory of the node has been released, and since the operations of adding, deleting, modifying and checking the timer node by the service modules such as IKE are quite complicated, the memory of a part of nodes in the timer linked list may have been released, while the nodes are still hung in the timer linked list, so once the timer mechanism traverses the invalid node, a serious problem such as a crash may be caused. A timer mechanism must ensure that the IKE traffic can still function properly in these complex scenarios.
For example, referring to fig. 2, the nodes of the existing timer linked list store pointers of a previous node and a next node, and if a current node is deleted when an event of a certain node is executed, the memory of the node is released, and data in the memory is poisoned, which may cause the IKE to access the wrong memory. In order to avoid the above problem, when a node is overtime-processed, the next node is usually taken out in advance, but this method still has the following problems:
taking fig. 2 as an example, if the memory of node 2 is deleted when node 1 is executed, the hang-up problem may be caused. If the timeout time of node 3 is modified while node 2 is executing, in the tick timer, it may cause a serious problem to the device. Because the pointer of the node 3 is taken out in advance in the scale timer, the node 3 may have been moved to the linked list of other array nodes when the node 3 is executed, which results in many events which are not timed out being executed in advance, and for the service modules such as IKE, this may result in the problem of hanging up of the device. If there are only three nodes in the IKE current linked list, when IKE is processing node 3, if a new node is inserted into node 3, since IKE is processing node 3, it is found that the node is the last node, then it will not process the new node event.
Disclosure of Invention
The invention aims to solve the problems that an existing timer mechanism is prone to occurrence of event scheduling errors and equipment hang-up, and provides a timer management method and device based on a linked list.
The technical scheme adopted by the invention for solving the technical problems is as follows:
in one aspect, a linked list-based timer management method is provided, and the method includes:
creating a linked list for managing timers;
when a timing event needs to be added, applying for and configuring a timer node, and inserting the timer node to a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node;
adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and when the service module executes overtime processing on the timer node, deleting the timer node and the corresponding virtual node.
Further, the method further comprises:
when the business module executes overtime processing to the timer node, a pointer pointing to a next node in the timer node is obtained in advance.
Further, the method further comprises:
and when a timing event needs to be deleted, deleting the corresponding timer node and reserving the virtual node corresponding to the timer node.
Further, the method further comprises:
when a timing event needs to be modified, modifying the overtime time of the corresponding timer node, and adjusting the timer node to the corresponding position of the linked list according to the sequence of the modified overtime time;
and adjusting the virtual node corresponding to the timer node to be behind the timer node.
Further, the linked list is a sorting linked list or a scale timer linked list.
In another aspect, an apparatus for linked list based timer management is provided, including:
a creating unit for creating a linked list for managing a timer;
the management unit is used for applying and configuring a timer node when a timing event needs to be added, and inserting the timer node into a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node; adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and the deleting unit is used for deleting the timer node and the corresponding virtual node after the business module executes overtime processing on the timer node.
Further, the service module is configured to, when performing timeout processing on a timer node, obtain a pointer pointing to a subsequent node in the timer node in advance.
Further, the management unit is further configured to:
and when a timing event needs to be deleted, deleting the corresponding timer node and reserving the virtual node corresponding to the timer node.
Further, the management unit is further configured to:
when a timing event needs to be modified, modifying the overtime time of the corresponding timer node, and adjusting the timer node to the corresponding position of the linked list according to the sequence of the modified overtime time;
and adjusting the virtual node corresponding to the timer node to be behind the timer node.
Further, the linked list is a sorting linked list or a scale timer linked list.
The invention has the beneficial effects that: according to the linked list-based timer management method and device, the virtual nodes are added in the timer linked list, and a timer mechanism is constructed based on the virtual nodes. Even aiming at some service modules with complex logic and high performance requirements, the problems of event scheduling errors and equipment hang-up under some special operations can be avoided on the basis of ensuring the performance of the timer.
Drawings
FIG. 1 is a diagram illustrating a structure of a linked list of a scale timer in the prior art;
FIG. 2 is a schematic diagram of a linked list node in the prior art;
FIG. 3 is a schematic structural diagram of a linked list node according to an embodiment of the present invention;
FIG. 4 is a schematic structural diagram of a linked list after adding a timer node according to an embodiment of the present invention;
FIG. 5 is a diagram illustrating a structure of a linked list after deleting a timer node according to an embodiment of the present invention;
FIG. 6 is a schematic structural diagram of a linked list of a scale timer according to an embodiment of the present invention;
fig. 7 is a schematic structural diagram of a scale timer linked list after modifying the timeout according to the embodiment of the present invention.
Detailed Description
Embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
The invention aims to provide a timer management method and a timer management device based on a linked list, so as to avoid the problems of event scheduling errors and equipment hang-up and improve the performance of a timer. The main technical scheme comprises the following steps: creating a linked list for managing timers; when a timing event needs to be added, applying for and configuring a timer node, and inserting the timer node to a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node; adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node; and after the business module executes overtime processing on the timer node, deleting the timer node and the corresponding virtual node.
The invention adds virtual nodes in the linked list and constructs a new timer mechanism according to the added virtual nodes. Specifically, when a timing event needs to be added, a timer node is applied and configured, and is inserted into a corresponding position of a linked list, meanwhile, a corresponding virtual node is added behind the timer node, and after the event corresponding to the timer node is executed, the timer node and the corresponding virtual node are deleted. Because the virtual nodes are only used for traversing the linked list, only can be created and deleted by the timer, and are opaque to the service module, in order to reduce the memory consumption, the virtual nodes only store the pointers of the previous node and the next node. By adding the virtual nodes and constructing a timer mechanism according to the virtual nodes, the problems of event scheduling errors and equipment hang-up under some special operations are avoided.
For example, when a timer node is executed, the current timer node or a subsequent timer node is deleted, and since the corresponding virtual node is retained when the timer node is deleted, the pointer of the next timer node can still be accurately found through the corresponding virtual node, thereby avoiding accessing an incorrect memory and the problem of hanging up. For another example, when a timer node is executed, a new timer node is inserted, and since the service module obtains the pointer of the virtual node in advance, even if a new timer node is added after the last timer node, the new timer node is processed in sequence. For another example, when executing a timer node, the timeout time of the next timer node is modified, and since the position of the corresponding virtual node is adjusted synchronously, when executing timeout processing, the service module can avoid that an event which is not timed out due to the fact that a pointer of the next node is obtained in advance is executed in advance.
Examples
The timer management method based on the linked list in the embodiment of the invention comprises the following steps:
step 1, creating a linked list for managing a timer;
step 2, when a timing event needs to be added, applying for and configuring a timer node, and inserting the timer node to a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node;
step 3, adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and 4, deleting the timer node and the corresponding virtual node after the service module performs overtime processing on the timer node.
In this embodiment, the linked list may be a sorting linked list or a scale timer linked list. The sorted linked list is a simple ordered link, and the earlier the node in the linked list is, the earlier the node expires, that is, the shorter the timeout time is. The scale timer linked list has two arrays, each array node is a linked list head, the linked list of each node of the first array stores all events expired within one second, and the linked list of each node of the second array stores events expired within 255 seconds.
Referring to fig. 3, in the embodiment, by adding virtual nodes in the linked list, the manner of adding and deleting event nodes in the timer mechanism is as follows:
1. increasing timing events
In this embodiment, when a timing event needs to be added, a timer node is applied and configured, the timer node is inserted into a corresponding position of the linked list, and a corresponding virtual node is added behind the timer node. Referring to fig. 3, if a new timing event needs to be added, a timer node 4 is applied and configured, and is inserted into a corresponding position of the linked list. For the sorting linked list, the timer node 4 is inserted into the corresponding position of the linked list according to the sequence of the overtime time. For the scale timer linked list, the timer node 4 is inserted into the corresponding position of the corresponding scale of the linked list according to the timeout time. Assuming timer node 4 is after timer node 3, the new linked list is shown in FIG. 4.
2. Deleting timed events
In this embodiment, if the timing event needs to be deleted, the corresponding timer node is deleted, and the virtual node corresponding to the timer node is reserved. As shown in fig. 3, if the timer node 2 is deleted, but the virtual node b cannot be deleted at the same time, because the pointer of the virtual node b may have been fetched by the timer scheduling mechanism and cannot be modified any more. Then the new linked list refers to fig. 5 after node 2 is deleted.
3. Modifying timed events
In the embodiment, when a timing event needs to be modified, the overtime time of the corresponding timer node is modified, and the timer node is adjusted to the corresponding position of the linked list according to the sequence of the modified overtime time; and adjusting the virtual node corresponding to the timer node to be behind the timer node.
Referring to fig. 6, taking a scale timer as an example (the order linked list timers have the same logic, and are not described herein), assuming that the timeout time of the timer nodes 1, 2, and 3 is 0s, the timeout time of the timer node 3 is now changed to 6s, and then when the position of the timer node 3 is modified, the virtual node c needs to be adjusted to the rear of the timer node 3 again. After the position of node 3 is changed, the new linked list is shown in fig. 7.
4. Timer scheduling
In this embodiment, after the service module executes timeout processing on the timer node, the corresponding timer node and the corresponding virtual node thereof are deleted. Referring to fig. 3, when the timer node 2 is scheduled and the event of the timer node 2 is executed, the corresponding virtual node b must be deleted at the same time to avoid memory leakage.
Based on the above timer mechanism, the present embodiment can avoid the problems of scheduling errors of events and hanging up of devices under some special operations, where the special operations at least include the following four scenarios:
1. when executing the timer node, the current timer node is deleted
In this embodiment, when the service module executes timeout processing on the timer node, a pointer pointing to a next node in the timer node is obtained in advance. Referring to fig. 3, when the service module processes the event of the timer node 1, the pointer of the virtual node a is taken out in advance, and if the node 1 is deleted when the event of the timer node 1 is executed, the pointer of the timer node 2 can still be accurately found through the virtual node a.
2. While executing a timer node, subsequent timer nodes are deleted
Referring to fig. 5, if the timer node 2 is deleted during the execution of the timer node 1, the next pointer of the virtual node a points to the virtual node b, and the timer node 3 can be accurately found by the virtual node b, and the event of the timer node 3 continues to be processed.
3. When executing the timer node, a new timer node is inserted
If the timer node is newly added when the timer node is executed, the newly added timer node can be directly accessed and executed after the timer node is executed.
4. While executing the timer node, the next timer node is modified
Referring to fig. 7, if the timeout time of the timer node 3 is modified during the execution of the timer node 2, the timer node 3 is adjusted to other linked lists, and after the execution of the timer node 2 is completed, the information of the virtual node b is updated, so that the wrong node is not accessed.
It can be seen that, with the timer mechanism of this embodiment, even for some service modules with relatively complex logic and relatively high performance requirements, the problems of event scheduling errors and equipment hang-up under some special operations can be avoided on the basis of ensuring the performance of the timer.
Based on the above technical solution, this embodiment further provides a timer management device based on a linked list, including:
a creating unit configured to create a linked list for managing a timer;
the management unit is used for applying and configuring a timer node when a timing event needs to be added, inserting the timer node into a corresponding position of a linked list, and storing a pointer pointing to a previous node and a pointer pointing to a next node in the timer node; adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and the deleting unit is used for deleting the timer node and the corresponding virtual node thereof after the business module executes overtime processing on the timer node.
It can be understood that, because the linked list-based timer management apparatus according to the embodiment of the present invention is an apparatus for implementing the linked list-based timer management method according to the embodiment, for the apparatus disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is simpler, and for relevant points, reference may be made to the partial description of the method.

Claims (10)

1. A linked list based timer management method, the method comprising:
creating a linked list for managing timers;
when a timing event needs to be added, applying for and configuring a timer node, and inserting the timer node to a corresponding position of a linked list, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the timer node;
adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and after the business module executes overtime processing on the timer node, deleting the timer node and the corresponding virtual node.
2. The linked-list based timer management method of claim 1, wherein the method further comprises:
when the business module executes overtime processing to the timer node, a pointer pointing to a next node in the timer node is obtained in advance.
3. The linked-list based timer management method of claim 2, wherein the method further comprises:
and when a timing event needs to be deleted, deleting the corresponding timer node and reserving the virtual node corresponding to the timer node.
4. The linked-list based timer management method of claim 1, wherein the method further comprises:
when a timing event needs to be modified, modifying the overtime time of the corresponding timer node, and adjusting the timer node to the corresponding position of the linked list according to the sequence of the modified overtime time;
and adjusting the virtual node corresponding to the timer node to be behind the timer node.
5. A linked list based timer management method according to any one of claims 1 to 4, wherein the linked list is a sorted linked list or a scale timer linked list.
6. A linked list based timer management apparatus, comprising:
a creating unit for creating a linked list for managing a timer;
the management unit is used for applying and configuring a timer node when a timing event needs to be added, inserting the timer node into a corresponding position of a linked list, and storing a pointer pointing to a previous node and a pointer pointing to a next node in the timer node; adding a corresponding virtual node behind the timer node, wherein a pointer pointing to a previous node and a pointer pointing to a next node are stored in the virtual node;
and the deleting unit is used for deleting the timer node and the corresponding virtual node after the business module executes overtime processing on the timer node.
7. The linked list-based timer management apparatus of claim 6, wherein the service module is configured to obtain a pointer to a next node in the timer node in advance when performing timeout processing on the timer node.
8. The linked-list based timer management apparatus of claim 7, wherein the management unit is further to:
and when a timing event needs to be deleted, deleting the corresponding timer node and reserving the virtual node corresponding to the timer node.
9. The linked-list based timer management apparatus of claim 6, wherein the management unit is further to:
when a timing event needs to be modified, modifying the overtime time of the corresponding timer node, and adjusting the timer node to the corresponding position of the linked list according to the sequence of the modified overtime time;
and adjusting the virtual node corresponding to the timer node to be behind the timer node.
10. A linked list-based timer management apparatus as claimed in any one of claims 6 to 9, wherein said linked list is a sorted linked list or a scaled timer linked list.
CN202211664217.3A 2022-12-22 2022-12-22 Timer management method and device based on linked list Pending CN115981854A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211664217.3A CN115981854A (en) 2022-12-22 2022-12-22 Timer management method and device based on linked list

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211664217.3A CN115981854A (en) 2022-12-22 2022-12-22 Timer management method and device based on linked list

Publications (1)

Publication Number Publication Date
CN115981854A true CN115981854A (en) 2023-04-18

Family

ID=85959194

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211664217.3A Pending CN115981854A (en) 2022-12-22 2022-12-22 Timer management method and device based on linked list

Country Status (1)

Country Link
CN (1) CN115981854A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117056059A (en) * 2023-10-12 2023-11-14 常州楠菲微电子有限公司 Timer realizing method and device, storage medium and electronic equipment

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117056059A (en) * 2023-10-12 2023-11-14 常州楠菲微电子有限公司 Timer realizing method and device, storage medium and electronic equipment
CN117056059B (en) * 2023-10-12 2024-02-02 常州楠菲微电子有限公司 Timer realizing method and device, storage medium and electronic equipment

Similar Documents

Publication Publication Date Title
US5870761A (en) Parallel queue propagation
US5781912A (en) Recoverable data replication between source site and destination site without distributed transactions
US5157620A (en) Method for simulating a logic system
WO2020024405A1 (en) Test method, device, server and storage medium based on distributed coordination
US5870760A (en) Dequeuing using queue batch numbers
US11132294B2 (en) Real-time replicating garbage collection
CN105279261B (en) Dynamic scalable database filing method and system
US20050004952A1 (en) Transaction processing method, transaction control apparatus and program thereof
CN106649057A (en) Method and device for testing server pressure
JP2010500673A (en) Storage management system for maintaining consistency of remote copy data (storage management system, storage management method, and computer program)
US9246852B2 (en) Lossless time based data acquisition and control in a distributed system
CN110647511A (en) Data synchronization method, computing device and computer storage medium
US20050166116A1 (en) Method and apparatus for encoding and generating transaction-based stimulus for simulation of VLSI circuits
CN110502523A (en) Business datum storage method, device, server and computer readable storage medium
CN115981854A (en) Timer management method and device based on linked list
US20050091026A1 (en) Modelling and simulation method
CN113157467B (en) Multi-process data output method
US20070162646A1 (en) Direct memory access devices and method based timer system
US20110302377A1 (en) Automatic Reallocation of Structured External Storage Structures
CN101071404A (en) Small-capacity FIFO storage device data-moving trigger and method
CN111158930A (en) Redis-based high-concurrency time-delay task system and processing method
US6587958B1 (en) Event timer
CN110445580A (en) Data transmission method for uplink and device, storage medium, electronic device
CN115495527A (en) Data synchronization management system and method
JPH05101015A (en) Device and method for simulation

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination