Dynamic data competition detection method, device, terminal and storage medium

By initializing the vector clock and lock set in a multi-threaded environment, updating the vector clock based on the sub-hb relationship, and combining the use of dynamic buffers, the problems of high complexity and high false alarm rate of data competition detection algorithms in the prior art are solved, and more accurate and efficient data competition detection is achieved.

CN115048220BActive Publication Date: 2025-04-22STATE GRID HEBEI ELECTRIC POWER RES INST +2
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210699473.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-20
Publication Date
2025-04-22
Estimated Expiration
2042-06-20

Smart Images

  • Figure CN115048220B_ABST
    Figure CN115048220B_ABST
Patent Text Reader

Abstract

The present invention provides a dynamic data competition detection method, device, terminal and storage medium. The method comprises: initializing the vector clock of each thread, and synchronously updating the vector clock of each thread when the thread is created, the thread is waiting, the thread sends a signal and the thread is waiting for a signal based on the sub-hb relationship; updating the lock set corresponding to each thread according to the state of each thread acquiring a lock and releasing a lock; performing data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread; and adjusting the size of the dynamic buffer according to the result of the data competition detection. The present invention can reduce the problem of false positives in the detection results, reduce the situation of missed reports, and reduce the complexity of the algorithm.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer program technology, and in particular to a dynamic data contention detection method, device, terminal and storage medium. Background Art

[0002] When a multithreaded program is running, different threads access their own private variables independently. For access to shared variables, the read and write order between threads is particularly critical, which is directly related to the final state of the shared variables in memory.

[0003] Data race is the most complex concurrency error in multithreaded programs. Data race must meet the following conditions: there are two or more threads accessing the same memory concurrently, at least one of the accesses is a write access operation, and there is no thread synchronization to control the execution order of the threads. In other words, there are at least two accesses without a strict order relationship, and the execution order between them is completely determined by the operating system's scheduling of threads. Therefore, determining whether there is a sequential order relationship between different threads accessing shared variables is an important basis for detecting data races.

[0004] Traditional data contention detection algorithms include Djit+, FastTrack, and Lockset. The Djit+ algorithm is based on a vector clock architecture. Its basic idea is to record each access to a shared variable and then detect whether the record satisfies the happen-before relationship with the most recent previous access. If not, data contention is detected. This shows that the Djit+ algorithm requires a high algorithm complexity.

[0005] The FastTrack algorithm uses a lighter data structure epoch to perform data competition detection. Compared with the Djit+ algorithm, the algorithm has improved efficiency, but while streamlining the vector clock, it only retains the most recent access information of the shared variable and performs special processing during shared reads, causing the algorithm to discard some useful thread execution data information.

[0006] In addition, both Djit+ and FastTrack algorithms are restricted to complete happen-before relationships.

[0007] The Lockset algorithm uses a simple locking rule to detect data contention, that is, each shared variable is protected by a lock, and accessing a shared variable that is not protected by a lock will cause data contention. The Lockset algorithm is one of the most basic data contention detection algorithms, but it is rarely used independently because it has many false positive problems.

[0008] In summary, the Djit+ algorithm and FastTrack algorithm follow a complete happen-before relationship, and usually do not have false positive detection results. However, their detection methods are only performed on a specific program execution path, so their detection results will be affected by different detection paths generated by thread scheduling, so that each detection can only detect part of the complete result, ignoring possible data competition, which greatly increases the work to be completed by the entire program data competition detection. The Lockset algorithm has a higher false positive rate because the locking rules used are relatively simple. The correct and incorrect results detected are mixed in the detection results and are difficult to distinguish. Summary of the invention

[0009] The embodiments of the present invention provide a dynamic data contention detection method, device, terminal and storage medium to solve the problems of high algorithm complexity and high false alarm rate in existing data contention detection algorithms.

[0010] In a first aspect, an embodiment of the present invention provides a dynamic data competition detection method, comprising:

[0011] Initialize the vector clock of each thread, and synchronously update the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal;

[0012] According to the state of each thread acquiring and releasing the lock, the lock set corresponding to each thread is updated respectively, and the lock set includes all the locks held by the corresponding thread;

[0013] Perform data competition detection on each thread based on the vector clock value of each thread and the lock set of each thread;

[0014] According to the data competition detection result, the size of the dynamic buffer is adjusted, and the dynamic buffer is used to store the access sequence of the shared variables in each thread.

[0015] In a possible implementation, the initializing the vector clock of each thread and synchronously updating the vector clock of each thread when the thread is created, the thread waits, sends a signal, and waits for a signal based on the sub-hb relationship includes:

[0016] When initializing the vector clock of each thread, set the local clock value of the current thread to 1 and the clock values ​​of other threads to 0;

[0017] When a thread is created, the initialization vector clock of the child thread is combined with the vector clock of the parent thread to obtain the maximum value, and the value is assigned to the vector clock of the child thread, and the local clock value of the parent thread is increased by 1;

[0018] When a thread is waiting, the vector clock of the waiting thread and the vector clock of the waiting thread are combined to get the maximum value, and the value is assigned to the vector clock of the waiting thread, and the local clock value of the waiting thread is increased by 1;

[0019] When a thread sends a signal, the vector clock of the current thread is combined with the vector clock of the synchronization object to obtain the maximum value, and the value is assigned to the vector clock of the synchronization object. The local clock of the current thread is increased by 1.

[0020] When a thread receives a signal, it combines the vector clock of the current thread with the vector clock of the synchronization object to obtain the maximum value, and assigns it to the vector clock of the current thread.

[0021] In a possible implementation, performing data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread includes:

[0022] When the current thread reads a variable, read-write data competition detection is performed based on the vector clock value of the current thread and the lock set of the current thread;

[0023] When the current thread writes a variable, write-write data competition detection and write-read data competition detection are performed based on the vector clock value of the current thread and the lock set of the current thread.

[0024] In a possible implementation, when the current thread reads a variable, read-write data competition detection is performed according to the vector clock value of the current thread and the lock set of the current thread, including:

[0025] Initialize index value;

[0026] Determine whether the current index value is less than the size of the write access buffer;

[0027] When the current index value is less than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and whether the local clock value of the thread recorded in the write access buffer is less than the clock value recorded at the i-th position in the write access buffer, where i represents the current index value;

[0028] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set in the i-th position record of the write access buffer is empty;

[0029] If it is empty, a read-write data race is detected; if it is not empty, no read-write data race is detected;

[0030] Add 1 to the current index value, and jump to the step of "determining whether the current index value is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0031] If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0032] When the current index value is greater than or equal to the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position of the read access buffer;

[0033] If so, set the shared read flag to true and add a new record to the read buffer;

[0034] If not, directly add a new record to the read buffer.

[0035] In a possible implementation, when the current thread writes a variable, a write-write data competition detection is performed according to the vector clock value of the current thread and the lock set of the current thread, including:

[0036] Initialize index value;

[0037] Determine whether the current index value is less than the size of the write access buffer;

[0038] When the current index value is smaller than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and the local clock value of the thread recorded in the write access buffer is smaller than the clock value recorded at the i-th position of the write access buffer;

[0039] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the write access buffer is empty;

[0040] If it is empty, a read-write data race is detected, if it is not empty, no write-write data race is detected;

[0041] Add 1 to the current index value, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0042] If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0043] If the current index value is greater than or equal to the size of the write access buffer, the index value is reinitialized and write-read data competition detection is performed.

[0044] In a possible implementation, the re-initializing the index value and performing write-read data competition detection includes:

[0045] Initialize index value;

[0046] Determine whether the current index value is less than the size of the read access buffer;

[0047] If yes, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position in the read access buffer;

[0048] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the read access buffer is empty;

[0049] If it is empty, a write-read data race is detected; if it is not empty, no write-read data race is detected;

[0050] Add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the read access buffer;

[0051] When the current index value is greater than or equal to the size of the read access buffer, the shared read flag is set to false, and a new record is added to the write buffer.

[0052] In a possible implementation, adjusting the size of the dynamic buffer according to the data competition detection result includes:

[0053] Determine whether the detected data contention quantity is greater than 0, and whether the current dynamic buffer size is less than a preset upper limit of the dynamic buffer;

[0054] If yes, then the size of the current dynamic buffer is doubled, and it is further determined whether the size of the current dynamic buffer is greater than the preset upper limit of the dynamic buffer;

[0055] If the current size of the dynamic buffer is greater than the preset upper limit of the dynamic buffer, setting the current size of the buffer to a new preset upper limit of the dynamic buffer;

[0056] If the current size of the dynamic buffer is not greater than the preset upper limit of the dynamic buffer, the preset upper limit of the dynamic buffer is not changed;

[0057] If not, continue to determine whether the number of accesses to the dynamic buffer is greater than a preset multiple of the current dynamic buffer size;

[0058] If so, the size of the current dynamic buffer is reduced by half, and it is further determined whether the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer;

[0059] If the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer, the size of the current dynamic buffer is set as the new preset lower limit of the dynamic buffer, and the number of accesses to the dynamic buffer is set to 1;

[0060] If the size of the current dynamic buffer is greater than or equal to the preset lower limit of the dynamic buffer, the number of accesses to the dynamic buffer is directly set to 1.

[0061] In a second aspect, an embodiment of the present invention provides a dynamic data competition detection device, including:

[0062] The vector clock module is used to initialize the vector clock of each thread and synchronously update the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal;

[0063] A lock set update module, used to update the lock set corresponding to each thread according to the state of each thread acquiring and releasing the lock, wherein the lock set includes all the locks held by the corresponding thread;

[0064] A detection module, used for performing data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread;

[0065] The adjustment module is used to adjust the size of the dynamic buffer according to the data competition detection result, and the dynamic buffer is used to store the access sequence of the shared variables in each thread.

[0066] In a third aspect, an embodiment of the present invention provides a terminal, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the steps of the method described in the first aspect or any possible implementation manner of the first aspect are implemented.

[0067] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method described in the first aspect or any possible implementation method of the first aspect are implemented.

[0068] The embodiment of the present invention provides a dynamic data competition detection method, device, terminal and storage medium, which initializes the vector clock of each thread and synchronously updates the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal and the thread waits for a signal; updates the lock set corresponding to each thread according to the state of each thread acquiring a lock and releasing a lock; performs data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread; adjusts the size of the dynamic buffer according to the data competition detection result; updates the vector clock based on the sub-hb relationship, thereby reducing the sensitivity of thread scheduling and reducing the problem of false positives in detection results; and reduces the situation of missed reports by using a dynamic buffer to record read and write access information, thereby reducing detection work and reducing algorithm complexity. BRIEF DESCRIPTION OF THE DRAWINGS

[0069] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, 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 labor.

[0070] Figure 1 is a flow chart of an implementation of a dynamic data competition detection method provided by an embodiment of the present invention;

[0071] Figure 2 is a flowchart of an implementation of updating a vector clock provided by an embodiment of the present invention;

[0072] Figure 3 It is a flow chart of implementing read-write data competition detection provided by an embodiment of the present invention;

[0073] Figure 4 It is a flowchart of implementing write-read data competition detection and write-write data competition detection provided by an embodiment of the present invention;

[0074] Figure 5 is a flowchart of adjusting the size of a dynamic buffer provided by an embodiment of the present invention;

[0075] Figure 6 is a bar diagram showing the response time of various data competition detection algorithms provided by an embodiment of the present invention;

[0076] Figure 7 is a structural diagram of a dynamic data competition detection device provided by an embodiment of the present invention;

[0077] Figure 8 is a schematic diagram of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0078] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present invention. However, it should be clear to those skilled in the art that the present invention may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present invention.

[0079] In order to make the purpose, technical solutions and advantages of the present invention more clear, specific embodiments will be described below in conjunction with the accompanying drawings.

[0080] Figure 1 The implementation flow chart of the dynamic data competition detection method provided by the embodiment of the present invention is described in detail as follows:

[0081] Step 101, initialize the vector clock of each thread, and synchronously update the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal;

[0082] See Figure 2 , when initializing the vector clock of each thread, set the local clock value of the current thread to 1 and the clock values ​​of other threads to 0;

[0083] When a thread is created, the initialization vector clock of the child thread is combined with the vector clock of the parent thread to obtain the maximum value, and the value is assigned to the vector clock of the child thread, and the local clock value of the parent thread is increased by 1;

[0084] When a thread is waiting, the vector clock of the waiting thread and the vector clock of the waiting thread are combined to get the maximum value, and the value is assigned to the vector clock of the waiting thread, and the local clock value of the waiting thread is increased by 1;

[0085] When a thread sends a signal, the vector clock of the current thread is combined with the vector clock of the synchronization object to obtain the maximum value, and the value is assigned to the vector clock of the synchronization object. The local clock of the current thread is increased by 1.

[0086] When a thread receives a signal, it combines the vector clock of the current thread with the vector clock of the synchronization object to obtain the maximum value, and assigns it to the vector clock of the current thread.

[0087] In order to reduce the sensitivity of data competition detection to threads, this method does not follow the complete happen-before relationship. This method does not change the state of the vector clock when the thread is locked and unlocked, which reduces the impact of the lock and unlock order on the detection result, and does not set the lock object as a synchronization object, so that it does not have a vector clock, and the lock object will not affect the thread's vector clock. This method only updates the synchronization object and the thread's vector clock according to the operations in the Djit+ algorithm during deterministic synchronization operations such as thread creation, thread waiting, thread sending signals, and thread receiving signals.

[0088] The embodiment of the present invention defines this optimized happen-before relationship as a sub-hb relationship. During the execution of a certain data competition detection, the space of the sub-hb relationship is a subspace of the happen-before space. Therefore, the sub-hb relationship has better stability. Even when the thread scheduling information is uncertain, the sub-hb relationship can still maintain a high consistency, thereby greatly reducing the dependence on thread scheduling.

[0089] Step 102, according to the state of each thread acquiring and releasing the lock, update the lock set corresponding to each thread respectively, and the lock set includes all the locks held by the corresponding thread;

[0090] It should be noted that locking and unlocking a thread only affects the set of thread locks, and does not affect the thread's vector clock.

[0091] Step 103, performing data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread;

[0092] Optionally, when the current thread reads a variable, read-write data competition detection is performed based on the vector clock value of the current thread and the lock set of the current thread;

[0093] When the current thread writes a variable, write-write data competition detection and write-read data competition detection are performed based on the vector clock value of the current thread and the lock set of the current thread.

[0094] Further, see Figure 3 When the current thread reads a variable, it performs read-write data competition detection based on the vector clock value of the current thread and the lock set of the current thread, including:

[0095] Initialize index value;

[0096] Determine whether the current index value is less than the size of the write access buffer;

[0097] When the current index value is less than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and whether the local clock value of the thread recorded in the write access buffer is less than the clock value recorded at the i-th position in the write access buffer, where i represents the current index value;

[0098] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set in the i-th position record of the write access buffer is empty;

[0099] If it is empty, a read-write data race is detected; if it is not empty, no read-write data race is detected;

[0100] Add 1 to the current index value, and jump to the step of "determining whether the current index value is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0101] If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0102] When the current index value is greater than or equal to the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position of the read access buffer;

[0103] If so, set the shared read flag to true and add a new record to the read buffer;

[0104] If not, directly add a new record to the read buffer.

[0105] Further, see Figure 4 When the current thread writes a variable, a write-write data competition check is performed based on the vector clock value of the current thread and the lock set of the current thread, including:

[0106] Initialize index value;

[0107] Determine whether the current index value is less than the size of the write access buffer;

[0108] When the current index value is smaller than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and the local clock value of the thread recorded in the write access buffer is smaller than the clock value recorded at the i-th position of the write access buffer;

[0109] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the write access buffer is empty;

[0110] If it is empty, a read-write data race is detected, if it is not empty, no write-write data race is detected;

[0111] Add 1 to the current index value, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0112] If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer;

[0113] If the current index value is greater than or equal to the size of the write access buffer, the index value is reinitialized and write-read data competition detection is performed.

[0114] Further optionally, the index value is reinitialized to perform write-read data competition detection, including:

[0115] Initialize index value;

[0116] Determine whether the current index value is less than the size of the read access buffer;

[0117] If yes, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position in the read access buffer;

[0118] If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the read access buffer is empty;

[0119] If it is empty, a write-read data race is detected; if it is not empty, no write-read data race is detected;

[0120] Add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the read access buffer;

[0121] When the current index value is greater than or equal to the size of the read access buffer, the shared read flag is set to false, and a new record is added to the write buffer.

[0122] Step 104, adjusting the size of a dynamic buffer according to the data competition detection result, wherein the dynamic buffer is used to store the access sequence of the shared variables in each thread.

[0123] Optional, see Figure 5 , according to the data competition detection results, adjust the size of the dynamic buffer, including:

[0124] Determine whether the detected data contention quantity is greater than 0, and whether the current dynamic buffer size is less than a preset upper limit of the dynamic buffer;

[0125] If yes, then the size of the current dynamic buffer is doubled, and it is further determined whether the size of the current dynamic buffer is greater than the preset upper limit of the dynamic buffer;

[0126] If the current size of the dynamic buffer is greater than the preset upper limit of the dynamic buffer, setting the current size of the buffer to a new preset upper limit of the dynamic buffer;

[0127] If the current size of the dynamic buffer is not greater than the preset upper limit of the dynamic buffer, the preset upper limit of the dynamic buffer is not changed;

[0128] If not, continue to determine whether the number of accesses to the dynamic buffer is greater than a preset multiple of the current dynamic buffer size;

[0129] If so, the size of the current dynamic buffer is reduced by half, and it is further determined whether the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer;

[0130] If the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer, the size of the current dynamic buffer is set as the new preset lower limit of the dynamic buffer, and the number of accesses to the dynamic buffer is set to 1;

[0131] If the size of the current dynamic buffer is greater than or equal to the preset lower limit of the dynamic buffer, the number of accesses to the dynamic buffer is directly set to 1.

[0132] After the data competition detection is completed, the dynamic buffer state is adjusted. If the buffer is set too large, the overhead during data competition detection will increase. If it is set too small, it will cause insufficient space and frequent expansion. Therefore, this method dynamically adjusts the size of the buffer according to the detection state and buffer access state after the data competition detection is completed.

[0133] The feasibility of the dynamic data detection method in the embodiment of the present invention is described below in combination with specific experimental data:

[0134] The typical multi-threaded program Apache's httpd server is used. This server generates multiple threads to handle a large number of requests. The experiment uses different levels of stress testing on the httpd server. First, httpd is executed under the Pin framework, and Pintools implemented with different comparison algorithms are linked. Then, the stress testing tool Apache-benchmark is used to access the httpd server locally, and the number of data competition detections is obtained from the detection results of Pintool.

[0135] The experimental results of using the stress testing tool Apache-benchmark to execute 100 requests to the local httpd server, where the maximum number of parallel requests is 50. The Djit+ algorithm, FastTrack algorithm and the dynamic data competition detection method (CachedTrack) disclosed in the embodiment of the present invention are used for detection respectively. The upper limit of the dynamic buffer size of CachedTrack is 8 and the lower limit is 1. The data competition detection results are shown in Table 1:

[0136] Experiment number Djit+ FastTrack CachedTrack 1 644 42 1321 2 88 371 1343 3 596 381 1319 4 93 41 1338 5 661 375 1325

[0137] Table 1 Apache-benchmark data competition detection results

[0138] From Table 1, we can see that the number of detections of CachedTrack is higher than that of Djit+ and FastTrack. This is because CachedTrack can predict the possible execution process based on the execution process of the current thread, and thus make contention predictions. The detection results include not only the data contention that has occurred, but also the contention that did not occur in this execution process but may occur in other execution processes, that is, potential data contention.

[0139] Furthermore, in order to compare the consumption of each detection algorithm on program execution, Apache is also run under the Pin framework, and the local httpd server is stress tested using the stress testing tool Apache-benchmark. The data in the test results is used to indicate the response speed of the server, thereby judging the impact of the algorithm on program operation, that is, the relative size of the consumption generated by the detection algorithm.

[0140] When using Apache-benchmark to perform stress testing, you need to set the total number of requests and the maximum number of concurrent requests. In the test results, the processing time of each request can most directly reflect the efficiency of the server. When conducting experiments, the total number of requests is maintained at 10,000, and the concurrent request amounts are set to 50, 100, 150, 200, and 250 for stress testing. First, execute Apache under the Pin framework without performing data competition detection, and then use Djit+, FastTrack, and CachedTrack for detection, respectively, to obtain the average response time of each request under different algorithm detection conditions.

[0141] See also Figure 6 , simply running Apache under Pin without data race detection, the average response time is about 0.5 milliseconds, and after executing the data race detection algorithm, the response time will increase significantly. Among them, the response time detected by the Djit+ algorithm is the longest, followed by the FastTrack algorithm, which is consistent with the complexity of the two algorithms. FastTrack optimizes most of the O(n) operations in the Djit+ algorithm to O(1). CachedTrack adopts the basic idea of ​​FastTrack, but does not follow the complete happen-before relationship, but uses a streamlined sub-hub. In the Djit+ algorithm and the FastTrack algorithm, both locking and unlocking operations require vector clock merging operations with a complexity of O(n), while in CachedTrack, the vector clock does not change when locking and unlocking. In this respect, CachedTrack saves a lot of vector clock merging operation time, so it is more efficient than FastTrack.

[0142] The beneficial effects of the present invention are as follows:

[0143] 1. The happen-before relationship in the Djit+ algorithm and the FastTrack algorithm has been optimized. The vector clock is no longer updated for the locking and unlocking operations in this relationship, thereby reducing the sensitivity of thread scheduling. Between concurrent accesses, the Lockset detection algorithm is combined to optimize the process of determining whether there is data contention in concurrent access.

[0144] 2. The present invention proposes the concept of dynamic buffer, simplifies the vector clock of FastTrack algorithm, and reduces the problems that may exist in buffer recording information. Dynamic buffer is used to record multiple read and write access information, thereby reducing the situation of missed reports and predicting potential data competition.

[0145] 3. The dynamic buffer proposed in the present invention can dynamically adjust the size of the buffer according to the access information, thereby minimizing the consumption of the buffer output, making the complexity of some detections close to that of the FastTrack algorithm, and increasing the number of data contention detections.

[0146] It should be understood that the order of execution of the steps in the above embodiment does not necessarily mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present invention.

[0147] The following is an embodiment of the device of the present invention. For details not described in detail, reference may be made to the corresponding method embodiments described above.

[0148] Figure 7 The following is a schematic diagram showing the structure of a dynamic data competition detection device provided by an embodiment of the present invention. For ease of explanation, only the parts related to the embodiment of the present invention are shown, which are described in detail as follows:

[0149] like Figure 7 As shown, the dynamic data contention detection device 7 includes: a vector clock module 71 , a lock set update module 72 , a detection module 73 and an adjustment module 74 .

[0150] A vector clock module 71 is used to initialize the vector clock of each thread and synchronously update the vector clock of each thread when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal based on the sub-hb relationship;

[0151] A lock set update module 72, used to update the lock set corresponding to each thread according to the state of each thread acquiring and releasing the lock, wherein the lock set includes all the locks held by the corresponding thread;

[0152] A detection module 73, configured to perform data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread;

[0153] The adjustment module 74 is used to adjust the size of the dynamic buffer according to the data competition detection result, and the dynamic buffer is used to store the access sequence of the shared variables in each thread.

[0154] The embodiment of the present invention uses a vector clock module 71 to initialize the vector clock of each thread, and synchronously updates the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal; a lock set update module 72 is used to update the lock set lock corresponding to each thread according to the state of each thread acquiring and releasing the lock; a detection module 73 is used to perform data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread; an adjustment module 74 is used to adjust the size of the dynamic buffer according to the data competition detection result; by updating the vector clock based on the sub-hb relationship, the sensitivity of thread scheduling is reduced, and the problem of false positives in detection results is reduced; by using a dynamic buffer to record read and write access information, the situation of missed reports can be reduced, thereby reducing the detection work and reducing the complexity of the algorithm.

[0155] Figure 8 is a schematic diagram of a terminal provided by an embodiment of the present invention. Figure 8 As shown, the terminal 8 of this embodiment includes: a processor 80, a memory 81, and a computer program 82 stored in the memory 81 and executable on the processor 80. When the processor 80 executes the computer program 82, the steps in the above-mentioned embodiments of the dynamic data competition detection method are implemented, for example Figure 1 Alternatively, when the processor 80 executes the computer program 82, the functions of each module / unit in the above-mentioned device embodiments are realized, for example Figure 7 Functions of modules / units 71 to 74 are shown.

[0156] Exemplarily, the computer program 82 may be divided into one or more modules / units, which are stored in the memory 81 and executed by the processor 80 to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program 82 in the terminal 8. For example, the computer program 82 may be divided into Figure 7 Modules / units 71 to 74 are shown.

[0157] The terminal 8 may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal 8 may include, but is not limited to, a processor 80 and a memory 81. Those skilled in the art will appreciate that Figure 8 It is only an example of terminal 8 and does not constitute a limitation on terminal 8. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the terminal may also include input and output devices, network access devices, buses, etc.

[0158] The processor 80 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0159] The memory 81 may be an internal storage unit of the terminal 8, such as a hard disk or memory of the terminal 8. The memory 81 may also be an external storage device of the terminal 8, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the terminal 8. Further, the memory 81 may also include both an internal storage unit of the terminal 8 and an external storage device. The memory 81 is used to store the computer program and other programs and data required by the terminal. The memory 81 may also be used to temporarily store data that has been output or is to be output.

[0160] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0161] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0162] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0163] In the embodiments provided by the present invention, it should be understood that the disclosed devices / terminals and methods can be implemented in other ways. For example, the device / terminal embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0164] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0165] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0166] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned dynamic data competition detection method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0167] The embodiments described above are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such 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, and should all be included in the protection scope of the present invention.

Claims

1. A dynamic data competition detection method, characterized in that: include: Initialize the vector clock of each thread, and synchronously update the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal; According to the state of each thread acquiring and releasing the lock, the lock set corresponding to each thread is updated respectively, and the lock set includes all the locks held by the corresponding thread; Perform data competition detection on each thread based on the vector clock value of each thread and the lock set of each thread; According to the data competition detection result, adjusting the size of the dynamic buffer, wherein the dynamic buffer is used to store the access sequence of the shared variables in each thread; The data competition detection is performed on each thread according to the vector clock value of each thread and the lock set of each thread, including: When the current thread reads a variable, read-write data competition detection is performed based on the vector clock value of the current thread and the lock set of the current thread; When the current thread writes a variable, write-write data competition detection and write-read data competition detection are performed based on the vector clock value of the current thread and the lock set of the current thread; When the current thread reads a variable, a read-write data competition detection is performed according to the vector clock value of the current thread and the lock set of the current thread, including: Initialize index value; Determine whether the current index value is less than the size of the write access buffer; When the current index value is less than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and whether the local clock value of the thread recorded in the write access buffer is less than the clock value recorded at the i-th position in the write access buffer, where i represents the current index value; If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set in the i-th position record of the write access buffer is empty; If it is empty, a read-write data race is detected; if it is not empty, no read-write data race is detected; Add 1 to the current index value, and jump to the step of "determining whether the current index value is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; When the current index value is greater than or equal to the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position of the read access buffer; If so, set the shared read flag to true and add a new record to the read buffer; If not, directly add a new record to the read buffer.

2. The dynamic data competition detection method according to claim 1, characterized in that: Initializing the vector clock of each thread and synchronously updating the vector clock of each thread when the thread is created, the thread waits, sends a signal, and waits for a signal based on the sub-hb relationship, includes: When initializing the vector clock of each thread, set the local clock value of the current thread to 1 and the clock values ​​of other threads to 0; When a thread is created, the initialization vector clock of the child thread is combined with the vector clock of the parent thread to obtain the maximum value, and the value is assigned to the vector clock of the child thread, and the local clock value of the parent thread is increased by 1; When a thread is waiting, the vector clock of the waiting thread and the vector clock of the waiting thread are combined to get the maximum value, and the value is assigned to the vector clock of the waiting thread, and the local clock value of the waiting thread is increased by 1; When a thread sends a signal, the vector clock of the current thread is combined with the vector clock of the synchronization object to obtain the maximum value, and the value is assigned to the vector clock of the synchronization object. The local clock of the current thread is increased by 1. When a thread receives a signal, it combines the vector clock of the current thread with the vector clock of the synchronization object to obtain the maximum value, and assigns it to the vector clock of the current thread.

3. The dynamic data competition detection method according to claim 1, characterized in that: When the current thread writes a variable, a write-write data competition detection is performed according to the vector clock value of the current thread and the lock set of the current thread, including: Initialize index value; Determine whether the current index value is less than the size of the write access buffer; When the current index value is smaller than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and the local clock value of the thread recorded in the write access buffer is smaller than the clock value recorded at the i-th position of the write access buffer; If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the write access buffer is empty; If it is empty, a read-write data race is detected, if it is not empty, no write-write data race is detected; Add 1 to the current index value, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; If the current index value is greater than or equal to the size of the write access buffer, the index value is reinitialized and write-read data competition detection is performed.

4. The dynamic data competition detection method according to claim 3, characterized in that: The re-initialization of the index value and the detection of write-read data competition include: Initialize index value; Determine whether the current index value is less than the size of the read access buffer; If yes, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position in the read access buffer; If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set of the i-th position record in the read access buffer is empty; If it is empty, a write-read data race is detected; if it is not empty, no write-read data race is detected; Add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the read access buffer; When the current index value is greater than or equal to the size of the read access buffer, the shared read flag is set to false, and a new record is added to the write buffer.

5. The dynamic data competition detection method according to claim 1, characterized in that: The step of adjusting the size of the dynamic buffer according to the data competition detection result includes: Determine whether the detected data contention quantity is greater than 0, and whether the current dynamic buffer size is less than a preset upper limit of the dynamic buffer; If yes, then the size of the current dynamic buffer is doubled, and it is further determined whether the size of the current dynamic buffer is greater than the preset upper limit of the dynamic buffer; If the current size of the dynamic buffer is greater than the preset upper limit of the dynamic buffer, setting the current size of the buffer to a new preset upper limit of the dynamic buffer; If the current size of the dynamic buffer is not greater than the preset upper limit of the dynamic buffer, the preset upper limit of the dynamic buffer is not changed; If not, continue to determine whether the number of accesses to the dynamic buffer is greater than a preset multiple of the current dynamic buffer size; If so, the size of the current dynamic buffer is reduced by half, and it is further determined whether the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer; If the size of the current dynamic buffer is less than the preset lower limit of the dynamic buffer, the size of the current dynamic buffer is set as the new preset lower limit of the dynamic buffer, and the number of accesses to the dynamic buffer is set to 1; If the size of the current dynamic buffer is greater than or equal to the preset lower limit of the dynamic buffer, the number of accesses to the dynamic buffer is directly set to 1.

6. A dynamic data competition detection device, characterized in that: include: The vector clock module is used to initialize the vector clock of each thread and synchronously update the vector clock of each thread based on the sub-hb relationship when the thread is created, the thread waits, the thread sends a signal, and the thread waits for a signal; A lock set update module, used to update the lock set corresponding to each thread according to the state of each thread acquiring and releasing the lock, wherein the lock set includes all the locks held by the corresponding thread; A detection module, used for performing data competition detection on each thread according to the vector clock value of each thread and the lock set of each thread; An adjustment module, used for adjusting the size of a dynamic buffer according to a data competition detection result, wherein the dynamic buffer is used for storing an access sequence of a shared variable in each thread; The data competition detection is performed on each thread according to the vector clock value of each thread and the lock set of each thread, including: When the current thread reads a variable, read-write data competition detection is performed based on the vector clock value of the current thread and the lock set of the current thread; When the current thread writes a variable, write-write data competition detection and write-read data competition detection are performed based on the vector clock value of the current thread and the lock set of the current thread; When the current thread reads a variable, a read-write data competition detection is performed according to the vector clock value of the current thread and the lock set of the current thread, including: Initialize index value; Determine whether the current index value is less than the size of the write access buffer; When the current index value is less than the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the write access buffer, and whether the local clock value of the thread recorded in the write access buffer is less than the clock value recorded at the i-th position in the write access buffer, where i represents the current index value; If yes, continue to determine whether the intersection of the lock set of the current thread and the lock set in the i-th position record of the write access buffer is empty; If it is empty, a read-write data race is detected; if it is not empty, no read-write data race is detected; Add 1 to the current index value, and jump to the step of "determining whether the current index value is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; If not, add 1 to the value of the current index, and jump to the step of "determining whether the current index i is less than the size of the write access buffer", re-determine and execute subsequent steps until the current index value is greater than or equal to the size of the write access buffer; When the current index value is greater than or equal to the size of the write access buffer, determine whether the current thread number is not equal to the thread number recorded in the read access buffer, and the local clock value of the thread recorded in the read access buffer is less than the clock value recorded at the i-th position of the read access buffer; If so, set the shared read flag to true and add a new record to the read buffer; If not, directly add a new record to the read buffer.

7. A terminal comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.

8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.