Linux Kernel Programming - Time,Delays,and Deferred Work

Measuring Time Lapses

The counter and the utility functions to read it live in <linux/jiffies.h>.Needless to say both jiffies and jiffies_64 must be considered read-only.

Processor-Specific Registers

#include <asm/msr.h>

rdtsc(low32,high32);
rdtscl(low32);
rdtscll(var64);

As an example using only the low half of the register,the following lines measure the execution of the instruction itself:

unsigned long ini,end;
rdtscl(ini);rdtscl(end);
printk("time lapse:%li\n",end - ini);

architecture-independent function:

#nclude <linux/timex.h>
cycles_t get_cycles(void);

Delaying Execution

#include <linux/wait.h>

long wait_event_timeout(wait_queue_head_t q,condition,long timeout);

long wait_event_interruptible_timeout(wait_queue_head_t q,condition,long timeout);

wait_queue_head_t wait;
init_waitqueue_head(&wait);
wait_event_interruptible_timeout(wait,0,delay);

Timeout value(delay) represents the number of jiffies to wait

#include <linux/sched.h>
signed long schedule_timeout(signed long timeout);

set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(delay);

Short Delays

#include <linux/delay.h>

void ndelay(unsigned long nsecs);
void udelay(unsigned long usecs); 
void mdelay(unsgined long msecs);

Kernel Timer

A kernel timer is a data structure that instructs the kernel to execute a user-defined function with a user-defined argument at user-defined time.

  • Timer functions must be atomic

  • No access to user space is allowed

  • No sleeping or scheduling may be performed

It's also worth knowing that in an SMP system,the timer function is executed by the same CPU that registered it

The Timer API
#include <linux/time.h>

struct timer_list{
    /*.................*/
    unsigned long expires;
    void (*function)(unsigned long );
    unsigned long data;
};

void init_timer(struct timer_list* timer);

struct timer_list TIMER_INITIALIZER(_function,_expires,_data);

void add_timer(struct timer_list *timer);
int del_timer(struct timer_list *timer);

Tasklets

Unlike kernel timers,however,you can't ask to execute the function at a speciic time.

Actually,a tasklet ,just like a kernel timer,is executed in the context of a "soft interrupt",a kernel mechanism that executes asynchronous task with hardware interrupts enabled.

#include <linux/interrupt.h>

struct tasklet_struct{
    /*...*/
    void (*func)(unsigned long);
    unsigned long data;
};

void tasklet_init(struct tasklet_struct *t,void (*func)(unsigned long),unsigned long data);
DECLARE_TASKLET(name,func,data);
DECLATE_TASKLET_DISABLE(name,func,data);

转载于:https://www.cnblogs.com/r1ng0/p/9824632.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值