Add automatic profiler events for C# methods invocation

This commit is contained in:
Wojtek Figat
2021-05-04 00:17:27 +02:00
parent 17e3625e5d
commit e4de83f2ed
3 changed files with 25 additions and 11 deletions

View File

@@ -6,6 +6,7 @@
#include "MType.h"
#include "MClass.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include <ThirdParty/mono-2.0/mono/metadata/mono-debug.h>
#include <ThirdParty/mono-2.0/mono/metadata/attrdefs.h>
@@ -51,15 +52,26 @@ MMethod::MMethod(MonoMethod* monoMethod, const char* name, MClass* parentClass)
default:
CRASH;
}
#if COMPILE_WITH_PROFILER
const MString& className = parentClass->GetFullName();
ProfilerName.Resize(className.Length() + 2 + _name.Length());
Platform::MemoryCopy(ProfilerName.Get(), className.Get(), className.Length());
ProfilerName.Get()[className.Length()] = ':';
ProfilerName.Get()[className.Length() + 1] = ':';
Platform::MemoryCopy(ProfilerName.Get() + className.Length() + 2, _name.Get(), _name.Length());
#endif
}
MonoObject* MMethod::Invoke(void* instance, void** params, MonoObject** exception) const
{
PROFILE_CPU_NAMED(*ProfilerName);
return mono_runtime_invoke(_monoMethod, instance, params, exception);
}
MonoObject* MMethod::InvokeVirtual(MonoObject* instance, void** params, MonoObject** exception) const
{
PROFILE_CPU_NAMED(*ProfilerName);
MonoMethod* virtualMethod = mono_object_get_virtual_method(instance, _monoMethod);
return mono_runtime_invoke(virtualMethod, instance, params, exception);
}
@@ -90,7 +102,7 @@ int32 MMethod::GetParametersCount() const
return mono_signature_get_param_count(sig);
}
MType MMethod::GetParameterType(int32 paramIdx)
MType MMethod::GetParameterType(int32 paramIdx) const
{
MonoMethodSignature* sig = mono_method_signature(_monoMethod);
ASSERT_LOW_LAYER(paramIdx >= 0 && paramIdx < (int32)mono_signature_get_param_count(sig));
@@ -99,7 +111,7 @@ MType MMethod::GetParameterType(int32 paramIdx)
return MType(((MonoType**)it)[paramIdx]);
}
bool MMethod::GetParameterIsOut(int32 paramIdx)
bool MMethod::GetParameterIsOut(int32 paramIdx) const
{
MonoMethodSignature* sig = mono_method_signature(_monoMethod);
ASSERT_LOW_LAYER(paramIdx >= 0 && paramIdx < (int32)mono_signature_get_param_count(sig));

View File

@@ -40,6 +40,10 @@ public:
public:
#if COMPILE_WITH_PROFILER
MString ProfilerName;
#endif
#if USE_MONO
/// <summary>
@@ -91,7 +95,6 @@ public:
/// <summary>
/// Gets the method name.
/// </summary>
/// <returns>The name.</returns>
FORCE_INLINE const MString& GetName() const
{
return _name;
@@ -100,7 +103,6 @@ public:
/// <summary>
/// Returns the parent class that this method is contained with.
/// </summary>
/// <returns>The parent class.</returns>
FORCE_INLINE MClass* GetParentClass() const
{
return _parentClass;
@@ -109,13 +111,11 @@ public:
/// <summary>
/// Returns the type of the return value. Returns null if method has no return value.
/// </summary>
/// <returns>Returns method return type</returns>
MType GetReturnType() const;
/// <summary>
/// Returns the number of parameters the method expects.
/// </summary>
/// <returns>The amount of the method parameters.</returns>
int32 GetParametersCount() const;
/// <summary>
@@ -123,19 +123,18 @@ public:
/// </summary>
/// <param name="paramIdx">The parameter type.</param>
/// <returns>The parameter type.</returns>
MType GetParameterType(int32 paramIdx);
MType GetParameterType(int32 paramIdx) const;
/// <summary>
/// Returns the value indicating whenever the method parameter at the specified index is marked as output parameter.
/// </summary>
/// <param name="paramIdx">The parameter type.</param>
/// <returns>True if parameter is marked as output, otherwise false.</returns>
bool GetParameterIsOut(int32 paramIdx);
bool GetParameterIsOut(int32 paramIdx) const;
/// <summary>
/// Gets method visibility in the class.
/// </summary>
/// <returns>The method visibility.</returns>
FORCE_INLINE MVisibility GetVisibility() const
{
return _visibility;
@@ -144,7 +143,6 @@ public:
/// <summary>
/// Returns true if the method doesn't require a class instance.
/// </summary>
/// <returns>True if the method is static, otherwise false.</returns>
FORCE_INLINE bool IsStatic() const
{
return _isStatic != 0;
@@ -155,7 +153,6 @@ public:
/// <summary>
/// Gets the Mono method handle.
/// </summary>
/// <returns>The native Mono method handle.</returns>
FORCE_INLINE MonoMethod* GetNative() const
{
return _monoMethod;

View File

@@ -952,6 +952,8 @@ namespace Flax.Build.Bindings
contents.Append(parameterInfo.Name);
}
CppIncludeFiles.Add("Engine/Profiler/ProfilerCPU.h");
contents.Append(')');
contents.AppendLine();
contents.AppendLine(" {");
@@ -985,6 +987,7 @@ namespace Flax.Build.Bindings
contents.AppendLine(" auto scriptVTable = (MMethod**)managedTypePtr->Script.ScriptVTable;");
contents.AppendLine($" ASSERT(scriptVTable && scriptVTable[{scriptVTableIndex}]);");
contents.AppendLine($" auto method = scriptVTable[{scriptVTableIndex}];");
contents.AppendLine(" PROFILE_CPU_NAMED(*method->ProfilerName);");
contents.AppendLine(" MonoObject* exception = nullptr;");
contents.AppendLine(" IsDuringWrapperCall = true;");
@@ -1228,6 +1231,7 @@ namespace Flax.Build.Bindings
CppIncludeFiles.Add("Engine/Scripting/ManagedCLR/MClass.h");
CppIncludeFiles.Add("Engine/Scripting/ManagedCLR/MEvent.h");
CppIncludeFiles.Add("Engine/Scripting/ManagedCLR/MClass.h");
CppIncludeFiles.Add("Engine/Profiler/ProfilerCPU.h");
contents.Append(" ");
if (eventInfo.IsStatic)
contents.Append("static ");
@@ -1244,6 +1248,7 @@ namespace Flax.Build.Bindings
contents.Append(" if (!mmethod)").AppendLine();
contents.AppendFormat(" mmethod = {1}::TypeInitializer.GetType().ManagedClass->GetMethod(\"Internal_{0}_Invoke\", {2});", eventInfo.Name, classTypeNameNative, paramsCount).AppendLine();
contents.Append(" CHECK(mmethod);").AppendLine();
contents.Append(" PROFILE_CPU_NAMED(*mmethod->ProfilerName);").AppendLine();
contents.Append(" MonoObject* exception = nullptr;").AppendLine();
if (paramsCount == 0)
contents.AppendLine(" void** params = nullptr;");