Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster (almost 2x) mutexes (was slower due to ThreadFuzzer) #60823

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

azat
Copy link
Collaborator

@azat azat commented Mar 4, 2024

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Faster (almost 2x) mutexes (was slower due to ThreadFuzzer)

Recently I noticed that DB::injection() pops up in perf top, so let's optimize it slightly:

  • Add -fomit-frame-pointer -momit-leaf-frame-pointer explicitly -- almost 0 effect
  • Add ALWAYS_INLINE for ThreadFuzzer::isStarted() (just in case)
  • Disable ThreadFuzzer if non of env variables had been set, this is needed to avoid extra checks in DB::injection()
  • Add ALWAYS_INLINE for ThreadFuzzer::injection()

And here are some results for ThreadFuzzer test:

  • before: elapsed 6.27368 / test time 654 ms
  • after: elapsed 3.14167 / test time 325 ms
  • disabled: elapsed 2.46666 / test time 263 ms

But note, it is still slower then with ThreadFuzzer disabled.

Note, that this numbers for AMD 5975WX, for server with 2x Xeon Silver 4216 2.10:

  • before: elapsed 12.109 / test time 1325 ms
  • after: elapsed 10.506 / test time 1101 ms
  • disabled: elapsed 8.41043 / test time 917 ms

P.S. I've also tried with the same glibc version as server had - zero changes.

Refs: https://gist.github.com/azat/51a5fcc3a40af9f678906a3a6e14e079

Recently I noticed that DB::injection() pops up in perf top, so let's
optimize it slightly:
- Add -fomit-frame-pointer -momit-leaf-frame-pointer explicitly --
  almost 0 effect
- Add ALWAYS_INLINE for ThreadFuzzer::isStarted() (just in case)
- Disable ThreadFuzzer if non of env variables had been set, this is
  needed to avoid extra checks in DB::injection()
- Add ALWAYS_INLINE for ThreadFuzzer::injection()

And here are some results for ThreadFuzzer test:
- before:   elapsed 6.27368 / test time 654 ms
- after:    elapsed 3.14167 / test time 325 ms
- disabled: elapsed 2.46666 / test time 263 ms

*But note, it is still slower then with ThreadFuzzer disabled.*

Note, that this numbers for AMD 5975WX, for server with 2x Xeon Silver
4216 2.10:
- before:   elapsed 12.109  / test time 1325 ms
- after:    elapsed 10.506  / test time 1101 ms
- disabled: elapsed 8.41043 / test time 917 ms

P.S. I've also tried with the same glibc version as server had - zero
changes.

Refs: https://gist.github.com/azat/51a5fcc3a40af9f678906a3a6e14e079
Signed-off-by: Azat Khuzhin <[email protected]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Mar 4, 2024
@robot-ch-test-poll4
Copy link
Contributor

robot-ch-test-poll4 commented Mar 4, 2024

This is an automated comment for commit d5825ec with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
PR CheckThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help❌ failure
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts❌ failure

@nickitat nickitat self-assigned this Mar 4, 2024
@alexey-milovidov
Copy link
Member

Ok. Where do we have most of the mutex contention?

@azat
Copy link
Collaborator Author

azat commented Mar 5, 2024

There are few very common loggers (per-table), initially I looked into this while looking at #59531

And with this patch I got:

localhost:9000, queries: 6416, QPS: 413.256, RPS: 413.256, MiB/s: 0.006, result RPS: 0.000, result MiB/s: 0.000.

While before it was:

localhost:9000, queries: 4463, QPS: 375.140, RPS: 375.140, MiB/s: 0.006, result RPS: 0.000, result MiB/s: 0.000.

With empty ReplicatedMergeTreeQueue::getAlterMutationCommandsForPart:

localhost:9000, queries: 8588, QPS: 653.918, RPS: 653.918, MiB/s: 0.010, result RPS: 0.000, result MiB/s: 0.000.

With ThreadFuzzer completely compiled out:

localhost:9000, queries: 7745, QPS: 434.357, RPS: 434.357, MiB/s: 0.007, result RPS: 0.000, result MiB/s: 0.000.

Without mutations:

localhost:9000, queries: 7381, QPS: 520.910, RPS: 520.910, MiB/s: 0.008, result RPS: 0.000, result MiB/s: 0.000.

@nickitat
Copy link
Member

nickitat commented Mar 5, 2024

a little silly question: it shouldn't be enabled on servers running prod workloads, so what are we optimising?

@azat
Copy link
Collaborator Author

azat commented Mar 5, 2024

a little silly question: it shouldn't be enabled on servers running prod workloads, so what are we optimising?

It is -

#define THREAD_FUZZER_WRAP_PTHREAD 1

Since the same binary used for CI and for releases

@alexey-milovidov
Copy link
Member

Yes, we should run the same binary in production and in CI.
It is good to be able to use ThreadFuzzer in production builds.

@nickitat nickitat assigned alexey-milovidov and unassigned nickitat Mar 5, 2024
@alexey-milovidov alexey-milovidov merged commit 4561579 into ClickHouse:master Mar 5, 2024
231 of 242 checks passed
@azat azat deleted the thread-fuzzer branch March 5, 2024 21:12
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 5, 2024
@@ -172,6 +176,8 @@ void ThreadFuzzer::stop()

void ThreadFuzzer::start()
{
if (!instance().isEffective())
Copy link
Member

Choose a reason for hiding this comment

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

start() checks for isEffective, and isEffective checks for isStarted(). Result: Impossible to start it. So fast 😄

Copy link
Member

Choose a reason for hiding this comment

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

Some consequences:

  • SYSTEM START THREAD FUZZER is useless. Once the fuzzer is stopped it can't start.
  • If you don't use THREAD_FUZZER_CPU_TIME_PERIOD_US sleep faults won't work because isEffective will return false.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

start() checks for isEffective, and isEffective checks for isStarted(). Result: Impossible to start it. So fast 😄

What a shame... But it should affect only SYSTEM START THREAD FUZZER, while the overhead was there all the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants