Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Aggregating CPU Usage Over Frames when Deep Profiling

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

  1. kubanator

    kubanator

    Joined:
    Mar 28, 2020
    Posts:
    3
    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

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    Hello,
    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
    Total
    to
    Self
    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
    Search
    Marker before going into the recursion and possibly also before going into
    TryMove
    and
    GenerateMoves
    , so that their time wouldn't be counted within
    Search
    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

    MartinTilo

    Unity Technologies

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