Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Profiler doesn`t see the thread group I have added to watch

Discussion in 'Scripting' started by unity_85579202F0A2DE16245B, Oct 24, 2023.

  1. unity_85579202F0A2DE16245B

    unity_85579202F0A2DE16245B

    Joined:
    Aug 23, 2023
    Posts:
    3
    I have an assembly without Unity Engine References. I want to monitor threads from that assembly definition in profiler window so, I have the following code in my thread job:

    Code (CSharp):
    1.  public void threadJob()
    2. {
    3.      beginProfileAction?.Invoke();
    4.      // ... thread code
    5.      endProfileAction?.Invoke();
    and in my other assembly (with Unity Engine Reference) I have the code:

    Code (CSharp):
    1. var request = someObject;
    2. var samplerPollState = CustomSampler.Create("GET Request");
    3. state.beginProfileAction = () =>
    4. {
    5.     Profiler.BeginThreadProfiling("Network Thread", "GET request " + request.GetHashCode());
    6.     samplerPollState.Begin();
    7. };
    8. state.endProfileAction = () =>
    9. {
    10.     samplerPollState.End();
    11.     Profiler.EndThreadProfiling();
    12. };
    13. state.call();
    Why can't unity monitor the code?

    After that I put teh begin and end profile action in the assembly that had Unity References Excluded and I have unchecked the exclusion of Unity References. The thread was present in profiler. Is it some Unity limitation?
     
    Last edited: Oct 24, 2023
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,836
    What does that mean? Do you actually start your own thread? Or do you talk about using Unity's job system? Note that
    - Profiler.BeginThreadProfiling.
     
  3. unity_85579202F0A2DE16245B

    unity_85579202F0A2DE16245B

    Joined:
    Aug 23, 2023
    Posts:
    3
    Actually, I start my own Thread. I have several threads that I start by my own and they have lifetime equals to the main thread.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,836
    I had never used the BeginThreadProfiling method before. However I quickly created a background thread, essentially added your code with some dummy Thread.Sleeps in it, removed the request hashcode from the thread name and have the thread run in the background. The thread shows up for me in the thread selection:
    upload_2023-10-26_16-47-24.png

    When I select the thread, I can find "occational" logs since I sleep for 50ms and have "load" for 10ms in between begin and end. Note that when the thread finishes it will disappear from the selection.

    So you should not use a variable name for the thread. The thread name is used to identify it in the thread selection. If you're changing the thread name at every begin, you will have issues picking that thread in the first place. Is your thread actually running in the background? Or do you spawn a new thread for every network request?

    The thread list is quite long:
    upload_2023-10-26_16-53-41.png
    So it might be difficult to actually find your thread. What should help is to switch the Profiler into Timeline mode. That way your thread actually show up under your thread group name:
    upload_2023-10-26_16-58-8.png
     
    MartinTilo likes this.