Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal sealed record CollectLinuxArgs(
string ClrEvents,
string[] PerfEvents,
string[] Profiles,
uint? BufferSizeInMB,
FileInfo Output,
TimeSpan Duration,
string Name,
Expand Down Expand Up @@ -175,6 +176,7 @@ public static Command CollectLinuxCommand()
CommonOptions.CLREventLevelOption,
CommonOptions.CLREventsOption,
PerfEventsOption,
BufferSizeInMBOption,
ProbeOption,
CommonOptions.ProfileOption,
CommonOptions.OutputPathOption,
Expand All @@ -198,6 +200,7 @@ public static Command CollectLinuxCommand()
ClrEvents: parseResult.GetValue(CommonOptions.CLREventsOption) ?? string.Empty,
PerfEvents: perfEventsValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
Profiles: profilesValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
BufferSizeInMB: parseResult.GetValue(BufferSizeInMBOption),
Output: parseResult.GetValue(CommonOptions.OutputPathOption) ?? new FileInfo(CommonOptions.DefaultTraceName),
Duration: parseResult.GetValue(CommonOptions.DurationOption),
Name: parseResult.GetValue(CommonOptions.NameOption) ?? string.Empty,
Expand Down Expand Up @@ -427,6 +430,14 @@ private byte[] BuildRecordTraceArgs(CollectLinuxArgs args, out string scriptPath
scriptPath = null;
List<string> recordTraceArgs = new();

if (args.BufferSizeInMB.HasValue)
{
if (args.BufferSizeInMB.Value == 0)
{
throw new DiagnosticToolException("Buffer size must be at least 1 MB.");
}
}

string[] profiles = args.Profiles;
if (args.Profiles.Length == 0 && args.Providers.Length == 0 && string.IsNullOrEmpty(args.ClrEvents) && args.PerfEvents.Length == 0)
{
Expand All @@ -435,6 +446,13 @@ private byte[] BuildRecordTraceArgs(CollectLinuxArgs args, out string scriptPath
}

StringBuilder scriptBuilder = new();
if (args.BufferSizeInMB.HasValue)
{
ulong totalBufferSizeBytes = args.BufferSizeInMB.Value * 1024UL * 1024UL;
scriptBuilder.AppendLine($"with_buffer_size_bytes({totalBufferSizeBytes});");
scriptBuilder.AppendLine();
}

List<EventPipeProvider> providerCollection = ProviderUtils.ComputeProviderConfig(args.Providers, args.ClrEvents, args.ClrEventLevel, profiles, true, "collect-linux", Console);
foreach (EventPipeProvider provider in providerCollection)
{
Expand Down Expand Up @@ -582,6 +600,12 @@ private int OutputHandler(uint type, IntPtr data, UIntPtr dataLen)
Description = @"Comma-separated list of perf events (e.g. syscalls:sys_enter_execve,sched:sched_switch)."
};

private static readonly Option<uint?> BufferSizeInMBOption =
new("--buffersize")
{
Description = "Requested total size of the event buffers, in megabytes. When omitted, the recorder chooses a default based on the enabled features."
};

private static readonly Option<bool> ProbeOption =
new("--probe")
{
Expand Down
41 changes: 41 additions & 0 deletions src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private static CollectLinuxCommandHandler.CollectLinuxArgs TestArgs(
string clrEvents = "",
string[] perfEvents = null,
string[] profile = null,
uint? bufferSizeInMB = null,
FileInfo output = null,
TimeSpan duration = default,
string name = "",
Expand All @@ -50,6 +51,7 @@ private static CollectLinuxCommandHandler.CollectLinuxArgs TestArgs(
clrEvents,
perfEvents ?? Array.Empty<string>(),
profile ?? Array.Empty<string>(),
bufferSizeInMB,
output ?? new FileInfo("trace.nettrace"),
duration,
name,
Expand Down Expand Up @@ -359,6 +361,45 @@ public void CollectLinuxCommand_DoesNotReadKey_WhenInputIsRedirected()
Assert.True(callbackInvoked);
}

[ConditionalFact(nameof(IsCollectLinuxSupported))]
public void CollectLinuxCommand_AddsBufferSizeToScript()
{
string outputPath = Path.Combine(Path.GetTempPath(), $"collect-linux-{Guid.NewGuid():N}.nettrace");
string scriptPath = Path.ChangeExtension(outputPath, ".script");
MockConsole console = new(200, 30, _outputHelper);
var handler = new CollectLinuxCommandHandler(console);
handler.RecordTraceInvoker = (cmd, len, cb) => {
Assert.Contains(
"with_buffer_size_bytes(268435456);",
File.ReadAllText(scriptPath));
return 0;
};

int exitCode = handler.CollectLinux(TestArgs(
bufferSizeInMB: 256,
output: new FileInfo(outputPath)));

Assert.Equal((int)ReturnCode.Ok, exitCode);
}

[ConditionalFact(nameof(IsCollectLinuxSupported))]
public void CollectLinuxCommand_RejectsZeroBufferSize()
{
MockConsole console = new(200, 30, _outputHelper);
var handler = new CollectLinuxCommandHandler(console);
handler.RecordTraceInvoker = (cmd, len, cb) => {
Assert.Fail("RecordTrace should not be invoked for an invalid buffer size.");
return 0;
};

int exitCode = handler.CollectLinux(TestArgs(bufferSizeInMB: 0));

Assert.Equal((int)ReturnCode.ArgumentError, exitCode);
console.AssertSanitizedLinesEqual(
null,
FormatException("Buffer size must be at least 1 MB."));
}

[ConditionalFact(nameof(IsCollectLinuxSupported))]
public void CollectLinuxCommand_PrintsStatusOnce_WhenCursorRepositioningUnsupported()
{
Expand Down