Linux Timer and time management "turn"

Source: Internet
Author: User

Transferred from: http://blog.chinaunix.net/uid-23228758-id-154820.html

Timer and Time management:

1 , beat rate-- HZ : In Alpha Architecture 1024x768 , and on other platforms, Ten order of magnitude times. 100 (2.6 cores) on embedded ARM . What is the meaning of this value, that is, the clock interrupts on the ARM platform for one second. In general, programmers do not change this value, because the kernel coding a lot of code is time-required, and the kernel is written in many places have done the corresponding optimization and trade-offs, changing the value of HZ will have a great impact on the performance of the system.

2. Jiffies: This value is used to record the total number of Beats generated by the system since the start of the system, the kernel initializes the variable to 0 at startup, and the value of the variable is incremented for each time the clock interrupt handler is added, jiffies the value of the increment in one second is Hz, and the system run time is calculated in seconds. The system is running for jiffies/hz seconds.

In the following definition (2.6 cores):

extern u64 __jiffy_data jiffies_64;
extern unsigned long volatile __jiffy_data jiffies;

In the 2.6 kernel, its variable type changed from unsigned long to u64, meaning that even 32-bit machines use unsigned 64-bit integers to represent jiffies. Most of the code involves only the low 32 bits of jiffies, and the code that accesses jiffies reads the low 32 bits of jiffies_64, and the entire 64 bits can be read through the get_ jiffies_64 () function. In a 64-bit architecture, jiffies_64 and jiffies refer to the same variable, and the code can be read either through Jiffies or get_ jiffies_64 ().

3, in order to avoid the jiffies value of the wrapping (overflow), the kernel provides several API functions to compare the beat count. defined in the.

/* time_is_before_jiffies(a) return true if a is before jiffies */
#define time_is_before_jiffies(a) time_after(jiffies, a)
/* time_is_after_jiffies(a) return true if a is after jiffies */
#define time_is_after_jiffies(a) time_before(jiffies, a) 
/* time_is_before_eq_jiffies(a) return true if a is before or equal to jiffies*/
#define time_is_before_eq_jiffies(a) time_after_eq(jiffies, a)
/* time_is_after_eq_jiffies(a) return true if a is after or equal to jiffies*/
#define time_is_after_eq_jiffies(a) time_before_eq(jiffies, a)

Cases:

You can use Jiffies to set timeouts, for example:

unsigned long timeout = jiffies + HZ * 2; // 2秒钟后超时
       if(time_before(jiffies, timeout){
       // 还没有超时
    }
    else{
       // 已经超时
    }

4. Time Type

There are 4 commonly used time types under Linux: Time_t,struct timeval,struct timespec,struct TM. Typically used for actual time (wall time), defined in the file.

(1) time_t is a long integer, generally used to denote the number of seconds since 1970 years, Greenwich Mean time.

(2) Struct Timeval has two members, one is seconds, and the other is subtle.

struct timeval{
            long tv_sec; /**//* seconds */
            long tv_usec; /**//* microseconds */
 };

(3) struct TIMESPEC has two members, one is seconds and one is nanosecond.

struct timespec{

         time_t tv_sec; /**//* seconds */格林威治时间
         long tv_nsec; /**//* nanoseconds */
 };

(4) struct TM is an intuitive means of time representation:

struct TM {
int tm_sec; /**//* seconds */
int tm_min; /**//* minutes */
int tm_hour; /**//* hours */
int tm_mday; /**//* Day of the month * *
int Tm_mon; /**//* Month * *
int tm_year; /**//* Year */
int tm_yday; /**//* Day in the year */
int tm_wday; /**//* Day of the week * *
int tm_isdst; /**//* Daylight Saving Time */
};

: General use int gettimeofday (struct timeval *tv, struct timezone *tz); Get the wall time, the wall time is generally used in the user space, and in the kernel space, in most cases as long as the relative time to get OK , which means using jiffies done.

5. Core Timer

The kernel timer can be understood as a software timer, which can be dynamically created, changed and destroyed, and there is no limit to the number of runs.

The timer is the same as the structure time_list, this struct is defined in.

struct timer_list {
       struct list_head entry; //包含定时器的链表
       unsigned long expires; //以jiffies为单位的定时值
       void (*function)(unsigned long); //定时时间到的处理函数
       unsigned long data; //传给片处理函数的参数,可以用在共用一个处理函数的情况。
       struct tvec_base *base; //内部值,用户呼略是安全的
#ifdef CONFIG_TIMER_STATS
       void *start_site;
       char start_comm[16];
       int start_pid;
#endif
#ifdef CONFIG_LOCKDEP
       struct lockdep_map lockdep_map;
#endif
};

When using timers, it is not necessary to gain a deeper understanding of the members of this data structure body. You can use the API in the to manipulate.

Add timer

void Add_timer (struct timer_list * timer);
Remove Timer

int Del_timer (struct timer_list * timer);
Modifying the expire of a timer

int Mod_timer (struct timer_list *timer, unsigned long expires);
The general process for using timers is:
(1) Define timer and write function;
(2) for the timer expires, data, function assignment value;
(3) Call Add_timer to add the timer to the list;
(4) When the timer expires, function is executed;
(5) in the program involved in the timer control where appropriate to call Del_timer, Mod_timer Delete timer or modify the timer expires.
Add: Generally should use Del_timer_sync () instead of Del_timer (), on my board is not necessary, because my board is a single processor, in the SMP must replace the use.

6. Short delay

It's been a long time before, and there are two shorter latency functions available in the kernel for developers to use

Udelay (unsigned long usecs); Microseconds
Mdelay (unsigned long msecs); Milliseconds
The former uses a software loop to specify the subtle number, which calls the former to reach the delay millisecond level. The Udelay function can only be used to obtain a short time delay because the Loops_per_second value has a precision of only 8 bits, so a considerable amount of error is accumulated when the longer delay is calculated. Although the maximum allowable delay is nearly 1 seconds (because the longer delay is going to overflow), the maximum value of the parameters of the recommended Udelay function is 1000 microseconds (1 milliseconds). function Mdelay can be used when the delay is greater than 11 milliseconds.
: It is important to note that Udelay is a busy wait function (so Mdelay is also) that it is not possible to run other tasks during the delay period, so be very careful, especially mdelay, unless there is no other way to avoid it.

Linux Timer and time management "turn"

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.