Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Concurrent Access to LinuxPerfScriptProcessNameBuilder #2066

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/TraceEvent/Stacks/Linux/LinuxPerfScriptProcessNameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace Microsoft.Diagnostics.Tracing.StackSources
{
Expand All @@ -16,36 +17,45 @@ internal sealed class LinuxPerfScriptProcessNameBuilder
".NET Server GC"
};

private readonly object _lock = new object();
cincuranet marked this conversation as resolved.
Show resolved Hide resolved
private Dictionary<StackSourceFrameIndex, HashSet<string>> _candidateProcessNames = new Dictionary<StackSourceFrameIndex, HashSet<string>>();
private Dictionary<StackSourceFrameIndex, string> _cachedProcessNames = new Dictionary<StackSourceFrameIndex, string>();
private Dictionary<StackSourceFrameIndex, int> _processIds = new Dictionary<StackSourceFrameIndex, int>();

internal void SaveProcessName(StackSourceFrameIndex frameIndex, string processName, int processId)
{
if (!_candidateProcessNames.TryGetValue(frameIndex, out HashSet<string> processNames))
lock (_lock)
{
processNames = new HashSet<string>();
_candidateProcessNames.Add(frameIndex, processNames);
}
if (!_candidateProcessNames.TryGetValue(frameIndex, out HashSet<string> processNames))
{
processNames = new HashSet<string>();
_candidateProcessNames.Add(frameIndex, processNames);
}

processNames.Add(processName);
processNames.Add(processName);

_processIds[frameIndex] = processId;
_processIds[frameIndex] = processId;
}
}

internal string GetProcessName(StackSourceFrameIndex frameIndex)
{
if (!_cachedProcessNames.TryGetValue(frameIndex, out string processName))
lock (_lock)
{
processName = BuildProcessName(frameIndex);
_cachedProcessNames.Add(frameIndex, processName);
}
if (!_cachedProcessNames.TryGetValue(frameIndex, out string processName))
{
processName = BuildProcessName(frameIndex);
_cachedProcessNames.Add(frameIndex, processName);
}

return processName;
return processName;
}
}

private string BuildProcessName(StackSourceFrameIndex frameIndex)
{
Debug.Assert(Monitor.IsEntered(_lock));

if (_candidateProcessNames.TryGetValue(frameIndex, out HashSet<string> processNames))
{
int processId = _processIds[frameIndex];
Expand Down