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

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:

 public void threadJob()
{
     beginProfileAction?.Invoke();
     // ... thread code
     endProfileAction?.Invoke();

and in my other assembly (with Unity Engine Reference) I have the code:

var request = someObject;
var samplerPollState = CustomSampler.Create("GET Request");
state.beginProfileAction = () =>
{
    Profiler.BeginThreadProfiling("Network Thread", "GET request " + request.GetHashCode());
    samplerPollState.Begin();
};
state.endProfileAction = () =>
{
    samplerPollState.End();
    Profiler.EndThreadProfiling();
};
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?

What does that mean? Do you actually start your own thread? Or do you talk about using Unity’s job system? Note that

[quote]
Calling this method on an internal Unity thread (such as main thread, render thread or job system threads) has no effect
[/quote] - Profiler.BeginThreadProfiling.

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.

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:
9432947--1322666--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:
9432947--1322672--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:
9432947--1322675--upload_2023-10-26_16-58-8.png

1 Like