33using System . Diagnostics ;
44using System . Linq ;
55using System . Text ;
6+ using System . Threading ;
67
78namespace Microsoft . Diagnostics . Tracing . StackSources
89{
@@ -16,36 +17,45 @@ internal sealed class LinuxPerfScriptProcessNameBuilder
1617 ".NET Server GC"
1718 } ;
1819
20+ private readonly object _dictionariesLock = new object ( ) ;
1921 private Dictionary < StackSourceFrameIndex , HashSet < string > > _candidateProcessNames = new Dictionary < StackSourceFrameIndex , HashSet < string > > ( ) ;
2022 private Dictionary < StackSourceFrameIndex , string > _cachedProcessNames = new Dictionary < StackSourceFrameIndex , string > ( ) ;
2123 private Dictionary < StackSourceFrameIndex , int > _processIds = new Dictionary < StackSourceFrameIndex , int > ( ) ;
2224
2325 internal void SaveProcessName ( StackSourceFrameIndex frameIndex , string processName , int processId )
2426 {
25- if ( ! _candidateProcessNames . TryGetValue ( frameIndex , out HashSet < string > processNames ) )
27+ lock ( _dictionariesLock )
2628 {
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+ }
3034
31- processNames . Add ( processName ) ;
35+ processNames . Add ( processName ) ;
3236
33- _processIds [ frameIndex ] = processId ;
37+ _processIds [ frameIndex ] = processId ;
38+ }
3439 }
3540
3641 internal string GetProcessName ( StackSourceFrameIndex frameIndex )
3742 {
38- if ( ! _cachedProcessNames . TryGetValue ( frameIndex , out string processName ) )
43+ lock ( _dictionariesLock )
3944 {
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+ }
4350
44- return processName ;
51+ return processName ;
52+ }
4553 }
4654
4755 private string BuildProcessName ( StackSourceFrameIndex frameIndex )
4856 {
57+ Debug . Assert ( Monitor . IsEntered ( _dictionariesLock ) ) ;
58+
4959 if ( _candidateProcessNames . TryGetValue ( frameIndex , out HashSet < string > processNames ) )
5060 {
5161 int processId = _processIds [ frameIndex ] ;
0 commit comments