Linux Kernel Time, latency, and Operation Delay

Source: Internet
Author: User
Linux Kernel Time, latency, and latency-Linux general technology-Linux programming and kernel information. For more information, see the following. In linux, clock interruption is generated at a periodic interval of the system timer. The interval is determined by the kernel according to the Hz value. Hz is a constant related to the architecture, which is defined in Or the file contains files related to a sub-platform. For real hardware, the released Linux kernel source code defines the default Hz value for most platforms as 50 ~ 120. The software policy Hz is 24. most platforms have 100 or 1000 clock interruptions per second. on common x86PC platforms, the default value is 1000. As a general rule, even if you know the Hz value of the corresponding platform, the Hz value should not be relied on during programming.

1: In the Linux kernel, when the clock is interrupted, the value of the internal counter in the kernel is automatically added. This value is initialized to 0 during system boot. Therefore, the value is the number of clock ticking times since the previous operating system boot. The value is a variable with jiffies as unsigned long, which is valititle, this prevents the compiler from optimizing statements for this variable, including File.

2: delayed execution. The driver has a long latency, that is, longer than a clock. there are many ways to implement the latency of this class of punishment, one is busy with latency, the use of the while loop for example:
While (time_before (jiffies, j1 ))
{
Cpu_relax ();
}
Here, the time_before () function compares two jiffies values. Among them, if jiffies is before j1, true is returned, so this simple loop can implement latency, however, a very important drawback is that if the interruption is disabled before the loop is started, that is, the tick answer will not be updated, the loop will never exit.

3: Timeout: similar to waitforsignalobject in windows. wait for an event at a specified time in linux. common functions include wait_event_timeout (wait_queue_head_t q, condition, long time_out) and wait_event_interruptible_timeout (wait_queue_head_t q, condition, long timeout );
Here, the value of timeout is the jiffies value to wait, not the absolute time value.
The implementation method is as follows:
Wait_queue_head_t wait;
Init_waitqueue_head (& wait );
Wait_event_interruptiable_timeout (wait, 0, delay );

4: short latency. when the device driver needs to handle the build delay, the compromise delay usually takes up to several milliseconds. In the compromise State, relying on the number of tick answers is obviously not the correct method. the following convenient functions are provided in the kernel: void ndelay (unsigned long nsecs );
Void udelay (unsigned long usecs); void mdelay (unsigned long msecs); latency in nanoseconds, microseconds, and milliseconds, respectively.

5: Kernel timer. If we want to schedule an action at a certain time point in the future and our colleagues will not block the current process before the time point arrives, we should use the kernel timer. the kernel timer can be used to execute a function at a specific time point in the future to accomplish many things. For example, if the hardware cannot be interrupted, the kernel timer is used to periodically patrol the device.
The APIS related to the kernel timer include add_timer and del_timer. File contains
Struct timer_list
{
Unsigned long expires; and jiffies values
Void (* function) (unsigned long); function called back after the arrival time value
Unsigned long data; value passed to the callback function
}

For example:
Unsigned long j = jiffies;
Data-> prevjiffies = j;
Data-> buf = buf2;
Data-> loops = JIT_ASYNC_LOOPS;

Data-> timer. data = (unsigned long) data;
Data-> timer. function = jit_time_fun;
Data-> timer. expires = j + delay;
Add_timer (& data-> timer );
Wait_event_interruptible (data-> wait ,! Data-> loops );


Void jit_time_fun (unsigned long arg)
{
Struct jit_data * data = (struct jit_data *) arg;
Unsigned long j = jiffies;
If (-- data-> loops)
{
Data-> timer. expires + = tdelay;
Data-> prevjiffies = j;
Add_timer (& data-> times );
}
Else
{
Wake_up_interruptible (& data-> wait );
}
}

6: tasklet

Compared with the kernel timer, tasklet is very similar to the kernel timer. The only difference is that we cannot require tasklet to be executed at a given time. A typical tasklet application is in the interrupt service function, during hardware interruption, you must manage the hardware interruption as quickly as possible, while most data management can safely delay to thick-layer execution.

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.