我们公司刚刚推出了一款新的游戏服务器,希望提供租赁服务。然而,游戏开发者并没有创建任何类型的休眠模式来在没有玩家连接时关闭物理,因此空服务器会占用 30% 左右的 CPU。
我找到了这个游戏面板插件 限制应用程序的CPU使用率。
我已经用 C# .NET 为我们公司编写了一些小应用程序,以帮助改进我们的服务,我想知道如何创建这样的应用程序。是否可以?
A new game server just came out which our company would like to offer for rental. However, the game developers did not create any sort of hibernation mode to shut down the physics when no players are connected, so an empty server is eating 30% or so CPU.
I found this game panel addon which limits the CPU usage of Applications.
I have written a few small apps in C# .NET for our company to help improve our services and I am wondering how I would go about creating something like this. Is it possible?
发布评论
评论(4)
您可能会考虑简单地降低进程的优先级。这不会直接限制 CPU,但会导致进程线程的调度频率低于具有正常优先级和更高优先级的进程。
检查
System.Diagnostics.Process.PriorityClass
(文档)You might consider simply lowering the priority of the process down. This won't limit CPU directly, but will cause the processes threads to be scheduled less often than processes with normal and higher priorities.
Check
System.Diagnostics.Process.PriorityClass
(doc)我的猜测是服务器应用程序正在进行轮询而不是事件驱动。轮询将使用CPU,除非这段代码被转换为事件驱动。应用程序将休眠,直到从操作系统接收到需要处理的事件。轮询只会旋转寻找事件并浪费 CPU。除非以任何方式减少 CPU 使用率,否则降低进程的优先级并没有真正的帮助。该应用程序需要重写以提高 CPU 效率。
My guess is that the server app is doing polling instead being event driven. Polling will use CPU unless this piece of code is converted to be event driven. The application will sleep until it receives an event from the OS that it needs to process. Polling will just spin looking for an event and wastes the CPU. Reducing the priority of the process will not really help unless with CPU usage reduction in any way. This app needs to be rewritten to be more CPU efficient.
这个答案可能会有趣你,我也会这么做。
This answer might be interesting for you and that's how I would do it.
我假设游戏服务器是线程化的。如果是这种情况,您也许可以切实地强制应用程序上的 CPU 关联性。如果您有办法判断游戏是否有用户,即 UDP 数据包是否进入指定端口,您可以说“嘿,没有人连接”。然后,您可以让程序将所有工作线程强制到同一核心上。
所以,如果你有一个 8 核 cpu,并且所有线程都在一个核上,那么最多会使用 12.5% 的 cpu。
一旦您看到数据包进入分配的端口,您就可以将关联性分配回所有核心。
您可以更进一步说“是否有任何“闲置”游戏。如果有任何闲置游戏,这些游戏都在……比方说……核心 7,然后以更高的速度运行 HLT 指令的无限循环优先级高于游戏,但强制线程休眠,这样它就不会完全饿死游戏,
会导致 CPU 使用更少的电量,但会做更多的工作,并且出现问题的可能性更高。
这 仅强制亲和力,并让所有闲置游戏共享某个给定的核心。
I'm assuming the game server is threaded. If this is the case, you may be able to pragmatically force CPU affinity on the application. If you had a way to tell if the game had users or not, ie if UDP packets are coming in on the assigned port, you could say "hey, no one is connected". You could then have your program force all working threads onto the same core.
So, if you had an 8 core cpu and all the threads were on one core, then at most it would use 12.5% cpu.
Once you see packets coming in on the assigned port, you could assign the affinity back to all cores.
You could take this a step further and say "Are there any "idle" games. If there are any idle games, which are all on.. lets say.. core 7, then run an infinite loop of the HLT instruction at a higher priority than the game, but force the thread to sleep so it doesn't completely starve the game.
This would cause the CPU to use less power, but would be a lot more work and have a higher chance of problems.
I would stick to forcing affinity only, and just let all the idle games share some given core.