A timer implementation method and device under a Linux system

By using a combination of a min-heap priority queue and hash buckets to manage timers in the Linux system, the problem of low efficiency in timer lookup and replacement is solved, achieving efficient timer management and memory optimization.

CN115495210BActive Publication Date: 2026-03-20WUHAN MARITIME COMMUNICATION RESEARCH INSTITUTE
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-06
Publication Date
2026-03-20

AI Technical Summary

Technical Problem

In existing Linux systems, timers cannot be quickly found and replaced, and the time wheel method for maintaining timers sacrifices some precision and flexibility.

Method used

A priority queue with a min-heap combined with a hash bucket is used to manage timers. The hash bucket stores timers with the same timeout value, and the size of the priority queue is dynamically adjusted to optimize memory usage.

Benefits of technology

It improves the efficiency of finding, deleting and adding timers, reduces memory usage, optimizes the timer creation process, and achieves the minimum timer acquisition with O(1) time complexity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115495210B_ABST
    Figure CN115495210B_ABST
Patent Text Reader

Abstract

The application provides a timer implementation method and device under a Linux system, comprising: creating a priority queue based on a minimum heap; allocating a hash bucket for the priority queue; when a new timer is added, judging whether the timeout time of the new timer is greater than a threshold, if the timeout time is not greater than the threshold, judging whether a bucket corresponding to the timeout time of the new timer exists in the hash bucket, if the bucket exists, adding the new timer to the corresponding bucket, if the bucket does not exist, adding the new timer to the hash bucket and the priority queue; the timeout time refers to the time difference between the timeout time of the new timer and the current time; when the timer is running, judging whether the timer at the head node of the priority queue is overdue, if the timer is overdue, removing the head node, moving the tail node of the priority queue to the position of the head node, and then moving the head node downward according to the minimum heap principle; then, emptying the bucket corresponding to the removed node in the hash bucket, and executing and deleting all timers in the corresponding bucket.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application belongs to the field of Linux system software, and more particularly relates to a timer implementation method and device under a Linux system. BACKGROUND

[0002] In data communication of the Linux system, timers are often needed to be used to realize data retransmission, state information acquisition at a fixed time, etc. The Linux system provides several technologies for realizing process timers, such as alarm, usleep, timerset, and epoll. Except for the epoll, the other timer technologies have the disadvantages of low precision, being easily affected by signals, and being unable to support multiple timers. Therefore, the present application selects to use the epoll technology to realize the process timer of the Linux system.

[0003] At present, common timer maintenance methods include ascending linked list, minimum heap priority queue, and time wheel. The ascending linked list has low insertion operation efficiency, and when there are a large number of timers in the system, performance problems exist. The minimum heap priority queue has an insertion performance several times better than that of the ascending linked list, but when there are same time timers, it cannot quickly find and delete the same time timers. The time wheel method sacrifices certain precision and flexibility. SUMMARY

[0004] In view of the defects of the prior art, the present application aims to provide a timer implementation method and device under a Linux system, and aims to solve the problems that the same timer in the existing Linux system cannot be quickly found and replaced, and that the same timer is maintained by the time wheel method, which sacrifices certain precision and flexibility.

[0005] To achieve the above-mentioned purpose, in a first aspect, the present application provides a timer implementation method under a Linux system, which is applied to a Linux system and includes the following steps:

[0006] A priority queue of a preset size is created based on a minimum heap to store the timers in order of timeout time from front to back;

[0007] A hash bucket is allocated for the priority queue; the hash bucket includes multiple buckets, and each bucket is used to store the timers of the same timeout time in the form of a linked list;

[0008] When a new timer is added, it is determined whether the timeout time of the new timer is greater than a threshold value. If the timeout time is greater than the threshold value, the new timer is directly added to the priority queue. If the timeout time is not greater than the threshold value, it is determined whether a bucket corresponding to the timeout time of the new timer exists in the hash bucket. If the bucket exists, the new timer is added to the corresponding bucket. If the bucket does not exist, the new timer is added to the hash bucket and the priority queue. The timeout time refers to a time difference between the timeout time of the new timer and a current time.

[0009] When a timer is run, it is determined whether a timer at a head node of the priority queue is timed out. If the timer is timed out, the head node is removed, and a tail node of the priority queue is moved to a position of the head node. Then, the head node is moved downward according to a minimum heap principle, so as to move a timer with a minimum timeout time to the head node of the priority queue. Subsequently, a bucket corresponding to the removed node in the hash bucket is emptied, and all timers in the corresponding bucket are executed and deleted.

[0010] In an optional example, the method further includes the following steps:

[0011] A timer linked list is reserved, the timer linked list including a plurality of unused timer nodes. When a new timer is added, a timer node is directly taken from the timer linked list. After a timer is deleted, a corresponding timer node is recycled to the timer linked list, so as to avoid frequent application and release of memory corresponding to the timer node.

[0012] In an optional example, if the timeout time is not greater than the threshold value, it is determined whether a bucket corresponding to the timeout time of the new timer exists in the hash bucket. If the bucket does not exist, the new timer is added to the hash bucket and the priority queue. Specifically, the following steps are performed:

[0013] An empty bucket is selected from the hash bucket to store the new timer.

[0014] It is determined whether the priority queue is full. If the priority queue is full, the priority queue is expanded. If the priority queue is not full, the priority queue is not expanded.

[0015] A node of the new timer is added to a tail of the priority queue. Then, the tail node is moved upward according to a minimum heap principle, so as to move a timer with a minimum timeout time to a head node of the priority queue.

[0016] In an optional example, the priority queue is expanded. Specifically, the following steps are performed:

[0017] A new priority queue with a size twice that of an existing priority queue is created, and contents of the existing priority queue are copied. Then, the existing priority queue is released, and the new priority queue is used as the expanded priority queue.

[0018] In an optional example, after the head node is moved down according to the minimum heap principle, and before the bucket corresponding to the removed node in the hash bucket is emptied, the method further comprises the following steps:

[0019] If the number of the priority queue in use is less than half, a new priority queue with a size of 1 / 2 of the existing priority queue is created, the content of the existing priority queue is copied, and then the existing priority queue is released, and the new priority queue is used as the reduced priority queue; if the number of the priority queue in use is not less than half, the existing priority queue is not reduced.

[0020] In an optional example, the method further comprises the following steps:

[0021] If the timeout time of a timer in the priority queue has not arrived, and the timer needs to be deleted, it is determined whether the timeout time of the timer to be deleted is greater than a threshold value; if the timeout time of the timer to be deleted is greater than the threshold value, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted; if the timeout time of the timer to be deleted is not greater than the threshold value, the corresponding timer is deleted in the hash bucket, and after the deletion, if the bucket where the timer to be deleted is located is empty, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted; if the bucket where the timer to be deleted is located is not empty, the head node and the pointer of the linked list at the bucket where the timer to be deleted is located are updated, and the node in the priority queue is updated.

[0022] After the tail node of the priority queue is moved to the node of the timer to be deleted, it is determined whether the timeout time of the node of the timer to be deleted is less than the timeout time of the parent node of the node of the timer to be deleted; if the timeout time of the node of the timer to be deleted is less than the timeout time of the parent node of the node of the timer to be deleted, the node of the timer to be deleted and the node of the parent node are exchanged, and the adjustment is repeated until the node of the timer to be deleted has no parent node or the timeout time of the node of the timer to be deleted is not less than the timeout time of the parent node; if the timeout time of the node of the timer to be deleted is not less than the timeout time of the parent node of the node of the timer to be deleted, the node of the timer to be deleted and the node with a smaller timeout time among the child nodes are compared; if the timeout time of the node of the timer to be deleted is greater than the timeout time of the compared node, the node of the timer to be deleted and the compared node are exchanged, and the adjustment is repeated until the node of the timer to be deleted has no child node or the timeout time of the node of the timer to be deleted is not greater than the timeout time of the child node, thereby completing the adjustment of the priority queue to meet the minimum heap requirement.

[0023] After the timer node is deleted, it is determined whether the number of the priority queue in use is less than half; if the number of the priority queue in use is less than half, the priority queue is reduced; if the number of the priority queue in use is not less than half, the priority queue is not reduced.

[0024] In a second aspect, the application provides an implementation device of a timer in a Linux system, which is applied to the Linux system, and the device comprises:

[0025] A priority queue creation unit is configured to create a priority queue with a preset size based on a minimum heap, so as to store the timers in the order of the timeout time from front to back.

[0026] a hash bucket allocation unit configured to allocate a hash bucket for the priority queue; the hash bucket comprises a plurality of buckets, each bucket being configured to store timers of a same timeout time via a linked list;

[0027] a timer adding unit configured to, when adding a timer, determine whether a timeout time of the added timer is greater than a threshold value, and if the timeout time is greater than the threshold value, directly add the added timer to the priority queue; if the timeout time is not greater than the threshold value, determine whether a bucket corresponding to a timeout time of the added timer exists in the hash bucket, and if the bucket exists, add the added timer to the corresponding bucket, and if the bucket does not exist, add the added timer to the hash bucket and the priority queue; the timeout time refers to a time difference between the timeout time of the added timer and a current time;

[0028] a timer running unit configured to, when running a timer, determine whether a timer at a head node of the priority queue is expired, and if the timer is expired, remove the head node, and move a tail node of the priority queue to a position of the head node, and then move the head node downward according to a minimum heap principle to move a timer with a smallest timeout time to the head node of the priority queue; and then empty a bucket corresponding to the removed node in the hash bucket, and execute and delete all timers in the corresponding bucket.

[0029] In an optional example, the apparatus further comprises:

[0030] a timer reserving unit configured to reserve a timer linked list, the timer linked list comprising a plurality of unused timer nodes, so that when adding a timer, a timer node is directly taken from the timer linked list, and after deleting a timer, a corresponding timer node is recycled to the timer linked list, thereby avoiding frequent application and release of memory corresponding to the timer node.

[0031] In an optional example, if the timeout time is not greater than the threshold value, the timer adding unit determines whether a bucket corresponding to a timeout time of the added timer exists in the hash bucket, and if the bucket does not exist, the timer adding unit adds the added timer to the hash bucket and the priority queue, specifically: the timer adding unit selects an empty bucket in the hash bucket to store the added timer; determines whether the priority queue is full, and if the priority queue is full, expands the priority queue, and if the priority queue is not full, does not expand the priority queue; and adds a node of the added timer to a tail of the priority queue, and then moves the tail node upward according to the minimum heap principle to move a timer with a smallest timeout time to a head node of the priority queue.

[0032] In an optional example, the apparatus further comprises a timer deleting unit;

[0033] If the timeout time of a timer in the priority queue has not arrived and the timer needs to be deleted, the timer deletion unit judges whether the timeout time of the timer to be deleted is greater than a threshold value, if yes, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted, if not, the corresponding timer is deleted in the hash bucket, after the deletion, if the bucket where the timer to be deleted is located is empty, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted, if the bucket where the timer to be deleted is located is not empty, the head node and the pointer of the linked list in the bucket where the timer to be deleted is located are updated, and the node in the priority queue is updated, after the tail node of the priority queue is moved to the node of the timer to be deleted, it is judged whether the timeout time of the node of the timer to be deleted is less than the timeout time of the parent node, if yes, the node of the timer to be deleted is exchanged with the parent node, and the adjustment is circularly performed until the node of the timer to be deleted has no parent node or the timeout time of the node of the timer to be deleted is not less than the timeout time of the parent node, if not, the node of the timer to be deleted is compared with the node with a smaller timeout time in the child node, if the timeout time of the node of the timer to be deleted is greater than the timeout time of the compared node, the node of the timer to be deleted is exchanged with the compared node, and the adjustment is circularly performed until the node of the timer to be deleted has no child node or the timeout time of the node of the timer to be deleted is not greater than the timeout time of the child node, thus the priority queue is adjusted to meet the characteristic requirement of the minimum heap, and after the node is deleted, it is judged whether the use quantity of the priority queue is less than half, if yes, the priority queue is reduced, if not, the priority queue is not reduced.

[0034] Compared with the prior art, the above technical scheme conceived by the present application has the following beneficial effects:

[0035] The present application provides a timer implementation method and device under Linux system, which manages the timer by the minimum heap priority queue, so that the time complexity of obtaining the minimum timer is only O(1); the hash bucket is used to store the timers with the same timeout time in the container of the key linked list corresponding to the timeout time, so that there is no node with the same element in the minimum heap priority queue, and the efficiency of searching, deleting and adding a timer with the same timeout time is improved. If there are N timers at a certain time, if only the minimum heap priority queue is used, the minimum heap needs to be adjusted N times when the timer at the time is executed, and the time complexity of each adjustment is O(n*logn), and in combination with the data structure of the hash bucket, the N timers can be quickly operated, and the minimum heap only needs to be adjusted once, which greatly improves the running efficiency of the timer.

[0036] The application provides a timer implementation method and device under a Linux system, in all operation processes of the timer, a process of dynamically adjusting a priority queue size is designed, and timer node memory is dynamically recycled according to the use condition of the priority queue, for example, after a large number of timers are created and run, the memory can be recycled, and unnecessary memory occupation is avoided.

[0037] The application provides a timer implementation method and device under a Linux system, the timer creation process is optimized, a certain number of timer node pools are designed as reservations, when a new timer is added, a node can be quickly obtained from the reserved pool for direct use, and when a timer is deleted, the timer can be directly recycled into the reserved pool, and frequent memory application and release are avoided. BRIEF DESCRIPTION OF DRAWINGS

[0038] Figure 1 is a timer implementation method flowchart provided by an embodiment of the application under a Linux system;

[0039] Figure 2 is a data structure relationship diagram provided by an embodiment of the application;

[0040] Figure 3 is a timer pool creation flowchart provided by an embodiment of the application;

[0041] Figure 4 is a timer addition flowchart provided by an embodiment of the application;

[0042] Figure 5 is a timer running flowchart provided by an embodiment of the application;

[0043] Figure 6 is a timer deletion flowchart provided by an embodiment of the application;

[0044] Figure 7 is a timer implementation device architecture diagram provided by an embodiment of the application under a Linux system. DETAILED DESCRIPTION

[0045] In order to make the purpose, technical scheme and advantages of the application clearer, the application is further described in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the application, and are not used to limit the application.

[0046] The application proposes a new timer maintenance method using a priority queue of a minimum heap in combination with a hash bucket, to realize efficient searching and deleting of the same time timer. The application uses an epoll technology, and realizes the Linux system timer based on the timer maintenance method of the priority queue of the minimum heap and the hash bucket.

[0047] The epoll asynchronous I / O technology monitors the timeout of the timer, and millisecond-level monitoring can be realized. The priority queue meets the characteristics of the minimum heap, and the minimum priority node is taken out from the queue each time. There are many ways to implement the priority queue, and the heap is used in the present application, so that the node of the priority queue is accessed most efficiently. The timer table entry of the Linux system process in the present application includes the priority queue (tpq) of the minimum heap of the timer, the timer linked list (tlist), and the timer hash bucket (thash). In order to realize efficient addition, deletion and running of the timer, and optimize the use of system resources, the implementation of the present application includes the following contents:

[0048] 1) A timer maintenance data structure combining the priority queue of the minimum heap and the hash bucket is designed. The priority queue tpq meeting the characteristics of the minimum heap stores all the timers in use, and the timer at a time exists only in the tpq; the hash bucket stores the timers with the same timeout time, and the timers are maintained in the structure of the linked list;

[0049] 2) The timer linked list is designed, and a certain number of unused timers are reserved to quickly add the timer and recycle the timer no longer in use;

[0050] 3) When the same timeout time timer is added, it is quickly added to the hash bucket, and the priority queue of the minimum heap does not need to be operated;

[0051] 4) When a large number of timers with the same timeout time are run, except the head node of the priority queue of the minimum heap, the remaining timers are quickly accessed through the timer linked list of the key at the timeout time in the hash bucket; only one adjustment of the priority queue is needed to meet the requirements of the minimum heap characteristics;

[0052] 5) When the timer is deleted, the timer can be quickly deleted in the hash bucket according to the timeout time key, and the priority queue of the minimum heap does not need to be adjusted;

[0053] 6) A certain number of timer node pools are designed as reservations, the new timer can be obtained from the reserved pool to be directly used, and the timer can be recycled into the reserved pool when the timer is deleted, so as to avoid frequent memory application and release.

[0054] Figure 1 The present application provides a timer implementation method of a Linux system, as shown in Figure 1 , including the following steps:

[0055] S101, a priority queue of a preset size is created based on a minimum heap to store the timers in the order of the timeout time from front to back;

[0056] S102, allocate a hash bucket for the priority queue; the hash bucket comprises a plurality of buckets, and each bucket is used to store timers of the same timeout time by means of a linked list;

[0057] S103, when a new timer is added, it is judged whether the timeout time of the new timer is greater than a threshold value, if the timeout time is greater than the threshold value, the new timer is directly added to the priority queue; if the timeout time is not greater than the threshold value, it is judged whether a bucket corresponding to the timeout time of the new timer exists in the hash bucket, if the bucket exists, the new timer is added to the corresponding bucket, if the bucket does not exist, the new timer is added to the hash bucket and the priority queue; the timeout time refers to the time difference between the timeout time of the new timer and the current time;

[0058] S104, when a timer is run, it is judged whether the timer at the head node of the priority queue is timed out, if the timer is timed out, the head node is removed, and the tail node of the priority queue is moved to the position of the head node, then the head node is moved downward according to the minimum heap principle, so as to move the timer with the smallest timeout time to the head node of the priority queue; subsequently, the bucket corresponding to the removed node in the hash bucket is emptied, and all the timers in the corresponding bucket are executed and deleted.

[0059] Figure 2 Fig. 1 is a data structure diagram of a timer in the application, tnode is a single timer node, and a plurality of timers in the system are jointly maintained by three data structures, i.e. a priority queue tpq satisfying the minimum heap feature, a hash bucket thash storing linked lists of different timeout times key, and a linked list tlist of unused timer nodes. Among them, the timeout time M represents that the device will be executed after M time; the timeout time N represents the start time of the device plus the timeout time.

[0060] Figure 3 Fig. 2 is a flow chart of creating a timer pool in the application, a priority queue tpq of a certain size of minimum heap is created and initialized in turn, a timer linked list tlist and a hash bucket thash are created, and the creation and initialization of the initial timer pool are completed.

[0061] As shown in Fig. 3, the adding process of the application is as follows: Figure 4

[0062] 1) Check the reserved timer linked list tlist, if there is an unused timer node tnode, take it out from tlist and initialize it, if there is no unused timer node, create a new timer and initialize it;

[0063] ​2) Further, when the timer timeout is greater than 10 seconds, it is directly added to tpq; when the timer timeout is less than or equal to 10 seconds and the timeout key does not exist in thash, the timer is added to both thash and tpq; if the timer timeout is less than or equal to 10 seconds and the timeout key exists in thash, it is directly added to thash, at which point the timer addition process ends, and there is no need to operate the priority queue tpq again. The timeout key is obtained by taking the timeout modulo 10000 (this invention implements a millisecond-level timer, so 10000 is 10 seconds).

[0064] 3) Furthermore, when adding to tpq, if the queue is full, a new priority queue twice the size of the existing queue is created, the contents of the existing queue are copied, and then the original queue is released.

[0065] 4) Further, the way to add a timer to tpq is to add the new timer to the end of tpq, compare its timeout time with the parent node in a loop, and if the timeout time is less than the parent node, swap its position with the parent node until there is no parent node or the timeout time is not less than the parent node, so that it meets the characteristics of a min-heap, thus completing the entire timer addition process.

[0066] like Figure 5 As shown, the timer operation process of this invention is as follows:

[0067] 1) When running the timer, the timeout time of the head node in TPQ is used as the timeout time of epoll_wait. The timer is monitored by a millisecond-level loop to check if it has timed out. If it has not timed out, the loop continues to block and monitor and wait. When the timeout time is reached, the timer is started to be processed.

[0068] 2) Further, when processing the timer node, the head node in tpq is retrieved and the tail node A is moved to the head node position. If node A has child nodes at this time, it is compared with node B, which has a smaller timeout time among the child nodes. If A has a larger timeout time than node B, its position is swapped with node B. This process is repeated until node A has no child nodes or its timeout time is not greater than that of its child nodes, so that it meets the minimum heap characteristics. At the same time, the bucket of the key with the timeout time in thash is set to empty.

[0069] 3) Furthermore, based on the number of tpqs used after adjustment, the size of the tpq is dynamically adjusted. If the current number of tpqs used is less than half of the number of tpqs requested, a priority queue of half the size is created, the current tpq is copied to the new tpq, and the original queue is released.

[0070] 4) Further, traverse the timer linked list of the timeout time instant, and execute the callback function of the timer one by one, and adjust the size of tlist, if the number of nodes to be used does not exceed the threshold M, then recycle the timer nodes into tlist, and if the number of nodes to be used exceeds the threshold M, then release the timer nodes, thus completing the deleting timer process.

[0071] 5) Further, after completing the traversal operation of the time linked list, enter the next epoll_wait loop monitoring processing flow.

[0072] As shown in Figure 6 the deleting timer flow of the application is as follows:

[0073] 1) When deleting the timer, judge whether the timeout time of the timer is greater than 10 seconds, if yes, then move the tail node of tpq to the position to be deleted, if no, then delete it in thash, if the linked list of the key of the timeout time instant is not empty after deletion, then update the node in tpq to the head node of the key linked list at present, update the corresponding pointer, thus completing the deleting timer flow, if the hash bucket linked list of the key of the timeout time instant is empty after deletion, then move the tail node of tpq to the position to be deleted.

[0074] 2) Further, after moving the tail node A of tpq to the deleting position, judge whether the timeout time instant of node A is less than the parent node, if yes, then exchange the position with the parent node, and adjust in a loop until node A has no parent node or the timeout time instant is not less than the parent node, if no, then compare node A with node B which has a smaller timeout time instant in the child nodes, if the timeout time instant of node A is greater than that of node B, then exchange the position with node B, and adjust in a loop until node A has no child node or the timeout time instant is not greater than the child node, thus completing the priority queue adjustment of the timer, so as to meet the characteristic requirement of the minimum heap;

[0075] 3) Further, according to the use quantity of the adjusted tpq, dynamically adjust the size of the tpq, if the current use quantity is less than half of the application quantity of the tpq, then create a priority queue with half size, copy the current tpq into the new tpq, and release the original queue.

[0076] 4) Further, adjust the size of tlist, if the number of nodes to be used does not exceed the threshold M, then recycle the timer nodes into tlist, if the number of nodes to be used exceeds the threshold M, then release the timer nodes, thus completing the deleting timer flow.

[0077] Figure 7 is the implementation device architecture diagram of the timer under Linux system provided by the embodiment of the application, as shown in Figure 7As shown, the method comprises: a priority queue creating unit 710, a hash bucket allocating unit 720, a timer adding unit 730, a timer running unit 740, a timer reserving unit 750, and a timer deleting unit 760.

[0078] The priority queue creating unit 710 is configured to create a priority queue of a preset size based on a minimum heap, so as to store timers in order of timeout time from front to back.

[0079] The hash bucket allocating unit 720 is configured to allocate a hash bucket for the priority queue; the hash bucket comprises a plurality of buckets, and each bucket is configured to store timers of the same timeout time in the form of a linked list.

[0080] The timer adding unit 730 is configured to, when adding a timer, determine whether the timeout time of the added timer is greater than a threshold value, and if the timeout time is greater than the threshold value, directly add the added timer to the priority queue; if the timeout time is not greater than the threshold value, determine whether there is a bucket corresponding to the timeout time of the added timer in the hash bucket, and if there is, add the added timer to the corresponding bucket, and if there is not, add the added timer to the hash bucket and the priority queue; the timeout time refers to the time difference between the timeout time of the added timer and the current time.

[0081] The timer running unit 740 is configured to, when running a timer, determine whether the timer at the head node of the priority queue is overdue, and if the timer is overdue, remove the head node, move the tail node of the priority queue to the position of the head node, and then move the head node downward according to the minimum heap principle to move the timer with the smallest timeout time to the head node of the priority queue; and then empty the bucket corresponding to the removed node in the hash bucket, and execute and delete all timers in the corresponding bucket.

[0082] The timer reserving unit 750 is configured to reserve a timer linked list, the timer linked list comprising a plurality of unused timer nodes, so that when a timer is added, a timer node is directly taken from the timer linked list, and after a timer is deleted, the corresponding timer node is recycled to the timer linked list, thereby avoiding frequent application and release of memory corresponding to the timer node.

[0083] If the timeout time of a timer in the priority queue has not arrived, and the timer needs to be deleted, the timer deletion unit 760 judges whether the timeout time of the timer to be deleted is greater than a threshold value. If yes, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted. If no, the corresponding timer is deleted in the hash bucket. After the deletion, if the bucket where the timer to be deleted is located is empty, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted. If the bucket where the timer to be deleted is located is not empty, the head node and the pointer of the linked list in the bucket are updated, and the node in the priority queue is updated. After the tail node of the priority queue is moved to the node of the timer to be deleted, it is judged whether the timeout time of the node of the timer to be deleted is less than the timeout time of its parent node. If yes, the node of the timer to be deleted is exchanged with the parent node. The adjustment is repeated until the node of the timer to be deleted has no parent node or its timeout time is not less than the timeout time of the parent node. If no, the node of the timer to be deleted is compared with the node having a smaller timeout time in the child nodes. If the timeout time of the node of the timer to be deleted is greater than the timeout time of the compared node, the node of the timer to be deleted is exchanged with the compared node. The adjustment is repeated until the node of the timer to be deleted has no child node or its timeout time is not greater than the timeout time of the child node. Thus, the adjustment of the priority queue is completed, and the priority queue meets the requirement of the minimum heap. After the node is deleted, it is judged whether the number of the priority queue is less than half. If yes, the priority queue is reduced. If no, the priority queue is not reduced.

[0084] It can be understood that, Figure 7 The detailed function implementation of each unit in the device can refer to the description in the foregoing method embodiments, which will not be repeated here.

[0085] It can be understood by those skilled in the art that the above description is only the preferred embodiment of the present application, and is not intended to limit the present application. Any modification, equivalent replacement and improvement within the spirit and principle of the present application should be included in the protection scope of the present application.

Claims

1. A method for implementing a timer under a Linux system, characterized in that, This method is applied to Linux systems and includes the following steps: A priority queue of a preset size is created based on a min-heap to store timers in order of their timeout times from front to back; Allocate hash buckets to the priority queue; each hash bucket includes multiple buckets, and each bucket is used to store timers for the same timeout period in a linked list manner; When adding a new timer, it is determined whether the timeout time of the new timer is greater than the threshold. If the timeout time is greater than the threshold, it is directly added to the priority queue. If the timeout time is not greater than the threshold, then it is determined whether there is a bucket in the hash bucket corresponding to the timeout time of the new timer. If there is, the new timer is added to the corresponding bucket. If there is no, the new timer is added to the hash bucket and the priority queue. The timeout time refers to the time difference between the timeout time of the new timer and the current time. When the timer is running, it is determined whether the timer at the head node of the priority queue has timed out. If it has timed out, the head node is removed and the tail node of the priority queue is moved to the head node. Then, according to the min-heap principle, the head node is moved down so that the timer with the shortest timeout is moved to the head node of the priority queue. After that, the bucket corresponding to the removed node in the hash bucket is cleared and all timers in the corresponding bucket are executed and deleted.

2. The method according to claim 1, characterized in that, It also includes the following steps: A timer list is reserved, which includes multiple unused timer nodes. When a new timer is added, the timer node is directly retrieved from the timer list. After a timer is deleted, the corresponding timer node is recycled back to the timer list, avoiding frequent allocation and release of memory corresponding to timer nodes.

3. The method according to claim 1, characterized in that, If the timeout period is not greater than the threshold, then it is determined whether the hash bucket contains the bucket corresponding to the timeout of the newly added timer. If not, the newly added timer is added to the hash bucket and the priority queue, specifically: Select an empty bucket from the hash bucket to store the newly added timer; Determine whether the priority queue is full. If it is full, expand the priority queue. If it is not full, do not expand the priority queue for the time being. The newly added timer node is added to the tail of the priority queue. Then, according to the min-heap principle, the tail node is moved up so that the timer with the smallest timeout is moved to the head node of the priority queue.

4. The method according to claim 3, characterized in that, The priority queue is expanded as follows: Create a new priority queue that is twice the size of the existing priority queue, copy the contents of the existing priority queue, then release the original priority queue and use the new priority queue as the expanded priority queue.

5. The method according to claim 1, characterized in that, After moving the head node down according to the min-heap principle and before clearing the hash bucket corresponding to the removed node, the following steps are also included: If the number of priority queues in use is less than half, a new priority queue of 1 / 2 the size of the existing priority queue is created, and the contents of the existing priority queue are copied. The original priority queue is then released, and the new priority queue is used as the shrunk priority queue. If the number of priority queues in use is not less than half, the existing priority queue is not shrunk.

6. The method according to any one of claims 1 to 5, characterized in that, It also includes the following steps: If a timer in the priority queue has not yet expired and needs to be deleted: determine if the timeout of the timer to be deleted is greater than a threshold. If it is greater than the threshold, move the tail node of the priority queue to the node corresponding to the timer to be deleted. If it is not greater than the threshold, delete the corresponding timer in the hash bucket. After deletion, if the bucket containing the timer to be deleted is empty, move the tail node of the priority queue to the node corresponding to the timer to be deleted. If the bucket containing the timer to be deleted is not empty, update the head node and pointer of the linked list in the bucket containing the timer to be deleted, and update the nodes in the priority queue. After the tail node of the priority queue is moved to the timer node to be deleted, it is determined whether the timeout of the timer node to be deleted is less than the timeout of its parent node. If it is less, the timer node is swapped with its parent node, and the process is repeated until the timer node to be deleted has no parent node or its timeout is not less than the timeout of its parent node. If it is not less, the timer node to be deleted is compared with the child node with the smaller timeout. If the timeout of the child node is larger than the timeout of the child node, the timer node to be deleted is swapped with the child node, and the process is repeated until the timer node to be deleted has no child nodes or its timeout is not greater than the timeout of its child nodes. This completes the priority queue adjustment, making it meet the characteristics of a min-heap. After deleting the aforementioned timer nodes, check if the number of priority queues in use is less than half. If it is, reduce the number of queues; otherwise, do not reduce the number of queues.

7. A timer implementation device under a Linux system, characterized in that, This device is used in Linux systems and includes: The priority queue creation unit is used to create a priority queue of a preset size based on a min-heap, so as to store timers in the order of timeout times from front to back; A hash bucket allocation unit is used to allocate hash buckets to the priority queue; the hash bucket includes multiple buckets, and each bucket is used to store timers for the same timeout period in a linked list; The timer addition unit is used to determine whether the timeout of the new timer is greater than a threshold when adding a new timer. If the timeout is greater than the threshold, the timer is directly added to the priority queue. If the timeout is not greater than the threshold, the unit checks whether the hash bucket corresponding to the timeout of the new timer exists. If it exists, the new timer is added to the corresponding bucket. If it does not exist, the new timer is added to both the hash bucket and the priority queue. The timeout refers to the time difference between the timeout of the new timer and the current time. The timer execution unit is used to determine whether the timer at the head node of the priority queue has timed out when the timer is running. If it has timed out, the head node is removed and the tail node of the priority queue is moved to the head node. Then, according to the min-heap principle, the head node is moved down so that the timer with the shortest timeout is moved to the head node of the priority queue. After that, the hash bucket corresponding to the removed node is cleared and all timers in the corresponding bucket are executed and deleted.

8. The apparatus according to claim 7, characterized in that, Also includes: A timer reservation unit is used to reserve a timer linked list. The timer linked list includes multiple unused timer nodes so that when a new timer is added, the timer node can be directly retrieved from the timer linked list. After a timer is deleted, the corresponding timer node is recycled back to the timer linked list, avoiding frequent allocation and release of memory corresponding to timer nodes.

9. The apparatus according to claim 7, characterized in that, If the timeout period is not greater than the threshold, the timer addition unit determines whether there is a bucket in the hash bucket corresponding to the timeout of the new timer. If not, the timer addition unit adds the new timer to the hash bucket and the priority queue. Specifically, the timer addition unit selects an empty bucket in the hash bucket to store the new timer; determines whether the priority queue is full. If it is full, the priority queue is expanded. If it is not full, the priority queue is not expanded for the time being; and adds the node of the new timer to the tail of the priority queue. Then, according to the min-heap principle, the tail node is moved up to move the timer with the smallest timeout period to the head node of the priority queue.

10. The apparatus according to any one of claims 7 to 9, characterized in that, Also includes: Timer deletion unit; If a timer in the priority queue has not yet expired and needs to be deleted: the timer deletion unit determines whether the timeout of the timer to be deleted is greater than a threshold. If it is greater than the threshold, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted. If it is not greater than the threshold, the corresponding timer is deleted from the hash bucket. After deletion, if the bucket containing the timer to be deleted is empty, the tail node of the priority queue is moved to the node corresponding to the timer to be deleted. If the bucket containing the timer to be deleted is not empty, the head node and pointer of the linked list in the bucket containing the timer to be deleted are updated, and the nodes in the priority queue are updated. After the tail node of the priority queue is moved to the node of the timer to be deleted, the timer to be deleted is determined... If the timeout of a node is less than the timeout of its parent node, it is swapped with its parent node. This process is repeated until the timer node to be deleted has no parent node or its timeout is not less than the parent node's timeout. If it is not less, the timer node to be deleted is compared with the child node with the smaller timeout. If the relative timeout of the child node is larger, it is swapped with the child node. This process is repeated until the timer node to be deleted has no child nodes or its timeout is not greater than the timeout of its child nodes. This completes the priority queue adjustment, ensuring it meets the min-heap requirements. After deleting the aforementioned timer node, it is determined whether the number of nodes in the priority queue is less than half. If it is less, the queue is reduced; otherwise, it is not reduced.

Citation Information

Patent Citations

  • Timer management device and management method for operating system

    CN101859260A

  • Processing method and processing device of timer

    CN103106222A