A method for executing a timing task and a computing device

By storing tasks hierarchically according to the trigger cycle in the time wheel timer and using the pause instruction in conjunction with the sleep function to drive the time wheel to rotate, the performance problem of the time wheel timer in scenarios with a large number of timed tasks is solved, and more efficient task triggering and CPU resource utilization are achieved.

CN115718649BActive Publication Date: 2026-03-27XFUSION DIGITAL TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-16
Publication Date
2026-03-27

AI Technical Summary

Technical Problem

In computer software systems, time wheel timers have limited storage space, which causes tasks with different delay lengths to be allocated to the same storage space. This results in all tasks needing to be traversed when triggered, reducing the performance of timed task triggering.

Method used

By using a task layering mechanism, timed tasks are stored in layers according to their trigger rounds. This allows the task queue with the same trigger round to be traversed when the time wheel timer is triggered, thus improving the triggering performance of timed tasks. Furthermore, the pause instruction combined with the sleep function drives the time wheel timer to rotate, optimizing CPU resource utilization and reducing power consumption.

Benefits of technology

The time wheel timer has improved its triggering performance in scenarios with a large number of timed tasks, reduced the number of task traversals, improved task triggering efficiency, and made better use of system CPU resources while reducing CPU power consumption without changing performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115718649B_ABST
    Figure CN115718649B_ABST
Patent Text Reader

Abstract

A method for executing a timing task, comprising: determining that a cycle pointer of a time wheel timer points to a first time of a first time slot, the first time slot being a next time slot of a time slot currently pointed to by the cycle pointer; in a case where a current time is greater than or equal to the first time, pointing the cycle pointer of the timer to the first time slot; obtaining a trigger round of the time wheel timer; and triggering at least one timing task in a first task list to execute, wherein a trigger round of each timing task in the first task list is the same as the trigger round of the time wheel timer. The timing tasks in the time wheel timer are hierarchically managed according to the trigger round, which improves the task triggering performance of the time wheel timer and enables the time wheel timer to support a larger number of timing tasks.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer data processing, and particularly relates to a method for executing a timing task and a computing device. BACKGROUND

[0002] In a computer software system, a timing task is a very common task form, for example: monthly statistical report, member points settlement, email push, etc., and the essence of all of them is a timing task. In a computer software system, the triggering of a timing task is generally completed by various types of timers. Among them, the time wheel timer is a simple, third-party component-independent, and efficient task triggering timing. However, due to the limited number of storage spaces divided in the time wheel timer, in the scenario of a large number of delay tasks, tasks of different delay lengths will be allocated to the same storage space. When the time wheel triggers, each task in the same storage space needs to be traversed, which reduces the performance of the timer task triggering. SUMMARY

[0003] The present application provides a method for executing a timing task and a computing device, which improves the performance of the time timer task triggering through a task layering mechanism, so that the time wheel timer can support a larger number of timing tasks.

[0004] In a first aspect, the present application provides a method for executing a timing task, comprising: determining a first time to which a cycle pointer of a time wheel timer points, the first time slot being a next time slot of a time slot currently pointed to by the cycle pointer; in a case where a current time is greater than or equal to the first time, pointing the cycle pointer of the time wheel timer to the first time slot; obtaining a triggering round of the time wheel timer; and triggering a timing task in a first task list in the first time slot according to the triggering round of the time wheel timer, wherein each timing task in the first task list has the same triggering round as the triggering round of the time wheel timer.

[0005] That is, when triggering the timing tasks stored in the time wheel timer to execute, after determining the time slot (such as the first time slot) pointed to by the cycle pointer of the time wheel timer, the task queue (such as the first task queue) with the same triggering round as the triggering round of the time wheel timer can be determined from the multiple task queues in the first time slot according to the triggering round of the time wheel timer. After determining the first task queue, the timing task in the first task queue can be triggered to execute. That is, the timing tasks in the time wheel timer are stored in layers according to the triggering round. When it is necessary to trigger the timing tasks in a certain time slot of the time wheel timer to execute, only the timing tasks with the same triggering round as the time wheel timer need to be traversed, and all the timing tasks stored in the time slot do not need to be traversed, which effectively improves the triggering performance of the time wheel timer.

[0006] In a possible implementation, triggering the execution of the timing task in the first task list comprises: traversing the first task list, and triggering the execution of at least one timing task if a trigger time of the at least one timing task in the first task list is less than a current time.

[0007] That is, because the time wheel timer has an error, to ensure that the timing task stored in the time wheel timer can be triggered more accurately, the timing task stored in the first task list can be further determined before triggering the execution of the timing task in the first task list, to ensure that the timing task executed has reached the trigger time.

[0008] In a possible implementation, after the target timing task is triggered to execute, the method further comprises: determining a second time to which a cycle pointer of the time wheel timer points, the second time being in a second time slot, the second time slot being a time slot next to the first time slot; determining a third time at which the time wheel timer needs to sleep, the third time being a time in a millisecond level, if the current time is less than the second time; triggering the time wheel timer to sleep for the third time by using a sleep function of a system, if the third time is greater than or equal to 1; and triggering the time wheel timer to sleep by using a Pause instruction, if the third time is less than 1.

[0009] That is, the rotation of the clock wheel timer can be simulated by the sleep of the time wheel timer. The rotation of the time wheel timer can be promoted by the Pause instruction in cooperation with the sleep function, so that the CPU resources of the system can be better utilized without changing the performance, and the CPU power consumption is reduced.

[0010] In a possible implementation, triggering the time wheel timer to sleep by using the Pause instruction comprises: after waiting for a fixed time interval, if the current time is less than the second time, triggering the time wheel timer to sleep again.

[0011] That is, after the process of the time wheel timer calls the Pause instruction, the process of the time wheel timer sleeps, but the process of the time wheel timer does not release the CPU. The process of the time wheel timer automatically wakes up after a certain time interval (for example, 10 clock periods), and checks whether the current time is greater than or equal to the second time. If the current time is less than the second time, the process of the time wheel timer continues to sleep and waits for the next wake-up.

[0012] In a possible implementation, before triggering the execution of the timed task in the first task list in the first time slot, the method further includes: receiving the timed task, the timed task including a delay execution time of the timed task; determining a trigger time of the timed task according to the delay execution time of the timed task; determining a trigger round of the timed task and a first time slot corresponding to the timed task according to the trigger time of the timed task; and storing the timed task into the first task list in the first time slot according to the trigger round of the timed task.

[0013] That is, when storing the timed task into the corresponding time slot in the time wheel timer, the timed task can be hierarchically stored in the corresponding time slot according to the trigger round of the timed task. That is, the corresponding time slot includes a plurality of task lists, and each task list is used to store the timed tasks of the same trigger round. When the cycle pointer of the time wheel timer points to a certain time slot, only the corresponding delay task list needs to be quickly searched (time complexity O(1)) according to the current trigger round of the time wheel timer, and the execution of the task is quickly triggered.

[0014] In a possible implementation, storing the timed task into the first task list in the first time slot according to the trigger round of the timed task includes: determining the first task list from at least one task list in the first time slot according to the trigger round of the timed task; in a case where the first task list exists in the first time slot, storing the timed task into the first task list; and in a case where the first task list does not exist in the first time slot, increasing the first task list in the first time slot, and storing the timed task into the first task list.

[0015] That is, when the first time slot of the time wheel timer already has a task list with the same trigger round as the received timed task, the received timed task can be directly stored into the task list. When the first time slot of the time wheel timer does not have a task list with the same trigger round as the received timed task, a task list with the same trigger round as the received timed task can be first created in the first time slot, and then the received timed task can be stored into the task list.

[0016] In a possible implementation, after triggering the execution of the timed task in the first task list, the method further includes: traversing a second task list in the first time slot, each timed task in the second task list having the same trigger round as a last trigger round of the time wheel timer; if there is a remaining timed task in the second task list, triggering the execution of the remaining timed task in the second task list; and deleting the second task list in the first time slot.

[0017] That is, after triggering the execution of the timed task in the first task list in the first time slot, it is also necessary to traverse the task list corresponding to the last trigger round of the current trigger round of the time wheel timer, and trigger the execution of the untriggered timed task in the task list, so as to ensure that all the timed tasks stored in the time wheel timer can be triggered. Further, in order to reduce the storage space occupied by the time wheel timer, after traversing the task list corresponding to the last trigger round of the current trigger round of the time wheel timer, the time wheel timer also needs to delete the task list from the first time slot.

[0018] In a possible implementation, the determining the first time to which the cycle pointer of the time wheel timer points to the first time slot comprises: determining the number of rotations of the cycle pointer when the cycle pointer points to the first time slot; and determining the first time according to the number of rotations of the cycle pointer and a unit time of the time wheel timer, wherein the unit time of the time wheel timer is the time required for one rotation of the cycle pointer.

[0019] In a possible implementation, the first task list is a double-linked list.

[0020] In a second aspect, the present application provides a computing device, comprising:

[0021] at least one memory for storing programs;

[0022] at least one processor for executing the programs stored in the memory, and when the programs stored in the memory are executed, the processor is configured to execute the method described in any one of the possible implementation manners of the first aspect.

[0023] In a third aspect, the present application provides a computer readable medium, and the computer storage medium stores instructions, and when the instructions are run on the computer, the computer is caused to execute the method described in any one of the possible implementation manners of the first aspect.

[0024] In a fourth aspect, the present application provides a computer program product comprising instructions, and when the instructions are run on the computer, the computer is caused to execute the method described in any one of the possible implementation manners of the first aspect.

[0025] It can be understood that the beneficial effects of the above-mentioned second aspect to fourth aspect can be referred to the related description in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF DRAWINGS

[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the embodiments description will be briefly introduced as follows. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without any creative effort on the basis of these drawings.

[0027] Figure 1 A time wheel timer structure schematic diagram provided for the embodiments of the present application;

[0028] Figure 2 A time wheel timer triggering execution delay task process schematic diagram provided for the embodiments of the present application;

[0029] Figure 3 An application scenario schematic diagram provided for the embodiments of the present application;

[0030] Figure 4 A time wheel timer structure schematic diagram provided for the embodiments of the present application;

[0031] Figure 5 A hardware structure schematic diagram of a computing device provided for the embodiments of the present application;

[0032] Figure 6 A flow chart of a delay task execution method provided for the embodiments of the present application;

[0033] Figure 7 A time wheel timer triggering execution delay task process schematic diagram provided for the embodiments of the present application;

[0034] Figure 8 A method flow chart of triggering a time wheel timer to sleep provided for the embodiments of the present application;

[0035] Figure 9 A flow chart of another delay task execution method provided for the embodiments of the present application;

[0036] Figure 10 A delay task storage method flow chart schematic diagram provided for the embodiments of the present application;

[0037] Figure 11a A time wheel timer structure schematic diagram provided for the embodiments of the present application;

[0038] Figure 11b A time wheel timer structure schematic diagram provided for the embodiments of the present application;

[0039] Figure 12 A chip structure schematic diagram provided for the embodiments of the present application. DETAILED DESCRIPTION

[0040] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described below with reference to the drawings.

[0041] In the description in the embodiments of the present application, any embodiment or design scheme of "exemplary", "for example" or "for instance" should not be understood as being more preferred or more advantageous than other embodiments or design schemes. Rather, the words "exemplary", "for example" or "for instance" are used herein to mean "an example of a specific implementation". Any embodiment or design scheme of "exemplary", "for example" or "for instance" is not necessarily preferred or superior over other embodiments or design schemes.

[0042] In addition, the terms "first", "second", "third", "fourth", "fifth", "sixth", "seventh" and "eight" are only used for descriptive purpose, and should not be understood as indicating or implying relative importance or implicitly indicating the technical features indicated. Therefore, the features defined with "first", "second", "third", "fourth", "fifth", "sixth", "seventh" and "eight" can explicitly or implicitly include one or more of the features. The terms "include", "contain", "have" and their variants mean "including but not limited to", unless otherwise specifically emphasized.

[0043] Before introducing the solutions of the present application, first, the key terms used in the embodiments of the present application will be explained.

[0044] 1. Pause instruction, which is an instruction in the instruction set in the 32-bit system (X86), the pause instruction provides a hint to the processor that the code being executed by the processor is a spin-wait loop. The processor will avoid memory order violation according to the hint, that is, the spin-wait loop will not be cached, and no instruction reordering will be performed, so as to improve the performance of the processor.

[0045] 2. Socket connection, two programs on the network exchange data through a bidirectional communication connection, one end of the connection is called a socket. At least one pair of port numbers (socket) is required to establish a network communication connection. The socket is essentially an encapsulation of the programming interface (API) to TCP / IP.

[0046] 3. Sleep function, the sleep function can make the computer program (process, task or thread) enter sleep, so that it is in an inactive state for a period of time. When the timer set by the function expires, or a signal is received, or the program is interrupted, the program will continue to execute. The time is the parameter of the sleep function, and the unit is millisecond.

[0047] Next, the technical solutions involved in the present application will be introduced.

[0048] Timing tasks can be divided into periodic tasks, delay tasks, and specified time point tasks according to the triggering mode of the tasks. The periodic task refers to a task that is repeatedly executed at a certain period of time; for example, a monthly financial report is generated at 0 o'clock on the first day of each month, and a daily sales report is generated at 0 o'clock in the morning. The delay task refers to a task that is executed after a period of time from a certain time point; for example, an order is automatically canceled after 30 minutes of non-payment after an e-commerce shopping order is generated, and a refund business needs to be performed if a red packet is not collected within 24 hours. The specified time point task refers to a task that is executed at a given time; for example, a meeting notice message is sent 30 minutes before the start of the meeting. Among them, the timing task in the embodiment of the present application mainly refers to the delay task.

[0049] In computer software systems, the triggering and execution of timing tasks are generally completed by various types of timers. Among them, the time wheel timer is a simple, third-party component-independent, and efficient implementation method for triggering timing tasks. The time wheel timer is composed of a time array and a counter (tick). The time wheel array is a ring array connected at the head and tail, and the ring array includes a plurality of slot positions (also referred to as time slots), and each time slot saves a bucket data structure. The entire time wheel data can simulate the dial of a clock, and the counter can simulate the movement of the hour hand of the clock.

[0050] Exemplarily, Figure 1 An implementation principle diagram of a time wheel timer is shown. Next, referring to the time wheel timer shown in Figure 1 The data structure in the time wheel timer and the terms are explained.

[0051] 1. Time wheel array: a ring array connected at the head and tail, wherein each slot position saves a bucket data structure, and the entire time wheel array simulates the dial of a clock; as Figure 1 shown, the time wheel array is composed of 8 time slots, and each time slot saves a bucket data structure.

[0052] 2. Bucket: a data structure saved in a time slot, the bucket includes a double-linked list, and the double-linked list saves the delay tasks to be triggered. The structure of the double-linked list can be as Figure 1As shown, each node in the doubly linked list, except the head node, consists of three parts, i.e. a preceding node pointer pre, a data field data, and a following node pointer next. Among them, the following node pointer next of the last node in the doubly linked list points to the following node next of the head node, and the preceding node pointer of the head node points to the data field of the last node in the doubly linked list. The data field is used to store the task information of the delay task submitted by the user. For example, the trigger round of the delay task and the trigger time of the delay task.

[0053] 3. Tick: The counter of the time wheel timer, which can be used to simulate the movement of the hour hand of a clock, and is usually counted from 0.

[0054] 4. TickDuration: Represents the unit time of the time wheel timer, i.e. the time elapsed after the tick counter is incremented by 1 each time; TickDuration is similar to the second hand of a clock, which indicates the time elapsed by one grid on the dial.

[0055] 5. WheelSize: Represents the length of the time wheel data array; as shown, the WheelSize of the time wheel timer is 8. Figure 1

[0056] 6. Trigger time: The time at which the delay task is expected to be triggered for execution; for example, if the user submits a delay task at 0 o'clock in the morning, and the expected delay execution time of the task is 5 minutes, then the trigger execution time of the delay task is 00:05:00.

[0057] 7. Trigger round: The trigger round is also a counter, which is used to simulate the number of circles walked by the hour hand on the dial, and is counted from 0. As shown, Figure 1 Figure 1 As shown, the WheelSize of the time wheel timer is 8, and the TickDuration is 1 second, so the time range that the entire time wheel can represent is only 8 seconds. If a task is submitted for execution with a delay of 10 seconds, the trigger round is needed to assist in determining the real trigger time of the delay task.

[0058] ​​The essence of the time wheel timer is to simulate the running of a clock, using a fixed-length data structure to simulate the clock dial, and each Bucket on the dial is a doubly linked list data structure, which stores the delay tasks to be executed. After the time wheel timer is started, the start time is recorded, and the thread sleep function provided by the operating system is used to drive the hour hand. When the hour hand points to the next Bucket, the delay tasks in the doubly linked list are traversed to determine whether the trigger condition is met. If there is a task that meets the trigger condition, the task is removed from the doubly linked list and triggered for execution. When the thread traverses the current Bucket pointed to by the hour hand, the thread sleeps, and after being awakened by the operating system, the hour hand points to the next Bucket. This process is repeated. Based on the running process of the time wheel timer, the process of executing the delay task by the user through the time wheel timer can be divided into three stages, namely the delay task submission stage, the delay task trigger stage, and the time wheel timer driving stage.

[0059] Delay task submission stage: The user submits the delay task to be executed and the delay time of the delay task to be executed to the time wheel timer. The time wheel timer uses the submission time of the delay task, the WheelSize of the time wheel timer, the tick of the time wheel timer, and the TickDuration of the time wheel timer to calculate the identification number of the target Bucket to which the delay task submitted by the user belongs, the trigger time of the delay task submitted by the user, and the trigger round. Then, the time wheel timer appends the delay task submitted by the user to the tail of the doubly linked list in the target Bucket, and waits for the time wheel trigger to trigger execution. When the time wheel timer appends the delay task submitted by the user to the doubly linked list in the target Bucket, it needs to save the trigger time and trigger round of the delay task in the doubly linked list in the Bucket.

[0060] Delay task trigger stage: After the time wheel timer is started, the entire running process is in the simulation of the running of a clock. Before Tick is incremented by 1, the operating system function sleep needs to be called to wait for a period of time, and the length of the waiting time is TickDuration. After Tick is incremented by 1 each time, it can be considered that Tick points to a new Bucket. When Tick points to a new Bucket each time, the time wheel timer needs to traverse the tasks in the task list in the Bucket pointed to by Tick according to the trigger time of the trigger round of the current time wheel timer to determine the delay task that needs to be triggered for execution in the trigger round of the current time wheel timer.

[0061] Push the time wheel timer to rotate: push the clock wheel timer to rotate, that is, push the Tick in the clock wheel timer to rotate. Wherein, pushing the Tick to rotate is an action of the time wheel timer in the running process, and the purpose is to wait for time to elapse, so that the real time can be mapped with the value represented by the Tick, so as to realize the effect of Tick rotation.

[0062] Next, the process of the time wheel timer triggering the execution of the delay task is described with a specific embodiment. As shown in the figure, the WheelSize of the time wheel timer is 8, the TickDuration is 1 second, the Tick is 0, and the start time of the time wheel timer is 00:00:00. Figure 2

[0063] When the time comes to 00:00:01, since the time has elapsed for 1 second, at 00:00:01, the tick is 1. At this time, the time wheel timer needs to traverse the delay task stored in the double-linked list in Bucket1, delete the delay task whose trigger time is less than or equal to 00:00:01 and trigger round is equal to 0 from the double-linked list in Bucket1, and trigger the execution of the deleted delay task.

[0064] If the user submits a delay task A and a delay task B at this time (00:00:01). The delay execution time of task A is 1 second, and the delay execution time of task B is 9 seconds. The time wheel timer can obtain the trigger time of the delay task A as 00:00:02 according to the time of the time wheel timer when the task is submitted (00:00:01) and the delay execution time of task A (1 second). Then, the time wheel timer obtains the identification number of the Bucket of the time wheel timer to which task A belongs as 2 (that is, task A needs to be stored in the double-linked list in Bucket2) according to the trigger time of task A, the TickDuration of the time wheel timer, and the WheelSize of the time wheel timer. And the time wheel timer obtains the trigger round of task A as 0 according to the trigger time of task A, the TickDuration of the time wheel timer, and the WheelSize of the time wheel timer. Wherein, the trigger time of the delay task A = the internal time of the time wheel timer when the task is submitted + the delay execution time of task A. The identification number of the Bucket of the time wheel timer to which task A belongs = the trigger time of the delay task A / TickDuration % WheelSize. The trigger round of task A = the trigger time of the delay task A / TickDuration / WheelSize.

[0065] ​The time wheel timer can obtain the trigger time of the delay task B of the task A as 00:00:10 according to the time of the time wheel timer when the task is submitted (00:00:01) and the delay execution time of the task B (9 seconds). Then, the time wheel timer obtains the identification number of the Bucket of the time wheel timer to which the task B belongs as 2 according to the trigger time of the task B, the TickDuration of the time wheel timer and the WheelSize of the time wheel timer. The time wheel timer obtains the trigger round of the task B as 1 according to the trigger time of the task B, the TickDuration of the time wheel timer and the WheelSize of the time wheel timer.

[0066] It can be understood that, in the embodiments of the present application, when the trigger times of the tasks A and B are calculated, the trigger times of the tasks A and B in the time wheel timer are calculated (which is a relative time rather than an absolute time). In an ideal case, the internal time of the time wheel timer is the same as the system time of the computing device in which the time wheel timer is located. In some abnormal cases, such as artificially modifying the system time of the computing device in which the time wheel timer is located or the computing device in which the time wheel timer is located fails, the internal time of the time wheel timer is not the same as the system time of the time wheel timer.

[0067] After the time wheel timer traverses the delay tasks in the Bucket1 or stores the delay tasks submitted by the user into the corresponding Buckets, the time wheel timer needs to be in a loop of sleeping and waiting until the current time is greater than or equal to the time of the Tick pointing to the Bucket2. After the time wheel timer ends each sleeping, it needs to determine whether the current time is greater than or equal to the time of the Tick pointing to the Bucket2. Specifically, the time wheel timer can calculate the time nextTick that the Tick pointing to the Bucket2 needs to wait relative to the start time of the time wheel timer. nextTick=(Tick+1)*TickDuration=2s. Then, the time wheel timer calculates the difference dt between the current time and the start time of the time wheel timer. The time wheel timer compares dt with nextTick. If dt

[0068] It can be understood that, in the embodiments of the present application, the current time refers to the system time when the time wheel timer is performing the corresponding operation.

[0069] When the time comes to 00:00:02, the time wheel timer calculates the difference dt between the current time and the start time of the time wheel timer as 2 seconds. At 00:00:02, the time wheel timer determines that dt = nextTick. At this time, the time wheel timer needs to traverse the delay task stored in Bucket2. Referring to Figure 2 It can be known that, at 00:00:02, the Bucket2 stores task A and task B, and the time wheel timer needs to compare the trigger time and the trigger round of task A and task B with the current time (00:00:02) and the current trigger round (0) of the time wheel timer respectively. Since the trigger time of task A is equal to the current time of the time wheel timer, and the trigger round of task A is equal to the trigger round of the clock wheel timer, it can be determined that task A needs to be triggered, and the time wheel timer deletes task A from the double circular linked list in Bucket2 and triggers task A to execute.

[0070] When the time comes to 00:00:09, the Tick of the time wheel timer points to Bucket1, and the trigger round of the time wheel is 1. At this time, it can be determined that the Tick points to Bucket2 needs to wait for the time nextTick of 10 seconds relative to the start time of the time wheel timer.

[0071] When the time comes to 00:00:10, the time wheel timer calculates the difference dt between the current time and the start time of the time wheel timer as 10 seconds. At 00:00:10, the time wheel timer determines that dt = nextTick. At this time, the time wheel timer needs to traverse the delay task stored in Bucket2. Referring to Figure 2 It can be known that, at 00:00:10, the Bucket2 stores task B, and the time wheel timer needs to compare the trigger time and the trigger round of task B with the current time (00:00:10) and the current trigger round (1) of the time wheel timer respectively. Since the trigger time of task B is equal to the current time of the time wheel timer, and the trigger round of task B is equal to the trigger round of the clock wheel timer, it can be determined that task B needs to be triggered, and the time wheel timer deletes task B from the double circular linked list in Bucket2 and triggers task B to execute.

[0072] Although, Figure 2The time wheel timer shown can schedule the time delay task provided by the user. However, in the case of a large number of time delay tasks, tasks with different time delay lengths can be allocated to the same Bucket. For example, a task with a time delay of one hour can be in the same Bucket list as a task with a time delay of 10 minutes, which causes the need to traverse each task in the list when the time wheel triggers to check the task trigger round and trigger time, thereby affecting the traversal efficiency when the task triggers. Further, in the above embodiment, when the time wheel rotates, the sleep function of the operating system is relied on for hibernation to wait for the time to elapse. The sleep function only supports milliseconds, while the system time accuracy is at least nanoseconds. Due to the difference in time units, sleep(0) milliseconds often occurs when the time wheel rotates. sleep(0) is a time-consuming operation of several instruction cycles for the operating system. The time wheel is essentially waiting when it rotates. At this time, the operating system should allocate more CPU time slices to other processes or threads to achieve the best CPU resource utilization of the entire system. Frequent sleep(0) can cause the operating system to frequently wake up the time wheel running thread to check whether the current time meets the waiting requirement, which wastes CPU resources from the perspective of the entire system.

[0073] Therefore, the embodiments of the present application provide an execution method of a time delay task, which mainly triggers the time delay task through time wheel timing. After the time wheel timer receives the time delay task submitted by the user, the time delay task submitted by the user is stored in the Bucket in the time wheel timer, and hierarchical storage is performed according to the trigger round of the time delay task, that is, the time delay tasks with the same trigger round are stored in the same task list in each Bucket, which reduces the number of tasks that need to be traversed when the time wheel timer triggers the tasks, improves the task triggering performance of the time wheel timer, and enables the time wheel timer to support a larger number of time delay tasks. Further, the time wheel timer can better utilize the system CPU resources and reduce CPU power consumption without changing the performance by calling the x86 instruction pause and the sleep system function to drive the time wheel to rotate.

[0074] An exemplary, Figure 3 An application scenario of the present application is shown. As Figure 3 As shown, the scenario can include a server and a client (Client), and multiple clients can establish a Socket connection with the server. When the multiple clients are connected with the server, the server needs to regularly check whether the connection between the client and the high-performance server is valid. Specifically, the server can detect the validity of the connection between the server and the client through the time wheel timer on the server.

[0075] It should be noted that Figure 3 The application scenarios shown do not constitute a limitation on the embodiments of the present application. The present application can be applied to business scenarios that require a large number of delay tasks, such as e-commerce order confirmation, red envelope refund inspection, determination of the validity of game props, and the like.

[0076] For example, the embodiments of the present application provide a structural diagram of a time wheel timer. As shown in Figure 4 The time wheel timer is composed of time array and counter (tick). The time wheel data is a ring array connected at both ends, which includes a plurality of slot positions (also referred to as time slots), and each time slot stores a bucket data structure. The entire time wheel data can simulate the dial of a clock, and the counter can simulate the movement of the hour hand of the clock. Each time slot is composed of at least one task list, and each delay task stored in each task list has the same clock trigger round. As shown in Figure 4 As shown in the time slot Bucket1, a plurality of double-linked lists are included, each double-linked list constitutes a task list, and each task list stores delay tasks with the same trigger round. That is, in the embodiments of the present application, the stored delay tasks in the time wheel timer are managed in layers according to the trigger round, so that when the time wheel timer triggers the delay tasks in a certain Bucket, it only needs to traverse the delay tasks in a task list in the Bucket according to the running round of the time wheel timer, without traversing all the delay tasks in the Bucket. The task trigger performance of the time wheel timer is improved from O(n) to O(1), improving the trigger performance of the task.

[0077] For example, Figure 5 A hardware structure of a computing device is shown, which is deployed with Figure 4 The time wheel timer, which can be Figure 3 a server in As shown in Figure 5 The computing device includes a processor 510, a network interface 520, and a memory 530. The processor 510, the network interface 520, and the memory 530 can be connected by a bus or other means.

[0078] In the embodiments of the present application, the processor 510 (also referred to as a central processing unit (CPU)) is the computing core and control core of the computing device. For example, the processor 510 can drive the rotation of the time wheel timer and trigger the execution of the delay tasks stored in the time wheel timer.

[0079] The network interface 520 may include a standard wired interface, a wireless interface (such as Wi-Fi, mobile communication interface, etc.), and is controlled by the processor 510 to send and receive data, for example, to receive delayed tasks sent by users or to establish connections with other terminal devices.

[0080] Memory 530 is a storage device in a computing device used to store programs and data, such as delayed tasks. It is understood that memory 530 can be high-speed RAM or non-volatile memory, such as at least one disk storage device; optionally, it can also be at least one storage device located remotely from the aforementioned processor 510. Memory 530 provides storage space for storing the server's operating system and executable program code, which may include, but is not limited to, Windows (an operating system), Linux (an operating system), HarmonyOS (an operating system), etc., without limitation.

[0081] It is understood that the embodiments of this application Figure 5 The illustrated structure does not constitute a specific limitation on the computing device. A computing device may include more or fewer components than illustrated, or combine some components, or separate some components, or have different component arrangements. The components illustrated may be implemented in hardware, software, or a combination of both.

[0082] Next, based on the content described above, in Figure 5 The present application describes a method for executing a delayed task provided by an embodiment of the computing device shown.

[0083] See Figure 6 , Figure 6 This is a flowchart illustrating a method for executing a delayed task, as provided in an embodiment of this application. The method can be... Figure 5 The processor in the shown computing device performs the execution. See also Figure 6 The method includes: S601-S609.

[0084] S601, determine the time nextTick when the loop pointer of the time wheel timer points to the first time slot; where the first time slot is the next time slot after the time slot currently pointed to by the loop pointer.

[0085] In this embodiment, when triggering the execution of a delayed task using a time wheel timer, it is necessary to determine the initialization state of the time wheel timer. The initialization state of the time wheel timer includes: the start time of the time wheel timer, the number of time slots (i.e., buckets) that the time wheel timer has, and the time wheel timer's unit time (TickDuration).

[0086] In one possible example, as shown in Figure 7 the time wheel timer has 12 buckets (Bucket0-Bucket11), the unit time TickDuration of the time wheel timer is 1 min, and the start time of the time wheel timer is 8:00. At 8:31, it can be determined that the cycle pointer of the time wheel timer points to Bucket7, the number of times tick that the cycle pointer of the time wheel timer rotates is 31, and the number of rotations of the time wheel timer is 2. At this time, the time nextTick that the time wheel timer points to Bucket8 can be calculated.

[0087] nextTick=(Tick+1)*TickDuration=32min. Wherein, the time nextTick that the time wheel timer points to Bucket8 represents the relative time inside the time wheel, that is, the time that the time wheel timer points to Bucket8 relative to the start time of the time wheel timer needs to wait.

[0088] It can be understood that, in an ideal case, the internal time (relative time) of the time wheel timer and the system time (absolute time) of the computing device where the time wheel timer is located are the same. In some abnormal cases, such as artificially modifying the system time of the computing device where the time wheel timer is located, or the computing device where the time wheel timer is located fails, the internal time of the time wheel timer and the system time of the time wheel timer will be different. The internal time of the time wheel timer cannot be changed, that is, the internal time of the time wheel timer is absolutely correct. Therefore, in order to ensure the accuracy of task triggering, in the embodiment of the present application, the internal time of the time wheel timer can be referred to for delay task triggering.

[0089] S602, determining the difference dt between the current time and the start time of the time wheel timer.

[0090] In this embodiment, the current time refers to the system display time of the computing device where the time wheel timer is located at the current time, and the current time is constantly updated as time goes by. For example, as time goes by, when the system time comes to 8:32 (i.e., the current time becomes 8:32), the start time of the time wheel timer is 8:00. Then dt = current time-start time of the time wheel timer = 32 min. Figure 7

[0091] S603, comparing nextTick with dt, if dt≥nextTick, executing S605, otherwise executing S604.

[0092] ​In the embodiment, whether the cycle pointer of the time wheel timer needs to be rotated is determined by comparing the nextTick with the dt. If the dt≥nextTick, it indicates that the absolute time has elapsed at least one unit time of the time wheel timer, and the cycle pointer of the time wheel timer needs to be rotated. If the dt<nextTick, it indicates that the elapsed time is less than one unit time of the time wheel timer, and the cycle pointer of the time wheel timer does not need to be rotated, and the time wheel timer sleeps.

[0093] It can be understood that, in the present application, the time wheel timer only simulates the rotation of the clock, and the time wheel timer is not really rotating. The time wheel timer increases the number of rotations of the cycle pointer to achieve the effect of rotating the cycle pointer on the time wheel timer.

[0094] S604, triggering the time wheel timer to sleep.

[0095] In the embodiment, the system time precision of the computing device is at least nanoseconds, and the system function sleep only supports millisecond units. Due to the difference in time units, the time wheel timer may sleep for 0 milliseconds during rotation. In order to avoid the time wheel timer sleeping for 0 milliseconds during rotation, the pause instruction can be used in combination with the sleep function to trigger the time wheel timer to sleep during the process of triggering the time wheel timer to sleep, that is, the pushing of the cycle pointer of the time wheel timer is optimized.

[0096] In one possible example, the process of triggering the time wheel timer to sleep is as shown in Figure 8 S6041, calculating the time st that the time wheel timer needs to sleep, st=(nextTick-dt) / 1000000, wherein the calculated time st that the time wheel timer needs to sleep is a time in millisecond level. S6042, determining whether the time st that the time wheel timer needs to sleep is greater than or equal to 1, if st≥1, executing S6043, otherwise executing S6044. S6043, calling the function sleep(st) to sleep. S6044, executing the pause instruction to sleep.

[0097] In the above example, since the system time precision of the computing device is at least nanoseconds, the system function sleep only supports millisecond units at the minimum. Therefore, when determining the sleep time of the time wheel timer, the nanosecond-level sleep time needs to be converted into millisecond, i.e., "nextTick-dt" is the nanosecond-level sleep time, and "(nextTick-dt) / 1000000" is the millisecond-level sleep time. When the time wheel timer determines that the time to be slept is less than 1 millisecond, the pause instruction is used to trigger the sleep of the time wheel timer. Therefore, when st≥1 millisecond, the function sleep(st) can be called to trigger the sleep of the time wheel timer, and when st<1 millisecond, the pause instruction can be executed to trigger the sleep of the time wheel timer.

[0098] Specifically, after the process of the time wheel timer calls the pause instruction, the process of the time wheel timer will sleep, but the process of the time wheel timer will not release the CPU. The process of the time wheel timer will automatically wake up after a certain time interval (such as 10 clock cycles) and check whether the current time is greater than the time of the next time slot of the time slot pointed to by the pointer of the time wheel timer. If the current time is greater than the time of the next time slot (the first time slot) pointed to by the pointer of the time wheel timer, the pointer of the time wheel timer is directed to the first time slot, otherwise the process of the time wheel timer will continue to sleep and wait for the next wake-up. By using the pause instruction in combination with the sleep function to drive the rotation of the time wheel timer, the CPU resources of the system can be better utilized without changing the performance, and the CPU power consumption is reduced.

[0099] S605, the cycle pointer of the time wheel timer is directed to the first time slot, and the index number of the first time slot is determined.

[0100] In this embodiment, if dt≥nextTick, it indicates that the absolute time has elapsed at least one unit time of the time wheel timer. At this time, the cycle pointer of the time wheel timer can be directed to the next time slot, i.e., the number of rotations of the cycle tick is increased by 1. For example, when the time comes to 8:32, dt=nextTick is calculated. At this time, tick=31+1=32, and tick points to Bucket8. Figure 7

[0101] S606, according to the number of rotations of the cycle pointer of the time wheel timer, the task round to be triggered by the time wheel timer is determined.

[0102] ​In the embodiment, when the cycle pointer of the time wheel timer points to a new Bucket, it is also needed to determine how many times the cycle pointer of the time wheel timer points to the Bucket since the time wheel timer is started, i.e. the task round that the time wheel timer needs to trigger.

[0103] In one possible example, referring to Figure 7 , the current time is 8:32, the tick is 32, and the time wheel timer has 12 time slots, i.e. the wheelSize is 12. At this time, the task trigger round of the time wheel timer can be obtained as 2.

[0104] S607, determining a first task list from the at least one task list in the first time slot according to the task round that the time wheel timer needs to trigger, wherein the first task list corresponds to the task round that the time wheel timer needs to trigger.

[0105] In the embodiment, referring to Figure 7 , when the current time is 8:32, the cycle pointer of the time wheel timer points to Bucket 8, and the trigger round of the time wheel timer is 2. At this time, the time wheel timer needs to traverse the delay tasks in the task list with the trigger round of 2 in Bucket 8 and trigger the delay tasks that meet the requirements.

[0106] S608, removing the delay tasks in the first task list from the first task list and triggering the removed delay tasks to execute.

[0107] In the embodiment, after the time wheel timer determines the first task list from the first time slot, the time wheel timer can remove the delay tasks in the first task list from the first task list to execute. The delay tasks removed from the first task list can be executed by the time wheel timer or by other processes.

[0108] S609, deleting the first task list in the first time slot.

[0109] In the embodiment, in order to reduce the storage space occupied by the time wheel timer, after the time wheel timer removes the delay tasks in the first task list, the time wheel timer can also delete the first task list in the first time slot.

[0110] In a possible embodiment, since the time precision of the time wheel timer is limited. In order to make the time of triggering the delay task by the time wheel timer more precise. After performing S607, determining the first task list in the first time slot that needs to be triggered, the time wheel timer compares the delay task in the first task list with the current time of the time wheel timer before triggering the delay task in the first task list to perform, and determines whether to trigger the delay task in the first task list according to the comparison result. Refer to Figure 9 After performing S607, the time wheel timer further includes S608-S613.

[0111] S608, determining the current time of the time wheel timer according to the system time of the computing device to which the time wheel timer belongs and the start time of the time wheel timer.

[0112] In the embodiment, in order to ensure the accuracy of task triggering, the current time of the time wheel timer, i.e., the value of the internal timer of the time wheel timer, needs to be determined before triggering the delay task in the first task list. Specifically, refer to Figure 7 The start time of the time wheel timer is 8:00, and the value of the internal timer of the time wheel timer is 0. When the system time of the computing device to which the time wheel timer belongs is 8:32, it can be determined that the value of the internal timer of the time wheel timer is: 0+32*60000000000=1,920,000,000,000 nanoseconds.

[0113] S609, traversing the delay tasks in the first task list, when there is a delay task in the first task list whose trigger time is less than the current time of the time wheel timer, performing S610, otherwise performing S611.

[0114] In the embodiment, after determining the current time of the time wheel timer, the current time of the time wheel timer can be compared with the trigger time of the delay task in the first task list. The trigger time of the delay task in the first task list refers to the trigger time relative to the start time of the time wheel timer, which is a relative time. In a possible example, refer to Figure 7 Taking the delay task 5 in the Bucket8 as an example, the trigger time of the delay task 5 is explained. It is assumed that the delay task 5 is submitted at 8:00, and the delay execution time of the delay task 5 is 32 min. It can be determined that the internal trigger time of the delay task 5 in the time wheel timer is: 0+32*60000000000=1,920,000,000,000 nanoseconds.

[0115] S610, removing the delay task whose trigger time is less than the current time of the time wheel timer from the first task list, and triggering the removed delay task to perform.

[0116] In the embodiment, after the time wheel timer determines the first task list from the first time slot, the time wheel timer can remove the delay task in the first task list whose trigger time is less than the current time of the time wheel timer. The delay task removed from the first task list can be executed by the time wheel timer or by other processes. When the trigger time of the delay task in the first task list is greater than the current time of the time wheel timer, the time wheel timer does not process the delay task.

[0117] In one possible example, referring to Figure 7 When the system time is 8:32, if the trigger time of the delay task 5 is greater than the current time of the time wheel timer, and the trigger time of the delay task 7 is less than the current time of the time wheel timer. Then the time wheel timer removes the delay task 7 from the corresponding task list and triggers the execution, without the need to process the delay task 5.

[0118] S611, traversing the second task list in the first time slot, determining whether there is a delay task in the second task list, if there is a delay task in the second task list, executing S612, otherwise executing S613; wherein the second task list corresponds to the last round of the task round that the time wheel timer needs to trigger.

[0119] In the embodiment, after the time wheel timer traverses the task list corresponding to the current trigger round of the time wheel timer in the first time slot, the time wheel timer also needs to process the task list corresponding to the last trigger round in the first time slot.

[0120] S612, removing the delay task from the second task list and triggering the removed delay task to execute.

[0121] In the embodiment, if there is a delay task in the second task list, the time wheel timer removes the delay task from the corresponding task list and triggers the delay task to execute.

[0122] S613, deleting the second task list from the first time slot, and executing S601.

[0123] In the embodiment, in order to reduce the occupation of the time wheel timer to the storage space, the time wheel timer needs to delete the task list corresponding to the last trigger round from the first time slot.

[0124] It can be understood that S612 is an optional step, and the time wheel timer can directly delete the task list when deleting the task list corresponding to the last trigger round. After triggering the unexecuted delay task in the last trigger round to execute, the task list can be deleted.

[0125] It can be understood that the relative time involved in the above embodiments can also be replaced by absolute time. The embodiments of the present application do not limit this.

[0126] In the embodiments of the present application, the time wheel timer stores the delay tasks in each Bucket according to the trigger round, so that the time wheel timer only needs to traverse the delay tasks in the task list corresponding to the current trigger round in the corresponding Bucket when triggering the tasks, without traversing all delay tasks in the Bucket. Through the task layering mechanism, the task triggering performance of the time wheel timer is effectively improved, so that the time wheel timer can support a larger number of delay tasks.

[0127] In one possible embodiment, before performing the method shown in Figure 6 or Figure 8 The delay task needs to be stored in the task list in the time slot of the time wheel timing. For example, the embodiments of the present application provide a storage method of the delay task. The method can be performed by the processor in the computing device shown in Figure 5 . Referring to Figure 10 , the method comprises:

[0128] S1001, determining the submission time and the delay execution time of the target delay task.

[0129] In this embodiment, after the time wheel timer receives the target delay task, the submission time and the delay execution time of the target delay task need to be determined. The target delay task can be a target delay task submitted by a user, or a delay task generated by a computing device to which the time wheel timer belongs.

[0130] S1002, determining the trigger time of the target delay task according to the submission time, the delay execution time of the target delay task and the start time of the time wheel timer.

[0131] In this embodiment, before storing the received target delay task in the storage space of the time wheel timer, the time wheel timer also needs to determine the trigger time of the target delay task.

[0132] In a possible example, the trigger time of the target delay task can refer to the trigger time of the target delay task relative to the internal time of the time wheel timer. Specifically, when determining the trigger time t1 of the target delay task, the submission time of the target delay task relative to the internal time t2 of the time wheel timer can be determined according to the start time of the time wheel timer and the submission time of the target delay task. Then, the trigger time t1 of the target delay task is determined according to the delay execution time t3 of the target delay task and the submission time of the target delay task relative to the internal time t2 of the time wheel timer. Wherein, t1=t2+t3.

[0133] It can be understood that, generally, the system time precision of the computing device is at least nanoseconds. Therefore, when calculating the internal time of the time wheel timer, the internal time of the time wheel timer can be converted into nanoseconds.

[0134] S1003, according to the trigger time of the target delay task, the unit time of the time wheel timer, and the number of time slots of the time wheel timer, determine the trigger round of the target delay task.

[0135] In this embodiment, since the number of time slots of the time wheel timer is limited, before storing the target delay task into the storage space of the time wheel timer, the time wheel timer also needs to determine the trigger round of the target delay task.

[0136] In a possible example, the time wheel timer can determine the trigger round of the target delay task according to the trigger time t1 of the target delay task, the unit time TickDuration of the time wheel timer, and the number of time slots wheelsize of the time wheel timer. Specifically, the trigger round of the target delay task=t1 / TickDuration / wheelsize.

[0137] S1004, according to the trigger time of the target delay task, the unit time of the time wheel timer, and the number of times of rotation of the cycle pointer of the time wheel timer, determine the index number of the time slot storing the target delay task.

[0138] In this embodiment, the time wheel timer can determine the index number of the time slot storing the target delay task according to the trigger time t1 of the target task, the unit time TickDuration of the time wheel timer, and the number of time slots wheelsize of the time wheel timer. Specifically, the index number of the time slot storing the target delay task=t1 / TickDuration%wheelsize.

[0139] S1005, according to the trigger round of the target delay task, determine the target task list from at least one task list in the time slot storing the target delay task.

[0140] In the embodiment, after determining the index number of the time slot in which the target delay task is to be stored, the time wheel timer can store the target delay task in the time slot corresponding to the time wheel timer according to the index number of the time slot in which the target delay task is to be stored. Referring to Figure 7 As shown in the time wheel timer, the delay tasks in each time slot are stored in layers, that is, the delay tasks belonging to the same trigger round are stored in the same task list. Therefore, after determining the time slot in which the target delay task is to be stored, the time wheel timer also needs to select a target task list in which the target delay task is to be stored from the time slot according to the trigger round of the target delay task. Then, the time wheel timer adds the target delay task to the target task list. When there is a task list corresponding to the trigger round of the target delay task in the time slot in which the target delay task is to be stored, the task list can be directly used as the target task list. When there is no task list corresponding to the trigger round of the target delay task in the time slot in which the target delay task is to be stored, the time wheel timer can create a task list corresponding to the trigger round of the target delay task in the time slot in which the target delay task is to be stored, as the target task list.

[0141] S1006, store the target delay task in the target queue.

[0142] In the embodiment, if there is a task list matching the trigger round of the target delay task in the time slot in which the target delay task is to be stored, the time wheel timer can directly add the delay task to the corresponding task list. Referring to Figure 7 As shown in the time wheel timer, the target delay task is task 15, the time slot in which the target delay task is to be stored is Bucket 8, the trigger round of the target delay task is 4, and the time wheel timer can directly store the target delay task in the task list with the storage round of 4. After storing the target delay task in the Figure 7 As shown in the time wheel timer, the structure of the time wheel timer is shown in Figure 11a As shown in the time wheel timer.

[0143] If there is no task list matching the trigger round of the target delay task in the time slot in which the target delay task is to be stored, the time wheel timer can create a new task list in the corresponding time slot and store the target delay task in the new task list. Specifically, referring to Figure 7The time wheel timer shown, the target delay task is task 15, the time slot storing the target delay task is Bucket8, and the trigger round of the target delay task is 4. However, there is no task list with a trigger round of 5 in Bucket8. At this time, the time wheel timer can create a task list with a trigger round of 5 in Bucket8 and store the target delay task in the task list. Wherein, when storing the target delay task in the task list, the time wheel timer can store the target delay task in the task list with a trigger round of 5 in Bucket8. Figure 7 The time wheel timer shown, the target delay task is task 15, the time slot storing the target delay task is Bucket8, and the trigger round of the target delay task is 4. However, there is no task list with a trigger round of 5 in Bucket8. At this time, the time wheel timer can create a task list with a trigger round of 5 in Bucket8 and store the target delay task in the task list. Wherein, when storing the target delay task in the task list, the time wheel timer can store the target delay task in the task list with a trigger round of 5 in Bucket8. Figure 11b

[0144] Next, taking the time wheel timer shown in Figure 7 as an example, the storage process of the delay task shown in Figure 10 is introduced. Referring to Figure 7 , the wheelsize of the time wheel timer is 12, that is, the time wheel timer has 12 time slots (Bucket0-Bucket11), the unit time TickDuration of the time wheel timer is 1min, and the start time of the time wheel timer is 8:00. At 8:31, the user submits task A, and the delay execution time of task A is 30min.

[0145] After receiving the user-submitted task A, the trigger time of task A needs to be calculated first. Wherein, in order to ensure the accuracy of the delay task trigger, when calculating the trigger time of task A, the internal representation time of the time wheel timer when task A triggers can be calculated.

[0146] Since the system time accuracy of the computing device to which the time wheel timer belongs is at least nanoseconds, when calculating the trigger time of task A, the calculation can be performed in nanoseconds. Wherein, 1min = 60000000000ns.

[0147] The time wheel timer starts at 8:00, and the internal timer of the timer does not use absolute time (that is, the system time of the computing device to which the time wheel timer belongs), but uses an internal timer. In order to facilitate calculation, it can be assumed that the internal timer of the time wheel timer is 0 at the start time (any value can be used, which does not affect the calculation result).

[0148] Next, the trigger time of task A, the trigger round of task A, and the index number of the time slot storing task A in the time wheel timer are calculated.

[0149] The time wheel timer receives task A at 8:31, and at 8:31, the internal timer value of the time wheel timer is: 0+31*60000000000 = 1,860,000,000,000ns.

[0150] ​When the task A is triggered, the value of each timer of the time wheel timer (i.e. the trigger time of the task A) is:

[0151] The trigger time of the task A = the internal timer value of the time wheel timer when the task is submitted + the delay execution time of the task A = 1,860,000,000,000 + 30*60000000000 = 3,660,000,000,000 nanoseconds.

[0152] The trigger round of the task A is:

[0153] Task

[0154] The index number of the time slot in which the task A is stored in the time wheel timer is:

[0155] Bucket index = trigger time of the task A / TickDuration % wheelsize = (3,660,000,000,000 / 60000000000) % 12 = 1.

[0156] That is, after the time wheel timer receives the task A at 8:31, the task A needs to be stored in the task list of the trigger round 5 in the Bucket 1 of the time wheel timer. When the task list of the trigger round 5 exists in the Bucket 1, the time wheel timer can directly store the task A in the task list. When the task list of the trigger round 5 does not exist in the Bucket 2, the time wheel timer needs to first create a task list of the trigger round 5 in the Bucket 1, and then store the task A in the task list.

[0157] It can be understood that, in the above embodiment, although the time units of the calculated times are unified as nanoseconds for convenience in calculating the trigger time of the delay task, the trigger round of the delay task, and the index number of the time slot in which the delay task is stored, the calculation in the unit of nanoseconds is only an exemplary description, and should not constitute any limitation on the actual implementation of the embodiment of the present application. In the actual operation of the time wheel timer, other time units can also be used for calculation, such as milliseconds.

[0158] It can be understood that the size of the serial number of each step in the above embodiments does not mean the order of execution, and the execution order of each process should be determined according to its function and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present application. In addition, in some possible implementations, each step in the above embodiments can be selectively executed, partially executed, or fully executed according to actual conditions, which is not limited here. In addition, all or part of any feature in the above embodiments can be freely and arbitrarily combined under the premise of no contradiction. The combined technical solution is also within the scope of the present application.

[0159] Based on the method in the above embodiments, the embodiments of the present application provide a computer readable storage medium, which stores a computer program. When the computer program is run on a processor, the processor executes the method in the above embodiments.

[0160] Based on the method in the above embodiments, the embodiments of the present application provide a computer program product, characterized in that when the computer program product is run on a processor, the processor executes the method in the above embodiments.

[0161] Based on the method in the above embodiments, the embodiments of the present application provide a computing device, which includes a mainboard and a chip. The chip is integrated on the mainboard, and the chip includes at least one memory for storing a program, and at least one processor for executing the program stored in the memory. When the program stored in the memory is executed, the processor is used to execute the method in the above embodiments. In the embodiments of the present application, the computing device can be a server, a host, or other network devices.

[0162] Based on the method in the above embodiments, the embodiments of the present application also provide a chip. Please refer to Figure 12 , Figure 12 for a structural schematic diagram of a chip provided by the embodiments of the present application. As shown in Figure 12 , the chip 1200 includes one or more processors 1201 and interface circuits 1202. Optionally, the chip 1200 can also include a bus 1203, and the processor 1201 can also include a business core and a security core, and the security core can further include an OTP register (not shown in the figure). Figure 12

[0163] ​The processor 1201 can be an integrated circuit chip having a processing capability for signals. In implementation, each step of the above method can be completed by integrated logic circuits or instructions in the form of software in the processor 1201. The processor 1201 described above can be a general processor, a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component. Each method and step disclosed in the embodiments of the present application can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor.

[0164] The interface circuit 1202 can be used for transmitting or receiving data, instructions or information. The processor 1201 can process the data, instructions or other information received by the interface circuit 1202, and can send out the processed information through the interface circuit 1202.

[0165] Optionally, the chip 1200 further includes a memory, which can include a read-only memory and a random access memory, and provide operation instructions and data for the processor. Part of the memory can also include a non-volatile random access memory (NVRAM).

[0166] Optionally, the memory stores executable software modules or data structures, and the processor can execute corresponding operations by calling operation instructions stored in the memory (which can be stored in an operating system).

[0167] Optionally, the interface circuit 1202 can be used to output the execution result of the processor 1201.

[0168] It should be noted that the functions of the processor 1201 and the interface circuit 1202 respectively can be realized by hardware design, software design or a combination of hardware and software, which is not limited here.

[0169] It should be understood that each step of the above method embodiment can be completed by the logic circuit in the form of hardware or the instruction in the form of software in the processor.

[0170] The method steps in the embodiments of the present application can be implemented by hardware, or by a combination of software and hardware executed by a processor. The software instructions can be composed of a corresponding software module, which can be stored in a random access memory (RAM), a flash memory, a read-only memory (ROM), a programmable read-only memory (PROM), an erasable PROM (EPROM), an electrically EPROM (EEPROM), a register, a hard disk, a mobile hard disk, a CD-ROM, or any other form of storage medium well known in the art. An exemplary storage medium is coupled to a processor, so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an ASIC.

[0171] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware, or any combination thereof. When implemented by software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium or transmitted by the computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server or data center to another website, computer, server or data center by wire (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server, data center, etc. integrated with one or more available media. The available media can be a magnetic medium (for example, a floppy disk, a hard disk, a magnetic tape), an optical medium (for example, a DVD), or a semiconductor medium (for example, a solid state disk (SSD)), etc.

[0172] It can be understood that the various numerical numbers involved in the embodiments of the present application are only for the convenience of differentiation, and do not limit the scope of the embodiments of the present application.

Claims

1. A method for executing a scheduled task, characterized in that, include: Determine that the loop pointer of the time wheel timer points to the first time slot of the first time slot, wherein the first time slot is the next time slot after the time slot currently pointed to by the loop pointer; If the current time is greater than or equal to the first time, the loop pointer of the time wheel timer will be pointed to the first time slot; Obtain the trigger cycle of the time wheel timer; According to the triggering round of the time wheel timer, the timed tasks in the first task list in the first time slot are triggered to execute. The triggering round of each timed task in the first task list is the same as the triggering round of the time wheel timer. The first time slot has different task lists, and the different task lists have different triggering rounds. The timed tasks in the same task list in the first time slot have the same triggering round.

2. The method according to claim 1, characterized in that, Triggering the execution of the scheduled task in the first task list includes: Traverse the first task list and trigger the execution of the target timed task; wherein, the target timed task is a timed task in the first task list whose trigger time is less than or equal to the current time.

3. The method according to any one of claims 2, characterized in that, After traversing the first task list and triggering the execution of the target scheduled task, the method further includes: The time wheel timer's loop pointer is determined to point to the second time slot of the second time slot, wherein the second time slot is the next time slot after the first time slot; If the current time is less than the second time, a third time is determined for the time wheel timer to need to sleep, and the third time is a time in milliseconds. If the third time is greater than or equal to 1 millisecond, the system function sleep is used to trigger the time wheel timer to sleep during the third time. If the third time is less than 1 millisecond, the time wheel timer is triggered to sleep using the Pause instruction.

4. The method according to claim 3, characterized in that, The step of triggering the time wheel timer to sleep using the Pause command includes: After waiting for a fixed time interval, if the current time is less than the second time, the time wheel timer will continue to be triggered to enter sleep mode.

5. The method according to any one of claims 1-4, characterized in that, Before triggering the execution of the scheduled task in the first task list in the first time slot, the method further includes: Receive a scheduled task, wherein the scheduled task includes a delay execution time; The trigger time of the scheduled task is determined based on the delay execution time of the scheduled task; Based on the trigger time of the scheduled task, determine the trigger round of the scheduled task and the first time slot corresponding to the scheduled task; Based on the triggering cycle of the scheduled task, the scheduled task is stored in the first task list in the first time slot.

6. The method according to claim 5, characterized in that, The step of storing the timed task into the first task list in the first time slot according to the triggering round of the timed task includes: Based on the triggering cycle of the timed task, a first task list is determined from at least one task list in the first time slot; If a first task list exists in the first time slot, the scheduled task is stored in the first task list; If the first task list does not exist in the first time slot, the first task list is added to the first time slot, and the scheduled task is stored in the first task list.

7. The method according to any one of claims 1-4 or 6, characterized in that, After triggering the execution of the scheduled task in the first task list, the method further includes: Traverse the second task list in the first time slot, where the trigger round of each timed task in the second task list is the same as the previous trigger round of the time wheel timer; If there are remaining scheduled tasks in the second task list, trigger the execution of the remaining scheduled tasks in the second task list; Delete the second task list from the first time slot.

8. The method according to any one of claims 1-4 or 6, characterized in that, The step of determining the first time when the cycle pointer of the time wheel timer points to the first time slot includes: Determine the number of rotations of the time wheel timer when its loop pointer points to the first time slot; The first time is determined based on the number of rotations of the loop pointer and the unit time of the time wheel timer; wherein, the unit time of the time wheel timer is the time required for the loop pointer to rotate once.

9. The method according to any one of claims 1-4 or 6, characterized in that, The first task list or the second task list is a doubly linked list.

10. A computing device, characterized in that, include: At least one memory for storing programs; At least one processor is configured to execute a program stored in the memory, which, when executed, causes the computing device to perform the method as described in any one of claims 1-9.

Citation Information

Patent Citations

  • Delay task execution method and device and electronic device

    CN110032438A