本文介绍了Unity MLAPI 服务器以 100% CPU 运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用他们的新 MLAPI(中级网络 API)制作 Unity 游戏.我完全按照本教程进行操作,没有做任何更改,并且游戏在我的本地 (Linux) 电脑上运行良好.

I am trying to make a Unity game with their new MLAPI (Mid-level networking API). I've followed this tutorial exactly without changing anything, and the game is running fine on my local (Linux) PC.

我更改了连接 IP 并将构建文件复制到我租用的云服务器(DigitalOcean Ubuntu 20.04),并使用了标志 -mlapi server-batchmode -nographics 选项,但我仍然怀疑它正在尝试在 CPU 上模拟图形.

I changed the connection IP and copied the build file over to a cloud server I rent (DigitalOcean Ubuntu 20.04), and used the flag -mlapi server, and the -batchmode -nographics options, but I still suspect it is trying to emulate graphics on the CPU.

100% CPU 问题似乎是 已记录,建议的解决方案包括 Application.targetFrameRate = 30; 行.我尝试执行以下操作(如果未禁用 vSync,则忽略 targetFrameRate):

The 100% CPU problem seems to be documented and the suggested solution is including the line Application.targetFrameRate = 30;. I tried doing the following (targetFrameRate is ignored if vSync is not disabled):

switch (mlapiValue)
{
    case "server":
        netManager.StartServer();
        // https://docs-multiplayer.unity3d.com/docs/troubleshooting/troubleshooting
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 1;
        break;
    case "host":
        netManager.StartHost();
        break;
    case "client":
        netManager.StartClient();
        break;
}

然而,当我移动客户端时,我仍然获得 100% 的 CPU(当然,每个动作在服务器上立即执行的额外奖励(!?)但在每个客户端上执行 1 秒后).

However, when I move the client, I still get 100% CPU (with of course the additional bonus of each action being executed instantly on the server(!?) but 1 second later on each client).

这里到底发生了什么?网上有人建议这可能与套接字轮询有关,但是当我启动 2 个实例时,其中一个实例被杀死(CPU 耗尽).请注意,单个服务器似乎仍然反应灵敏.

What is even going on here? Someone online suggested it might be related to socket polling, but when I start 2 instances one of them gets killed (out of CPU). Note that the single server still seems pretty responsive.

推荐答案

这个问题似乎只发生在只有 1 个内核的 Linux 机器上.我尝试在单核上将 fps 降低到 1,这确实将 CPU 降低到 5% 以下,但前提是我启动了单个服务器(例如在端口 7778 上).一旦我在端口 7779 上启动第二个服务器,我们就会回到 100% CPU.总结:

This issue only seems to occur on Linux machines with exactly 1 core. I tried reducing fps to 1 on a single-core, which did reduce CPU below 5%, but only if I start a single server (e.g. on port 7778). Once I start a second server on port 7779, we are back at 100% CPU. In summary:

设置结果
1 个核心,60 fps,1 个实例100% CPU(坏)
1 个核心,1 fps,1 个实例
1 个核心,1 fps,2 个实例100% CPU(坏)
2 个核心,60 fps,10+ 个实例

所以我的建议是租一个有 2 个以上 vCPU 的云实例.

So my recommendation is to just rent a cloud instance with 2+ vCPUs.

这篇关于Unity MLAPI 服务器以 100% CPU 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:21