Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Aggregating CPU Usage Over Frames when Deep Profiling

Discussion in 'Editor & General Support' started by kubanator, Dec 1, 2021.

  1. kubanator


    Mar 28, 2020
    I'm trying to figure out what my bottlenecks are on code that runs on a thread separate from the main thread. To do this, I'm using Deep Profiling to see the hierarchy of function calls, and looking at various frames to see where the CPU time is spent.

    Two questions:

    1. Is there a way to gather CPU usages statistics over multiple frames and have them aggregated? I'd like to see where the CPU time is spent over the course of the game, instead of having to check frame by frame.
    2. Is there a way to see how much CPU time is spent in a particular function?

    I have a fair amount of recursion, so my hierarchy is something like:

    Search - CPU time: 100%
    -> GenerateMoves - CPU time: 25%
    -> TryMove - CPU time: 25%
    -> Search - CPU time: 50%
    --> GenerateMoves- CPU time: 10%
    --> TryMove - CPU time: 10%
    --> Search - CPU time: 30%
    ---> GenerateMoves - CPU time: 5%
    ---> TryMove - CPU time: 5%
    ---> Search - CPU time: 20%

    Where as I'd like to see:

    Search - CPU Usage: 20%
    GenerateMoves - CPU Usage: 40%
    TryMove - CPU Usage: 40%

    Thanks in advance for any help!
  2. MartinTilo


    Unity Technologies

    Aug 16, 2017
    The answer to both points is that the Profile Analyzer is here to help you with that. Just like the Profiler itself, it only helps you somewhat with the 2nd point when recursion enters the picture though. Then you'll need to switch the Analysis Type from
    and in deep profiling that will then move a lot of the time out of your three functions and into the leaf calls of your call stack. That's still an interesting way to look at it, but I guess not quite what you asked for.

    What you could also do though would be to manually add ProfilerMarkers to your code. Then you won't need to Deep Profile to see the time taken for these functions and there will be less markers underneath them taking up their self time, when you use the Profile Analyer.

    You could also make sure to emit an .End() sample for the
    Marker before going into the recursion and possibly also before going into
    , so that their time wouldn't be counted within
    marker. But I guess calculating them out is also the easier calculation.

    An additional benefit of using explicit ProfilerMarkers for this is that you can also record these timings directly in the Player via ProfilerRecorders and that you won't need Deep Profiling to get these measurements, which will make them more accurate.

    I'm making a note of the need for an easier way to get statistics for markers in cases of recursion though.
  3. MartinTilo


    Unity Technologies

    Aug 16, 2017
    Actually, I think you might also be able to just use the ProfileAnalyzer in
    Analysis Type : Self
    and just set the
    Name Filter
    filter for
    Search GenerateMoves TryMove