Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\TypeDependencyAttribute.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\DependentHandle.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\GCSettings.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\JitInfo.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\ComTypes\IEnumerable.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\ComTypes\IEnumerator.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\DynamicInterfaceCastableHelpers.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,6 @@ private static unsafe void DispatchTailCalls(
}
}
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern long GetILBytesJitted();

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int GetMethodsJittedCount();
}
// Helper class to assist with unsafe pinning of arbitrary objects.
// It's used by VM code.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;

namespace System.Runtime
{
public static partial class JitInfo
{
/// <summary>
/// Get the number of bytes of IL that have been compiled. If <paramref name="currentThread"/> is true,
/// then this value is scoped to the current thread, otherwise, this is a global value.
/// </summary>
/// <param name="currentThread">Whether the returned value should be specific to the current thread. Default: false</param>
/// <returns>The number of bytes of IL the JIT has compiled.</returns>
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern long GetCompiledILBytes(bool currentThread = false);

/// <summary>
/// Get the number of methods that have been compiled. If <paramref name="currentThread"/> is true,
/// then this value is scoped to the current thread, otherwise, this is a global value.
/// </summary>
/// <param name="currentThread">Whether the returned value should be specific to the current thread. Default: false</param>
/// <returns>The number of methods the JIT has compiled.</returns>
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetCompiledMethodCount(bool currentThread = false);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern long GetCompilationTimeInTicks(bool currentThread = false);
}
}
9 changes: 7 additions & 2 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,12 @@ FCFuncStart(gInterlockedFuncs)
QCFuncElement("_MemoryBarrierProcessWide", COMInterlocked::MemoryBarrierProcessWide)
FCFuncEnd()

FCFuncStart(gJitInfoFuncs)
FCFuncElement("GetCompiledILBytes", GetCompiledILBytes)
FCFuncElement("GetCompiledMethodCount", GetCompiledMethodCount)
FCFuncElement("GetCompilationTimeInTicks", GetCompilationTimeInTicks)
FCFuncEnd()

FCFuncStart(gVarArgFuncs)
FCFuncElementSig(COR_CTOR_METHOD_NAME, &gsig_IM_IntPtr_PtrVoid_RetVoid, VarArgsNative::Init2)
FCFuncElementSig(COR_CTOR_METHOD_NAME, &gsig_IM_IntPtr_RetVoid, VarArgsNative::Init)
Expand Down Expand Up @@ -879,8 +885,6 @@ FCFuncStart(gRuntimeHelpers)
QCFuncElement("AllocateTypeAssociatedMemory", RuntimeTypeHandle::AllocateTypeAssociatedMemory)
FCFuncElement("AllocTailCallArgBuffer", TailCallHelp::AllocTailCallArgBuffer)
FCFuncElement("GetTailCallInfo", TailCallHelp::GetTailCallInfo)
FCFuncElement("GetILBytesJitted", GetJittedBytes)
FCFuncElement("GetMethodsJittedCount", GetJittedMethodsCount)
FCFuncEnd()

FCFuncStart(gMngdFixedArrayMarshalerFuncs)
Expand Down Expand Up @@ -1158,6 +1162,7 @@ FCClassElement("IReflect", "System.Reflection", gStdMngIReflectFuncs)
FCClassElement("InterfaceMarshaler", "System.StubHelpers", gInterfaceMarshalerFuncs)
#endif
FCClassElement("Interlocked", "System.Threading", gInterlockedFuncs)
FCClassElement("JitInfo", "System.Runtime", gJitInfoFuncs)
#if TARGET_UNIX
FCClassElement("Kernel32", "", gPalKernel32Funcs)
#endif
Expand Down
38 changes: 34 additions & 4 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,33 @@ GARY_IMPL(VMHELPDEF, hlpDynamicFuncTable, DYNAMIC_CORINFO_HELP_COUNT);

uint64_t g_cbILJitted = 0;
uint32_t g_cMethodsJitted = 0;
int64_t g_cQPCTicksInJit = 0;
thread_local uint64_t g_cbILJittedForThread = 0;
thread_local uint32_t g_cMethodsJittedForThread = 0;
thread_local int64_t g_cQPCTicksInJitForThread = 0;

#ifndef CROSSGEN_COMPILE
FCIMPL0(INT64, GetJittedBytes)
FCIMPL1(int64_t, GetCompiledILBytes, bool currentThread)
{
FCALL_CONTRACT;

return g_cbILJitted;
return currentThread ? g_cbILJittedForThread : g_cbILJitted;
}
FCIMPLEND

FCIMPL0(INT32, GetJittedMethodsCount)
FCIMPL1(int32_t, GetCompiledMethodCount, bool currentThread)
{
FCALL_CONTRACT;

return g_cMethodsJitted;
return currentThread ? g_cMethodsJittedForThread : g_cMethodsJitted;
}
FCIMPLEND

FCIMPL1(int64_t, GetCompilationTimeInTicks, bool currentThread)
{
FCALL_CONTRACT;

return currentThread ? g_cQPCTicksInJitForThread : g_cQPCTicksInJit;
}
FCIMPLEND
#endif
Expand Down Expand Up @@ -13030,9 +13042,16 @@ PCODE UnsafeJitFunction(PrepareCodeConfig* config,
MethodDesc* ftn = nativeCodeVersion.GetMethodDesc();

PCODE ret = NULL;
int64_t jitStartTimestamp = 0;
int64_t jitEndTimestamp = 0;
int64_t jitTimeQPCTicks = 0;
LARGE_INTEGER qpcValue;

COOPERATIVE_TRANSITION_BEGIN();

QueryPerformanceCounter(&qpcValue);
jitStartTimestamp = static_cast<int64_t>(qpcValue.QuadPart);

#ifdef FEATURE_PREJIT

if (g_pConfig->RequireZaps() == EEConfig::REQUIRE_ZAPS_ALL &&
Expand Down Expand Up @@ -13394,8 +13413,19 @@ PCODE UnsafeJitFunction(PrepareCodeConfig* config,
printf(".");
#endif // _DEBUG

QueryPerformanceCounter(&qpcValue);
jitEndTimestamp = static_cast<int64_t>(qpcValue.QuadPart);

jitTimeQPCTicks = jitEndTimestamp - jitStartTimestamp;

FastInterlockExchangeAddLong((LONG64*)&g_cQPCTicksInJit, jitTimeQPCTicks);
FastInterlockExchangeAddLong((LONG64*)&g_cQPCTicksInJitForThread, jitTimeQPCTicks);

FastInterlockExchangeAddLong((LONG64*)&g_cbILJitted, methodInfo.ILCodeSize);
FastInterlockExchangeAddLong((LONG64*)&g_cbILJittedForThread, methodInfo.ILCodeSize);

FastInterlockIncrement((LONG*)&g_cMethodsJitted);
FastInterlockIncrement((LONG*)&g_cMethodsJittedForThread);

COOPERATIVE_TRANSITION_END();
return ret;
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/vm/jitinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,9 @@ CORJIT_FLAGS GetDebuggerCompileFlags(Module* pModule, CORJIT_FLAGS flags);

bool __stdcall TrackAllocationsEnabled();

FCDECL0(INT64, GetJittedBytes);
FCDECL0(INT32, GetJittedMethodsCount);
FCDECL1(int64_t, GetCompiledILBytes, bool currentThread);
FCDECL1(int32_t, GetCompiledMethodCount, bool currentThread);
FCDECL1(int64_t, GetCompilationTimeInTicks, bool currentThread);

#endif // JITINTERFACE_H

Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector64_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector64DebugView_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\X86\Enums.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\JitInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Loader\AssemblyLoadContext.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Loader\LibraryNameVariation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\MemoryFailPoint.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal sealed partial class RuntimeEventSource : EventSource
private PollingCounter? _assemblyCounter;
private PollingCounter? _ilBytesJittedCounter;
private PollingCounter? _methodsJittedCounter;
private IncrementingPollingCounter? _jitTimeCounter;

public static void Initialize()
{
Expand Down Expand Up @@ -83,8 +84,9 @@ protected override void OnEventCommand(EventCommandEventArgs command)
_lohSizeCounter ??= new PollingCounter("loh-size", this, () => GC.GetGenerationSize(3)) { DisplayName = "LOH Size", DisplayUnits = "B" };
_pohSizeCounter ??= new PollingCounter("poh-size", this, () => GC.GetGenerationSize(4)) { DisplayName = "POH (Pinned Object Heap) Size", DisplayUnits = "B" };
_assemblyCounter ??= new PollingCounter("assembly-count", this, () => System.Reflection.Assembly.GetAssemblyCount()) { DisplayName = "Number of Assemblies Loaded" };
_ilBytesJittedCounter ??= new PollingCounter("il-bytes-jitted", this, () => System.Runtime.CompilerServices.RuntimeHelpers.GetILBytesJitted()) { DisplayName = "IL Bytes Jitted", DisplayUnits = "B" };
_methodsJittedCounter ??= new PollingCounter("methods-jitted-count", this, () => System.Runtime.CompilerServices.RuntimeHelpers.GetMethodsJittedCount()) { DisplayName = "Number of Methods Jitted" };
_ilBytesJittedCounter ??= new PollingCounter("il-bytes-jitted", this, () => System.Runtime.JitInfo.GetCompiledILBytes()) { DisplayName = "IL Bytes Jitted", DisplayUnits = "B" };
_methodsJittedCounter ??= new PollingCounter("methods-jitted-count", this, () => System.Runtime.JitInfo.GetCompiledMethodCount()) { DisplayName = "Number of Methods Jitted" };
_jitTimeCounter ??= new IncrementingPollingCounter("time-in-jit", this, () => System.Runtime.JitInfo.GetCompilationTime().TotalMilliseconds) { DisplayName = "Time spent in JIT", DisplayUnits = "ms", DisplayRateTimeScale = new TimeSpan(0, 0, 1) };
}

}
Expand Down
19 changes: 19 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Runtime/JitInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime
{
public static partial class JitInfo
{
/// <summary>
/// Get the amount of time the JIT Compiler has spent compiling methods. If <paramref name="currentThread"/> is true,
/// then this value is scoped to the current thread, otherwise, this is a global value.
/// </summary>
/// <param name="currentThread">Whether the returned value should be specific to the current thread. Default: false</param>
/// <returns>The amount of time the JIT Compiler has spent compiling methods.</returns>
public static TimeSpan GetCompilationTime(bool currentThread = false)
{
return TimeSpan.FromTicks(GetCompilationTimeInTicks(currentThread));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ internal enum RuntimeCounters
GC_LARGE_OBJECT_SIZE_BYTES,
GC_LAST_PERCENT_TIME_IN_GC,
JIT_IL_BYTES_JITTED,
JIT_METHODS_JITTED
JIT_METHODS_JITTED,
JIT_TICKS_IN_JIT
}

#if FEATURE_PERFTRACING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ public static object GetUninitializedObject(
return GetUninitializedObjectInternal(new RuntimeTypeHandle(rt).Value);
}

internal static long GetILBytesJitted()
{
return (long)EventPipeInternal.GetRuntimeCounterValue(EventPipeInternal.RuntimeCounters.JIT_IL_BYTES_JITTED);
}

internal static int GetMethodsJittedCount()
{
return (int)EventPipeInternal.GetRuntimeCounterValue(EventPipeInternal.RuntimeCounters.JIT_METHODS_JITTED);
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern unsafe void PrepareMethod(IntPtr method, IntPtr* instantiations, int ninst);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace System.Runtime
{
public static partial class JitInfo
{
/// <summary>
/// Get the number of bytes of IL that have been compiled. If <paramref name="currentThread"/> is true,
/// then this value is scoped to the current thread, otherwise, this is a global value.
/// </summary>
/// <param name="currentThread">Whether the returned value should be specific to the current thread. Default: false</param>
/// <returns>The number of bytes of IL the JIT has compiled.</returns>
public static long GetCompiledILBytes(bool currentThread = false)
{
return currentThread ? 0 : (long)EventPipeInternal.GetRuntimeCounterValue(EventPipeInternal.RuntimeCounters.JIT_IL_BYTES_JITTED);
}

/// <summary>
/// Get the number of methods that have been compiled. If <paramref name="currentThread"/> is true,
/// then this value is scoped to the current thread, otherwise, this is a global value.
/// </summary>
/// <param name="currentThread">Whether the returned value should be specific to the current thread. Default: false</param>
/// <returns>The number of methods the JIT has compiled.</returns>
public static int GetCompiledMethodCount(bool currentThread = false)
{
return currentThread ? 0 : (int)EventPipeInternal.GetRuntimeCounterValue(EventPipeInternal.RuntimeCounters.JIT_METHODS_JITTED);
}

public static long GetCompilationTimeInTicks(bool currentThread = false)
{
return currentThread ? 0 : (long)EventPipeInternal.GetRuntimeCounterValue(EventPipeInternal.RuntimeCounters.JIT_TICKS_IN_JIT);
}
}
}
26 changes: 23 additions & 3 deletions src/mono/mono/metadata/icall-eventpipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ typedef enum {
EP_RT_COUNTERS_GC_LARGE_OBJECT_SIZE_BYTES,
EP_RT_COUNTERS_GC_LAST_PERCENT_TIME_IN_GC,
EP_RT_COUNTERS_JIT_IL_BYTES_JITTED,
EP_RT_COUNTERS_JIT_METOHODS_JITTED
EP_RT_COUNTERS_JIT_METOHODS_JITTED,
EP_RT_COUNTERS_JIT_TICKS_IN_JIT
} EventPipeRuntimeCounters;

static
Expand Down Expand Up @@ -265,9 +266,10 @@ get_il_bytes_jitted (void)
gint64 methods_compiled = 0;
gint64 cil_code_size_bytes = 0;
gint64 native_code_size_bytes = 0;
gint64 jit_time = 0;

if (mono_get_runtime_callbacks ()->get_jit_stats)
mono_get_runtime_callbacks ()->get_jit_stats (&methods_compiled, &cil_code_size_bytes, &native_code_size_bytes);
mono_get_runtime_callbacks ()->get_jit_stats (&methods_compiled, &cil_code_size_bytes, &native_code_size_bytes, &jit_time);
return cil_code_size_bytes;
}

Expand All @@ -279,9 +281,10 @@ get_methods_jitted (void)
gint64 methods_compiled = 0;
gint64 cil_code_size_bytes = 0;
gint64 native_code_size_bytes = 0;
gint64 jit_time = 0;

if (mono_get_runtime_callbacks ()->get_jit_stats)
mono_get_runtime_callbacks ()->get_jit_stats (&methods_compiled, &cil_code_size_bytes, &native_code_size_bytes);
mono_get_runtime_callbacks ()->get_jit_stats (&methods_compiled, &cil_code_size_bytes, &native_code_size_bytes, &jit_time);
return (gint32)methods_compiled;
}

Expand All @@ -296,6 +299,21 @@ get_exception_count (void)
return excepion_count;
}

static
inline
gint64
get_ticks_in_jit (void)
{
gint64 methods_compiled = 0;
gint64 cil_code_size_bytes = 0;
gint64 native_code_size_bytes = 0;
gint64 jit_time = 0;

if (mono_get_runtime_callbacks ()->get_jit_stats)
mono_get_runtime_callbacks ()->get_jit_stats (&methods_compiled, &cil_code_size_bytes, &native_code_size_bytes, &jit_time);
return jit_time;
}

guint64 ves_icall_System_Diagnostics_Tracing_EventPipeInternal_GetRuntimeCounterValue (gint32 id)
{
EventPipeRuntimeCounters counterID = (EventPipeRuntimeCounters)id;
Expand All @@ -316,6 +334,8 @@ guint64 ves_icall_System_Diagnostics_Tracing_EventPipeInternal_GetRuntimeCounter
return (guint64)get_il_bytes_jitted ();
case EP_RT_COUNTERS_JIT_METOHODS_JITTED :
return (guint64)get_methods_jitted ();
case EP_RT_COUNTERS_JIT_TICKS_IN_JIT :
return (gint64)get_ticks_in_jit ();
default:
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/object-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ typedef struct {
void (*init_mem_manager)(MonoMemoryManager*);
void (*free_mem_manager)(MonoMemoryManager*);
void (*metadata_update_published) (MonoAssemblyLoadContext *alc, uint32_t generation);
void (*get_jit_stats)(gint64 *methods_compiled, gint64 *cil_code_size_bytes, gint64 *native_code_size_bytes);
void (*get_jit_stats)(gint64 *methods_compiled, gint64 *cil_code_size_bytes, gint64 *native_code_size_bytes, gint64 *jit_time);
void (*get_exception_stats)(guint32 *exception_count);
// Same as compile_method, but returns a MonoFtnDesc in llvmonly mode
gpointer (*get_ftnptr)(MonoMethod *method, MonoError *error);
Expand Down
3 changes: 2 additions & 1 deletion src/mono/mono/mini/mini.h
Original file line number Diff line number Diff line change
Expand Up @@ -1744,11 +1744,12 @@ typedef struct {
extern MonoJitStats mono_jit_stats;

static inline void
get_jit_stats (gint64 *methods_compiled, gint64 *cil_code_size_bytes, gint64 *native_code_size_bytes)
get_jit_stats (gint64 *methods_compiled, gint64 *cil_code_size_bytes, gint64 *native_code_size_bytes, gint64 *jit_time)
{
*methods_compiled = mono_jit_stats.methods_compiled;
*cil_code_size_bytes = mono_jit_stats.cil_code_size;
*native_code_size_bytes = mono_jit_stats.native_code_size;
*jit_time = mono_jit_stats.jit_time;
}

guint32
Expand Down
Loading