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);

Linux Kernel Programming - Time,Delays,and Deferred Work的更多相关文章

  1. Linux kernel Programming - Allocating Memory

    kmalloc #include <linux/slab.h> void *kmalloc(size_t size,int flags); void kfree(void *addr); ...

  2. Linux kernel Programming - Advanced Char Driver Operations

    ioctl //user space int ioctl(int fd,unsigned long cmd,...); //kernel space int (*ioctl)(struct inode ...

  3. Linux kernel Programming - Concurrency and Race Conditions

    Concurrency and Its Management Race condition can often lead to system crashes, memory leak,corrupte ...

  4. linux kernel RCU 以及读写锁

    信号量有一个很明显的缺点,没有区分临界区的读写属性,读写锁允许多个线程进程并发的访问临界区,但是写访问只限于一个线程,在多处理器系统中允许多个读者访问共享资源,但是写者有排他性,读写锁的特性如下:允许 ...

  5. Linux Kernel中断子系统来龙去脉浅析【转】

    转自:http://blog.csdn.net/u011461299/article/details/9772215 版权声明:本文为博主原创文章,未经博主允许不得转载. 一般来说,在一个device ...

  6. Linux Kernel C语言编程范式

    介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...

  7. [中英对照]Linux kernel coding style | Linux内核编码风格

    Linux kernel coding style | Linux内核编码风格 This is a short document describing the preferred coding sty ...

  8. 如何进行Linux Kernel 开发

    转自:http://www.cppblog.com/flyonok/archive/2011/04/15/144316.html 如何进行Linux Kernel 开发? (Take 3) 译者序:这 ...

  9. Linux Kernel Maintainers

    http://en.wikipedia.org/wiki/Ingo_Molnár http://zh.wikipedia.org/wiki/英格·蒙內 Ingo Molnár Ingo Molnár, ...

随机推荐

  1. SpringCloud初体验之Eureka

    Eureka简介 SpringBoot简化了Spring工程的复杂度,之前复杂的Spring工程被拆分成了一个个小的SpringBoot工程.那么SpringBoot之间如何通讯,相互获取信息呢?这就 ...

  2. Git的概念及常用命令

    一.概念 Git是一个分布式的版本控制工具,区别于集中式管理的SVN. 二.优势 每个开发者都拥有自己的本地版本库,可以在本地任意修改代码.创建分支,不会影响到其他开发者的使用: 所有版本信息均保存在 ...

  3. jQuery之$.ajax()方法详解及实例

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  4. 【代码笔记】Web-HTML-头部

    代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!--ti ...

  5. 使用fiddle处理跨域

    认真的用fiddle处理跨域 相信很多前端的同学都或多或少被跨域这个问题烦恼过,网上很多处理的方式其实都是要后端处理, 用fiddle来处理 ,就不必看后端的脸色了,自己安安心心的倒腾接口,何乐而不为 ...

  6. 转载---解决 eclipse 中发布的java-web工程,jar包无法发布到tomcat 的lib下。

    1.首先: Server Locations修改后会变灰,如果需要更改设置,则需要移除与Tomcat服务器关联的项目,同时,鼠标右键菜单Clean清除Tomcat服务器的状态^^就可以修改了. 此时E ...

  7. Kotlin入门(10)七十二变的输入参数

    上一篇文章介绍了Kotlin对函数的基本用法,包括函数的定义.输入参数的声明.输出参数的声明等等,这些足够对付简单的场合了.当然了,倘若一门新语言仅仅满足于这些雕虫小技,那也实在没什么前途.既然Kot ...

  8. python变量的命名空间

    首先必须要提一下python程序执行过程中变量的查找规则 较官方的查找机制是: 局部作用域--外部函数作用域--全局作用域--内建函数作用域 其实一般内建函数中的作用域很少会涉及到,因为内建函数其实是 ...

  9. centos7下安装rabbitmq

    RabbitMQ: RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.Rab ...

  10. Maven 安装 on centos7

    本文演示如何在CentOS7上安装maven. 1 准备工作 1.1 进入官网下载栏目 http://maven.apache.org/download.cgi 找到下载列表中 Binary tar. ...