c - 使用 procfs 从内核读取时间戳 - 从内核读取后它存储在哪里?

标签 c linux linux-kernel linux-device-driver 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->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

关于c - 使用 procfs 从内核读取时间戳 - 从内核读取后它存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183838/

相关文章:

Linux 时间命令——真实 vs 用户 vs 系统

linux-kernel - gnu/Linux 上 pthread 和 fork 的区别

linux - 如何在内核模块 Makefile 中提供包含目录路径

c - 使用 for 循环逐行打印,然后在 c 中扫描到另一个

linux - 如何通过命令行自动响应密码提示?

linux - 如何使用 grep 和 sed 打印多个字符串?

Linux bash if - elif 没有按预期工作

c - 如何将从文件扫描的数字 x 舍入到 i 小数位,从 C 中的同一文件扫描?

c++ - GLEW 链接错误。错误 LNK2019

c - 为什么字符串末尾的 '\b' 没有作用?