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 — Sample contended locks by overflowing interval bucket by krk · Pull Request #982 · async-profiler/async-profiler

Conversation

@krk

@krk krk commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

Description

Implement a sampling method for contended locks, controlled by the lock
threshold (lock=DURATION). Bucket is filled by each contended lock's
duration, sampling is done only when the bucket overflows.

Also included are two commits to make testing reliable and easier:

  • Update PmuTests to have more cache misses
  • Allow setting TESTS variable from make cli

Related issues

#805

Motivation and context

Goal is to reduce the overhead while profiling lock contentions.

How has this been tested?

Different threshold values were tested with the included LockProfiling.java and Renaissance benchmarks.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
krk added 2 commits August 27, 2024 18:07
@krk
Update PmuTests to have more cache misses.
d6d8633
Makes the test more reliable, passes in higher spec machines.
@krk
Allow setting TESTS variable from make cli.
8725043
e.g. `make test TESTS=lock` will run tests under test/test/lock folder.
apangin
apangin reviewed Sep 4, 2024
View reviewed changes
Comment thread src/lockTracer.cpp Outdated
unsigned long tid = (unsigned long)syscall(SYS_gettid);

// _threshold is used both as a duration threshold and a bucket interval. When the counter overflows _threshold, the event is sampled.
if (_enabled && duration >= _threshold && enter_time >= _start_time && updateCounter(_total_duration, duration, _threshold)) {

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duration >= _threshold condition is exactly how the current algorithm works.
The idea of a counter was to replace this condition.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread src/lockTracer.h
private:
static double _ticks_to_nanos;
static jlong _threshold;
static jlong _interval;

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where _interval is used.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/one/profiler/test/Output.java Outdated

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outputStream)) {
PrintStream out = new PrintStream(outputStream)) {

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat untouched code, especially since the existing style already matches default IDEA settings.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using vscode default rules, will not format, thanks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/one/profiler/test/Test.java Outdated
boolean enabled() default true;

// Optional inputs to the test method.
double[] inputs() default {};

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double[] is probably not the best option for a generic case. String[] would likely be a better universal choice, similarly to main method accepting String[] args.
Optionally, the framework could automatically translate inputs to proper types derived from a method signature. This may unnecessarily complicate the code, though.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will accept String[] and convert to double[] now, leaving the option to automatically support other types.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/test/lock/LockTests.java Outdated
@Test(mainClass = LockProfiling.class, inputs = { 10, 75 })
@Test(mainClass = LockProfiling.class, inputs = { 100, 75 })
@Test(mainClass = LockProfiling.class, inputs = { 1000, 75 })
@Test(mainClass = LockProfiling.class, inputs = { 10000, 75 })

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many options, IMO - this will significantly increase test run time. I'd reduce the number of tests to the required minimum (let's say, up to 10 seconds in total).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/test/pmu/Dictionary.java
public static void test8M() {
long[] array = new long[1024 * 1024];
testRandomRead(array, 1024 * 1024);
long[] array = new long[8 * 1024 * 1024];

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8M meant 8 Megabytes, you made it 64. Perhaps, we can rename it to test8MB to avoid confusion.
Does the test fail with 8MB?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fail with 8MB:

WARNING: PmuTests.cacheMisses failed
java.lang.AssertionError: Expected 0.6086956521739131 < 0.2
        at one.profiler.test.Assert.isLess(Assert.java:18)
        at test.pmu.PmuTests.cacheMisses(PmuTests.java:40)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can rename the functions to test16KB/test8MB or testSmallRead/testLargeRead.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@krk
krk force-pushed the lock-mon branch from 68b27e1 to b33813b Compare September 4, 2024 11:16
apangin
apangin reviewed Sep 4, 2024
View reviewed changes
Comment thread src/arguments.cpp Outdated
// alloc[=BYTES] - profile allocations with BYTES interval
// live - build allocation profile from live objects only
// lock[=DURATION] - profile contended locks longer than DURATION ns
// lock[=DURATION] - profile contended locks overflowing the DURATION ns bucket (default: 10u)

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10us

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread src/lockTracer.cpp Outdated


double LockTracer::_ticks_to_nanos;
jlong LockTracer::_threshold;

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, _interval? Similarly to other engines.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/one/profiler/test/TestProcess.java Outdated
if (args != null && !args.isEmpty()) {
args = substituteFiles(args);
for (StringTokenizer st = new StringTokenizer(args, " "); st.hasMoreTokens(); ) {
for (StringTokenizer st = new StringTokenizer(args, " "); st.hasMoreTokens();) {

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extraneous formatting changes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/one/profiler/test/Output.java Outdated
long samples = extractSamples(s);
matched1 += pattern1.matcher(s).find() ? samples : 0;
matched2 += pattern2.matcher(s).find() ? samples : 0;
}

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reusing samples method?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/one/profiler/test/Runner.java Outdated
if (p.inputs().length == 0) {
m.invoke(holder, p);
} else {
m.invoke(holder, p, p.inputs());

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to pass inputs() explicitly.
Test method already accepts p and can extract inputs whenever it wants.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/test/lock/LockProfiling.java Outdated
public class LockProfiling {
final static int timeOutsideLock = 1_000_000; // 1 ms
final static ThreadLocal<Double> totalUsefulWork = ThreadLocal.withInitial(() -> 0.0);
final static ThreadLocal<Double> totalWait = ThreadLocal.withInitial(() -> 0.0);

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canonical order of modifiers is static final

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/test/lock/LockTests.java Outdated

// 0 is equivalent to disabling sampling of locks, so all profiles are included.
@Test(mainClass = LockProfiling.class, inputs = { "0", "75" })
@Test(mainClass = LockProfiling.class, inputs = { "1", "75" })

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, there is no difference between 0 and 1, you can leave only 0 case here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

krk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread test/test/lock/LockTests.java
@Test(mainClass = LockProfiling.class, inputs = { "10000", "75" })

// Large (for the specific paylod) interval value skews the sampled lock
// contention distribution.

@apangin apangin Sep 4, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it an expected behavior? Is it what we want to assert here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 5, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an expected behavior. As the interval increases, lock contentions with longer durations will be favored.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 9, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's an expected, it is probably not intended behavior.
A test should verify an intended behavior.
What is the actual ratio in all these cases?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 9, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interval = 0, ratio = 75.84415584415585, minRatio = 70.0
interval = 10000, ratio = 75.125, minRatio = 70.0
interval = 1000000, ratio = 95.3307392996109, minRatio = 90.0
interval = 1000000000, ratio = NaN, minRatio = NaN

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@krk
krk force-pushed the lock-mon branch from b33813b to e2e979f Compare September 5, 2024 17:04
apangin
apangin reviewed Sep 9, 2024
View reviewed changes
Comment thread test/one/profiler/test/Runner.java Outdated
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

@apangin apangin Sep 9, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there are no meaningful changes in this file - please revert it back.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 9, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/test/lock/LockTests.java Outdated

public class LockTests {
private static void contendedLocks(TestProcess p, int interval, double minRatio) throws Exception {
Output out = p.profile("-e lock --lock " + interval + " --threads -o collapsed");

@apangin apangin Sep 9, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-e lock is an obsolete form. --lock option is enough.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@krk krk Sep 9, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/test/lock/LockTests.java
@Test(mainClass = LockProfiling.class, inputs = { "10000", "75" })

// Large (for the specific paylod) interval value skews the sampled lock
// contention distribution.

@apangin apangin Sep 9, 2024

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it's an expected, it is probably not intended behavior.
A test should verify an intended behavior.
What is the actual ratio in all these cases?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@krk
Sample contended locks by overflowing interval bucket
82b5fcf
Implement a sampling method for contended locks, controlled by the lock
threshold (lock=DURATION). Bucket is filled by each contended lock's
duration, sampling is done only when the bucket overflows.

Goal is to reduce the overhead while profiling lock contentions.
@krk
krk force-pushed the lock-mon branch from e2e979f to 82b5fcf Compare September 9, 2024 13:20
@apangin
Minor formatting changes
29d7f39
@apangin
apangin merged commit 25fa02e into async-profiler:master Sep 9, 2024
@krk krk mentioned this pull request Sep 9, 2024
Lock monitoring overhead/accuracy #805
Closed
@krk krk mentioned this pull request Oct 9, 2024
Assert on total wait time in lock contention tests #1017
Merged
@apangin apangin mentioned this pull request Nov 5, 2024
Restructure and update documentation #1029
Merged
@AnishDatta247 AnishDatta247 mentioned this pull request Oct 27, 2026
Lock profiling can latch a hot monitor into a convoy that lasts until profiling stops #1835
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@apangin apangin apangin left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@krk @apangin
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.