Nevermind
Nevermind

Reputation: 1551

What do "CLR LocksAndThreads" performance counters actually mean

I read about the lock and thread performance counters, but I still don't understand what some of them actually mean. I'm talking specifically about Queue length and Contention rate counters, and their per-second counterparts. MSDN says that first shows number of threads that wait for a lock, and second shows number of threads that acquire a lock "unsuccessfully". I thought that if a thread is waiting for a lock, that implies that the lock wasn't acquired, but apparently I'm wrong?

Suppose I have this sample program:

static void Main(string[] args)
{
    var t1 = new Thread(RunThread1);
    var t2 = new Thread(RunThread2);
    t1.Start();
    t2.Start();
    t1.Join();
    t2.Join();
}

static void RunThread1()
{
    Thread.Sleep(1000);
    // this lock is acquired immediately. What will counters show at this moment?
    // probably both will be zero?
    lock (m_Lock)
    {
        Thread.Sleep(10000);
    }
}

static void RunThread2()
{
    Thread.Sleep(2000);
    // this lock has to wait for about 9 seconds. What will counters show?
    lock (m_Lock)
    {
        Thread.Sleep(10000);
    }
}

What will counters show as it runs?

Upvotes: 9

Views: 7216

Answers (1)

Nevermind
Nevermind

Reputation: 1551

The "queue length" counter is for the number of threads that are waiting to acquire a lock at this very moment; while "contention rate" is the number of threads that had to wait sometime in the past.

Accordingly, "queue length / sec" is the change in queue per second - how many threads became waiting during last second; and "contention rate / sec" is how many threads waited for at least some time during the last second.

This explains how the queue length can be 0 when contention rate is high: a lot of threads wait for a little bit of time. And vice versa, 0 for the total # of contentions but a long queue: the same threads waiting for a really long time.

Upvotes: 8

Related Questions