-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return int.MaxValue in GC.GetGeneration(nongc_obj) and same for profi…
…ler's GetObjectGeneration (#85017)
- Loading branch information
Showing
16 changed files
with
222 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,53 @@ | ||
// 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.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
namespace Profiler.Tests | ||
{ | ||
class NonGCHeapTests | ||
{ | ||
static readonly Guid GcAllocateEventsProfilerGuid = new Guid("EF0D191C-3FC7-4311-88AF-E474CBEB2859"); | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static void AllocateNonGcHeapObjects() | ||
{ | ||
// When this method is invoked, JIT is expected to trigger allocations for these | ||
// string literals and they're expected to end up in a nongc segment (also known as frozen) | ||
Consume("string1"); | ||
Consume("string2"); | ||
Consume("string3"); | ||
Consume("string4"); | ||
Consume("string5"); | ||
Consume("string6"); | ||
|
||
int gen = GC.GetGeneration("string7"); | ||
if (gen != int.MaxValue) | ||
throw new Exception("object is expected to be in a non-gc heap for this test to work"); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static void Consume(object o) {} | ||
|
||
public static int RunTest(String[] args) | ||
{ | ||
AllocateNonGcHeapObjects(); | ||
Console.WriteLine("Test Passed"); | ||
return 100; | ||
} | ||
|
||
public static int Main(string[] args) | ||
{ | ||
if (args.Length > 0 && args[0].Equals("RunTest", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return RunTest(args); | ||
} | ||
|
||
return ProfilerTestRunner.Run(profileePath: System.Reflection.Assembly.GetExecutingAssembly().Location, | ||
testName: "NonGCHeapAllocate", | ||
profilerClsid: GcAllocateEventsProfilerGuid); | ||
} | ||
} | ||
} |
This file contains 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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier> | ||
<OutputType>exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<Optimize>true</Optimize> | ||
<!-- This test provides no interesting scenarios for GCStress --> | ||
<GCStressIncompatible>true</GCStressIncompatible> | ||
<!-- The test launches a secondary process and process launch creates | ||
an infinite event loop in the SocketAsyncEngine on Linux. Since | ||
runincontext loads even framework assemblies into the unloadable | ||
context, locals in this loop prevent unloading --> | ||
<UnloadabilityIncompatible>true</UnloadabilityIncompatible> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" /> | ||
<ProjectReference Include="../common/profiler_common.csproj" /> | ||
<CMakeProjectReference Include="$(MSBuildThisFileDirectory)/../native/CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |
This file contains 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 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 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,74 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include "nongcheap.h" | ||
|
||
GUID NonGcHeapProfiler::GetClsid() | ||
{ | ||
// {EF0D191C-3FC7-4311-88AF-E474CBEB2859} | ||
GUID clsid = { 0xef0d191c, 0x3fc7, 0x4311, { 0x88, 0xaf, 0xe4, 0x74, 0xcb, 0xeb, 0x28, 0x59 } }; | ||
return clsid; | ||
} | ||
|
||
HRESULT NonGcHeapProfiler::Initialize(IUnknown* pICorProfilerInfoUnk) | ||
{ | ||
Profiler::Initialize(pICorProfilerInfoUnk); | ||
|
||
HRESULT hr = S_OK; | ||
if (FAILED(hr = pCorProfilerInfo->SetEventMask2( | ||
COR_PRF_ENABLE_OBJECT_ALLOCATED | COR_PRF_MONITOR_OBJECT_ALLOCATED, | ||
COR_PRF_HIGH_BASIC_GC))) | ||
{ | ||
printf("FAIL: ICorProfilerInfo::SetEventMask2() failed hr=0x%x", hr); | ||
return hr; | ||
} | ||
|
||
return S_OK; | ||
} | ||
|
||
HRESULT STDMETHODCALLTYPE NonGcHeapProfiler::ObjectAllocated(ObjectID objectId, ClassID classId) | ||
{ | ||
COR_PRF_GC_GENERATION_RANGE gen; | ||
HRESULT hr = pCorProfilerInfo->GetObjectGeneration(objectId, &gen); | ||
|
||
// non-GC objects (same for GC.GetGeneration() API) have generation = -1 | ||
if (gen.generation == (COR_PRF_GC_GENERATION)INT32_MAX) | ||
{ | ||
if (!FAILED(hr)) | ||
{ | ||
// We expect GetObjectGeneration to return an error (CORPROF_E_NOT_GC_OBJECT) | ||
// for non-GC objects. | ||
_failures++; | ||
} | ||
_nonGcHeapObjects++; | ||
if (gen.rangeLength != 0 || gen.rangeLengthReserved != 0 || gen.rangeStart != 0) | ||
{ | ||
_failures++; | ||
} | ||
} | ||
else if (FAILED(hr)) | ||
{ | ||
_failures++; | ||
} | ||
return S_OK; | ||
} | ||
|
||
HRESULT NonGcHeapProfiler::Shutdown() | ||
{ | ||
if (_failures > 0) | ||
{ | ||
printf("PROFILER TEST FAILS\n"); | ||
} | ||
else if (_nonGcHeapObjects == 0) | ||
{ | ||
printf("PROFILER TEST FAILS: non-GC heap objects were not allocated\n"); | ||
} | ||
else | ||
{ | ||
printf("PROFILER TEST PASSES\n"); | ||
} | ||
printf("Non-GC objects allocated: %d\n", (int)_nonGcHeapObjects); | ||
printf("PROFILER TEST PASSES\n"); | ||
fflush(stdout); | ||
return S_OK; | ||
} |
This file contains 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,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "../profiler.h" | ||
|
||
class NonGcHeapProfiler : public Profiler | ||
{ | ||
public: | ||
NonGcHeapProfiler() : Profiler(), | ||
_nonGcHeapObjects(0), | ||
_failures(0) | ||
{} | ||
|
||
static GUID GetClsid(); | ||
virtual HRESULT STDMETHODCALLTYPE Initialize(IUnknown* pICorProfilerInfoUnk); | ||
virtual HRESULT STDMETHODCALLTYPE ObjectAllocated(ObjectID objectId, ClassID classId); | ||
virtual HRESULT STDMETHODCALLTYPE Shutdown(); | ||
|
||
private: | ||
std::atomic<int> _nonGcHeapObjects; | ||
std::atomic<int> _failures; | ||
}; |