阅读使用procfs的内核时间戳 - 它在哪儿存储从内核看完之后? [英] reading the timestamp from kernel using procfs - where is it stored after reading from kernel?

查看:216
本文介绍了阅读使用procfs的内核时间戳 - 它在哪儿存储从内核看完之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当中断在内核发生。如果我读内核中的时间戳。我通过阅读PROCFS从内核时间戳给用户。其中,该中断时间值将被保存?用户应该如何读取用户空间??

这价值

  ssize_t供dev_read(结构文件*的filp,为const char * buf中,为size_t计数,参数loff_t *偏移量)
{  如果(计数<的sizeof(InterruptTime)){
    //没有提供足够的空间。
    返回0; //或者某些错误code可能。
  }  如果(copy_to_user(BUF,&安培; InterruptTime,sizeof的(InterruptTime)){
    返回-EFAULT;
  }其他{
    返回的sizeof(InterruptTime); //我们复制的字节数。  }}

这是code我/linuxversion/net/core/dev.c修改

  INT netif_rx(sk_buff的结构* SKB)
{
     &是skb- GT; TSTAMP = ktime_get_real(); //这会给时间戳,它将被存储在// SKB缓冲
     //我在这里计算的时间戳。因为每当内核接收数据,那么内核
     //中断,并开始执行新来的任务,但是我要读的时候
    //发生中断,并得到了它的价值。
}

但如何保存在&是skb-这个GT复制值; TSTAMP PROCFS 驱动程序?
最后我想这个时间戳值发​​送给用户??


解决方案

 有样品PROC code和其输出
样品PROC code
===============[根@本地P]#猫test.c的#包括LT&; Linux的/ - module.h中GT;
#包括LT&;的Linux / kernel.h>
#包括LT&; Linux的/ init.h中>
#包括LT&; Linux的/ fs.h文件>
#包括LT&; Linux的/ proc_fs.h>
#包括LT&; Linux的/ jiffies.h>
#包括LT&; Linux的/ seq_file.h>// EXTERN uint64_t中interrupt_time;静态结构体struct proc_dir_entry * test_dir;静态INT my_proc_show(结构seq_file * M,无效* V)
{
    seq_printf(男,鲁%\\ N的jiffies);
    // seq_printf(男,鲁%,interrupt_time);
    返回0;
}静态INT my_proc_open(结构的inode的inode *,结构*文件的文件)
{
    返回single_open(文件,my_proc_show,NULL);
}静态常量结构的file_operations tst_fops = {
    。开= my_proc_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};静态INT __init test_init(无效)
{
    test_dir = proc_mkdir(myproc的,NULL);    如果(test_dir)
            proc_create(jiffies的,0,test_dir,&放大器; tst_fops);    返回0;
}
静态无效__exit test_exit(无效)
{
    remove_proc_entry(jiffies的,test_dir);
    proc_remove(test_dir);
}
宏module_init(test_init);
宏module_exit(test_exit);MODULE_LICENSE(GPL);
MODULE_AUTHOR(测试);    产量
   ======
    [根@本地P]#执行cat / proc / myproc的/个jiffies
    4325737301

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 ??

解决方案

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

这篇关于阅读使用procfs的内核时间戳 - 它在哪儿存储从内核看完之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆