In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Brief introduction to the working principle of 1.Tick
The working principle of Tick is actually the working principle of hardware timer. A system tick represents a timer hardware interrupt. The timer works simply because there is an internal decreasing counter that produces an interrupt when it is reduced to 0, as shown in figure 1-1:
Figure 1-1 how the timer works
Assuming that the input frequency of the timer module is 1MHz, and the number of tick in 1s defined by the system is 100, that is, 100Hz, we can calculate that the value to be set for the decrement counter is 1MHz/100Hz=10000. It can be seen that the decreasing counter is equivalent to a frequency divider, and the value of each pulse at the input is subtracted by 1. When it is reduced to 0, an interrupt is generated, and its value is automatically reloaded to 10000, so the cycle goes on.
two。 The system acquires time operation
The system acquires the time-dependent interface based on tick, but there is an error, as shown in figure 2-1:
Figure 2-1 system acquisition time
The dotted line indicates that the next tick interrupt has not yet been generated. If the time is obtained at this time, the time obtained is only the time accumulated by the previous tick. Assuming that the span between the time when the tick interrupt is generated and the time when the time is acquired is 4ms, then the acquired time has the error of 4ms, and the high-precision clock is born to eliminate this error.
3. High precision clock principle 3.1 basic principle
The root cause of the above error is that the span between the tick interrupt generation time and the acquisition time is not updated to the time. If the time is calculated and added to the acquisition time, the acquisition time can be corrected. Combined with figures 1-1 and 2-1, the basic correction principles are as follows:
1 tick time is equivalent to the initial value of the decrement counter, assuming that it is 10000, that is, decreasing 10000 times equals to elapsing a tick time.
Use 1000 * 1000 * 1000 / 10000 to get a decreasing time in ns
Subtract the value in the counter at the time when the time is obtained by subtracting the initial value of the counter from the initial value of the counter, and you get the number of times that the counter has been decremented to get the time.
Using the decreasing time * the decreasing number of times, the time span between the time when the tick interrupt is generated and the time when the time is obtained
3.2 Special circumstances
When the system is multi-core, the system produces a tick interrupt handled by CPU0. When CPU0 has not updated the tick number of the whole system, then CPU1 gets the time. According to the basic principle, it is correct to add a tick time after calculation.
4. Code presentation
Program listing 4-1
VOID bspTickHighResolution (struct timespec * ptv)
{
REGISTER UINT32 uiCntCur, uiDone
UiCntCur = (UINT32) timerGetCnt (4)
UiDone = GuiFullCnt-uiCntCur
/ *
* check if there is a TICK interrupt request
, /
If (rSRCPND & BIT_TIMER4) {
/ *
* here, because the TICK is not updated in time, it needs to be re-acquired and add a TICK time
, /
UiCntCur = (UINT32) timerGetCnt (4)
UiDone = GuiFullCnt-uiCntCur
If (uiCntCur! = 0) {
UiDone + = GuiFullCnt
}
}
Ptv- > tv_nsec + = (LONG) ((Gui64NSecPerCnt7 * uiDone) > > 7)
If (ptv- > tv_nsec > = 1000000000) {
Ptv- > tv_nsec-= 1000000000
Ptv- > tv_sec++
}
}
GuiFullCnt represents the initial value of the decreasing counter, that is, the count value of 1 tick time; Gui64NSecPerCnt7 represents the time of decreasing once, but this time has been expanded 128times in order to improve the accuracy of calculation.
UiDone indicates the number of times that the time counter has been decremented, and uiCntCur indicates the current value of the time counter.
If (rSRCPND & BIT_TIMER4) is used to judge special cases. The condition holds that the tick number of the system has not been updated yet, and the tick number of the system has been updated if it is not established.
When uiCntCur=0, it means that the tick interrupt has just occurred, and then uiDone already represents a count value of tick, so there is no need to add a count value of tick.
Finally, the revised time is assigned to the tv_nsec members.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.