|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Diagnostics.Tracing; |
| 8 | +using System.Linq; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | +using System.Runtime.CompilerServices; |
| 12 | +using System.Runtime.InteropServices; |
| 13 | +using System.Runtime.Loader; |
| 14 | + |
| 15 | +using Microsoft.Diagnostics.NETCore.Client; |
| 16 | +using Microsoft.Diagnostics.Tracing; |
| 17 | +using Microsoft.Diagnostics.Tracing.Parsers; |
| 18 | +using Microsoft.Diagnostics.Tracing.Parsers.Clr; |
| 19 | +using Tracing.Tests.Common; |
| 20 | + |
| 21 | +namespace Profiler.Tests |
| 22 | +{ |
| 23 | + class DynamicOptimization |
| 24 | + { |
| 25 | + static readonly Guid DynamicOptimizationProfilerGuid = new Guid("C26D02FE-9E4C-484E-8984-F86724AA98B5"); |
| 26 | + |
| 27 | + public static int RunTest(String[] args) |
| 28 | + { |
| 29 | + // This test validates that: |
| 30 | + // - Switching COR_PRF_DISABLE_OPTIMIZATIONS can be done dynamically (by calling SwitchJitOptimization) that makes the profiler update the event mask |
| 31 | + // - Modules loaded while COR_PRF_DISABLE_OPTIMIZATIONS is 0 are Jitted with optimizations even if it set to 1 later |
| 32 | + // - Modules loaded while COR_PRF_DISABLE_OPTIMIZATIONS is 1 are Jitted without optimization even if it is set to 0 later |
| 33 | + |
| 34 | + // to do so, we load the same assembly 3 time in different alc before / after enabling / disalbling COR_PRF_DISABLE_OPTIMIZATIONS. |
| 35 | + // then we explicitly JIT a method in each of the loaded modules and use event traces to check that the method is loaded with an expected Optimization Tier |
| 36 | + |
| 37 | + // Then it make similar tests for inlining (we get inline counts from the profiler callbacks this time) |
| 38 | + |
| 39 | + // Load assembly in different states of COR_PRF_DISABLE_OPTIMIZATIONS |
| 40 | + string currentAssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); |
| 41 | + string testAssemblyFullPath = Path.Combine(currentAssemblyDirectory, "..", "DynamicOptimizationTestLib", "DynamicOptimizationTestLib.dll"); |
| 42 | + var beforeDisableOptimizations = new AssemblyLoadContext("before disable", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 43 | + SwitchJitOptimization(true); |
| 44 | + var afterDisableOptimizations = new AssemblyLoadContext("after disable", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 45 | + SwitchJitOptimization(false); |
| 46 | + var afterReenableOptimizations = new AssemblyLoadContext("after reenable", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 47 | + |
| 48 | + // JIT and check each case |
| 49 | + Console.WriteLine("Trigger JIT and check that module loaded before disabling optimizations is not jitted with MinOpts"); |
| 50 | + var r = CompileTestMethodAndCheckOptimizationTier(beforeDisableOptimizations, false); |
| 51 | + if (r != 100) |
| 52 | + { |
| 53 | + return r; |
| 54 | + } |
| 55 | + Console.WriteLine("Trigger JIT and check that module loaded after disabling optimizations is jitted with MinOpts"); |
| 56 | + r = CompileTestMethodAndCheckOptimizationTier(afterDisableOptimizations, true); |
| 57 | + if (r != 100) |
| 58 | + { |
| 59 | + return r; |
| 60 | + } |
| 61 | + Console.WriteLine("Trigger JIT and check that module loaded after re-enabling optimizations is not jitted with MinOpts"); |
| 62 | + r = CompileTestMethodAndCheckOptimizationTier(afterReenableOptimizations, false); |
| 63 | + if (r != 100) |
| 64 | + { |
| 65 | + return r; |
| 66 | + } |
| 67 | + |
| 68 | + // now we do a similar test for inlining |
| 69 | + Console.WriteLine("Testing disabling inlining"); |
| 70 | + |
| 71 | + var beforeDisableInlining = new AssemblyLoadContext("before disable inlining", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 72 | + SwitchInlining(true); |
| 73 | + var afterDisableInlining = new AssemblyLoadContext("after disable inlining", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 74 | + SwitchInlining(false); |
| 75 | + var afterReenableInlining = new AssemblyLoadContext("after reenable inlining", true).LoadFromAssemblyPath(testAssemblyFullPath); |
| 76 | + |
| 77 | + var initialInlineCount = GetInlineCount(); |
| 78 | + Console.WriteLine($"Before starting inlining tests, inline count is: {initialInlineCount}"); |
| 79 | + // first case: inlining count should have increased |
| 80 | + RuntimeHelpers.PrepareMethod(GetMainMethod(beforeDisableInlining).MethodHandle); |
| 81 | + var actual = GetInlineCount(); |
| 82 | + Console.WriteLine($"After jitting first case, inline count is: {actual}"); |
| 83 | + var expected = initialInlineCount + 1; |
| 84 | + if (expected != actual) |
| 85 | + { |
| 86 | + throw new Exception($"Expected {expected}, got {actual}"); |
| 87 | + } |
| 88 | + // first case: inlining count should not have increased |
| 89 | + RuntimeHelpers.PrepareMethod(GetMainMethod(afterDisableInlining).MethodHandle); |
| 90 | + actual = GetInlineCount(); |
| 91 | + Console.WriteLine($"After jitting second case, inline count is: {actual}"); |
| 92 | + if (expected != actual) |
| 93 | + { |
| 94 | + throw new Exception($"Expected {expected}, got {GetInlineCount()}"); |
| 95 | + } |
| 96 | + // third case: should have inlined |
| 97 | + RuntimeHelpers.PrepareMethod(GetMainMethod(afterReenableInlining).MethodHandle); |
| 98 | + actual = GetInlineCount(); |
| 99 | + Console.WriteLine($"After jitting third case, inline count is: {actual}"); |
| 100 | + expected++; |
| 101 | + if (expected != actual) |
| 102 | + { |
| 103 | + throw new Exception($"Expected {expected}, got {GetInlineCount()}"); |
| 104 | + } |
| 105 | + |
| 106 | + Console.WriteLine("PROFILER TEST PASSES"); |
| 107 | + return 100; |
| 108 | + } |
| 109 | + |
| 110 | + static EventPipeProvider _jitEventProvider = new EventPipeProvider("Microsoft-Windows-DotNETRuntime", eventLevel: EventLevel.Verbose, keywords: (long)ClrTraceEventParser.Keywords.Jit); |
| 111 | + |
| 112 | + static MethodInfo GetMainMethod(Assembly assembly) => assembly.GetType("Profiler.Tests.DynamicOptimizationTestLib").GetMethod("Main"); |
| 113 | + static int CompileTestMethodAndCheckOptimizationTier(Assembly assembly, bool optimizationsDisabled) |
| 114 | + { |
| 115 | + var method = GetMainMethod(assembly); |
| 116 | + |
| 117 | + return |
| 118 | + IpcTraceTest.RunAndValidateEventCounts( |
| 119 | + new Dictionary<string, ExpectedEventCount>(), |
| 120 | + () => RuntimeHelpers.PrepareMethod(method.MethodHandle), |
| 121 | + new List<EventPipeProvider> { _jitEventProvider }, |
| 122 | + 1024, |
| 123 | + optimizationsDisabled ? ValidateUnoptimized : ValidateOptimized); |
| 124 | + } |
| 125 | + |
| 126 | + private static Func<int> ValidateUnoptimized(EventPipeEventSource source) |
| 127 | + { |
| 128 | + OptimizationTier lastTier = OptimizationTier.Unknown; |
| 129 | + |
| 130 | + source.Clr.MethodLoadVerbose += e => |
| 131 | + { |
| 132 | + if (e.MethodName == "Main") |
| 133 | + { |
| 134 | + lastTier = e.OptimizationTier; |
| 135 | + Console.WriteLine($"MethodLoadVerbose: {e}"); |
| 136 | + } |
| 137 | + }; |
| 138 | + return () => |
| 139 | + { |
| 140 | + if (lastTier != OptimizationTier.MinOptJitted && lastTier != OptimizationTier.Unknown) |
| 141 | + { |
| 142 | + Console.WriteLine($"Expected MinOptJitted, got {lastTier}"); |
| 143 | + return -1; |
| 144 | + } |
| 145 | + return 100; |
| 146 | + }; |
| 147 | + } |
| 148 | + private static Func<int> ValidateOptimized(EventPipeEventSource source) |
| 149 | + { |
| 150 | + OptimizationTier lastTier = OptimizationTier.Unknown; |
| 151 | + |
| 152 | + source.Clr.MethodLoadVerbose += e => |
| 153 | + { |
| 154 | + if (e.MethodName == "Main") |
| 155 | + { |
| 156 | + lastTier = e.OptimizationTier; |
| 157 | + Console.WriteLine($"MethodLoadVerbose: {e}"); |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + return () => |
| 162 | + { |
| 163 | + if (lastTier == OptimizationTier.MinOptJitted) |
| 164 | + { |
| 165 | + Console.WriteLine($"Expected not MinOptJitted, got {lastTier}"); |
| 166 | + return -1; |
| 167 | + } |
| 168 | + return 100; |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + public static int Main(string[] args) |
| 173 | + { |
| 174 | + if (args.Length > 0 && args[0].Equals("RunTest", StringComparison.OrdinalIgnoreCase)) |
| 175 | + { |
| 176 | + return RunTest(args); |
| 177 | + } |
| 178 | + |
| 179 | + return ProfilerTestRunner.Run(profileePath: System.Reflection.Assembly.GetExecutingAssembly().Location, |
| 180 | + testName: "DynamicOptimization", |
| 181 | + profilerClsid: DynamicOptimizationProfilerGuid); |
| 182 | + } |
| 183 | + |
| 184 | + // this makes the profiler enable / disable COR_PRF_DISABLE_OPTIMIZATIONS dynamically |
| 185 | + [DllImport("Profiler")] |
| 186 | + public static extern int SwitchJitOptimization(bool disable); |
| 187 | + |
| 188 | + // this makes the profiler enable / disable COR_PRF_DISABLE_INLINING dynamically |
| 189 | + [DllImport("Profiler")] |
| 190 | + public static extern int SwitchInlining(bool disable); |
| 191 | + |
| 192 | + // this retrieves the number of inlining operations that occured |
| 193 | + [DllImport("Profiler")] |
| 194 | + public static extern int GetInlineCount(); |
| 195 | + } |
| 196 | +} |
0 commit comments