Time wheel Algorithm

Source: Internet
Author: User
Tags ranges

Problem Introduction: each player in the game has a lot of buffs. In every tick (minimum time period), you must check whether every buff in the buff has expired and how it works, this causes a long list to be traversed in each tick, which is obviously not good.

How to optimize it?

1. Original Model:

The status of the buff must be updated in every tick! As you can imagine, every time the pointer is moved, it will drag all the buckets very heavily, so terrible ......

2. Optimization Model 1:

We need to avoid that the original model traverses the list in every tick, so we try to use times as the key, specify the end time and start time of a buff when it is added,

Note: When the total number of tick rounds has exceeded the total number of buckets to be added! For example, if the total number of tick times is 12 and the pointer reaches tick = 2, add a buff that passes through tick for 15 (effective). What should I do?

It can be calculated as follows: 2 + 15% 12 = 5. Put this buff in the tick = 5 slot (each buff records its end time ), after tick jumps from 2 to 2 and then 3 to tick = 5, this buff will be executed.

This model perfectly solves the problem of the original model (every tick traverses the entire bufflist). It seems perfect, but it introduces new problems. Our minimum tick is obviously impossible to calculate by hour, if we divide the tick into seconds, there will be 12*3600 = 43200 keys in a round,

If a buff has an effect every 3 seconds: (blood is added every 3 seconds), this buff will be saved as 43200/3 = 14400 !!!! If there are a large number of such buckets, data redundancy will be very large, and the storage space will be very large ......

How can we ensure both precision and efficiency? See model 3.

3. Optimization Model 3

A cool clock chart is provided on the Internet for example:

This is to use the layered time wheel model:

% 1. Clock principle description:
% 1.1. initialize a three-tier time wheel: second dial: 0 ~ 59 seclist, engraved Disk: 0 ~ 59 minlists, time Disk: 0 ~ 12 hourlists;
% 1.2. sectick is pushed by the outside world. In each hop round (60 cells), sectick is reset to 0, and mintick jumps to 1 cell;
% 1.3. Similarly, mintick resets each hop round (60 cells) to 0, and hourtick skips 1 cell;
% 1.4. Top Level: hourtick performs one round (12 cells), hourtick resets to 0, and the full cycle of a time wheel is completed.
% 2. Event principles:
% 2.1. When setting the Event Time To timeout, calculate the pointer position {triggerhour, triggermin, triggersec} at this time point based on timeout };
% 2.2. Compare {triggerhour, triggermin, triggersec} with the current pointer {nowhour, nowmin, nowsec} to find the pointer where the event is stored (tick );
% 2.3. Every time the pointer of all layers jumps to the next cell (tick01), a checkpoint event list is triggered to process each event event01:
% 2.3.1 according to the remaining timeout of event event01, event01 should have the position POS of the previous (faster hop) layer;
% 2.3.2 update the event to the new pos (update timeout );
% 2.3.3 repeat all the events in tick01;
% 2.3.4 clear the tick01 event;
% 2.3.5 all events at the bottom (fastest hop) layer will be executed immediately when pointer tick is encountered;

I used Erlang to implement a Tiering time wheel. If you are interested, you can visit it. :) you are welcome to use your own good language to create a beautiful wheel, you can still find many interesting details by hand.

Https://gist.github.com/zhongwencool/eca6609b59ed635de164

Translation: Real-time concepts for Embedded Systems Chapter 11 timer and timer serviceshttp: // www.embeddedlinux.org.cn/rtconforembsys/5107final/lib0071.html

The above buff optimization idea also comes from this, a very simple and easy-to-understand concept of time wheel.

11.6 time Wheel)

As shown in figure11.11: The time wheel is a fixed array structure. Each slot (element) in this array represents the precision of the soft timer (similar to the minimum scale of the clock ). advantages of time wheel: timers can be effectively updated through the sorted time list. it can install (instaallation) very efficiently and cancel (Cancellation) timer. (the time complexity of these two operations O (1 )).

Figure 11.11 timing wheel.

Soft-timer facility uses hadware timer to determine a minimum scale (tick ). based on the hard time cycle timer, it drives all the soft time installed on it. the time-out frequency determines the precision of the soft time. For example, if the tick precision is defined as 50 ms, each slot represents 50 ms, this is also the minimum timeout event that can be installed in this timer. at the same time, a two-way linked list stores timeout event handlers (also called callback funciton or callbacks) in each slot, and CALLS callbacks when timer expires (expired. Therefore, the timers list also represents the list of events processed during the expiration time. Description of each time slot figure11.12:

Figure 11.12: timeout event handlers.

Every time the clock turntable goes through a tick, it will point to the next time. When the Pointer Points to the last slot of the array, the next time will return to the first slot of the pointer. The concept of time wheel comes from this. Therefore, when a new event is installed, the current position of the turntable determines which slot the new event should be placed, as described in figure11.13, each slot goes through represents the past 50 ms

Figure 11.13: Installing a timeout event.

This time slot indicates that if someone wants to install a 200 ms timeout event, the event can be placed in the + + slot, of course, the starting position of the turntable is at the beginning of the slot (the clock dial in the figure). In other words, when the turntable (clock dial) points to the initial slot, this event will return the index of the array ).

11.6.1 problem (issues)

The series of problems in the above time wheel method:

Question 1:The number of slots is limited (maybe different systems have different limits). For example, you can see in figure11.13 that the maximum processor length (total slot) is very obvious) it is 350 ms. What should I do when I want to install a MS Event? This will cause time wheel overflow. To solve this problem, one method is to disable installation of events that exceed the range, and the other better method: place these overflow events in a unified event buffer. When the turntable is switched to the next scale, events that match the range are retrieved from the buffer, in this way, these events can also be handled. You can carefully study figure11.14 to get the answer:

Figure 11.14: Timing wheel Overflow Event buffer.

For example, if the turntable is located at a zero Scale (Position 1 in the figure) and a Ms timeout is to be installed, this event must temporarily exist in the overflow buffer, as the turntable turns to + 50 ms (2 in the figure), this event will be taken out of the buffer zone for installation. similarly, a 150 ms event can only be installed on the turntable to + MS (3 in the figure. Every time the turntable points to the next moment, it will check the event buffer, which requires that the list of events in the buffer zone is growing. If this list is very long, in this case, the price for new events will be very high.

Question 2:For the accuracy of this time wheel, imagine: When tick points to time wheel and points to the next time scale, if another 150 ms event is installed, this event is installed in + MS, or in the case of + 200? On average, when the error probability is equal, the error may be delayed or half of the minimum scale in advance, where the error is 50 ms/2 = 25 ms.

Question 3:Important question: how to install callbacks. in theory, each callback should occur at the same time when the time expires, but in reality, this is impossible, and the working status of each callback is unpredictable. Therefore, the length of each callback executed is unpredictable. As a result, there is no way to ensure that a callback after a long list will be executed immediately. This problem is not necessary and cannot be introduced into the system. Figure11.15 describes the problem:

Figure 11.15: unbounded soft-timer handler invocation.

When T1 timeout expires, event processing function 1 immediately occurs. Similarly, event processing function n is triggered when T (n-1) is reached, since the execution length of each processing function is uncertain, the length of x and y in all graphs is also uncertain.

In theory (ideally), this time processing sets an upper limit for processing events. For example, when the event processing function n is executed, it is more than MS from event processing function 1, other events that are not executed will be ignored. This problem is very difficult. The solution is also specific to the application.

11.6.2 hierarchical timing wheels)

In figure11.14Question 1: The overflow problem can be solved using a layered time wheel.

Soft-time devices need to adapt to events in different ranges. This span can be very large. For example, it takes 3000 (5 × 60 × 10) to adapt to the timers category from MS to 5 minutes) because the accuracy of the time wheel must be at least 100 ms, which is also the minimum precision of the time wheel:

10 × 100 Ms = 1 sec 10 entries/sec 60 sec = 1 minute 60 × 10 entries/min therefore: 5 × 60 × 10 = requires 3000 scales (entries ).

A layered time wheel is like a digital dial pointer clock. It is installed in this layered structure with multiple time wheels to replace the preceding single time wheel. Each time wheel has its own granularity precision. The time turntable is associated with all the time wheels. When the time wheel at the lowest layer rotates round, the time wheel of the previous layer is converted to a unit. The Tiering time wheel requires only 75 (10 + 60 + 5) entries to ensure that the timeout ranges from MS to 5 minutes. The multidimensional array used here:

10 × 100 Ms = 1 sec 10 entries/sec 60 sec = 1 minute 60 entries/min 5 entries for 5 minutes so: 5 + 60 + 10 = requires only 75 scales (entries)

Figure 11.16: A hierarchical timing Wheel

This model not only saves a lot of space, but also maintains a high accuracy and span. figure11.16 illustrates this. For example, it may install a timeout event at 2 minutes 4 seconds Ms. First Install 2 min. When 2 min occurs, it checks that there are still 4.3s events before timeout. Therefore, it installs the 4S timeout event slot. When 4s passes, check that there is still 300 ms before timeout, and a MS Event is installed. After ms, the real timeout will be executed.

 

 

If you think the above is not enough: there is a big meal here:

1. Analysis of the implementation method of timer in Linux http://www.ibm.com/developerworks/cn/linux/l-cn-timers/

2. http://www.ibm.com/developerworks/cn/linux/1308_liuming_linuxtime3/ of time programming and implementation principle in Linux

Isn't the time wheel amazing: As mentioned above: the layered model figure11.16 saves a lot of space? Can you tell me how to do it? Think about it. If there are 1000 events and the space of these events cannot be reduced, what space does it mean?

If you know, please do not mean :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.