Skip to content

Optimize process of thread counter #619

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open

Conversation

wjunLu
Copy link

@wjunLu wjunLu commented Jul 22, 2025

Analysis

During MySQL's sysbench point-select performance testing, the server's CPU utilization approaches 100%. Using the perf top tool, it can be observed that the dispatch_command function in mysqld is a significant hotspot, accounting for over 20% of the CPU usage. Further analysis of dispatch_command reveals two atomic variable increment/decrement operations, each contributing to more than 40% of its execution time. These operations exhibit severe contention and represent clear bottlenecks, as illustrated in Figures 1 to 3.

Figure 1 - hotspots
image

Figure 2 - The first bottleneck in dispatch_command
image

Figure 3 - The second bottleneck in dispatch_command
image

Optimization

The bottleneck code locations are as follows:

  1. The first line performs an atomic increment operation on a shared variable.
  2. The second line performs an atomic decrement operation on the same variable.

The primary reason these two lines become bottlenecks is due to severe contention caused by high-frequency atomic operations on the same variable.

Upon analyzing the related code, we found that these operations belong to the "running thread count" statistics and query module, which serves two key purposes:

  • Increment/decrement the running thread count during SQL statement execution.
  • Provide an interface to query the current running thread count.

The main workflow of this module is illustrated in Figure 4.

Figure 4 - Current main process of thread counter
thread1

The current running thread count statistics and query mechanism works by incrementing/decrementing the atomic counter during SQL statement execution, while queries simply returned the atomic variable's value. This design significantly impacted business performance.

The optimization removes the atomic operations during SQL execution and instead calculates the running thread count only during queries. This trade-off boosts business performance at the cost of slightly slower queries, as illustrated in the optimized workflow (Figure 5).

Figure 5 - The optimized process of thread counter
thread2

@mysql-oca-bot
Copy link

Hi, thank you for submitting this pull request. In order to consider your code we need you to sign the Oracle Contribution Agreement (OCA). Please review the details and follow the instructions at https://oca.opensource.oracle.com/
Please make sure to include your MySQL bug system user (email) in the returned form.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants