Search Unity

Bug iOS Performance issue. Background thread runs 4x slower at 30fps compared to at 29 fps.

Discussion in 'Editor & General Support' started by Peter_chatain, Nov 27, 2023.

  1. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    I made a small testing app and realized that Unity interferes with all asynchronous background tasks and makes them run 4x slower. Unity no longer interferes with background tasks when I set application.targetFrameRate to 29, but then the animation fps drops to 20 which is not ideal. The reason this is a problem is that I originally noticed that my speech recognition model runs 4x slower after running quickly for a few seconds, but it runs perfectly fine at 29 fps cap. I control the fps cap with
    Code (CSharp):
    1. Application.targetFrameRate = 29;
    .

    Why is the performance for the app characteristically different between 29 and 30 target fps? What's causing the significant interference with background CPU jobs?

    This is really easy to re-produce on older iPads. Here are the steps:
    1. Use the latest stable release of Unity (2022.3 in my case).
    2. Simply select the template 2D app with either URP or default rendering pipeline.
    3. Add a single square game object. Attach the `StartupScript.cs` to this cube, which spawns a busy loop thread with timing code. This script is an attached file here.
    4. When building the app, switch the project to iOS. Run the app on any iPads with an A8x or A10 chip. These correspond to ipad 6 or 7th gen and earlier. This problem does not exist on the latest A15 and later. A11, A12, A13, A14 not tested.
    5. Notice in the xcode console log that it runs fast (~0.2seconds) for about 4 seconds, and then it runs 3-5x slower (around 0.9 seconds) with a lot of indeterministic variance.
    The busy work is this, but any busy work will do:
    Code (CSharp):
    1. for (var i = 0; i < 100000000; i++)
    2.         {
    3.             if (i % 2 == 0) {
    4.                 sum *= i;
    5.             } else if (i % 3 == 0){
    6.                 sum /= i / 3;
    7.             } else {
    8.                 sum += i;
    9.             }
    10.         }
    On it's own, this takes about 0.2s to complete. When Unity interferes with it somehow, it takes 0.9s, the same exact slow down observed with my speech recognition model.

    A natural question is "ah, maybe the thread isn't getting time on the CPU due to limited resources," but this isn't true. The slow performance is all despite having ample resources for the thread. From profiling in Xcode and Unity on A8x which has 3 cores, at any given time 2 cores are mostly idle while the one worker thread works. That worker thread get’s about 95% of the time on a core when the target frame rate is 29, while about 85% when it’s 30. In both cases, the worked thread is preempted ~700 times per second, except in the slow case these preempts last 10x longer on average. I can discuss the profiling results in more detail if people think that's relevant.

    This thread was originally posted at https://forum.unity.com/threads/per...lower-at-30fps-compared-to-at-29-fps.1519810/ but I couldn't figure out how to move that thread to a more appropriate section.
     

    Attached Files:

  2. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    Here's a plot of the speed of the busy work over time
     

    Attached Files:

  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    The difference is between 20 and 30 fps, as you noted. That is 66% vs 100%. There's about 33% more CPU time available for background tasks. Perhaps it's as simple as that? Yes, there are multiple threads but the main thread may still be the bottleneck and you say it's at 95% so practically not available at a moment's notice.

    Could you share how and where you start these asynchronous background tasks, and how they synchronize with the main thread? Are these async/await or System.Thread or Jobs or ... ?
     
  4. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    Thanks for the reply @CodeSmile ! Yes the exact file with everything needed for re-production is an attachment called StartupScript.cs in the post above. The device is certainly not bound by being out of CPU resources. It is at 95% of one core, meaning that total resource utilization is ⅓ or roughly 30%. When I run larger workloads I see numbers closer to 280% utilization of the 3 cores.

    There are no waits.
     
  5. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    Some further notes:
    1. On Unity 2022.3, you actually have to set the target fps to <= 23 in order for the realtime fps to change to 20. If you set it to 24,25,26,27,28,29 the realtime fps will revert to 30.
    2. In Unity 2022.2, if you set the target fps to anything in [29-18], the realtime fps will get pinned to 20.
    3. When I pause the app in the xcode debugger and then hit run again, the background work runs quickly for a few seconds before getting 4x slower again.
    I measure the realtime fps with
    Code (CSharp):
    1. private string _lastFpsText;
    2. private void OnGUI()
    3. {
    4.     if (Time.frameCount % 10 == 0)
    5.     {
    6.         _lastFpsText = $"Realtime FPS: {1 / Time.deltaTime}";
    7.     }
    8.  
    9.     GUI.Label(new Rect(20, 20, 1000, 50), _lastFpsText);
    10. }
    Below is a screenshot of profiling the app in Unity. You can see that for a few seconds on the left it runs fast, and then when the green spikes show up things run slow.

    Screenshot 2023-11-28 at 2.16.30 PM.png


    The main function that is being called is WaitForTargetFPS. In the case when the real FPS is 20, the profiling results look like this:
    Screenshot 2023-11-28 at 2.19.59 PM.png
     
    Last edited: Nov 28, 2023
  6. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    Related stack overflow threads with apparently the same performance characteristics as this problem (the fast then slow, and the fast again when re-started in the debugger):
    1. https://stackoverflow.com/questions...conds-speeds-back-up-if-paused-and-resumed-in
    2. https://stackoverflow.com/questions/52727578/ios-11-cpu-throttling-and-idle-timer
    In neither case were they able to figure out how to fix this. #1 found that by reducing the amount of updates from 60 fps to only when needed, they were able to fix it.

    One might wonder, "is this also true for apps built purely in swift?" And the answer appears to be no. I was able to run the speech model at 55 fps in an app written in Swift where I verified the realtime FPS in the XCode profiler. The difference between 20 fps and 55 fps in the xcode app is roughly a 20% slower speech recognition model, which is not much compared to a 400% slow down.
     
    Last edited: Nov 30, 2023
  7. huonguvw

    huonguvw

    Joined:
    Aug 30, 2023
    Posts:
    1
    Hi,
    I have some thought on this:
    +Some CPU heating task like your background task( without idle) is very stressful for multi-threading design of OS thread pooling.
    +The fact behind multi-thread design of OS is scheduling all thread task to hardware core. If a task which cannot be interrupted like just count 1->infinity requires dedicated core to avoid scheduling throttling randomly.
    All task which takes long time "should" be performed in lazy way.

    I wonder what your actual background task is in details and can it run without throttling cpu?
     
  8. Peter_chatain

    Peter_chatain

    Joined:
    Nov 16, 2023
    Posts:
    8
    Hi @huonguvw , thanks for the thoughts.

    1. When I run the task with a slightly lower frame rate of 20 fps, that same intensive task runs quickly.
    2. When measuring in the profile, there are no heating events and temperature remains nominal. Furthermore, at any point while running the background work, if I pause the app for 0.5 seconds and start it up again the busy work runs fast again for 5-10 seconds.


    When I run the full speech recognition workload in an iOS app with FPS 60, or a unity app with FPS <= 20, the speech model utilizes 250% out of 3 cores without any breaks, and the temperature of the device stays nominal and does not overheat. Also the profiler says that the speech model is not being throttled, and it's getting the expected amount of time "running" on the CPU.
     
  9. ssedlmayr

    ssedlmayr

    Joined:
    Feb 28, 2013
    Posts:
    36
    This has peaked my interest and I want to take a look at this; I also responded on LinkedIn. A couple of things jump out right away from your script, which may or may not have anything to do with the root cause or the solution, but just brainstorming:
    1. Believe it or not, that Debug.Log statement could be a contributing factor. Debug.Log statements and debug builds in general unfortunately can have a rather large impact on performance by themselves.
    2. I see you have some commented out UnityEngine.Random.Range code in there... a lot of the Unity stuff is not thread safe, I assume you tried that out and it was even worse, and hence the commenting?
    3. Coroutines or async tasks might be a more performant way to do things in Unity; they are used quite frequently for this very reason.
    4. I find it interesting that it is render that appears to be taking up the majority of your performance; all render takes place in the main thread in Unity, seems like it is being blocked even though you've opened up a new thread, perhaps because this script is on a GameObject in the scene. Does it need to be? Have you tried a ScriptableObject, or instantiating an object completely outside of the scene graph that performs this work?
    This thread is kind of old, but still has a lot of relevant stuff in it in 2023 as far as Unity best practices: https://gamedev.stackexchange.com/questions/113096/how-to-not-freeze-the-main-thread-in-unity
     
    Last edited: Dec 23, 2023
  10. ssedlmayr

    ssedlmayr

    Joined:
    Feb 28, 2013
    Posts:
    36
    So I unfortunately don't have a modern enough MacOS machine or iOS device so cannot build this for iOS, but even just in the Unity editor I notice some things right away pasting your script onto a 2D Square sprite:
    • The thread isn't being shutdown properly when the apps exits, so it keeps running and logging to my debug log even after I've stopped the app.
    • The sum never increases beyond 10.
    • Performance drops from ~1000 fps to ~250 fps; I have to do an apples-to-oranges comparison since I'm not set up for iOS, but a 75% performance hit is pretty huge.
     
  11. ssedlmayr

    ssedlmayr

    Joined:
    Feb 28, 2013
    Posts:
    36