Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How Unity choose how many parallel job instance to create?

Discussion in 'Entity Component System' started by Pitou22, Jan 26, 2024.

  1. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    69
    Hi,

    I'm currently learning ECS.
    I have a curiosity question: how does Unity choose the number of instance to create for a job?

    I'm profiling an
    IJobEntity
    on an old pc with 3rd gen i5 4 cores @3.1GHz. The profiling is done with a development build. I have constantly 10000 entities for this job launched with
    ScheduleParallel()
    in a system. Batch size is automatic, vsync disabled so I can go above 60 fps.

    Most of the time, there are 4 instances (one for each core, including main thread), but sometime, only 3 instances are created and the main thread is waiting, doing nothing. It results in a small spike in term of frame time.

    4 instances:
    upload_2024-1-26_23-56-38.png

    Next frame, only 3 instances:
    upload_2024-1-26_23-57-51.png

    Why is is not always 4 instances for the job?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,923
    If I remember correctly the Job system (any job system really) doesn't use 100% of all cores all the time, it's typically number of physical cores minus one or two. At least the main thread core remains unoccupied - after all it may need to do "main thread stuff".

    The main thread also does the scheduling of jobs and that needs to be very responsive otherwise you'd be stalling all the other CPUs because the main thread couldn't assign them new work instantly.
     
  3. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    303
    That doesn't seem to be the case here - the main thread should be helping out the worker threads (like in the first screenshot) when JobHandle.Complete() is called.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,319
    There's a mechanism to prevent the main thread from taking on long jobs while waiting, to avoid accidentally taking a big job while waiting on a small job. The mechanism is pretty inconsistent though.
     
    apkdev likes this.