Skip to content

Commit 7abd780

Browse files
committed
Drop --dsrouter argument, adjust default IPC channel probing to detect dsrouter.
1 parent f4a5528 commit 7abd780

File tree

7 files changed

+54
-101
lines changed

7 files changed

+54
-101
lines changed

src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsIpc/IpcTransport.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,43 +275,67 @@ private string GetDefaultAddress()
275275
throw new ServerNotAvailableException($"Process {_pid} seems to be elevated.");
276276
}
277277

278-
if (!TryGetDefaultAddress(_pid, false, out string transportName))
278+
if (!TryGetDefaultAddress(_pid, out string transportName))
279279
{
280280
throw new ServerNotAvailableException($"Process {_pid} not running compatible .NET runtime.");
281281
}
282282

283283
return transportName;
284284
}
285285

286-
private static bool TryGetDefaultAddress(int pid, bool dsRouter, out string defaultAddress)
286+
private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
287287
{
288288
defaultAddress = null;
289289

290-
string addressPrefix = !dsRouter ? "dotnet-diagnostic" : "dotnet-dsrouter";
290+
string dsrouterFilePath = "";
291+
string diagnosticFilePath = "";
291292

292293
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
293294
{
294-
defaultAddress = $"{addressPrefix}-{pid}";
295+
diagnosticFilePath = Path.Combine(IpcRootPath + $"dotnet-diagnostic-{pid}");
296+
dsrouterFilePath = Path.Combine(IpcRootPath, $"dotnet-diagnostic-dsrouter-{pid}");
295297
}
296298
else
297299
{
298300
try
299301
{
300-
defaultAddress = Directory.GetFiles(IpcRootPath, $"{addressPrefix}-{pid}-*-socket") // Try best match.
302+
diagnosticFilePath = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-{pid}-*-socket")
301303
.OrderByDescending(f => new FileInfo(f).LastWriteTime)
302-
.FirstOrDefault();
304+
.FirstOrDefault();
305+
306+
dsrouterFilePath = Directory.GetFiles(IpcRootPath, $"dotnet-diagnostic-dsrouter-{pid}-*-socket")
307+
.OrderByDescending(f => new FileInfo(f).LastWriteTime)
308+
.FirstOrDefault();
303309
}
304310
catch (InvalidOperationException)
305311
{
306312
}
307313
}
308314

309-
return !string.IsNullOrEmpty(defaultAddress);
310-
}
315+
FileInfo diagnosticFile = new(diagnosticFilePath);
316+
FileInfo dsrouterFile = new(dsrouterFilePath);
311317

312-
public static string GetDefaultAddressForProcessId(int pid, bool dsRouter)
313-
{
314-
return TryGetDefaultAddress(pid, dsRouter, out string defaultAddress) ? defaultAddress : string.Empty;
318+
if (diagnosticFile.Exists && !dsrouterFile.Exists)
319+
{
320+
defaultAddress = diagnosticFile.FullName;
321+
}
322+
else if (dsrouterFile.Exists && !diagnosticFile.Exists)
323+
{
324+
defaultAddress = dsrouterFile.FullName;
325+
}
326+
else
327+
{
328+
if (dsrouterFile.LastWriteTime < diagnosticFile.LastWriteTime)
329+
{
330+
defaultAddress = diagnosticFile.FullName;
331+
}
332+
else
333+
{
334+
defaultAddress = dsrouterFile.FullName;
335+
}
336+
}
337+
338+
return !string.IsNullOrEmpty(defaultAddress);
315339
}
316340

317341
public override bool Equals(object obj)

src/Tools/dotnet-counters/CounterMonitor.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ public async Task<int> Monitor(
503503
bool resumeRuntime,
504504
int maxHistograms,
505505
int maxTimeSeries,
506-
TimeSpan duration,
507-
bool dsRouter)
506+
TimeSpan duration)
508507
{
509508
try
510509
{
@@ -520,12 +519,6 @@ public async Task<int> Monitor(
520519
}
521520
ct.Register(() => _shouldExit.TrySetResult((int)ReturnCode.Ok));
522521

523-
if (_processId > 0 && dsRouter)
524-
{
525-
diagnosticPort = PidIpcEndpoint.GetDefaultAddressForProcessId(_processId, dsRouter) + ",connect";
526-
_processId = -1;
527-
}
528-
529522
DiagnosticsClientBuilder builder = new("dotnet-counters", 10);
530523
using (DiagnosticsClientHolder holder = await builder.Build(ct, _processId, diagnosticPort, showChildIO: false, printLaunchCommand: false).ConfigureAwait(false))
531524
using (VirtualTerminalMode vTerm = VirtualTerminalMode.TryEnable())
@@ -586,8 +579,7 @@ public async Task<int> Collect(
586579
bool resumeRuntime,
587580
int maxHistograms,
588581
int maxTimeSeries,
589-
TimeSpan duration,
590-
bool dsRouter)
582+
TimeSpan duration)
591583
{
592584
try
593585
{
@@ -603,12 +595,6 @@ public async Task<int> Collect(
603595
}
604596
ct.Register(() => _shouldExit.TrySetResult((int)ReturnCode.Ok));
605597

606-
if (_processId > 0 && dsRouter)
607-
{
608-
diagnosticPort = PidIpcEndpoint.GetDefaultAddressForProcessId(_processId, dsRouter) + ",connect";
609-
_processId = -1;
610-
}
611-
612598
DiagnosticsClientBuilder builder = new("dotnet-counters", 10);
613599
using (DiagnosticsClientHolder holder = await builder.Build(ct, _processId, diagnosticPort, showChildIO: false, printLaunchCommand: false).ConfigureAwait(false))
614600
{

src/Tools/dotnet-counters/Program.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ private delegate Task<int> CollectDelegate(
3535
bool resumeRuntime,
3636
int maxHistograms,
3737
int maxTimeSeries,
38-
TimeSpan duration,
39-
bool dsRouter);
38+
TimeSpan duration);
4039

4140
private delegate Task<int> MonitorDelegate(
4241
CancellationToken ct,
@@ -50,8 +49,7 @@ private delegate Task<int> MonitorDelegate(
5049
bool resumeRuntime,
5150
int maxHistograms,
5251
int maxTimeSeries,
53-
TimeSpan duration,
54-
bool dsRouter);
52+
TimeSpan duration);
5553

5654
private static Command MonitorCommand() =>
5755
new(
@@ -70,8 +68,7 @@ private static Command MonitorCommand() =>
7068
ResumeRuntimeOption(),
7169
MaxHistogramOption(),
7270
MaxTimeSeriesOption(),
73-
DurationOption(),
74-
DSRouterOption()
71+
DurationOption()
7572
};
7673

7774
private static Command CollectCommand() =>
@@ -93,8 +90,7 @@ private static Command CollectCommand() =>
9390
ResumeRuntimeOption(),
9491
MaxHistogramOption(),
9592
MaxTimeSeriesOption(),
96-
DurationOption(),
97-
DSRouterOption()
93+
DurationOption()
9894
};
9995

10096
private static Option NameOption() =>
@@ -211,14 +207,6 @@ private static Option DurationOption() =>
211207
Argument = new Argument<TimeSpan>(name: "duration-timespan", getDefaultValue: () => default)
212208
};
213209

214-
private static Option<bool> DSRouterOption() =>
215-
new(
216-
aliases: new[] { "--dsrouter" },
217-
description: "Process identified by -p|-n|--process-id|--name is a dotnet-dsrouter process.")
218-
{
219-
Argument = new Argument<bool>(name: "dsrouter", getDefaultValue: () => false)
220-
};
221-
222210
private static readonly string[] s_SupportedRuntimeVersions = KnownData.s_AllVersions;
223211

224212
public static int List(IConsole console, string runtimeVersion)

src/Tools/dotnet-dsrouter/DiagnosticsServerRouterCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private static string GetDefaultIpcServerPath(ILogger logger)
354354

355355
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
356356
{
357-
path = $"dotnet-dsrouter-{processId}";
357+
path = $"dotnet-diagnostic-dsrouter-{processId}";
358358
}
359359
else
360360
{
@@ -365,11 +365,11 @@ private static string GetDefaultIpcServerPath(ILogger logger)
365365
unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
366366
#endif
367367
TimeSpan diff = Process.GetCurrentProcess().StartTime.ToUniversalTime() - unixEpoch;
368-
path = Path.Combine(PidIpcEndpoint.IpcRootPath, $"dotnet-dsrouter-{processId}-{(long)diff.TotalSeconds}-socket");
368+
path = Path.Combine(PidIpcEndpoint.IpcRootPath, $"dotnet-diagnostic-dsrouter-{processId}-{(long)diff.TotalSeconds}-socket");
369369
}
370370

371371
logger?.LogDebug($"Using default IPC server path, {path}.");
372-
logger?.LogDebug($"Attach to default IPC server using -p {processId} and --dsrouter diagnostic tooling arguments.");
372+
logger?.LogDebug($"Attach to default dotnet-dsrouter IPC server using -p {processId} diagnostic tooling argument.");
373373

374374
return path;
375375
}

src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Diagnostics.Tools.GCDump
1616
{
1717
internal static class CollectCommandHandler
1818
{
19-
private delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort, bool dsRouter);
19+
private delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort);
2020

2121
/// <summary>
2222
/// Collects a gcdump from a currently running process.
@@ -29,9 +29,8 @@ internal static class CollectCommandHandler
2929
/// <param name="verbose">Enable verbose logging.</param>
3030
/// <param name="name">The process name to collect the gcdump from.</param>
3131
/// <param name="diagnosticPort">The diagnostic IPC channel to collect the gcdump from.</param>
32-
/// <param name="dsRouter">Process identified by processId is a dotnet-dsrouter process.</param>
3332
/// <returns></returns>
34-
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort, bool dsRouter)
33+
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort)
3534
{
3635
if (!CommandUtils.ValidateArgumentsForAttach (processId, name, diagnosticPort, out int resolvedProcessId))
3736
{
@@ -40,11 +39,6 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
4039

4140
processId = resolvedProcessId;
4241

43-
if (processId > 0 && dsRouter)
44-
{
45-
diagnosticPort = PidIpcEndpoint.GetDefaultAddressForProcessId(processId, dsRouter) + ",connect";
46-
}
47-
4842
if (!string.IsNullOrEmpty(diagnosticPort))
4943
{
5044
try
@@ -150,8 +144,7 @@ public static Command CollectCommand() =>
150144
VerboseOption(),
151145
TimeoutOption(),
152146
NameOption(),
153-
DiagnosticPortOption(),
154-
DSRouterOption()
147+
DiagnosticPortOption()
155148
};
156149

157150
private static Option<int> ProcessIdOption() =>
@@ -202,13 +195,5 @@ private static Option<string> DiagnosticPortOption() =>
202195
{
203196
Argument = new Argument<string>(name: "diagnostic-port", getDefaultValue: () => string.Empty)
204197
};
205-
206-
private static Option<bool> DSRouterOption() =>
207-
new(
208-
aliases: new[] { "--dsrouter" },
209-
description: "Process identified by -p|-n|--process-id|--name is a dotnet-dsrouter process.")
210-
{
211-
Argument = new Argument<bool>(name: "dsrouter", getDefaultValue: () => false)
212-
};
213198
}
214199
}

src/Tools/dotnet-gcdump/CommandLine/ReportCommandHandler.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Diagnostics.Tools.GCDump
1616
{
1717
internal static class ReportCommandHandler
1818
{
19-
private delegate Task<int> ReportDelegate(CancellationToken ct, IConsole console, FileInfo gcdump_filename, int? processId = null, ReportType reportType = ReportType.HeapStat, string diagnosticPort = null, bool dsRouter = false);
19+
private delegate Task<int> ReportDelegate(CancellationToken ct, IConsole console, FileInfo gcdump_filename, int? processId = null, ReportType reportType = ReportType.HeapStat, string diagnosticPort = null);
2020

2121
public static Command ReportCommand() =>
2222
new(
@@ -30,10 +30,9 @@ public static Command ReportCommand() =>
3030
ProcessIdOption(),
3131
ReportTypeOption(),
3232
DiagnosticPortOption(),
33-
DSRouterOption()
3433
};
3534

36-
private static Task<int> Report(CancellationToken ct, IConsole console, FileInfo gcdump_filename, int? processId = null, ReportType type = ReportType.HeapStat, string diagnosticPort = null, bool dsRouter = false)
35+
private static Task<int> Report(CancellationToken ct, IConsole console, FileInfo gcdump_filename, int? processId = null, ReportType type = ReportType.HeapStat, string diagnosticPort = null)
3736
{
3837
//
3938
// Validation
@@ -72,7 +71,7 @@ private static Task<int> Report(CancellationToken ct, IConsole console, FileInfo
7271

7372
return (source, type) switch
7473
{
75-
(ReportSource.Process, ReportType.HeapStat) => ReportFromProcess(processId ?? 0, diagnosticPort, dsRouter, ct),
74+
(ReportSource.Process, ReportType.HeapStat) => ReportFromProcess(processId ?? 0, diagnosticPort, ct),
7675
(ReportSource.DumpFile, ReportType.HeapStat) => ReportFromFile(gcdump_filename),
7776
_ => HandleUnknownParam()
7877
};
@@ -84,7 +83,7 @@ private static Task<int> HandleUnknownParam()
8483
return Task.FromResult(-1);
8584
}
8685

87-
private static Task<int> ReportFromProcess(int processId, string diagnosticPort, bool dsRouter, CancellationToken ct)
86+
private static Task<int> ReportFromProcess(int processId, string diagnosticPort, CancellationToken ct)
8887
{
8988
if (!CommandUtils.ValidateArgumentsForAttach(processId, string.Empty, diagnosticPort, out int resolvedProcessId))
9089
{
@@ -93,11 +92,6 @@ private static Task<int> ReportFromProcess(int processId, string diagnosticPort,
9392

9493
processId = resolvedProcessId;
9594

96-
if (processId > 0 && dsRouter)
97-
{
98-
diagnosticPort = PidIpcEndpoint.GetDefaultAddressForProcessId(processId, dsRouter) + ",connect";
99-
}
100-
10195
if (!string.IsNullOrEmpty(diagnosticPort))
10296
{
10397
try
@@ -182,14 +176,6 @@ private static Option<string> DiagnosticPortOption() =>
182176
Argument = new Argument<string>(name: "diagnostic-port", getDefaultValue: () => string.Empty)
183177
};
184178

185-
private static Option<bool> DSRouterOption() =>
186-
new(
187-
aliases: new[] { "--dsrouter" },
188-
description: "Process identified by -p|--process-id is a dotnet-dsrouter process.")
189-
{
190-
Argument = new Argument<bool>(name: "dsrouter", getDefaultValue: () => false)
191-
};
192-
193179
private enum ReportSource
194180
{
195181
Unknown,

src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static void ConsoleWriteLine(string str)
3131
}
3232
}
3333

34-
private delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string port, bool showchildio, bool resumeRuntime, bool dsRouter);
34+
private delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string port, bool showchildio, bool resumeRuntime);
3535

3636
/// <summary>
3737
/// Collects a diagnostic trace from a currently running process or launch a child process and trace it.
@@ -52,9 +52,8 @@ private static void ConsoleWriteLine(string str)
5252
/// <param name="diagnosticPort">Path to the diagnostic port to be used.</param>
5353
/// <param name="showchildio">Should IO from a child process be hidden.</param>
5454
/// <param name="resumeRuntime">Resume runtime once session has been initialized.</param>
55-
/// <param name="dsRouter">Process identified by processId is a dotnet-dsrouter process.</param>
5655
/// <returns></returns>
57-
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string diagnosticPort, bool showchildio, bool resumeRuntime, bool dsRouter)
56+
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string diagnosticPort, bool showchildio, bool resumeRuntime)
5857
{
5958
bool collectionStopped = false;
6059
bool cancelOnEnter = true;
@@ -108,12 +107,6 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
108107
return (int)ReturnCode.ArgumentError;
109108
}
110109

111-
if (processId > 0 && dsRouter)
112-
{
113-
diagnosticPort = PidIpcEndpoint.GetDefaultAddressForProcessId(processId, dsRouter) + ",connect";
114-
processId = -1;
115-
}
116-
117110
if (profile.Length == 0 && providers.Length == 0 && clrevents.Length == 0)
118111
{
119112
ConsoleWriteLine("No profile or providers specified, defaulting to trace profile 'cpu-sampling'");
@@ -462,8 +455,7 @@ public static Command CollectCommand() =>
462455
CommonOptions.NameOption(),
463456
DiagnosticPortOption(),
464457
ShowChildIOOption(),
465-
ResumeRuntimeOption(),
466-
DSRouterOption()
458+
ResumeRuntimeOption()
467459
};
468460

469461
private static uint DefaultCircularBufferSizeInMB() => 256;
@@ -554,13 +546,5 @@ private static Option ResumeRuntimeOption() =>
554546
{
555547
Argument = new Argument<bool>(name: "resumeRuntime", getDefaultValue: () => true)
556548
};
557-
558-
private static Option<bool> DSRouterOption() =>
559-
new(
560-
aliases: new[] { "--dsrouter" },
561-
description: "Process identified by -p|-n|--process-id|--name is a dotnet-dsrouter process.")
562-
{
563-
Argument = new Argument<bool>(name: "dsrouter", getDefaultValue: () => false)
564-
};
565549
}
566550
}

0 commit comments

Comments
 (0)