3
3
using System . Diagnostics ;
4
4
using System . Linq ;
5
5
using System . Text ;
6
+ using System . Threading ;
6
7
7
8
namespace Microsoft . Diagnostics . Tracing . StackSources
8
9
{
@@ -16,36 +17,45 @@ internal sealed class LinuxPerfScriptProcessNameBuilder
16
17
".NET Server GC"
17
18
} ;
18
19
20
+ private readonly object _dictionariesLock = new object ( ) ;
19
21
private Dictionary < StackSourceFrameIndex , HashSet < string > > _candidateProcessNames = new Dictionary < StackSourceFrameIndex , HashSet < string > > ( ) ;
20
22
private Dictionary < StackSourceFrameIndex , string > _cachedProcessNames = new Dictionary < StackSourceFrameIndex , string > ( ) ;
21
23
private Dictionary < StackSourceFrameIndex , int > _processIds = new Dictionary < StackSourceFrameIndex , int > ( ) ;
22
24
23
25
internal void SaveProcessName ( StackSourceFrameIndex frameIndex , string processName , int processId )
24
26
{
25
- if ( ! _candidateProcessNames . TryGetValue ( frameIndex , out HashSet < string > processNames ) )
27
+ lock ( _dictionariesLock )
26
28
{
27
- processNames = new HashSet < string > ( ) ;
28
- _candidateProcessNames . Add ( frameIndex , processNames ) ;
29
- }
29
+ if ( ! _candidateProcessNames . TryGetValue ( frameIndex , out HashSet < string > processNames ) )
30
+ {
31
+ processNames = new HashSet < string > ( ) ;
32
+ _candidateProcessNames . Add ( frameIndex , processNames ) ;
33
+ }
30
34
31
- processNames . Add ( processName ) ;
35
+ processNames . Add ( processName ) ;
32
36
33
- _processIds [ frameIndex ] = processId ;
37
+ _processIds [ frameIndex ] = processId ;
38
+ }
34
39
}
35
40
36
41
internal string GetProcessName ( StackSourceFrameIndex frameIndex )
37
42
{
38
- if ( ! _cachedProcessNames . TryGetValue ( frameIndex , out string processName ) )
43
+ lock ( _dictionariesLock )
39
44
{
40
- processName = BuildProcessName ( frameIndex ) ;
41
- _cachedProcessNames . Add ( frameIndex , processName ) ;
42
- }
45
+ if ( ! _cachedProcessNames . TryGetValue ( frameIndex , out string processName ) )
46
+ {
47
+ processName = BuildProcessName ( frameIndex ) ;
48
+ _cachedProcessNames . Add ( frameIndex , processName ) ;
49
+ }
43
50
44
- return processName ;
51
+ return processName ;
52
+ }
45
53
}
46
54
47
55
private string BuildProcessName ( StackSourceFrameIndex frameIndex )
48
56
{
57
+ Debug . Assert ( Monitor . IsEntered ( _dictionariesLock ) ) ;
58
+
49
59
if ( _candidateProcessNames . TryGetValue ( frameIndex , out HashSet < string > processNames ) )
50
60
{
51
61
int processId = _processIds [ frameIndex ] ;
0 commit comments