0

I have been asked to measure the contention of locks a write process is causing. I was looking at the data of the lockstat for that write process.

My questions are below:

  1. Is contention related to the number of times threads wait for the particular lock, as it is taken by another thread or the time for which threads have to wait for that lock to get freed?

  2. Is it correct to calculate the contention as a measure of both:

    • nsec (avg amount of time threads have to wait for the event to occur/lock to get freed) and
    • cnt (number of times event occurred)

    from profiling data collected from lockstat for a particular lock? i.e contention ~ nsec * cnt

  • 1
    Based on your tags, is it safe to assume you're asking about the Linux kernel? If so we'd like to add the tag 'kernel' to the question. – slm Jul 6 '18 at 17:31
2

Looking at the Linux kernel docs it looks like it it's waiting for the lock to get freed.

- HOW

Lockdep already has hooks in the lock functions and maps lock instances to
lock classes. We build on that (see Documentation/locking/lockdep-design.txt).
The graph below shows the relation between the lock functions and the various
hooks therein.

        __acquire
            |
           lock _____
            |        \
            |    __contended
            |         |
            |       <wait>
            | _______/
            |/
            |
       __acquired
            |
            .
          <hold>
            .
            |
       __release
            |
         unlock

lock, unlock    - the regular lock functions
__*     - the hooks
<>      - states

NOTE: Take a look at that link, it shows usage as well.

Measuring contention

By the way, you can/could use mutrace to calculate contention for a given executable as well. It's discussed here in this article titled: Measuring Lock Contention.

For example

$ LD_PRELOAD=/home/lennart/projects/mutrace/libmutrace.so gedit
mutrace: 0.1 sucessfully initialized.

mutrace: 10 most contended mutexes:

 Mutex #   Locked  Changed    Cont. tot.Time[ms] avg.Time[ms] max.Time[ms]       Type
      35   368268      407      275      120,822        0,000        0,894     normal
       5   234645      100       21       86,855        0,000        0,494     normal
      26   177324       47        4       98,610        0,001        0,150     normal
      19    55758       53        2       23,931        0,000        0,092     normal
      53      106       73        1        0,769        0,007        0,160     normal
      25    15156       70        1        6,633        0,000        0,019     normal
       4      973       10        1        4,376        0,004        0,174     normal
      75       68       62        0        0,038        0,001        0,004     normal
       9     1663       52        0        1,068        0,001        0,412     normal
       3   136553       41        0       61,408        0,000        0,281     normal
     ...      ...      ...      ...          ...          ...          ...        ...

mutrace: Total runtime 9678,142 ms.

References

  • How do you know that the question is about the kernel called Linux. (The question is tagged Linux, however this tag has the common incorrect meaning of Gnu/linux, Not a kernel named Linux.) – ctrl-alt-delor Jul 6 '18 at 17:20
  • @ctrl-alt-delor - can only assume what the OPs provide. They tagged it as such, therefore I assume that's the case. If the OP incorrectly tagged it then they'll need to refine. – slm Jul 6 '18 at 17:21
  • It is not a kernel tag. (Yes I know that Linux is a kernel, but this site uses it to mean Gnu/Linux.) I am discussing the meaning of the tag, not what the OP said. – ctrl-alt-delor Jul 6 '18 at 17:24
  • @ctrl-alt-delor - it's been my experience that when OPs use tags such as linux, process, thread that they're implying the kernel. Perhaps we should apply the kernel tag to this Q then? I think that's your point, right? – slm Jul 6 '18 at 17:28
  • To fix it up stream, we would have to fix the tag. To fix it here we would have to ask. – ctrl-alt-delor Jul 6 '18 at 17:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.