Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 7b73f05
Author: Marcel Wiessler <[email protected]>
Date:   Sun Apr 25 11:22:34 2021 +0200

    lookahead test

commit 77379f6
Author: Marcel Wiessler <[email protected]>
Date:   Sun Apr 25 11:22:23 2021 +0200

    only resolve enabled methods at startup

commit e0cbd3f
Author: Marcel Wiessler <[email protected]>
Date:   Sun Apr 25 11:20:13 2021 +0200

    project versions, remove open project sub, update ptr

commit c4eb8ed
Author: Marcel Wiessler <[email protected]>
Date:   Thu Apr 22 12:15:55 2021 +0200

    minor

commit 2cb8e40
Author: Marcel Wiessler <[email protected]>
Date:   Wed Apr 21 13:22:54 2021 +0200

    replace { } in debug log message when calling LogFormat

commit 90ac4ef
Author: Marcel Wiessler <[email protected]>
Date:   Wed Apr 21 12:35:05 2021 +0200

    test with try catch block, issue is error is caused by nested methods throwing exceptions and being captured by parent

commit 4565e14
Author: Marcel Wiessler <[email protected]>
Date:   Wed Apr 21 12:34:13 2021 +0200

    when debug log is enabled add method name and instruction index to sample info

commit 35a3d7c
Author: Marcel Wiessler <[email protected]>
Date:   Wed Apr 21 10:22:17 2021 +0200

    moved before inject

commit ffb5f0d
Author: Marcel Wiessler <[email protected]>
Date:   Wed Apr 21 10:21:48 2021 +0200

    some cleanup
  • Loading branch information
marwie committed Apr 25, 2021
1 parent d3fd8ac commit 978d8a6
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 57 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "thirdparty/Smart-Hierarchy"]
path = thirdparty/Smart-Hierarchy
url = https://github.com/neon-age/Smart-Hierarchy
[submodule "projects/unity-open-project-1"]
path = projects/unity-open-project-1
url = https://github.com/UnityTechnologies/open-project-1.git
[submodule "projects/BoatAttack"]
path = projects/BoatAttack
url = https://github.com/Unity-Technologies/BoatAttack
Expand Down
66 changes: 33 additions & 33 deletions package/Editor/CodeWrapper/MethodWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,7 @@ public void Apply(MethodBase method, IList<CodeInstruction> instructions, IList<
{
if (start < 0)
start = index;


// search currently loaded stack variables, if any matches
// var found = false;
// var loaded = new StackVariable(inst, index);
// for (var i = 0; i < currentLoadedVars.Count; i++)
// {
// var lv = currentLoadedVars[i];
// if (!lv.Equals(inst)) continue;
// currentLoadedVars[i] = loaded;
// found = true;
// }
// if (!found) currentLoadedVars.Add(loaded);
}
// else if (inst.IsStarg() || inst.IsStloc())
// {
// // for (var i = currentLoadedVars.Count - 1; i >= 0; i--)
// // {
// // var loaded = currentLoadedVars[i];
// // if (loaded.Equals(inst))
// // {
// // }
// // }
// }

var hasBranch = inst.Branches(out var branch);
// if (branch != null)
Expand All @@ -106,8 +83,11 @@ public void Apply(MethodBase method, IList<CodeInstruction> instructions, IList<
start = index + 1;
}

var isMethodCall = inst.opcode == OpCodes.Call || inst.opcode == OpCodes.Callvirt;
if (isMethodCall || inst.opcode == OpCodes.Newobj || inst.opcode == OpCodes.Newarr)
bool IsMethodCall(CodeInstruction instruction) => instruction.opcode == OpCodes.Call || instruction.opcode == OpCodes.Callvirt;
bool IsAllocation(CodeInstruction instruction) => instruction.opcode == OpCodes.Newobj || instruction.opcode == OpCodes.Newarr;
bool ShouldCapture(CodeInstruction instruction) => IsMethodCall(instruction) || IsAllocation(instruction);

if (ShouldCapture(inst))
{
bool IsProfilerMarkerOrSampler(Type type)
{
Expand Down Expand Up @@ -176,7 +156,7 @@ bool MethodIsProfilerMethodOrHasProfilerMethodArgument()

if (start > index && hasLabel) start = prevStart;

if (isMethodCall && exceptionBlockStack > 0)
if (IsMethodCall(inst) && exceptionBlockStack > 0)
{
start = -1;
continue;
Expand All @@ -186,13 +166,28 @@ bool MethodIsProfilerMethodOrHasProfilerMethodArgument()
// if we move the label to the beginning of the loads we might cause wrong branching
// e.g. when a branch jumps over some load/store and we move the label to the beginning of those
if (hasLabel) start = -1;

beforeInject?.Invoke(method, inst, index);
// start = index;

// void LookAheadPotentiallyWrappingStoreResultAndConstrained()
// {
// for(var k = 1; index+k <= instructions.Count; k++)
// {
// var i = index + k;
// if (i >= instructions.Count || ShouldCapture(instructions[i]))
// {
// index = i - 1;
// break;
// }
// }
// }
// LookAheadPotentiallyWrappingStoreResultAndConstrained();


// we arrived at the actual method call
wrapper.Start = index;
wrapper.Start = index;// start <= -1 ? index : start;
wrapper.MethodIndex = index;
beforeInject?.Invoke(method, inst, index);
wrapper.Apply(method, instructions, before, after);
index = wrapper.MethodIndex;
start = -1;
Expand All @@ -203,18 +198,23 @@ bool MethodIsProfilerMethodOrHasProfilerMethodArgument()
{
var prefix = debugLog && method != null ? "<b>Transpiled</b> " + method.FullDescription() + "\n" : string.Empty;
var IL_After = string.Join("\n", instructions);

string SanitizeFormatMessage(string message)
{
return message.Replace("{", string.Empty).Replace("}", string.Empty);
}
if (debugLog && IL_Before?.Length + IL_After.Length > 12000)
{
var msg = "Before " + prefix + IL_Before;
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, msg);
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, SanitizeFormatMessage(msg));
msg = "After " + prefix + IL_After;
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, msg);
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, SanitizeFormatMessage(msg));
}
else if(debugLog)
{
var msg = prefix + IL_Before + "\n\n----\n\n" + IL_After;
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, msg);
}
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, SanitizeFormatMessage(msg));
}

try
{
Expand Down Expand Up @@ -311,4 +311,4 @@ private static bool ShouldSaveIL(bool _debugLog)
Bgt -> Transfers control to a target instruction if the first value is greater than the second value.
*/
}
}
8 changes: 6 additions & 2 deletions package/Editor/Patches/ProfilerSamplePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ internal string GetSampleName(MethodBase currentMethod, CodeInstruction instruct
private void OnBeforeInjectBeginSample(MethodBase currentMethod, CodeInstruction instruction, int index)
{
var parentType = currentMethod.DeclaringType?.Name;
if (SelectiveProfiler.DebugLog)
parentType += "." + currentMethod.Name + "[" + index + "]";

var sampleName = GetSampleName(currentMethod, instruction);

// when using the custom rows patch prefix the sample with the method name
Expand Down Expand Up @@ -136,6 +139,7 @@ private void OnBeforeInjectBeginSample(MethodBase currentMethod, CodeInstruction

private static readonly List<CodeInstruction> InsertBeforeConstant = new List<CodeInstruction>()
{
// new CodeInstruction(OpCodes.Nop,null).WithBlocks(new ExceptionBlock(ExceptionBlockType.BeginExceptionBlock)),
new CodeInstruction(OpCodes.Ldstr, "ReplacedWithProfilerSampleName"),
CodeInstruction.Call(typeof(Profiler), nameof(Profiler.BeginSample), new[] {typeof(string)}),
};
Expand All @@ -144,13 +148,13 @@ private void OnBeforeInjectBeginSample(MethodBase currentMethod, CodeInstruction
{
new CodeInstruction(OpCodes.Ldarg_0), // load "this"
new CodeInstruction(OpCodes.Ldstr, "ReplacedWithProfilerSampleName"),
new CodeInstruction(OpCodes.Nop), // will be replaced by load call to SelectiveProfiler.GetName
new CodeInstruction(OpCodes.Nop), // will be replaced by load call to SelectiveProfiler.GetName
CodeInstruction.Call(typeof(Profiler), nameof(Profiler.BeginSample), new[] {typeof(string)}),
};

private static readonly List<CodeInstruction> InsertAfter = new List<CodeInstruction>()
{
CodeInstruction.Call(typeof(Profiler), nameof(Profiler.EndSample)),
CodeInstruction.Call(typeof(Profiler), nameof(Profiler.EndSample))//.WithBlocks(new ExceptionBlock(ExceptionBlockType.BeginFaultBlock), new ExceptionBlock(ExceptionBlockType.EndExceptionBlock)),
};

// private static LocalBuilder Builder => AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("test"), AssemblyBuilderAccess.Run).DefineDynamicModule("test")
Expand Down
7 changes: 5 additions & 2 deletions package/Editor/SelectiveProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ void HandleDeepProfiling()
{
HandleDeepProfiling();
}
else if(DebugLog) Debug.LogError("Did not enable: " + method + ", " + info.IsActive + ", " + info.Identifier);
else if (DebugLog)
{
Debug.LogError("Did not enable: " + method + ", " + info.IsActive + ", " + info.Identifier + ", hasBody: " + method.HasMethodBody());
}
}
}
return enabled;
Expand Down Expand Up @@ -386,7 +389,7 @@ private static async void ApplyProfiledMethods()
var methodsList = Application.isPlaying ? ml.ToArray() : ml;
foreach (var m in methodsList)
{
if (m.TryResolveMethod(out var info))
if (m.Enabled && m.TryResolveMethod(out var info))
await EnableProfilingAsync(info, false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"com.unity.ide.rider": "3.0.5",
"com.unity.ide.visualstudio": "2.0.7",
"com.unity.ide.vscode": "1.2.3",
"com.unity.test-framework": "1.1.22",
"com.unity.test-framework": "1.1.24",
"com.unity.textmeshpro": "3.0.1",
"com.unity.timeline": "1.4.6",
"com.unity.ugui": "1.0.0",
Expand Down Expand Up @@ -38,7 +38,7 @@
"com.unity.modules.unitywebrequestwww": "1.0.0",
"com.unity.modules.vehicles": "1.0.0",
"com.unity.modules.video": "1.0.0",
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.vr": "1.0.0",
"com.unity.modules.wind": "1.0.0",
"com.unity.modules.xr": "1.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"com.needle.editorpatching": {
"version": "1.1.1-exp",
"version": "1.2.0-exp",
"depth": 1,
"source": "registry",
"dependencies": {},
Expand All @@ -12,7 +12,9 @@
"depth": 0,
"source": "local",
"dependencies": {
"com.needle.editorpatching": "1.1.1-exp"
"com.needle.editorpatching": "1.2.0-exp",
"com.unity.nuget.mono-cecil": "1.10.1-preview.1",
"com.unity.code-analysis": "0.1.2-preview"
}
},
"com.needle.selective-profiling.extras": {
Expand All @@ -21,6 +23,13 @@
"source": "local",
"dependencies": {}
},
"com.unity.code-analysis": {
"version": "0.1.2-preview",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": {
"version": "1.3.9",
"depth": 0,
Expand Down Expand Up @@ -58,8 +67,15 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.10.1-preview.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.22",
"version": "1.1.24",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.3.0f1
m_EditorVersionWithRevision: 2020.3.0f1 (c7b5465681fb)
m_EditorVersion: 2020.3.3f1
m_EditorVersionWithRevision: 2020.3.3f1 (76626098c1c4)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"dependencies": {
"com.av.smart-hierarchy": "file:../../../thirdparty/Smart-Hierarchy",
"com.needle.editorpatching": "file:../../../../../package",
"com.needle.selective-profiling": "file:../../../package",
"com.needle.selective-profiling.extras": "file:../../needle.selective-profiling.extras",
"com.av.smart-hierarchy": "file:../../../thirdparty/Smart-Hierarchy",
"com.unity.collab-proxy": "1.3.9",
"com.unity.ide.rider": "3.0.5",
"com.unity.ide.visualstudio": "2.0.7",
"com.unity.ide.vscode": "1.2.3",
"com.unity.performance.profile-analyzer": "1.0.3",
"com.unity.test-framework": "1.1.22",
"com.unity.test-framework": "1.1.24",
"com.unity.textmeshpro": "3.0.1",
"com.unity.timeline": "1.4.6",
"com.unity.timeline": "1.4.7",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"depth": 0,
"source": "local",
"dependencies": {
"com.needle.editorpatching": "1.1.1-exp"
"com.needle.editorpatching": "1.2.0-exp",
"com.unity.nuget.mono-cecil": "1.10.1-preview.1",
"com.unity.code-analysis": "0.1.2-preview"
}
},
"com.needle.selective-profiling.extras": {
Expand All @@ -26,6 +28,13 @@
"source": "local",
"dependencies": {}
},
"com.unity.code-analysis": {
"version": "0.1.2-preview",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.collab-proxy": {
"version": "1.3.9",
"depth": 0,
Expand Down Expand Up @@ -63,6 +72,13 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.10.1-preview.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.performance.profile-analyzer": {
"version": "1.0.3",
"depth": 0,
Expand All @@ -71,7 +87,7 @@
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.22",
"version": "1.1.24",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -91,7 +107,7 @@
"url": "https://packages.unity.com"
},
"com.unity.timeline": {
"version": "1.4.6",
"version": "1.4.7",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ MonoBehaviour:
- Needle.SelectiveProfiling.ProfilerFrameDataView_Patch
- Needle.SelectiveProfiling.ContextMenuPatches
- Needle.SelectiveProfiling.ProfilerPinning+ProfilerPinning_Patch
- needle.EditorPatching.HarmonyInstanceRegistry
disabledPatchIds:
- MyNamespace.MySlowScript.Update
- UnityEngine.Debug.Log
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.3.1f1
m_EditorVersionWithRevision: 2020.3.1f1 (77a89f25062f)
m_EditorVersion: 2020.3.5f1
m_EditorVersionWithRevision: 2020.3.5f1 (8095aa901b9b)
1 change: 0 additions & 1 deletion projects/unity-open-project-1
Submodule unity-open-project-1 deleted from 4f5a16
2 changes: 1 addition & 1 deletion thirdparty/Smart-Hierarchy
Submodule Smart-Hierarchy updated 187 files

0 comments on commit 978d8a6

Please sign in to comment.