5

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?

pravprab
  • 2,301
  • 3
  • 26
  • 43
Michael Pfiffer
  • 145
  • 3
  • 7
  • Depending on what the game is doing there is always the possibility that slowing it down artificially will just cause lots of events to get backed up or something such that it will basically be unresponsive when you do then want to use it again while it clears the backlog that the throttling has caused. You might be ok, it just strikes me that understanding what it is doing in that time with 30% of the cpu might be valuable. Might be that it is unnecessary nice to have stuff of course. :) – Chris Feb 07 '11 at 17:20
  • Wow, that sounds annoying. If anything, it'll eat up lots of extra power. – Bengie Feb 07 '11 at 18:39

4 Answers4

3

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)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • That seems likely to be what the linked program does, so +1 for this answer. I can't imagine any way to limit the actions of a program internally without reverse-engineering and patching the existing code, which is likely a violation of copyright/EULA depending on your country and the program in question. – Jason Feb 07 '11 at 17:15
  • The game panel we use has the option of running services at a lower priority, however this game seems to use whatever CPU is available. That is why I would prefer to throttle it as opposed to simply lowering the priority. – Michael Pfiffer Feb 07 '11 at 17:15
  • @Michael, if you lower the priority, then it isn't a problem. It will effectively get out of the way when another process wants to use the CPU. Don't let your CPU% gauge fool you. Also, check to make sure there isn't another running process. Some of these game servers spawn other processes. – Brad Feb 07 '11 at 17:22
2

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.

Ravi
  • 383
  • 3
  • 6
1

This answer might be interesting for you and that's how I would do it.

How to restrict the CPU usage a C# program takes?

I don't know if you can do that, but you can change the thread priority of the executing thread via the Priority property. You would set that by:

Thread.CurrentThread.Priority = ThreadPriority.Lowest;

Also, I don't think you really want to cap it. If the machine is otherwise idle, you'd like it to get busy on with the task, right? ThreadPriority helps communicate this to the scheduler.

Community
  • 1
  • 1
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
0

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.

Bengie
  • 1,035
  • 5
  • 10