Multi-thread lock overhead evaluation method based on linux system
By using uprobe to track the locking and unlocking interface of mutex lock on Linux system, and analyzing log files with the awk command, the statistical deviation problem of mutex overhead evaluation in the existing technology is solved, and a fast and accurate mutex lock is achieved. Competitive overhead assessment, assist in positioning system bottlenecks and speeding up the development process.
Patent Information
- Application Number
- CN202411081362.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-12-06
AI Technical Summary
The prior art has statistical bias in evaluating the overhead of mutexes in multithreaded programs, especially in competition overhead analysis, which is difficult to accurately reach the microsecond level, affecting the analysis and resolution of performance bottlenecks.
By using uprobe on Linux system to track the locking and unlocking interface of the mutex, using the thread id and timestamp in the trace log, combining the awk command to analyze the log file, and calculate the time between each locking and unlocking of the mutex lock The difference, obtains its average value to quickly evaluate the competition overhead of the mutex.
It provides a fast and accurate method to help system developers locate the competitive overhead of mutexes, assist in eliminating and positioning system bottlenecks, and speed up the system development process.
Smart Images

Figure CN119088579A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer applications, and in particular to a method for evaluating multi-thread lock overhead based on a Linux system. Background Art
[0002] In a multi-threaded program, simultaneous access to shared resources (global variables, shared memory, etc.) will cause the program to enter a state of contention, resulting in unpredictable behavior during program execution. The Linux system provides a variety of synchronization mechanisms to prevent programs from entering a state of contention, among which mutexes are used to implement mutually exclusive access to global variables or serial communication devices. The method of using a mutex is that a thread must first obtain the corresponding lock before accessing a shared resource, and then release the lock after the resource access is completed. Only after the current thread releases the lock can other threads obtain the lock.
[0003] In more complex multi-threaded situations, the overhead of mutex locks is mainly concentrated in contention overhead (the time a thread needs to wait for another thread to release the lock when it needs to acquire the lock). Therefore, accurately evaluating the overhead of mutex locks can assist in analyzing and solving multi-threaded program performance bottlenecks.
[0004] At present, the commonly used evaluation method for evaluating the overhead of mutex locks is to add timestamps and thread ids before locking and after releasing the locks and print them out to the log, and then analyze and count the printed log information. This method requires two calls to the function to obtain the timestamp and the function to obtain the thread id. Scheduling is likely to occur during these two calls. The contention overhead analysis of mutex locks focuses on the time at the microsecond level, so this method has a slightly larger statistical deviation. Therefore, studying new low-overhead and fast evaluation methods is crucial to accurately evaluate program performance bottlenecks. Summary of the invention
[0005] The present invention provides a method for evaluating multi-thread lock overhead based on a Linux system, so as to solve the technical problems existing in the above-mentioned prior art.
[0006] To achieve the above object, the present invention provides a method for evaluating multi-thread lock overhead based on a Linux system, which comprises:
[0007] S1: Determine the mutex to be tracked, and add a lock encapsulation function to the lock function and unlock function of the mutex respectively;
[0008] S2: Use the readelf -s instruction to obtain the symbolic address of the lock encapsulation function in step S1;
[0009] S3: In the debugfs directory, register the symbolic address of the lock encapsulation function obtained in the previous step into uprobeevent;
[0010] S4: Turn on the trace function, run the program for a while, and import the trace information into the log file;
[0011] S5: according to at least one thread ID to be tracked, use the awk command to process the log file to obtain at least one log file of the thread ID to be tracked;
[0012] S6: Use the awk command to obtain the timestamp of each time the mutex is locked and unlocked in each log file, then calculate the time difference between each time the mutex is unlocked and locked, and calculate the average value of the multiple time differences obtained.
[0013] The evaluation method of multi-thread lock overhead based on the Linux system provided by the present invention can assist system developers to quickly and accurately locate the competition overhead of a mutex lock in the system, so as to help eliminate and locate system bottlenecks and speed up the system development process. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0015] Figure 1 A flowchart of a method for evaluating multi-thread lock overhead based on a Linux system provided by the present invention. DETAILED DESCRIPTION
[0016] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0017] The present invention provides a method for quickly evaluating the cost of competition for a mutex lock, using the Linux uprobe to track the locking and unlocking interface of a mutex lock, because the thread id and timestamp carried in the trace log are introduced into a log file, and then the log file is analyzed with a script code, and the whole process hardly requires modifying the source code. Because the principle of uprobe is to insert a debugging instruction and then trigger a debugging interrupt when the program runs to the inserting position, the maximum possible guarantee of statistical data during the interruption process does not increase additional overhead. The operating environment of the present invention is a Linux operating system, and debugfs and uprobe functions need to be turned on, and the readelf command is used.
[0018] Figure 1 The flowchart of the method for evaluating the multi-thread lock overhead based on the Linux system provided by the present invention is as follows: Figure 1 As shown, the present invention provides a method for evaluating multi-thread lock overhead based on a Linux system, which includes:
[0019] S1: Determine the mutex to be tracked, and add a lock encapsulation function to the lock function and unlock function of the mutex respectively;
[0020] S2: Use the readelf -s instruction to obtain the symbolic address of the lock encapsulation function in step S1;
[0021] S3: In the debugfs directory, register the symbolic address of the lock encapsulation function obtained in the previous step into uprobeevent;
[0022] S4: Turn on the trace function, run the program for a while, and import the trace information into the log file;
[0023] S5: according to at least one thread ID to be tracked, use the awk command to process the log file to obtain at least one log file of the thread ID to be tracked;
[0024] If we want to track several thread IDs here, we will get several log files, each of which corresponds to one of the thread IDs.
[0025] S6: Use the awk command to obtain the timestamp of each time the mutex is locked and unlocked in each log file, then calculate the time difference between each time the mutex is unlocked and locked, and calculate the average value of the multiple time differences obtained.
[0026] The evaluation method of multi-thread lock overhead based on the Linux system provided by the present invention can assist system developers to quickly and accurately locate the competition overhead of a mutex lock in the system, so as to help eliminate and locate system bottlenecks and speed up the system development process.
[0027] Those skilled in the art can understand that the accompanying drawings are only schematic diagrams of an embodiment, and the modules or processes in the accompanying drawings are not necessarily required to implement the present invention.
[0028] Those skilled in the art can understand that the modules in the device in the embodiment can be distributed in the device in the embodiment according to the description of the embodiment, or can be changed accordingly and located in one or more devices different from the embodiment. The modules in the above embodiment can be combined into one module, or can be further divided into multiple sub-modules.
[0029] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for evaluating multi-thread lock overhead based on Linux system, It is characterized in that include: S1: Determine the mutex to be tracked, and add a lock encapsulation function to the lock function and unlock function of the mutex respectively; S2: Use the readelf -s instruction to obtain the symbolic address of the lock encapsulation function in step S1; S3: In the debugfs directory, register the symbolic address of the lock encapsulation function obtained in the previous step into the uprobe event; S4: Turn on the trace function, run the program for a while, and import the trace information into the log file; S5: according to at least one thread ID to be tracked, use the awk command to process the log file to obtain at least one log file of the thread ID to be tracked; S6: Use the awk command to obtain the timestamp of each time the mutex is locked and unlocked in each log file, then calculate the time difference between each time the mutex is unlocked and locked, and calculate the average value of the multiple time differences obtained.
