1

在 linux 内核代码中,当自旋锁被锁定时,spin_lock 函数将自旋。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");
}

我的问题是:

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

4

1 回答 1

0

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

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

于 2013-07-01T11:29:29.853 回答