Asset - Extension CPU Mercy - Lower CPU usage [100% to 10%]


Hi everyone,

I was working on my dedicated servers for my game, and noticed that (mainly on Linux) the game seems to use an entire CPU core, 100%, no matter the FPS.

The FPS real was about 2300, the FPS was 60/60, yet the CPU usage was 100% on a single core machine. 100% on 1 core on a quadcore, and on my i7 7700k Windows machine I had 14% usage on a single room with a single instance.

In order to fix this, I wrote a sleep extension to natively (and cpu-safely) sleep the thread.

This extension calculates the required frame time, and calculates how much time it took to process the current frame. The difference is the amount the thread should wait (until the next frame). On my windows machine on the example file it dropped CPU usage from 14% (1 core) to 0.9%. The example file does a random amount of calculations to make the FPS fluctuate - in an actual empty room without those calculations the difference is much bigger.

On my real game server, it dropped usage from 100% to 10-14%.

Please note that profiling will not work right with this extension enabled (the waiting time is also shown in the profiler, giving weird numbers).

So far it only supports Windows and Linux but the source is out there, so feel free to add more support :)

- dscpu_init() //Start the system
- dscpu_active() //Check if its active
- dscpu_destroy() //Stop the system
- dscpu_set_debug() //Toggle debug information
- dscpu_set_grace() //A small grace period that always gets deducted from the calculated time
- dscpu_set_tightness() //Simple multiplier on the calculated time. Usually 0.9. This is to keep the few "clock ticks" before the next frame free from waiting, so you won't have 58 - 59 fps instead of a solid 60. The higher the better!

This uses DSSleep:

GitHub / Source: