Tuning Performance Bottlenecks
Though the execution of the function code is fast, using locks in multi-thread programming will cause lock contention, which is time-consuming. To reduce the preemption of public resources, you can tune the code by using atomic variable–based lock-free programming.
In this practice, pthread_atomic is the tuned application. Execute an analysis again to check whether the tuning takes effect.
- Create a hotspot function analysis task again for the pthread_atomic application.
Click
next to the System Profiler and select General analysis. On the task creation page that is displayed, select Hotspot Function, set the required parameters, and click OK to start the hotspot function analysis task. Retain the default values for the parameters unless otherwise specified.If you create a hotspot function analysis task to monitor lock contention of your system, run the corresponding application demo in the backend when starting the task. Otherwise, lock contention data cannot be collected.
Figure 1 Creating a hotspot function analysis task
Table 1 Task parameters Parameter
Description
Analysis Type
Set it to Hotspot functions.
Analysis Object
Set it to Application.
Application Path
/opt/testdemo/pthread_atomic
Sampling Duration
Set it to 30.
Sampling Frequency
100 times per second
Sampling Range
Set it to User mode.
dwarf
Enable this option.
C/C++ Source File Directory
Associates the source code during collection. Example: /opt/testdemo/
- View the analysis result.Figure 2 Hotspot function analysis result
- Create a new lock and wait analysis task (pthread_atomic application).
Click
next to the System Profiler and select General analysis. On the task creation page that is displayed, select Locks and Waits, set the required parameters, and click OK to start the lock and wait analysis task. Retain the default values for the parameters unless otherwise specified.Figure 3 Creating a lock and wait analysis task
Table 2 Task parameters Parameter
Description
Analysis Type
Set it to Lock and wait analysis.
Analysis Object
Set it to Application.
Mode
Set it to Attach to process.
PID
Select the process ID corresponding to the pthread_atomic application.
Lock Analysis Type
Select all (select Histogram statistics and Lock variable analysis).
Predefined Lock Type
Retain the default value.
Sampling Duration
Set it to 60.
Max. Collected Data
Set it to 1024.
Stack Analysis
Set it to Disabled.
- View the analysis result.Figure 4 Lock and Wait Information
The analysis result shows that the number and total duration of lock instances have been greatly reduced.
Summary
According to the hotspot function analysis and lock and wait analysis results, extra overheads are caused by the lock mechanism when public resources in the multi-thread application code are preempted. By implementing lock-free programming using atomic variables, such unnecessary overheads are reduced.