Introduction to Linux-driven timers and kernel-time learning

Source: Internet
Author: User

This chapter is excerpted from the following netizens:

Http://blog.sina.com.cn/s/blog_6e5b342e0100m87d.html

First, how to record the time in the kernel

Any program requires time control and its main purpose is to:

    • Measuring time lapse and comparison time
    • Know the current time
    • Delay action for a specified amount of time

To achieve this, the application uses calendar time (month, day, minute), or 0 minutes and 0 seconds from January 1, 1970 to the current number of seconds to measure the elapsed time, but the kernel requires a more accurate time measurement, so the kernel uses clock ticks to record time. When the clock interrupt occurs, the internal time counter of the kernel increases by 1 (i.e. add a clock tick), the system boots at 0, the current value is the number of clock ticks since the last system boot, and the program can be accessed through the kernel-defined global variables jiffies_64 or jiffies. The number of ticks per second on real hardware varies from 50 to 1200, the default is 1000 on x86, and 200 on s3c2440, and for unified programming interfaces, the kernel defines a macro Hz that represents a 1-second tick, which can be used by the program.

Jiffies and jiffies_64 are unsigned long type read-only variables, which are used as follows:

    • #include <linux/jiffies.h>
    • unsigned long J, Stamp_1, Stamp_half, Stamp_n;
    • j = jiffies;
    • Stamp_1 = j + HZ;
    • Stamp_half = j + hz/2;
    • Stamp_n = j + N * hz/1000;

Note: When the 32-bit platform is 1000 HZ, the counter only overflows every 50 days, and your code should be prepared to handle this event if necessary

Compare the size of 2 time, the following macro definition of common kernel:

    • #include <linux/jiffies.h>
    • int Time_after (unsigned long A, unsigned long B);
    • int Time_before (unsigned long A, unsigned long B);
    • int time_after_eq (unsigned long A, unsigned long B);
    • int time_before_eq (unsigned long A, unsigned long B);

They are true for time a after time B, before, after, or equal, before or equal, and vice versa.

      • Find the difference between the 2 Jiffies instances:
        • diff = (long) T2-(long) T1;.
      • You can convert a jiffies difference to milliseconds, generally by:
        • msec = diff * 1000/hz;
      • Jiffies conversion function with Calendar time:
        • #include <linux/time.h>
        • unsigned long timespec_to_jiffies (struct timespec *value);
        • void Jiffies_to_timespec (unsigned long jiffies, struct timespec *value);
        • unsigned long timeval_to_jiffies (struct timeval *value);
        • void Jiffies_to_timeval_r (unsigned long jiffies, struct *timeval)

Second, the core timer

1. Overview

    • Whenever you need to schedule an action to occur, you can use a kernel timer, such as: when the hardware cannot be interrupted, you can check the status of a device at regular intervals by using a kernel timer.
    • A kernel timer is a data structure that directs the kernel to execute a user-defined function at a user-defined time, using a user-defined parameter
    • Execution by kernel thread-soft interrupt (ksoftirqd/0) dispatch

    A CPU, a KSOFTIRQD

Ksoftirqd belongs to Atomic context

KSOFTIRQD runtime, IRQ is not disabled

2. Timer API

#include <linux/timer.h>struct  timer_list {    long  expires;     void Long );     Long data;    other fields};

      static initialization of the timer structure:
      struct Timer_list timerval = Timer_initializer (_function,_expires,_data)
      dynamic initialization of the timer structure:
      setuo_timer (struct timer_list *timer,_function,_data); After initializing function and data, call Init_timer
      init_timer (struct timer_list *timer);
      timer.expires = jiffies+100; or: Timer.expires = jiffies + HZ/10 (Specify trigger time manually)
      Add the already initialized timer to the system Timer list:
      void Add_timer (struct timer_list *timer);
      Note: After the timer executes, it will automatically exit the system timer chain list, if need to be executed again, after updating expires, add the system timer list again
      update the time-out of a timer while adding the System link List
      int Mod_timer (struct timer_list *timer,unsigned long expires)
      Remove timer, exit system timer
    list
      int Del_timer (struct timer_list *timer)
      third, how to implement the delay in the kernel
        device drivers often need to delay the execution of code for a specific fragment for a period of time to allow the hardware to complete a task. Delay is generally divided into short delay and long delay
            1, short delay: When a device driver needs to wait for the response time of the hardware, the delay involved is often up to a few milliseconds. This kind of delay is short delay, generally use busy waiting. (Busy waiting for my personal understanding that the processor is still in this process)
          the
            relevant functions are as follows:
#include <linux/delay.h>voidlong  nsecs); void Long usecs); void long msecs);

2, Long delay:

If you need to postpone it for a long time, you can take a long delay. Long delay can be divided into busy waiting and yield two ways of CPU.

1), Busy waiting:

unsigned long J1 = jiffies + 2*hz;
while (Time_before (Jiffies, J1))

Cpu_relax ();

The Cpu_relex call uses a system-specific approach, and you're not doing anything with the processor at this time, comparing the wasted processor resources

2), give up the processor

unsigned long J1 = jiffies + 3600*hz;

while (Time_before (Jiffies, J))

{

Set_current_state (task_interruptible);

Schedule_timeout (30*hz);

}

3) Also, if your driver uses a wait queue to wait for some other event, but you also want to make sure that it runs in a certain period of time, rather than waiting forever, you can use timeouts

  

#include <linux/wait.h>longlong  timeout); Long long timeout);

Later, you will write a Linux modular driver programming instance and delay.

Introduction to Linux-driven timers and kernel-time learning

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.