-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Privately expose MsQuic performance counters #98422
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 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47da7c7
Dump select counters after running functional tests.
rzikm 49142ad
V1 of counter implementation
rzikm 3c84899
Fix compilation
rzikm 52269b1
Switch to Metrics API
rzikm f23ecd4
Code review feedback
rzikm 49be6e4
Rename to QuicTestCollection
rzikm 7c59cd4
Move to instrument per metric
rzikm 976715c
Remove unwanted code
rzikm 1589227
Rename file
rzikm 1a33acf
Redisable paralellization
rzikm 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
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
121 changes: 121 additions & 0 deletions
121
src/libraries/System.Net.Quic/src/System/Net/Quic/NetEventSource.Quic.Counters.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,121 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.Tracing; | ||
| using System.Diagnostics.Metrics; | ||
| using System.Net.Quic; | ||
|
|
||
| using Microsoft.Quic; | ||
| using static Microsoft.Quic.MsQuic; | ||
|
|
||
| namespace System.Net | ||
| { | ||
| internal sealed partial class NetEventSource | ||
| { | ||
| private static Meter s_meter = new Meter("Private.InternalDiagnostics.System.Net.Quic"); | ||
| private static readonly long[] s_counters = new long[(int)QUIC_PERFORMANCE_COUNTERS.MAX]; | ||
|
|
||
| public static readonly ObservableGauge<long> MsQuicCountersGauge = s_meter.CreateObservableGauge<long>( | ||
rzikm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| name: "MsQuic", | ||
| observeValues: GetGauges, | ||
| unit: null, | ||
| description: "MsQuic performance counters"); | ||
|
|
||
| public static readonly ObservableCounter<long> MsQuicCountersCounter = s_meter.CreateObservableCounter<long>( | ||
| name: "MsQuic", | ||
| observeValues: GetCounters, | ||
| unit: null, | ||
| description: "MsQuic performance counters"); | ||
|
|
||
| [NonEvent] | ||
rzikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static void UpdateCounters() | ||
rzikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| unsafe | ||
| { | ||
| fixed (long* pCounters = s_counters) | ||
| { | ||
| uint size = (uint)s_counters.Length * sizeof(long); | ||
| MsQuicApi.Api.ApiTable->GetParam(null, QUIC_PARAM_GLOBAL_PERF_COUNTERS, &size, (byte*)pCounters); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [NonEvent] | ||
| private static IEnumerable<Measurement<long>> GetGauges() | ||
| { | ||
| if (!MsQuicApi.IsQuicSupported) | ||
| { | ||
| // Avoid calling into MsQuic if not supported (or not initialized yet) | ||
| return Array.Empty<Measurement<long>>(); | ||
| } | ||
|
|
||
| UpdateCounters(); | ||
|
|
||
| var measurements = new List<Measurement<long>>(); | ||
|
|
||
| static void AddMeasurement(List<Measurement<long>> measurements, QUIC_PERFORMANCE_COUNTERS counter) | ||
| { | ||
| measurements.Add(new Measurement<long>(s_counters[(int)counter], new KeyValuePair<string, object?>("Name", counter))); | ||
| } | ||
|
|
||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_ACTIVE); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_CONNECTED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.STRM_ACTIVE); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_QUEUE_DEPTH); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_OPER_QUEUE_DEPTH); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.WORK_OPER_QUEUE_DEPTH); | ||
|
|
||
| return measurements; | ||
| } | ||
|
|
||
|
|
||
| [NonEvent] | ||
| private static IEnumerable<Measurement<long>> GetCounters() | ||
| { | ||
| if (!MsQuicApi.IsQuicSupported) | ||
| { | ||
| // Avoid calling into MsQuic if not supported (or not initialized yet) | ||
| return Array.Empty<Measurement<long>>(); | ||
| } | ||
|
|
||
| UpdateCounters(); | ||
|
|
||
| var measurements = new List<Measurement<long>>(); | ||
|
|
||
| static void AddMeasurement(List<Measurement<long>> measurements, QUIC_PERFORMANCE_COUNTERS counter) | ||
| { | ||
| measurements.Add(new Measurement<long>(s_counters[(int)counter], new KeyValuePair<string, object?>("Name", counter))); | ||
| } | ||
|
|
||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_CREATED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_HANDSHAKE_FAIL); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_APP_REJECT); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_RESUMED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_PROTOCOL_ERRORS); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_NO_ALPN); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.PKTS_SUSPECTED_LOST); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.PKTS_DROPPED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.PKTS_DECRYPTION_FAIL); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_RECV); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_SEND); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_RECV_BYTES); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_SEND_BYTES); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_RECV_EVENTS); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.UDP_SEND_CALLS); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.APP_SEND_BYTES); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.APP_RECV_BYTES); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_OPER_QUEUED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_OPER_COMPLETED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.WORK_OPER_QUEUED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.WORK_OPER_COMPLETED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.PATH_VALIDATED); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.PATH_FAILURE); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.SEND_STATELESS_RESET); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.SEND_STATELESS_RETRY); | ||
| AddMeasurement(measurements, QUIC_PERFORMANCE_COUNTERS.CONN_LOAD_REJECT); | ||
|
|
||
| return measurements; | ||
| } | ||
| } | ||
| } | ||
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
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
66 changes: 66 additions & 0 deletions
66
src/libraries/System.Net.Quic/tests/FunctionalTests/QuicCountersFixture.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,66 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.DotNet.XUnitExtensions; | ||
| using System; | ||
| using System.Net.Quic; | ||
| using System.Reflection; | ||
| using System.Text; | ||
| using System.Linq; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| using Microsoft.Quic; | ||
| using static Microsoft.Quic.MsQuic; | ||
|
|
||
| public class QuicCountersListener : IDisposable | ||
rzikm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| public unsafe void Dispose() | ||
| { | ||
| Type msQuicApiType = Type.GetType("System.Net.Quic.MsQuicApi, System.Net.Quic"); | ||
| object msQuicApiInstance = msQuicApiType.GetProperty("Api", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>()); | ||
| QUIC_API_TABLE* apiTable = (QUIC_API_TABLE*)(Pointer.Unbox(msQuicApiType.GetProperty("ApiTable").GetGetMethod().Invoke(msQuicApiInstance, Array.Empty<object?>()))); | ||
|
|
||
| long[] counters = new long[(int)QUIC_PERFORMANCE_COUNTERS.MAX]; | ||
| int countersAvailable; | ||
|
|
||
| int status; | ||
| fixed (long* pCounters = counters) | ||
| { | ||
| uint size = (uint)counters.Length * sizeof(long); | ||
| status = apiTable->GetParam(null, QUIC_PARAM_GLOBAL_PERF_COUNTERS, &size, (byte*)pCounters); | ||
| countersAvailable = (int)size / sizeof(long); | ||
| } | ||
|
|
||
| if (StatusFailed(status)) | ||
| { | ||
| System.Console.WriteLine($"Failed to read MsQuic counters: {status}"); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| sb.AppendLine("MsQuic Counters:"); | ||
|
|
||
| int maxlen = Enum.GetNames(typeof(QUIC_PERFORMANCE_COUNTERS)).Max(s => s.Length); | ||
| void DumpCounter(QUIC_PERFORMANCE_COUNTERS counter) | ||
| { | ||
| var name = $"{counter}:".PadRight(maxlen + 1); | ||
| var index = (int)counter; | ||
| var value = index < countersAvailable ? counters[(int)counter].ToString() : "N/A"; | ||
| sb.AppendLine($" {counter} {value}"); | ||
| } | ||
|
|
||
| DumpCounter(QUIC_PERFORMANCE_COUNTERS.CONN_CREATED); | ||
| DumpCounter(QUIC_PERFORMANCE_COUNTERS.CONN_HANDSHAKE_FAIL); | ||
| DumpCounter(QUIC_PERFORMANCE_COUNTERS.CONN_APP_REJECT); | ||
| DumpCounter(QUIC_PERFORMANCE_COUNTERS.CONN_LOAD_REJECT); | ||
|
|
||
| System.Console.WriteLine(sb.ToString()); | ||
ManickaP marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| [CollectionDefinition(nameof(QuicCountersListener), DisableParallelization = true)] | ||
rzikm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public class QuicCountersCollection : ICollectionFixture<QuicCountersListener> | ||
| { | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is related to microsoft/msquic#4120, we should wait until that one merges.
We don't need to wait with MsQuic update before merging, the counter will be 0 until a version which supports it is loaded.