diff options
author | Brian Robbins <brianrob@microsoft.com> | 2018-10-05 16:44:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-05 16:44:15 -0700 |
commit | 457148064c836daa746542f1559b7958d6420c22 (patch) | |
tree | 637c49662f44b301afc0f3450055a7c72fd00119 /tests | |
parent | 27c848e37e9998142b60e776cf5b5d08a3543fe1 (diff) | |
download | coreclr-457148064c836daa746542f1559b7958d6420c22.tar.gz coreclr-457148064c836daa746542f1559b7958d6420c22.tar.bz2 coreclr-457148064c836daa746542f1559b7958d6420c22.zip |
Enable Config-File Based Control of EventPipe (#20238)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/issues.targets | 6 | ||||
-rw-r--r-- | tests/src/tracing/tracecontrol/TraceControl.cs | 113 | ||||
-rw-r--r-- | tests/src/tracing/tracecontrol/tracecontrol.csproj | 29 |
3 files changed, 148 insertions, 0 deletions
diff --git a/tests/issues.targets b/tests/issues.targets index 53eed75165..d49472f9e9 100644 --- a/tests/issues.targets +++ b/tests/issues.targets @@ -245,6 +245,12 @@ </ExcludeList> </ItemGroup> + <ItemGroup Condition="'$(XunitTestBinBase)' != '' and '$(TargetsWindows)' == 'true'"> + <ExcludeList Include="$(XunitTestBinBase)/tracing/tracecontrol/tracecontrol/*"> + <Issue>Unable to write config file to app location</Issue> + </ExcludeList> + </ItemGroup> + <!-- Windows x64 specific excludes --> <ItemGroup Condition="'$(XunitTestBinBase)' != '' and '$(BuildArch)' == 'x64' and '$(TargetsWindows)' == 'true'"> <ExcludeList Include="$(XunitTestBinBase)/Loader/classloader/TypeGeneratorTests/TypeGeneratorTest612/Generated612/*"> diff --git a/tests/src/tracing/tracecontrol/TraceControl.cs b/tests/src/tracing/tracecontrol/TraceControl.cs new file mode 100644 index 0000000000..fbdc97abf8 --- /dev/null +++ b/tests/src/tracing/tracecontrol/TraceControl.cs @@ -0,0 +1,113 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Tracing.Tests.Common; + +using Microsoft.Diagnostics.Tracing; +using Microsoft.Diagnostics.Tracing.Parsers.Clr; + +namespace Tracing.Tests +{ + public static class TraceControlTest + { + private static string ConfigFileContents = @" +OutputPath=. +CircularMB=2048 +Providers=*:0xFFFFFFFFFFFFFFFF:5 +"; + + private const int BytesInOneMB = 1024 * 1024; + + /// <summary> + /// This test collects a trace of itself and then performs some basic validation on the trace. + /// </summary> + public static int Main(string[] args) + { + // Calculate the path to the config file. + string configFileName = Assembly.GetEntryAssembly().GetName().Name + ".eventpipeconfig"; + string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFileName); + Console.WriteLine("Calculated config file path: " + configFilePath); + + // Write the config file to disk. + File.WriteAllText(configFilePath, ConfigFileContents); + Console.WriteLine("Wrote contents of config file."); + + // Wait 5 seconds to ensure that tracing has started. + Console.WriteLine("Waiting 5 seconds for the config file to be picked up by the next poll operation."); + Thread.Sleep(TimeSpan.FromSeconds(5)); + + // Do some work that we can look for in the trace. + Console.WriteLine("Do some work that will be captured by the trace."); + GC.Collect(2, GCCollectionMode.Forced); + Console.WriteLine("Done with the work."); + + // Delete the config file to start tracing. + File.Delete(configFilePath); + Console.WriteLine("Deleted the config file."); + + // Build the full path to the trace file. + string[] traceFiles = Directory.GetFiles(".", "*.netperf", SearchOption.TopDirectoryOnly); + Assert.Equal("traceFiles.Length == 1", traceFiles.Length, 1); + string traceFilePath = traceFiles[0]; + + // Poll the file system and wait for the trace file to be written. + Console.WriteLine("Wait for the config file deletion to be picked up and for the trace file to be written."); + + // Wait for 1 second, which is the poll time when tracing is enabled. + Thread.Sleep(TimeSpan.FromSeconds(1)); + + // Poll for file size changes to the trace file itself. When the size of the trace file hasn't changed for 5 seconds, consider it fully written out. + Console.WriteLine("Waiting for the trace file to be written. Poll every second to watch for 5 seconds of no file size changes."); + long lastSizeInBytes = 0; + DateTime timeOfLastChangeUTC = DateTime.UtcNow; + do + { + FileInfo traceFileInfo = new FileInfo(traceFilePath); + long currentSizeInBytes = traceFileInfo.Length; + Console.WriteLine("Trace file size: " + ((double)currentSizeInBytes / BytesInOneMB)); + + if (currentSizeInBytes > lastSizeInBytes) + { + lastSizeInBytes = currentSizeInBytes; + timeOfLastChangeUTC = DateTime.UtcNow; + } + + Thread.Sleep(TimeSpan.FromSeconds(1)); + + } while (DateTime.UtcNow.Subtract(timeOfLastChangeUTC) < TimeSpan.FromSeconds(5)); + + int retVal = 0; + + // Use TraceEvent to consume the trace file and look for the work that we did. + Console.WriteLine("Using TraceEvent to parse the file to find the work that was done during trace capture."); + using (var trace = TraceEventDispatcher.GetDispatcherFromFileName(traceFilePath)) + { + string gcReasonInduced = GCReason.Induced.ToString(); + string providerName = "Microsoft-Windows-DotNETRuntime"; + string gcTriggeredEventName = "GC/Triggered"; + + trace.Clr.GCTriggered += delegate (GCTriggeredTraceData data) + { + if (gcReasonInduced.Equals(data.Reason.ToString())) + { + Console.WriteLine("Detected an induced GC"); + retVal = 100; + } + }; + + trace.Process(); + } + + // Clean-up the resulting trace file. + File.Delete(traceFilePath); + + return retVal; + } + } +} diff --git a/tests/src/tracing/tracecontrol/tracecontrol.csproj b/tests/src/tracing/tracecontrol/tracecontrol.csproj new file mode 100644 index 0000000000..dca25a2e34 --- /dev/null +++ b/tests/src/tracing/tracecontrol/tracecontrol.csproj @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{8E3244CB-407F-4142-BAAB-E7A55901A5FA}</ProjectGuid> + <OutputType>Exe</OutputType> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + <CLRTestKind>BuildAndRun</CLRTestKind> + <DefineConstants>$(DefineConstants);STATIC</DefineConstants> + <CLRTestPriority>0</CLRTestPriority> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"></PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"></PropertyGroup> + <ItemGroup> + <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> + <Visible>False</Visible> + </CodeAnalysisDependentAssemblyPaths> + </ItemGroup> + <ItemGroup> + <Compile Include="TraceControl.cs" /> + <ProjectReference Include="../common/common.csproj" /> + </ItemGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> +</Project> |