日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

linux上的定时器上的jiffies,Linux kernel -- 定时器/jiffies

發布時間:2024/9/27 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux上的定时器上的jiffies,Linux kernel -- 定时器/jiffies 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0. 測試環境

Linux 2.6.39 AT91SAM9G45

1. 定時器簡單的測試例子

#include

#include

MODULE_LICENSE("GPL");

struct timer_list tm;

static int num;

static void func()

{

num++;

mod_timer(&tm, jiffies + HZ);

printk("Hello, timer :%d\n", num);

}

static int timer_init(void)

{

num = 0;

init_timer(&tm);

tm.expires = jiffies + HZ;

tm.function = func;

add_timer(&tm);

return 0;

}

static void timer_exit(void)

{

del_timer(&tm);

printk("remove timer\n");

}

module_init(timer_init);

module_exit(timer_exit);

Makefile

obj-m := timer.o

KERNEL := /Android/linux-2.6.39-android_altus

PWD := $(shell pwd)

modules:

$(MAKE) -C $(KERNEL) M=$(PWD) modules

.PHONEY:clean

clean:

rm -f *.o *.ko

insmod timer.ko

使用命令cat /proc/kmsg查看效果

2. jiffies和jiffies_64變量定義的位置

http://bbs.chinaunix.net/archiver/tid-1982818.html?page=2

jiffies_64獲取的驅動部分代碼

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define JIFFIES_64_DEVICE_NODE_NAME "jiffies_64"

#define JIFFIES_64_DEVICE_CLASS_NAME "jiffies_64"

#define JIFFIES_64_DEVICE_FILE_NAME "jiffies_64"

static unsigned long long value_of_jiffies_64;

static dev_t devno;

static struct class *jiffies_64_class;

static struct cdev jiffies_64_dev;

static ssize_t jiffies_64_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos)

{

if (buf == NULL || size != sizeof(value_of_jiffies_64))

return -EINVAL;

if (value_of_jiffies_64 == 0)

value_of_jiffies_64 = get_jiffies_64();

if (copy_to_user(buf, &value_of_jiffies_64, sizeof(value_of_jiffies_64)))

return -ENOMEM;

memset(&value_of_jiffies_64, 0, sizeof(value_of_jiffies_64));

return 0;

}

static ssize_t jiffies_64_open(struct inode *inode, struct file *filp)

{

return 0;

}

static ssize_t jiffies_64_release(struct inode *inode, struct file *filp)

{

return 0;

}

static struct file_operations jiffies_64_fops = {

.owner = THIS_MODULE,

.open = jiffies_64_open,

.read = jiffies_64_read,

.release = jiffies_64_release,

};

static ssize_t jiffies_64_val_show(struct device *dev, struct device_attribute *attr, char *buf);

static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, jiffies_64_val_show, NULL);

static ssize_t jiffies_64_val_show(struct device *dev, struct device_attribute *attr, char *buf)

{

return snprintf(buf, PAGE_SIZE, "%lld\n", value_of_jiffies_64);

}

static int __init jiffies_64_init(void)

{

int err = -1;

struct device *device = NULL;

err = alloc_chrdev_region(&devno, 0, 1, JIFFIES_64_DEVICE_NODE_NAME);

if (err < 0) {

printk(KERN_ALERT"Failed to alloc char dev region.\n");

goto fail;

}

/* setup cdev */

cdev_init(&jiffies_64_dev, &jiffies_64_fops);

jiffies_64_dev.owner = THIS_MODULE;

err = cdev_add(&jiffies_64_dev, devno, 1);

if (err) {

printk(KERN_ALERT"cdev_add failed.\n");

goto unregister;

}

jiffies_64_class = class_create(THIS_MODULE, JIFFIES_64_DEVICE_CLASS_NAME);

if (IS_ERR(jiffies_64_class)) {

err = PTR_ERR(jiffies_64_class);

printk(KERN_ALERT"Failed to create jiffies_64 class\n");

goto destroy_cdev;

}

device = device_create(jiffies_64_class, NULL, devno, NULL, "%s", JIFFIES_64_DEVICE_FILE_NAME);

if (IS_ERR(device)) {

err = PTR_ERR(device);

printk(KERN_ALERT"Failed to create jiffies_64 device.\n");

goto destroy_class;

}

err = device_create_file(device, &dev_attr_val);

if (err < 0) {

printk(KERN_ALERT"Failed to create attribute val.\n");

goto destroy_device;

}

return 0;

destroy_device:

device_destroy(jiffies_64_class, devno);

destroy_class:

class_destroy(jiffies_64_class);

destroy_cdev:

cdev_del(&jiffies_64_dev);

unregister:

unregister_chrdev_region(devno, 1);

fail:

return err;

}

static void __exit jiffies_64_exit(void)

{

if (jiffies_64_class) {

device_destroy(jiffies_64_class, devno);

class_destroy(jiffies_64_class);

}

cdev_del(&jiffies_64_dev);

unregister_chrdev_region(devno, 1);

}

module_init(jiffies_64_init);

module_exit(jiffies_64_exit);

MODULE_LICENSE("GPL");

jiffies_64獲取的應用部分代碼

#include

#include

#include

#include

#include

#include

#define DEVICE_NODE_NAME "/dev/jiffies_64"

int main(int argc, char **argv)

{

unsigned long long jiffies_64; /* It's in userspace, so without trouble */

int fd = -1;

fd = open(DEVICE_NODE_NAME, O_RDONLY);

if (fd == -1) {

perror(DEVICE_NODE_NAME);

return -1;

}

read(fd, &jiffies_64, sizeof(jiffies_64));

printf("jiffies_64 = %lld\n", jiffies_64);

close(fd);

return 0;

}

在編譯Linux內核,配置時:make menuconfig ---> Kernel hacking --> show timing information on printks

當選中這個選項后,啟動內核,會在日志信息前面加上時間戳。

從linux啟動信息可以看出,時間精確到微秒(us)。

討論群:

Atmel技術交流討論群

群號是305940105

Linux驅動開發調試群

群號是297141784

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的linux上的定时器上的jiffies,Linux kernel -- 定时器/jiffies的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。