Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer High CPU usage even with server doing nothing.

Discussion in 'Connected Games' started by MrsPiggy, Jun 13, 2018.

  1. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Hi,
    I've seen several topics complaining about different performance issues but the one I am reporting seems different. If not I apologize in advance.

    Working with a simple prototype I've noticed that running the Unity exe as server only (even from command line) causes major CPU usage without doing anything. So I setup a new Project with no assets and just added one game object in the scene with a NetworkManager and NetworkManager HUD component attached to it.

    Then I build and run on an i7 MacBook PRO. The empty scene takes ~13% CPU but as soon as I click on the "LAN Server Only" button the process jumps at ~37-40% usage, while sitting there waiting for a connection.

    The project is built with Unity 2018.1.4f1 under macOS 10.11. I also tested under Windows 8.1 and 10 with even worse results as the machine I was using runs an i3. (process takes 60% CPU doing nothing).

    Am I missing something?
    Is this "normal"?

    Thanks
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,041
    What have you set Application.targetFrameRate to? If you're not aware, on Win/Mac/Linux without setting Application.targetFrameRate it will try to reach the maximum frame rate the hardware can handle, which should be pretty high without having to wait on any graphics processing.

    So I usually set Application.targetFrameRate to 60 or so to start with for a server project.
     
    Ellernate likes this.
  3. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Thanks, I've tried setting the framerate to 60 and even 30 but it doesn't change the situation at all.
    As soon as I hit the "LAN Server" button the CPU jumps to 45% and it goes down when I hit the "Stop Server" button.

    I have no idea why this happens. I hope I am missing something major here, otherwise this is literally unusable.

    Any clues?
     
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    300
    How exactly are you determining CPU usage? CPU monitors can be misleading if you don't know what you're looking for. Here is a copy-paste from `top` of one of my servers right now.

    top - 11:47:08 up 165 days, 10:33, 2 users, load average: 0.05, 0.17, 0.22
    Tasks: 376 total, 1 running, 375 sleeping, 0 stopped, 0 zombie
    Cpu(s): 0.5%us, 0.2%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st <- Mostly idle
    Mem: 16359660k total, 16045920k used, 313740k free, 258036k buffers
    Swap: 18415608k total, 320k used, 18415288k free, 13697660k cached

    PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
    8039 apache 15 0 423m 38m 21m S 62.6 0.2 0:04.42 httpd <- Misleading CPU percentage (likely waiting on sockets)
    8023 apache 15 0 423m 38m 21m S 1.7 0.2 0:06.18 httpd


    Instead of looking at the CPU usage of your application, check how idle the CPU is instead. Once you open a socket, you introduce IO wait. You're also dealing with a listener loop. Both of these things can affect your CPU usage reports.
     
  5. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Hi,
    CPU monitors are far from misleading. If your process takes 60% and you have 6 cores your idle % will be ~90% because the process usage is per single core.

    But that doesn't tell me anything about the % of resources used by the specific process, which is what I am looking for. And it's not out of curiosity but rather because when the server starts it pushes the fans to top speed which is really bizarre for something that is sitting there waiting for connections.

    Not really.
    I am very familiar with sockets and IO loops and there's no universe where a socket waiting for connections is affecting the CPU in any significant capacity unless you're running it on a C64.

    So I am very puzzled as to why this is happening, and I can't see a potential error on my part since the test application is literally made of two components which are bundled in Unity itself and no custom code...

    I am also quite surprised this hasn't been reported before... If anyone has an idea I'd be grateful to hear about it.

    Thanks
     
    Last edited: Jun 15, 2018
  6. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    300
    Well, I’ve seems cpus go high when there completely idle. Top does weird things.

    Anyway, where in your code is the cpu being used? You’re 100% sure it’s not in the listener loop or the event driver loop? Fast moving loops report high cpu.
     
  7. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Sorry, I don't know what you mean by that.

    As I wrote, there is no code.
    I created an empty project, added a single GameObject to the scene with a NetworkManager and NetworkManager HUD.

    Again, don't know what you mean by that, what I am asking here is why does a server doing absolutely nothing is taking a huge amount of CPU resources?

    Try opening a .Net socket, a leave it there to listen for connections. Then go check the CPU usage for that process and see if you can find any...

    Thanks
     
  8. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    300
    There is still plenty of stuff going on. You should be able to profile the application still.

    Sort of. Depends on the type of listener, if it's blocking then yeah, the CPU won't do anything. I highly doubt Unity uses a blocking socket call because it may have to deal with multiple requests at the same time. The likely open a listener, then have a loop that's constantly polling it. Depending on how fast that loop is polling, it can take a significant amount of CPU usage.

    pseudocode:
    Code (csharp):
    1.  
    2. socket = open_socket();
    3. listen(socket);
    4. while(true){
    5.      select(socket);
    6.      if (FD_ISSET(readFds)){
    7.           launchNewConnection();
    8.      } else {
    9.           sleep(10);  //This value will affect CPU usage
    10.      }
    11. }
    12.  
    I've not seen Unity's code, but this is how most servers that handle lots of requests work. How many milliseconds it sleeps for between checks can greatly affect how the CPU usage appears. Lots of times people use the value 1, which doesn't really sleep at all, it simply yields the thread.
     
  9. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    @newjerseyrunner
    I am sure you're trying to help but this isn't going anywhere.

    Speculations as to how Unity might be running the dedicated server are not very useful because:

    a) we have no idea how it works
    b) it makes no sense that an idle server takes 40%+ on an quad core i7 and over 60% of a dual core 3Ghz CPU
    (Imagine what happens when you run the same thing on mid-range mobile phone)

    As a side note, non blocking servers register for events and don't need to run tight expensive loops. There exist a million API to do this efficiently and without wasting resources from low level BSD sockets up to C# and Java.

    I have written a lousy Java server that runs a non blocking socket in a tight loop. It's a terrible approach, considering that you can use selectors that keep a single thread blocked until data is available (thus avoiding to waste any cpu cycle). Even that terrible, home made server takes 5% of the same quad core i7. Not 45%.

    So, yeah.
    Unless I am missing something glaring, something is horribly wrong with how the NetworkManager class is currently implemented.
     
  10. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    286
    You could write a message to Alex @aabramychev. He is active in the forums and very helpful. At least he can forward the question within the UNET team.
     
  11. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,723
    If it's repeatable with such a simple project - file a bug report
     
  12. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Thanks, will do.
     
  13. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Done, thanks
     
    Whippets likes this.
  14. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    545
    for quick fix globalConfig.ThreadAwakeTimeout = 5.
    and use NetworkTransport.Init(globalConfig);
     
  15. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
    Hi,
    thanks for the reply.
    My test example has no code in it, so I am not entirely sure what's the appropriate way to use this snippet.

    Assuming it must be done very early in the player startup I've added an empty object with a script and added the code in the Awake() method.

    Code (CSharp):
    1. private void Awake()
    2. {
    3.     var cfg = new GlobalConfig();
    4.     cfg.ThreadAwakeTimeout = 5;
    5.     NetworkTransport.Init(cfg);
    6. }
    7.  
    This however makes things even worse, causing the CPU to skyrocket to ~76% when I hit the Server button.
    If I comment out the code and re-build and launch it goes back to 45-48% after starting the server.

    Any other ideas?
     
  16. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    9
  17. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    545
    OK if you send/receive a lot it is (probably) expected. As network threads will awake on any message which you will receive. If you obtain that without any traffic on the LAST unity build or path - probably it is a bug, generate report about this. On my computer, without any network traffic, i have only loading from the main unity thread so ~ 0... Without detailed inspection of the project i cannot say more :( sorry about