From 9d82e635fe6f5b23e980c1cff645fc70fd142b2b Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Tue, 15 Apr 2025 12:13:32 -0700 Subject: [PATCH] Try to find memory usage in LibraryImportTests --- eng/testing/xunit/xunit.console.targets | 2 +- .../ArrayTests.cs | 6 ++++ .../BlittableStructTests.cs | 8 ++++- .../BooleanTests.cs | 6 ++++ .../CollectionMarshallingFails.cs | 6 ++++ .../CollectionTests.cs | 6 ++++ .../CustomMarshallingTests.cs | 6 ++++ .../DelegateTests.cs | 6 ++++ .../FunctionPointerTests.cs | 6 ++++ .../LibraryImportGenerator.Tests/PrintTop.cs | 32 +++++++++++++++++++ .../SafeHandleTests.cs | 6 ++++ .../LibraryImportGenerator.Tests/SpanTests.cs | 6 ++++ 12 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/PrintTop.cs diff --git a/eng/testing/xunit/xunit.console.targets b/eng/testing/xunit/xunit.console.targets index b682117b75a9ec..82fb4273791ddc 100644 --- a/eng/testing/xunit/xunit.console.targets +++ b/eng/testing/xunit/xunit.console.targets @@ -14,7 +14,7 @@ - + diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs index c0d7398b039c12..6378e1cb68dee8 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices.Marshalling; using SharedTypes; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -184,6 +185,11 @@ public static partial BoolStruct[] NegateBools( public class ArrayTests { + public ArrayTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + private int[] GetIntArray() => new[] { 1, 5, 79, 165, 32, 3 }; [Fact] diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BlittableStructTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BlittableStructTests.cs index c3a6ea8588837f..42cbd492eb7dde 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BlittableStructTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BlittableStructTests.cs @@ -6,6 +6,7 @@ using SharedTypes; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -45,7 +46,7 @@ public static partial class IntStructExtensions { [LibraryImport(NativeExportsNE.NativeExportsNE_Binary, EntryPoint = "blittablestructs_return_instance")] public static partial IntFields DoubleIntFields(this IntFields result); - + [LibraryImport(NativeExportsNE.NativeExportsNE_Binary, EntryPoint = "blittablestructs_double_intfields_refreturn")] public static partial void DoubleIntFieldsOutReturn( this IntFields input, @@ -54,6 +55,11 @@ public static partial void DoubleIntFieldsOutReturn( public class BlittableStructTests { + public BlittableStructTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void ValidateIntFields() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BooleanTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BooleanTests.cs index 58cb2008753673..f5e2d778ed5427 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BooleanTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/BooleanTests.cs @@ -6,6 +6,7 @@ using System.Runtime.InteropServices; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -71,6 +72,11 @@ partial class NativeExportsNE public class BooleanTests { + public BooleanTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + // See definition of Windows' VARIANT_BOOL const ushort VARIANT_TRUE = unchecked((ushort)-1); const ushort VARIANT_FALSE = 0; diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionMarshallingFails.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionMarshallingFails.cs index e238ca51f24044..4b80035fbc3fe4 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionMarshallingFails.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionMarshallingFails.cs @@ -7,6 +7,7 @@ using System.Runtime.InteropServices.Marshalling; using SharedTypes; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -109,6 +110,11 @@ public static partial void NegateBoolsRef2D_ClearMarshalling( public class CollectionMarshallingFails { + public CollectionMarshallingFails(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void UTFStringConversionFailures() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionTests.cs index 5892dcce98b0f4..89ba4e5c91f7ad 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CollectionTests.cs @@ -11,6 +11,7 @@ using System.Text; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -192,6 +193,11 @@ public void Free() { } public class CollectionTests { + public CollectionTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void BlittableElementColllection_ByValue() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs index 9262e5a36d5151..dce89349db4632 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/CustomMarshallingTests.cs @@ -9,6 +9,7 @@ using SharedTypes; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -119,6 +120,11 @@ public void Free() {} public class CustomMarshallingTests { + public CustomMarshallingTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void NonBlittableStructWithFree() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/DelegateTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/DelegateTests.cs index 639c607b378694..a0ac4382850eec 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/DelegateTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/DelegateTests.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -22,6 +23,11 @@ partial class NativeExportsNE public class DelegateTests { + public DelegateTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void DelegateIsKeptAliveDuringCall() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/FunctionPointerTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/FunctionPointerTests.cs index 7557cf2e51921f..71fbb42160dba2 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/FunctionPointerTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/FunctionPointerTests.cs @@ -6,6 +6,7 @@ using System.Runtime.InteropServices; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -35,6 +36,11 @@ public partial class FunctionPointer public class FunctionPointerTests { + public FunctionPointerTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + private static bool wasCalled; [Fact] diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/PrintTop.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/PrintTop.cs new file mode 100644 index 00000000000000..f0dca974122489 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/PrintTop.cs @@ -0,0 +1,32 @@ + +using System; +using Xunit.Abstractions; + +internal class PrintTopHelper +{ + public static void PrintOutputOfTopCommand(ITestOutputHelper output) + { + if (Environment.OSVersion.Platform == PlatformID.Unix) + { + // grab the output of the `top` command to get the memory usage + // and CPU usage of the process + var process = new System.Diagnostics.Process + { + StartInfo = new System.Diagnostics.ProcessStartInfo + { + FileName = "top", + Arguments = "-b -n 1", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + process.Start(); + string outputText = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + + output.WriteLine(outputText); + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SafeHandleTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SafeHandleTests.cs index 2d9a2cbc6326c5..5774c30570721b 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SafeHandleTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SafeHandleTests.cs @@ -5,6 +5,7 @@ using Microsoft.Win32.SafeHandles; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -49,6 +50,11 @@ protected override bool ReleaseHandle() public class SafeHandleTests { + public SafeHandleTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void ReturnValue_CreatesSafeHandle() { diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SpanTests.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SpanTests.cs index fd7a4fd4d0b9fc..594156d1c4c94c 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SpanTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/SpanTests.cs @@ -11,6 +11,7 @@ using System.Text; using Xunit; +using Xunit.Abstractions; namespace LibraryImportGenerator.IntegrationTests { @@ -48,6 +49,11 @@ public partial class Span public class SpanTests { + public SpanTests(ITestOutputHelper output) + { + PrintTopHelper.PrintOutputOfTopCommand(output); + } + [Fact] public void BlittableElementSpanMarshalledToNativeAsExpected() {