当内核中发生中断时,如果我正在读取内核中的时间戳。我正在通过 procfs 从内核读取时间戳给用户。该中断时间值将存储在哪里??用户应该如何从用户空间读取该值??
ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
if ( count < sizeof(InterruptTime) ) {
// Not enough space provided.
return 0; // Or some error code maybe.
}
if (copy_to_user(buf,&InterruptTime,sizeof(InterruptTime)) {
return -EFAULT;
} else {
return sizeof(InterruptTime); // Number of bytes we copied.
}
}
这是我在/linuxversion/net/core/dev.c中修改的代码
int netif_rx(struct sk_buff *skb)
{
skb->tstamp = ktime_get_real(); //this will give a timestamp and it will be stored in //skb buffer
//I am calculating a timestamp here. because whenever kernel receive the data then the kernel is
//interrupted and start executing the newly arrived task but I have to read the time when the
//interrupt occurs and get the value of it.
}
但是如何将存储在 skb->tstamp
中的值复制到 procfs
驱动程序?最后我想将这个时间戳值发送给用户??
There is sample proc code and its output
Sample proc code
===============
[root@localhost p]# cat test.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/jiffies.h>
#include <linux/seq_file.h>
//extern uint64_t interrupt_time;
static struct proc_dir_entry *test_dir;
static int my_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "%lu\n", jiffies);
//seq_printf(m, "%lu", interrupt_time);
return 0;
}
static int my_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, my_proc_show, NULL);
}
static const struct file_operations tst_fops = {
.open = my_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init test_init(void)
{
test_dir = proc_mkdir("myproc", NULL);
if (test_dir)
proc_create("jiffies", 0, test_dir, &tst_fops);
return 0;
}
static void __exit test_exit(void)
{
remove_proc_entry ("jiffies", test_dir);
proc_remove (test_dir);
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Test");
Output
======
[root@localhost p]# cat /proc/myproc/jiffies
4325737301
我是一名优秀的程序员,十分优秀!