"Grooming"--linux core timers

Source: Internet
Author: User

The Linux kernel timer is a mechanism that the kernel uses to control the execution of a function at some point in the future (based on jiffies), which is implemented in the <linux/timer.h> and kernel/timer.c files.

The dispatched function must be executed asynchronously, similar to a "software break," and is in a non-process context, so the dispatch function has to adhere to the following rules:

1) No current pointer, no access to user space. Because there is no process context, there is no connection between the associated code and the interrupted process.

2) You cannot perform hibernation (or functions that may cause hibernation) and dispatch.

3) Any data structures that are accessed should be protected against concurrent access to prevent competitive conditions.

The scheduler function of the kernel timer is run once and will not be run again (equivalent to Automatic logoff), but can be run periodically by rescheduling itself in the function being dispatched.

Second, function use

Here are the API functions for the timer:

(0) Initialize the timer:

void Init_timer (struct timer_list * timer);

(1) Add timer:

void Add_timer (struct timer_list * timer);

Register the timer and place the timer in the core timer list

(2) Remove timer:

int Del_timer (struct timer_list * timer);

(3) Modify the expire of the timer:

int Mod_timer (struct timer_list *timer, unsigned long expires);

Third, the use of the process

The general process for using timers is:

(1) Timer, 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.

Attention:

The Mod_timer kernel uses the function Mod_timer to implement the timer timeout that has already been activated.

The Mod_timer function can also operate a timer that has been initialized but has not yet been activated, and if the timer is not activated, Mod_timer activates it. If the timer is not activated when called, the function returns 0, otherwise 1 is returned. Once returned from the Mod_timer function, the timer is activated and a new timing value is set. If you need to stop the timer before the timer times out, you can use the Del_timer function.

The function can be used by either the activated or inactive timer, which returns 0 if the timer has not been activated, otherwise 1. When removing timers, one must be careful of a potential competitive condition. When Del_timer returns, it is guaranteed that the timer will not be reactivated, but the timer interrupt on the multiprocessor may already be running on the other processing, so you need to wait for the timer handler that may be running on the other processor to exit, and then you need to use Del_timer_ The sync function performs the removal work. Unlike the Del_timer function, the Del_timer_sync number cannot be used in the context of an interrupt.

Iv. Hz and Jiffies

The system timer can interrupt the processor at a programmable frequency. This frequency is the number of timer beats per second, which corresponds to the kernel variable Hz. Choosing the right Hz value requires a trade-off. When the Hz value is large and the timer interval is small, the accuracy of the process scheduling is higher. However, the higher the Hz value will result in more overhead and power consumption because more processor cycles will be spent in the context of the timer interrupt.

The value of Hz depends on the architecture. On the x86 system, in the 2.4 kernel, the value is set to 100 by default, and in the 2.6 kernel the value becomes 1000, and in 2.6.13 it is lowered to 250. On an arm-based platform, the 2.6 core sets the Hz to 100. In the current kernel, you can select a Hz value from the configuration menu when compiling the kernel. The default value for this option depends on the version of the architecture.

The jiffies variable records the number of times the system timer has been triggered since the system started. The kernel increments the jiffies variable by a Hz time per second. Therefore, for systems with a Hz value of 100, 1 jiffy equals 10ms, and for systems with a Hz of 1000, 1 jiffy are only 1ms.

Vi. examples

...

#include <linux/timer.h>

...

static struct Timer_list Key_autorepeat_timer =

{

Function:key_callback

};

static void

Kbd_processkeycode (unsigned char keycode, char up_flag, int autorepeat)

{

Char Raw_mode = (Kbd->kbdmode = = Vc_raw);

if (Up_flag) {

Rep = 0;

if (!test_and_clear_bit (KeyCode, Key_down))

Up_flag = Kbd_unexpected_up (keycode);

} else {

Rep = Test_and_set_bit (KeyCode, Key_down);

/* If the keyboard autorepeated for us, ignore it.

* We do our own autorepeat processing.

*/

If (Rep &&!autorepeat)

Return

}

if (Kbd_repeatkeycode = = KeyCode | |!up_flag | | raw_mode) {

Kbd_repeatkeycode =-1;

Del_timer (&key_autorepeat_timer);

}

...

/*

* Calculate The next time we have to do some autorepeat

* Processing. Note that we don't do autorepeat processing

* While in raw mode autorepeat processing in

* Medium Raw mode.

*/

if (!up_flag &&!raw_mode) {

Kbd_repeatkeycode = KeyCode;

if (Vc_kbd_mode (KBD, vc_repeat)) {

if (rep)

Key_autorepeat_timer.expires = jiffies + kbd_repeatinterval;

Else

Key_autorepeat_timer.expires = jiffies + kbd_repeattimeout;

Add_timer (&key_autorepeat_timer);

}

}

...

}

"Grooming"--linux core timers

Related Article

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.