Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Semaphore.WaitForSignal causing performance issues?

Discussion in 'General Graphics' started by Ljaljevic1120, Feb 8, 2020.

  1. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I am making a 2D Android game and the profiler is showing Semaphore.WaitForSignal as the root cause for a high CPU rendering (about 9.7ms), but it shows up a lot.

    What is Semaphore.WaitForSignal?
    What could I do to improve performance?
     
  2. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    149
    I think this means something in your Graphics pipeline is waiting for something else to finish. Usually it means rendering is taking a while to finish, and some other part of the graphics pipeline can't proceed until rendering is complete.

    Try replacing any complex shaders with very simple/unlit shaders, and see if the issue goes away. Make sure you also don't have huge amounts of overdraw. (Eg overlapping transparent textures covering large amounts of the screen)
     
    richardfu71 likes this.
  3. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Thanks for the reply!

    I should mention that this is strictly UI. I know for a fact that the shaders are not the problem (default UI shader), and I've only got five draw calls. My UI elements, however, all have large amounts of transparency.

    I have a feeling that this might be a problem...
     
  4. KokkuHub

    KokkuHub

    Joined:
    Feb 15, 2018
    Posts:
    706
    If the GPU takes way too much of the fram time compared to the CPU, the main thread will have to sit and wait for the current fre to finish rendering before it can start preparing the next one. To confirm if that is the case, lower you rendering resolution to see if the timings change at all.
     
  5. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Thanks for the help!

    But how do I lower the rendering resolution?
     
  6. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,314
    Just a sidenote but: Semaphore.WaitForSignal can't cause performance issues because it's literally just waiting for another thread. So what's causing the issue is on another thread and possibly originates from workload of a previous frame.

    Check out what happens in other threads in parallel using Timeline view and check out the Common Samples section of the CPU Profiler documentation.

    (We're working on improving the UX in the Profiler to guide towards the actual issue)
     
  7. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Thanks for your help!

    This is a sample of my game running idle on a physical Android device. I can't seem to figure out what is halting the thread? Profiler Screen Shot.png
     
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,314
    Right so you're on 2019.3 and using Vulkan? Are you using an SRP?

    Anyways the Semaphore on the main thread gets triggered under a sample that tells you it is waiting for the Render Thread. The interesting samples are the ones on the Render thread that correlate to the beginning and the end of the wait, and a bit too small to read in the screenshot. I must admit that the long pause in between without any sample on the render thread looks suspiciously like a bug to me, at the very least it's missing a profiler marker. So that'd be worth filing a bug report for.

    As to what actually happens, I'd guess, since the Vulkan submit thread isn't doing much of anything, the stall is on the GPU side. I'd test what happens if you change the Application.targetFramerate and QualitySettings.vSync values and try to find a native platform Profiler from the GPU manufacturer of the GPU in your phone (Unity doesn't currently support GPU profiling when using Vulkan).
     
  9. florianpenzkofer

    florianpenzkofer

    Unity Technologies

    Joined:
    Sep 2, 2014
    Posts:
    335
    We are probably waiting for the next Vulkan backbuffer/swapchain image in the large gap.
    Even if that's the case we should improve the profiler output. Feel free to report a bug with repro project.

    If you want to avoid the spikes you can try to enable "Optimized Frame Pacing" in PlayerSettings.
    You can also try to disable Graphics Jobs in PlayerSettings. Typically Multithreaded Rendering without Graphics Jobs is the best compromise on Android, across different games and devices.
     
  10. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Hello,
    I've tried enabling Optimized Frame Pacing and disabling Graphics Jobs in my PlayerSettings, but this only increased VSync in the profiler.
     
  11. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Thanks a lot for the help so far, but I still can't seem to nail down what's happening on the GPU side of things.

    My phone has a Qualcomm Snapdragon 820 in it, so I tried using the Snapdragon Profiler from Qualcomm. But it doesn't provide any useful info about the GPU. I've tried some other things, but they were no help as well. I don't have access to an Android device that can be profiled within Unity...

    Any suggestions? What would you do?

    Thanks, once again.
     
  12. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,314
    You could switch to OpenGL and turn off graphics jobs. That should be profilable with Unity's GPU Profiler. Then again, at this point your profiling different behavior as it's a different graphics Backend.

    Also, before you switch, please consider filing a bug report so that this gap can be investigated and fixed by us.
     
    Last edited: Feb 16, 2020
  13. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Switched to OpenGL and turned off graphics jobs. Not much of a difference, but I did file a bug report before I switched.
    I can't thank you enough for the help! I think it's time to finish this thread here...

    Thanks, once again :)
     
    richardkettlewell likes this.
  14. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    113
    Hi mate good morning do you have a still same problem or did you fixed it if you could you tell me how did you fixed :D
     
  15. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Hello Batuhan13.

    Like I mentioned in my posts above, I've filed a bug report to Unity and they were able to find the bug.
    As far as I know, the bug is still not fixed because I still see it in my profiler.

    Screen Shot.png

    Also, I keep track of the release notes, and I still haven't seen it there.
    What I recommend, from my own experience, is that you try to optimize your game as much as you can, so that it can run as efficiently as possible. Also, make sure to always use the latest version of Unity.

    Not much we can do for now, but I hope this helps. ;)
     
    Batuhan13 likes this.
  16. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    374
    In case it's of use to someone... been struggling with this while profiling a Quest2 app via adb (i.e. running a development build) As soon as I ran a normal build, just on the headset, everything was suddenly running as expected with no spikes (at least no FPS variations that could be seen in the Oculus Debug Tools overlay.)I guess it's the equivalent of the Editor overhead but in a development build with script debugging. (Unity 2019.4.15)
     
  17. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,314
    Yeah, to profile closer to release performance, turn off script debugging.
     
    jeromeWork likes this.
  18. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    166
    Is there any news on this issue and the bug report?

    -Pieter
     
    Game-Savvy likes this.
  19. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,314
    I don't have an issue ID for that bug report so I can't check that report directly and all bugs I can find searching for Optimized Frame Pacing seem closed but maybe @Ljaljevic1120 still has the issue ID handy for us to check. If this is still an issue for you on the latest patch version of the Minor Unity version you are on, it might be best to file a separate bug report on this.
     
  20. dkaloger

    dkaloger

    Joined:
    Jul 7, 2019
    Posts:
    39

    Attached Files:

    Last edited: Apr 18, 2021 at 7:32 PM
  21. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I figured out what was causing the Semaphore.WaitForSignal


    I was running a test build of my Android game to my Samsung smartphone and I wanted to profile the game running on it. To do this I connected my smartphone to my computer via micro USB (charging cable) and ran the profiler.

    I didn't know that there is actually a way to profile your game on an Android device wirelessly rather than connecting your smartphone to your computer with a cable.

    In the build settings of my project, I had an option called "Autoconnected Profiler" turned on. This meant that I was profiling my game wirelessly even though I thought I was profiling it through the micro USB cable.

    The profiler is collecting real-time data through wifi. This was naturally causing large delays (or spikes) in the profiler as Semaphore.WaitForSignal.



    All I had to do is turn off Autoconnected Profiler in the build settings of my project. Then when I profiled my game on my smartphone with a micro USB cable, the Semaphore.WaitForSignal was gone or at least A LOT less noticeable.


    Screen Shot - Build Settings.png


    Hope this helps and thank you to all for offering your time and support!
     
    MartinTilo likes this.
  22. WBonX

    WBonX

    Joined:
    Aug 7, 2018
    Posts:
    11
    Problem is still there, and is there since years, it pops up randomly, my first guess would be it is a bug.
    This is supposed to be an empty scene (unity template, Camera+Light), thus no special shaders in use.

    Can't debug further, I'm on mac, no Vulcal support. Have no access to a Windows machine.
    But searching online seems there is a lot of related posts.
    Unity 2020.02.5f1.
    Tried with and without deep profiling, and with the standalone one, call is always the same, repeated every few ms like in pic.

    upload_2021-4-20_10-25-4.png
     
  23. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    327
    I'm getting the same issue with 2020.3.3f1 HDRP 10.4
    upload_2021-4-20_15-30-29.png

    As you can see that the issue isn't continuous here
     
  24. WBonX

    WBonX

    Joined:
    Aug 7, 2018
    Posts:
    11
    I just noticed it is possible to profile the editor alone (menu is on the top left). There seem to be a spike with a similar profile to the one I posted with the semaphoreWait in the player profiler.

    It could be something editor related, I didn't notice it in the builds.
    For sure the call on the Timeline/Hierarchy should be more informative, this is just very confusing.
     
unityunity