Skip to content

Commit

Permalink
Moderenize triggerdump in docs (#3507)
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 authored Nov 15, 2022
1 parent 4de3802 commit 8647f5f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 64 deletions.
94 changes: 45 additions & 49 deletions documentation/tutorial/src/triggerdump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,71 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.Diagnostics.Tools.RuntimeClient;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing;

namespace Microsoft.Diagnostics.Tools.Counters
{
internal class Program
{
private static ulong _sessionId;
private static int threshold;
private static int pid;

private static int pid;

private static void Main(string[] args)
{
if(args.Length<2)
if (args.Length < 2)
{
Console.WriteLine("triggerdump <pid> <mem threshold in MB>");
}
else
{
pid = Convert.ToInt32(args[0]);
threshold = Convert.ToInt32(args[1]);
pid = Convert.ToInt32(args[0]);
threshold = Convert.ToInt32(args[1]);
DiagnosticsClient diagnosticsClient = new(pid);
EventPipeSession session = null;

Task monitorTask = new Task(() =>
Task monitorTask = new Task(() =>
{
var provider = new List<EventPipeProvider>
{
var prov = new List<Provider>();
prov.Add(new Provider("System.Runtime", filterData:"EventCounterIntervalSec=1"));

var configuration = new SessionConfiguration(
circularBufferSizeMB: 1000,
outputPath: "",
providers: prov);

var binaryReader = EventPipeClient.CollectTracing(Int32.Parse(args[0]), configuration, out _sessionId);
EventPipeEventSource source = new EventPipeEventSource(binaryReader);
source.Dynamic.All += Dynamic_All;
source.Process();
});

Task commandTask = new Task(() =>
new EventPipeProvider("System.Runtime", EventLevel.Verbose,
arguments: new Dictionary<string, string> { ["EventCounterIntervalSec"] = "1" })
};

session = diagnosticsClient.StartEventPipeSession(provider, false);
EventPipeEventSource source = new(session.EventStream);
source.Dynamic.All += Dynamic_All;
source.Process();
});

Task commandTask = new Task(() =>
{
while (true)
{
while(true)
while (!Console.KeyAvailable) { }
ConsoleKey cmd = Console.ReadKey(true).Key;
if (cmd == ConsoleKey.Q)
{
while (!Console.KeyAvailable) { }
ConsoleKey cmd = Console.ReadKey(true).Key;
if (cmd == ConsoleKey.Q)
{
break;
}
break;
}
});
}
});

monitorTask.Start();
commandTask.Start();
commandTask.Wait();
monitorTask.Start();
commandTask.Start();
commandTask.Wait();

try
{
EventPipeClient.StopTracing(Int32.Parse(args[0]), _sessionId);
}
catch (System.IO.EndOfStreamException) {}
try
{
session?.Stop();
}
catch (System.IO.EndOfStreamException) { }
}
}

Expand All @@ -83,13 +79,13 @@ private static void Dynamic_All(TraceEvent obj)
IDictionary<string, object> payloadFields = (IDictionary<string, object>)(payloadVal["Payload"]);

ICounterPayload payload = payloadFields.Count == 6 ? (ICounterPayload)new IncrementingCounterPayload(payloadFields) : (ICounterPayload)new CounterPayload(payloadFields);
string displayName = payload.GetDisplay();
string displayName = payload.GetDisplay();
if (string.IsNullOrEmpty(displayName))
{
displayName = payload.GetName();
}

if(string.Compare(displayName, "GC Heap Size") == 0 && Convert.ToInt32(payload.GetValue())>threshold)
if (string.Compare(displayName, "GC Heap Size") == 0 && Convert.ToInt32(payload.GetValue()) > threshold)
{
Console.WriteLine("Memory threshold has been breached....");
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(pid);
Expand All @@ -107,11 +103,11 @@ private static void Dynamic_All(TraceEvent obj)
if (!File.Exists(createDumpPath))
{
Console.WriteLine("Unable to locate 'createdump' tool in '{runtimeDirectory}'");
Environment.Exit(1);
}
Environment.Exit(1);
}

var createdump = new System.Diagnostics.Process()
{
{
StartInfo = new System.Diagnostics.ProcessStartInfo()
{
FileName = createDumpPath,
Expand Down
15 changes: 4 additions & 11 deletions documentation/tutorial/src/triggerdump/triggerdump.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ToolCommandName>triggerdump</ToolCommandName>
<RootNamespace>Diagnosticv.TriggerDump</RootNamespace>
<Description>Trigger dump</Description>
<PackageTags>Diagnostic</PackageTags>
<PackageReleaseNotes>$(Description)</PackageReleaseNotes>
<TargetName>triggerdump</TargetName>
<OutputType>Exe</OutputType>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\src\Tools\dotnet-trace\Extensions.cs" Link="Extensions.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\..\..\..\src\Microsoft.Diagnostics.Tools.RuntimeClient\Microsoft.Diagnostics.Tools.RuntimeClient.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.41" />
<PackageReference Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.351802" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.0.5" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions src/Tools/dotnet-trace/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static EventPipeProvider ToProvider(string provider)

// Event counters
string filterData = tokens.Length > 3 ? tokens[3] : null;
var argument = string.IsNullOrWhiteSpace(filterData) ? null : ParseArgumentString(filterData);
var argument = string.IsNullOrWhiteSpace(filterData) ? null : ParseArgumentString(filterData);
return new EventPipeProvider(providerName, eventLevel, keywords, argument);
}

Expand Down Expand Up @@ -186,13 +186,13 @@ private static Dictionary<string, string> ParseArgumentString(string argument)
if (c == '=')
{
keyEnd = curIdx;
valStart = curIdx+1;
valStart = curIdx + 1;
}
else if (c == ';')
{
valEnd = curIdx;
AddKeyValueToArgumentDict(argumentDict, argument, keyStart, keyEnd, valStart, valEnd);
keyStart = curIdx+1; // new key starts
keyStart = curIdx + 1; // new key starts
}
else if (c == '\"')
{
Expand All @@ -201,7 +201,7 @@ private static Dictionary<string, string> ParseArgumentString(string argument)
}
curIdx += 1;
}
if(valStart > valEnd)
if (valStart > valEnd)
{
valEnd = curIdx;
}
Expand Down

0 comments on commit 8647f5f

Please sign in to comment.