Search Unity

Thread Profiling

Discussion in 'Scripting' started by wevel, Jun 10, 2018.

  1. wevel

    wevel

    Joined:
    Jan 25, 2015
    Posts:
    1
    TLDR: If you wan to make use of a profiler in a seperate plugin you need to define the scripting symbol ENABLE_PROFILER, but the documentation doesn't mention this.

    This isn't exactly an issue I'm having, but I it took me a long time to figure this out. Basically I'm making a profiler from a thread that I have running, due to how everything is setup the thread is running code from a plugin that has no reference to unity, while the profiler is also a separate plugin, but does have a reference to unity. As part of the code we have:

    Code (CSharp):
    1. public void Init ()
    2. {
    3.     initialised = true;
    4.     Profiler.BeginThreadProfiling (groupName, Thread.CurrentThread.Name);
    5.  
    6.     Debug.Log ("Starting thread profiling: " + Thread.CurrentThread.Name);
    7. }
    However, if we look at how this is decompiled we get:

    Code (CSharp):
    1. public void Init()
    2. {
    3.     this.initialised = true;
    4.     Debug.Log("Starting thread profiling: " + Thread.CurrentThread.Name);
    5. }
    There is a significant line of code that is missing here, the Profiler.BeginThreadProfiling call which is the main thing that the method is doing. There are also similar method calls going missing in other places as well.

    So why is this line missing. Well, this is would took me a while, we can look at the C# reference of unity on github and find that the method is defined as:

    Code (CSharp):
    1. [System.Diagnostics.ConditionalAttribute("ENABLE_PROFILER")]
    2.     [ThreadAndSerializationSafe ()]
    3.     [UnityEngine.Scripting.GeneratedByOldBindingsGeneratorAttribute] // Temporarily necessary for bindings migration
    4.     [System.Runtime.CompilerServices.MethodImplAttribute((System.Runtime.CompilerServices.MethodImplOptions)0x1000)]
    5. extern public static void BeginThreadProfiling (string threadGroupName, string threadName) ;
    So its an external method, but we have the conditional attribute "ENABLE_PROFILER", now this is the first time I have come across this but clearly defining this as a conditional compilation symbol will then allow the call to be made, and sure enough that fixed it and the other missing calls.

    Now this is all well and good, but I had to do some digging around to find this. The documentation has no mention to this, I guess due to the method being fine inside of unity, but really the docs should have something about this to prevent more people just assuming the profiling doesn't work.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    At the bottom of the BeginThreadProfiling documentation, you can report a problem, such as incomplete or unclear documentation.

    The information about "a ConditionalAttribute" is mentioned on the BeginProfiling documentation though. But it's not mentioned what attribute it is, so also unclear.