c - 如何在 gcc asm 中添加一个计数器?

标签 c linux gcc assembly

在linux内核代码中,当一个自旋锁被锁定时,spin_lock函数就会自旋。自旋锁的代码如下:

static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
{
    int inc = 0x00010000;
    int tmp;

    asm volatile(LOCK_PREFIX "xaddl %0, %1\n"
             "movzwl %w0, %2\n\t"
             "shrl $16, %0\n\t"
             "1:\t"
             "cmpl %0, %2\n\t"
             "je 2f\n\t"
             "rep ; nop\n\t"
             "movzwl %1, %2\n\t"
             /* don't need lfence here, because loads are in-order */
             "jmp 1b\n"
             "2:"
             : "+r" (inc), "+m" (lock->slock), "=&r" (tmp)
             :
             : "memory", "cc");
}

我的问题是:

如何添加一个计时器来监控锁的旋转时间?请给我一些建议。

最佳答案

您可以使用rdtsc时间戳计数器来测量时间间隔,您可以查看以下链接http://www.xml.com/ldd/chapter/book/ch06.html

http://wiki.osdev.org/Inline_Assembly/Examples

关于c - 如何在 gcc asm 中添加一个计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402744/

相关文章:

c++ - 简单的程序崩溃

c++ - 错误 : ambiguous default type conversion (c++)

c - 在C中检测文件系统

c++ - IID_MediaControl 未声明的标识符

c - C语言图案打印

linux - read得到的参数和$1有什么区别

linux - Gnome-terminal:重置键盘快捷键

c - C 中推箱子游戏加载程序的段错误

c - 如何在我的 C 代码中使用 PREFETCHT0 指令?

android - 交叉构建 native 编译器问题 : Segfaults in Compiler or Compiled Binary