从内核读取使用procfs的时间戳 – 从内核读取后存储在哪里?

当中断发生在内核和如果我正在读取内核中的时间戳。 我正在通过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->tstampprocfs驱动程序? 最后我想发送这个时间戳值给用户?

 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 

我想你已经添加了这一行interrupt_time = skb -> timestamp 。 如果是,那么

  1. 在内核空间打开proc文件(检查fs / proc /并添加时间戳项)
  2. 注册您的开放/阅读电话。
  3. 每当用户尝试读取文件Linux coreel调用已注册的读取调用,在你的情况下它是dev_read。

检查这个链接如何使用proc fs