Search Unity

Why is my job not running on worker thread?

Discussion in 'Entity Component System' started by Muhammad_Taha, Oct 10, 2020.

  1. Muhammad_Taha

    Muhammad_Taha

    Joined:
    Jun 21, 2015
    Posts:
    22
    Hi guys,
    I am new to DOTS and that kind of programming. I am scheduling a CollisionJob and simple IJob with a .Schedule() command in OnUpdate function implemented by JobComponentSystem, but it seems the system is running on main thread from profiler(blue bar) with a cost of less or high 8ms. Also, I can't see the Job running like blue bar on main thread or worker thread, but they perform action.
    Currently I can't share screen shot as I was working on office system will share that ASAP.
    Any idea why?

    Edit:
    Profiler Stats
    Screenshot 2020-10-12 at 9.32.24 AM.png
    You can see the Default World Collision System in the main thread.

    The Collision Job Code:
    Code (CSharp):
    1. [BurstCompile]
    2.     struct CollisionFall : ICollisionEventsJobBase
    3.     {
    4.         public ComponentDataFromEntity<BucketComponent> bucket;
    5.         public ComponentDataFromEntity<BlastComponent> component;
    6.         public ComponentDataFromEntity<PhysicsGravityFactor> gravity;
    7.         public ComponentDataFromEntity<PhysicsMassOverride> physicMass;
    8.         public BufferFromEntity<BufferComponent> buffer;
    9.         public void Execute(CollisionEvent collisionEvent)
    10.         {
    11.             Entity entityA = collisionEvent.EntityA;
    12.             Entity entityB = collisionEvent.EntityB;
    13.             if (component.HasComponent(entityA) && bucket.HasComponent(entityB))
    14.             {
    15.                 if (!component[entityA].isBlasted)
    16.                 {
    17.                     physicMass[entityA] = new PhysicsMassOverride
    18.                     {
    19.                         IsKinematic = 0
    20.                     };
    21.                     gravity[entityA] = new PhysicsGravityFactor
    22.                     {
    23.                         Value = 1
    24.                     };
    25.                     BlastComponent blastComponent = component[entityA];
    26.                     blastComponent.isBlasted = true;
    27.                     component[entityA] = blastComponent;
    28.                 }
    29.             }
    30.             if (component.HasComponent(entityB) && bucket.HasComponent(entityA))
    31.             {
    32.                 if (!component[entityB].isBlasted)
    33.                 {
    34.                     physicMass[entityB] = new PhysicsMassOverride
    35.                     {
    36.                         IsKinematic = 0
    37.                     };
    38.                     gravity[entityB] = new PhysicsGravityFactor
    39.                     {
    40.                         Value = 1
    41.                     };
    42.                     component[entityA] = new BlastComponent
    43.                     {
    44.                         isBlasted = true
    45.                     };
    46.                 }
    47.             }
    48.         }
    49.     }
    The Main System
    Code (CSharp):
    1.  protected override JobHandle OnUpdate(JobHandle inputDeps)
    2.     {
    3.         var job = new CollisionFall();
    4.         job.component = GetComponentDataFromEntity<BlastComponent>(false);
    5.         job.bucket = GetComponentDataFromEntity<BucketComponent>(false);
    6.         job.gravity = GetComponentDataFromEntity<PhysicsGravityFactor>(false);
    7.         job.physicMass = GetComponentDataFromEntity<PhysicsMassOverride>(false);
    8.        JobHandle jobHandle = job.Schedule(m_stepPhysicsWorld.Simulation, ref m_BuildPhysicsWorld.PhysicsWorld, inputDeps);
    9.        return jobHandle;
    10. }
     
    Last edited: Oct 12, 2020
  2. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    283
    Are you sure the job is not running on a worker thread? You can try logging the current thread ID (you'll need to temporarily disable Burst):

    Code (CSharp):
    1. int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
    2. UnityEngine.Debug.Log(threadId == 1 ? "Main thread" : $"Worker thread {threadId}");


    If you're getting "Main thread" only, maybe make sure that you have job threads enabled:
     
    Last edited: Oct 14, 2020
    Krajca likes this.
  3. Muhammad_Taha

    Muhammad_Taha

    Joined:
    Jun 21, 2015
    Posts:
    22
    I will check and let you know.