Profiler and worker threads problem with jobs

Hello everyone. I have problems with optymalization my game using jobs.
When i profile my game sometimes jobs that i create are not in work thread but calculated in main thread instead.
Sometimes in one frame my game will use only 1 worker and in other frame will be using 3 workers at the same time. Im testing this on machine that will have minimum requirment so cpu have only 4 cores.
Im sending profile data and here are some screenshots that show examples of my problem.

The second problem i have with physics. I have 50fps physics but on minimal settings in pc i will have 30fps game frame rate, so the physics calculate 2x per frame. Sometimes in profiler Physics.Completion task take so much more to calculate and i dont know why i have this spikes of callculation in game. Some tips where the problem is will be a life saver.

Link to google drive for profiler data:

If you want some code for jobs i can provide but i start job in update frame and try ending it in lateUpdate to give enough time to prcess it.
Tthe RuntimeManager from screenshot its a original file from fmod implementation for sound i dont have there any job code there.

Thanks in advance for all help.

IJobParallelForTransform can be completed by the engine internally when you or the engine access specific transforms. The reason your jobs are running on the main thread is because one of your MonoBehaviours tries to access a transform for a job that has been scheduled. And in this case, the two unused worker threads were both sleeping and didn’t wake up in time to take on the work, so the main thread started doing it. Additionally, all your jobs seems to be single-threaded and have sequential dependencies, so the main thread just did the whole chain.

I can’t comment on physics since I use my own physics engine.

Thanks for the answer.

  1. I dont really know if my job is single threaded, screenshoot doesnt show all, here is example of profiler (the same file other frame) where my job scheduled on 3 workers, thats why i asked about workers, becouse sometimes unity using one worker sometimes 3 or 2 and i dont know why.

So if i understand correclly your response, looking on screenshot that i sended now my spline manager use one transform from object that is in job so job was ended in main thread?

In this case, you have a bunch of single-threaded jobs that don’t all depend on each other, so Unity is allowed to distribute them across different threads. Each thread is likely processing a whole chain of jobs.

That would explain the behavior. I can’t say for certain though without looking at code.

If the app runs fixed at 30 Hz you really needn’t run physics at 50 Hz. Just lower the Physics / Fixed Update timestep in Project Settings => Time to 0.333333 or perhaps even 0.05 (20 Hz).

Do note that this will alter any existing physics behaviour! You may have to retweak values such as velocities and forces applied.

However, if physics isn’t contributing much to the overall frame time it’s probably not worth doing.

I fixed problems with jobs ending on main thread there was independienc with code between monobahaviour i change execution order and that fixed my problem with this.
Thanks @DreamingImLatios

Stil have problem with physics. When PhysX.Completion Task is working its frozing all other workers from be active. And normaly its not a problem becouse this task dont take more than 2ms. But i have spikes and then this task can take up to 15ms and block all other workers, can someone explain it why that hapen?? What can be problem??

@CodeSmile i know i can turn it down but my character caluclation need changes if that settings is diffrent. I would like avoid it and try understand why physics in some frames explodes with calculation. Especially when in one frame i have 2 calculattion of physics and and only one of them have problems with this task.

Im trying understands (and learn more) what can be problems in project with physics.