linux - printk 的时间不到纳秒? - getnstimeofday() 问题

标签 linux linux-kernel nstimer

我正在做模块编程。我有时间测量我正在做的一些 printk 对性能的影响。我有这样的代码设置。

在代码的“declare-y”开头部分:

struct timespec ts_start,ts_end,test_of_time;

在方法中:

{
    //..other stuff
    getnstimeofday(&ts_start);
    printk("mkdir being hijacked\n");
    printk("pid is %d ", current->pid);
    printk("call #: 39 \n");
    printk("user_id of process: %d, effuid: %d\n\n", current->uid, current->euid);
    getnstimeofday(&ts_end);
    test_of_time = timespec_sub(ts_end,ts_start);
    printk("%lu", test_of_time.tv_nsec);
    return val;
}

我 dmesg 并奇怪地看到了值 0。我非常怀疑这种情况发生需要 0 纳秒。这里出了什么问题?

谢谢

最佳答案

您使用的内核版本是什么?您的计时器实际上可能没有纳秒分辨率。如果您想测量 printks 所花费的时间,您应该在循环中运行它们,以便它们花费有限且可测量的时间。它不会完全准确(例如,由于缓存未命中等原因,第一个 prink 可能比后续的 prink 慢),但这应该会给您一个大概的想法。

如果您想了解为什么会发生这种情况,请尝试分配一个大缓冲区,循环旋转将 getnstimeofday 的值写入缓冲区一段时间,然后将缓冲区输出到可以分析它的地方。您可能能够在数据中看到实际的时钟分辨率。

关于linux - printk 的时间不到纳秒? - getnstimeofday() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993309/

相关文章:

linux - epoll:我必须使用多线程吗

linux-kernel - 是否可以从 Linux (ioctl) 的内核空间调用用户空间回调函数?

ios - swift/Xcode6 : How to change when the UIButton functionality is called after clicking the button

c - mount() after clone() with CLONE_NEWNS set effects parent

linux:这两个符号链接(symbolic link)命令有什么区别

linux 0.01内核交叉引用

linux - 调试 linux 内核 panic

ios - 带有 NSTimer 的单例不从 AppDelegate 触发

iphone - 发布 NSTimer iPhone?

c - Linux C Shell,子进程抛出段错误