reading the timestamp from kernel using procfs - where is it stored after reading from kernel?

╄→гoц情女王★ 提交于 2019-12-23 04:45:27

问题


when the interrupt occurs in the kernel and If I am reading a timestamp in the kernel. I am reading the timestamp from kernel to the user via procfs. where that interrupt time value will be stored ?? how should the user read that value from the user space ??

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.

  }

}

this is the code I modified in /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.
} 

but how to copy this value stored in skb->tstamp to procfs driver ?? finally I want to send this timestamp value to the user ??


回答1:


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



回答2:


I guess you have added this line interrupt_time = skb -> timestamp. If yes, then

  1. Open proc file in kernel space (check fs/proc/ and add an entry for timestamp)
  2. Register your open/read calls.
  3. Whenever user tries to read a file Linux Kernel calls registered read call, in your case it is dev_read.

Check this link for how proc fs is used



来源:https://stackoverflow.com/questions/23183838/reading-the-timestamp-from-kernel-using-procfs-where-is-it-stored-after-readin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!