-
Notifications
You must be signed in to change notification settings - Fork 382
[dotnet-trace] Add collect-linux verb #5570
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
536cd50
[DotnetTrace] Update CLREventKeywords
mdh1418 49a875b
[DotnetTrace] Move MergeProfileAndProviders to Extensions
mdh1418 b4787fc
[DotnetTrace] Rename Extensions to ProviderUtils
mdh1418 c921869
[DotnetTrace] Add provider unifying helper
mdh1418 e1c15c7
[DotnetTrace] Move shared options to CommonOptions
mdh1418 b4112b5
[DotnetTrace] Add collect-linux skeleton
mdh1418 e5f3975
[DotnetTrace][CollectLinux] Start record-trace
mdh1418 677e54e
[DotnetTrace][CollectLinux] Build record-trace args
mdh1418 31186a8
[DotnetTrace] Update profiles
mdh1418 44593bb
[DotnetTrace] Update collect to new provider unifier
mdh1418 b190d73
[DotnetCounters] Remove Extensions reference
mdh1418 9f1ef6d
[DotnetTrace] Update tests
mdh1418 c8e53ea
Address Feedback
mdh1418 590a203
[DotnetTrace] Print profile effects and clrevents ignore warning
mdh1418 8e2453f
[DotnetTrace] Print PerfEvents and include in default condition
mdh1418 573d769
[DotnetTrace] Update OneCollect package with FFI
mdh1418 1b3b583
Fix dotnet-trace build for repo root build
mdh1418 e6d9de9
Add Linux events table
mdh1418 676efa9
Add Progress status and Address feedback
mdh1418 d90d1be
Merge remote-tracking branch 'upstream/main' into dotnet_trace_collec…
mdh1418 096f325
Update collect functional tests for ProviderUtils refactor
mdh1418 1f75125
[DotnetTrace] Add Collect Linux Functional Tests
mdh1418 55bc84a
Adjust CollectLinux tests for non-Linux platforms
mdh1418 4c1a361
[DotnetTrace][CollectLinux] Remove process specifier
mdh1418 208086f
Adjust functional tests and add failure cases
mdh1418 40c5905
[DotnetTrace][CollectLinux] Add ProgressStatus and output file to tests
mdh1418 32f01dc
Cleanup variable names
mdh1418 25d5559
Handle invalid provider config exception
mdh1418 dc5b0b0
Throw custom CommandLineErrorException in lieu of general ArgumentExc…
mdh1418 6f88dc7
Add Preview message
mdh1418 3a4b1f2
Fix remnant tests expecting ArgumentException
mdh1418 fc05a0a
Update LineToRewrite prior to rewriting
mdh1418 2126a8d
Add banner to preview message
mdh1418 119f101
Bump RecordTrace to support ActivityIDs
mdh1418 e9b0aa8
Improve preview banner message
mdh1418 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
src/Tools/dotnet-trace/CommandLine/Commands/CollectLinuxCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.CommandLine; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Internal.Common.Utils; | ||
|
|
||
| namespace Microsoft.Diagnostics.Tools.Trace | ||
| { | ||
| internal static partial class CollectLinuxCommandHandler | ||
| { | ||
| private static int s_recordStatus; | ||
|
|
||
| internal sealed record CollectLinuxArgs( | ||
| string[] Providers, | ||
| string ClrEventLevel, | ||
| string ClrEvents, | ||
| string[] PerfEvents, | ||
| string[] Profiles, | ||
| FileInfo Output, | ||
| TimeSpan Duration, | ||
| string Name, | ||
| int ProcessId); | ||
|
|
||
| /// <summary> | ||
| /// Collects diagnostic traces using perf_events, a Linux OS technology. collect-linux requires admin privileges to capture kernel- and user-mode events, and by default, captures events from all processes. | ||
| /// This Linux-only command includes the same .NET events as dotnet-trace collect, and it uses the kernel’s user_events mechanism to emit .NET events as perf events, enabling unification of user-space .NET events with kernel-space system events. | ||
| /// </summary> | ||
| private static int CollectLinux(CollectLinuxArgs args) | ||
| { | ||
| if (!OperatingSystem.IsLinux()) | ||
| { | ||
| Console.Error.WriteLine("The collect-linux command is only supported on Linux."); | ||
| return (int)ReturnCode.ArgumentError; | ||
| } | ||
|
|
||
| return RunRecordTrace(args); | ||
| } | ||
|
|
||
| public static Command CollectLinuxCommand() | ||
| { | ||
| Command collectLinuxCommand = new("collect-linux") | ||
| { | ||
| CommonOptions.ProvidersOption, | ||
| CommonOptions.CLREventLevelOption, | ||
| CommonOptions.CLREventsOption, | ||
| PerfEventsOption, | ||
| CommonOptions.ProfileOption, | ||
| CommonOptions.OutputPathOption, | ||
| CommonOptions.DurationOption, | ||
| CommonOptions.NameOption, | ||
| CommonOptions.ProcessIdOption | ||
| }; | ||
| collectLinuxCommand.TreatUnmatchedTokensAsErrors = true; // collect-linux currently does not support child process tracing. | ||
| collectLinuxCommand.Description = "Collects diagnostic traces using perf_events, a Linux OS technology. collect-linux requires admin privileges to capture kernel- and user-mode events, and by default, captures events from all processes. This Linux-only command includes the same .NET events as dotnet-trace collect, and it uses the kernel’s user_events mechanism to emit .NET events as perf events, enabling unification of user-space .NET events with kernel-space system events."; | ||
|
|
||
| collectLinuxCommand.SetAction((parseResult, ct) => { | ||
| string providersValue = parseResult.GetValue(CommonOptions.ProvidersOption) ?? string.Empty; | ||
| string perfEventsValue = parseResult.GetValue(PerfEventsOption) ?? string.Empty; | ||
| string profilesValue = parseResult.GetValue(CommonOptions.ProfileOption) ?? string.Empty; | ||
|
|
||
| int rc = CollectLinux(new CollectLinuxArgs( | ||
| Providers: providersValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), | ||
| ClrEventLevel: parseResult.GetValue(CommonOptions.CLREventLevelOption) ?? string.Empty, | ||
| ClrEvents: parseResult.GetValue(CommonOptions.CLREventsOption) ?? string.Empty, | ||
| PerfEvents: perfEventsValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), | ||
| Profiles: profilesValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), | ||
| Output: parseResult.GetValue(CommonOptions.OutputPathOption) ?? new FileInfo(CommonOptions.DefaultTraceName), | ||
| Duration: parseResult.GetValue(CommonOptions.DurationOption), | ||
| Name: parseResult.GetValue(CommonOptions.NameOption) ?? string.Empty, | ||
| ProcessId: parseResult.GetValue(CommonOptions.ProcessIdOption))); | ||
| return Task.FromResult(rc); | ||
| }); | ||
|
|
||
| return collectLinuxCommand; | ||
| } | ||
|
|
||
| private static int RunRecordTrace(CollectLinuxArgs args) | ||
| { | ||
| s_recordStatus = 0; | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ConsoleCancelEventHandler handler = (sender, e) => | ||
| { | ||
| e.Cancel = true; | ||
| s_recordStatus = 1; | ||
| }; | ||
| Console.CancelKeyPress += handler; | ||
|
|
||
| IEnumerable<string> recordTraceArgList = BuildRecordTraceArgs(args, out string scriptPath); | ||
|
|
||
| string options = string.Join(' ', recordTraceArgList); | ||
| byte[] command = Encoding.UTF8.GetBytes(options); | ||
| int rc; | ||
| try | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| rc = RecordTrace(command, (UIntPtr)command.Length, OutputHandler); | ||
| } | ||
| finally | ||
| { | ||
| Console.CancelKeyPress -= handler; | ||
| if (!string.IsNullOrEmpty(scriptPath)) | ||
| { | ||
| try { | ||
| if (File.Exists(scriptPath)) | ||
| { | ||
| File.Delete(scriptPath); | ||
| } | ||
| } catch { } | ||
| } | ||
| } | ||
|
|
||
| return rc; | ||
| } | ||
|
|
||
| private static List<string> BuildRecordTraceArgs(CollectLinuxArgs args, out string scriptPath) | ||
| { | ||
| Console.WriteLine($"{args.ProcessId}"); | ||
| List<string> recordTraceArgs = new(); | ||
|
|
||
| recordTraceArgs.Add("--on-cpu"); | ||
|
|
||
| return recordTraceArgs; | ||
| } | ||
|
|
||
| private static int OutputHandler(uint type, IntPtr data, UIntPtr dataLen) | ||
| { | ||
| OutputType ot = (OutputType)type; | ||
| if (ot != OutputType.Progress) | ||
| { | ||
| int len = checked((int)dataLen); | ||
| if (len > 0) | ||
| { | ||
| byte[] buffer = new byte[len]; | ||
| Marshal.Copy(data, buffer, 0, len); | ||
| string text = Encoding.UTF8.GetString(buffer); | ||
| switch (ot) | ||
| { | ||
| case OutputType.Normal: | ||
| case OutputType.Live: | ||
| Console.Out.WriteLine(text); | ||
| break; | ||
| case OutputType.Error: | ||
| Console.Error.WriteLine(text); | ||
| break; | ||
| default: | ||
| Console.Error.WriteLine($"[{ot}] {text}"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return s_recordStatus; | ||
| } | ||
|
|
||
| private static readonly Option<string> PerfEventsOption = | ||
| new("--perf-events") | ||
| { | ||
| Description = @"Comma-separated list of kernel perf events (e.g. syscalls:sys_enter_execve,sched:sched_switch)." | ||
mdh1418 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| private enum OutputType : uint | ||
| { | ||
| Normal = 0, | ||
| Live = 1, | ||
| Error = 2, | ||
| Progress = 3, | ||
| } | ||
|
|
||
| [UnmanagedFunctionPointer(CallingConvention.Cdecl)] | ||
| private delegate int recordTraceCallback( | ||
| [In] uint type, | ||
| [In] IntPtr data, | ||
| [In] UIntPtr dataLen); | ||
|
|
||
| [LibraryImport("recordtrace")] | ||
| private static partial int RecordTrace( | ||
| byte[] command, | ||
| UIntPtr commandLen, | ||
| recordTraceCallback callback); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.