GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Lock monitoring overhead/accuracy · Issue #805 · async-profiler/async-profiler

Lock monitoring overhead/accuracy #805

New issue
Copy link
New issue
Copy link
Closed
Closed
Lock monitoring overhead/accuracy#805
Copy link
Labels
help wanted

Description

@olivergillespie
olivergillespie
opened on Aug 22, 2023
Issue body actions

I noticed in my application that -e lock causes noticeable overhead. I believe that's in part because the default threshold for recording the 'contention' is 0.

It also attributed a large amount of time in an area I believe is mostly uncontended, and comparing with e.g. -e wall, the picture is totally different.

I have a guess as to how the overhead leads to inaccurate results - the time taken to record the sample by the lock holder is not included in the acquire time reported by that thread, which is good, but thread 2 sees a longer acquire time because thread 1 is recording the sample while still holding the lock. So lock tracing actually increases contention, which is bad for performance but also messes with the result accuracy because it's not accounted for.

I suggest at least increasing the default threshold, in my test 10us is enough to remove the undue overhead while still catching the most interesting cases.

As far as I can tell, the overhead is from:

a) getting the thread id via gettid syscall
b) getting the stack trace via jvmti

I'm wondering if there's any way to move these outside the lock. I suppose you could do them pre-emptively in ContendedMonitorEnter before the lock is acquired, but that means you don't know if the threshold has been crossed yet. However, when the threshold is 0 that shouldn't be a problem so it might be beneficial. I found some discussion on the old MonitorContendedExit event which I guess would be even better it if was available, I don't know if there's any way for async-profiler to create an equivalent hook something like:

monitorExitHook() {
  if (tag.acquireTime > threshold) recordSample() // expensive work done outside lock
}

As I mentioned, I saw this in a real application, but I also tried to create a simple repro:

public class Test {
    static int x = 0;
    public static void main(String[] args) throws Exception {
        for (int t = 0; t < Integer.parseInt(args[0]); t++) {
            new Thread(Test::loop).start();
        }
        Thread.sleep(30_000L);
    }

    static void loop() {
        while (true) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10_000_000; i++) {
                if (i % 1_000 == 0) {
                    synchronized(Test.class) {
                        if (x++ % 123 == 456) System.out.println("!");
                    }
                }
            }
            long dur = System.currentTimeMillis() - start;
            System.out.println(dur + "ms");
        }
    }
}

Attaching async-profiler after startup shows the overhead. Using --lock 10000 does not show any overhead.

java17 Test.java 5
13ms
14ms
13ms
14ms
14ms
14ms
Profiling started
14ms
21ms
40ms
44ms
47ms
68ms
81ms
88ms
81ms
86ms

And you can compare a wall profile (doesn't show significant contention) to the results shown by a lock profile (suggests high contention) to show the inaccuracy I was talking about.

Thanks for reading! Let me know what you think, I'm happy to help with implementation (even just changing the default threshold) if you think any of the ideas are good.

Activity

  1. zdyj3170101136 commented on Aug 23, 2023

    @zdyj3170101136
    zdyj3170101136
    on Aug 23, 2023
    More actions

    i think the default threshold is not easy to decide.
    the best is a random lock tracer: #775

  2. olivergillespie commented on Aug 23, 2023

    @olivergillespie
    olivergillespie
    on Aug 23, 2023
    ContributorAuthor
    More actions

    Interesting point. Lock profiling could work on an interval instead of a threshold. It doesn't really need the rand() style operation, though I think it works out the same mostly.

    if (totalAcquireTime + latestAcquireTime) > interval
       recordSample() // weight of the sample = interval, like cpu profiling or tlab allocs
       totalAcquireTime = (totalAcquireTime + latestAcquireTime) % interval
    

    It would still be separately valuable to reduce overhead and improve accuracy by reducing work done under lock, if at all possible.

    Reacted by Andrei Pangin
  3. apangin
    added a commit that references this issue on Aug 28, 2023

    #805: Lock profiling optimizations

    d1de831
  4. apangin
    added
    help wanted
    on Jul 15, 2024
  5. krk commented on Sep 9, 2024

    @krk
    krk
    on Sep 9, 2024
    Contributor
    More actions

    Thank you @olivergillespie for the repro and analysis.

    Contended locks are now sampled by default with the interval bucket, eliminating the overhead concerns, ref. #982

  6. krk
    closed this as completedon Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.