• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

WINDOWS SLEEP MARGIN EXPLAINED

R

renex

Guest
I've seen way too many people asking about this and recently even got a dm asking about it. So here's a rant explaining windows sleep margin...



Say your room speed is 60. What happens is that each frame should be spaced by roughly 16 milliseconds. When the game is done rendering the frame, it sleeps for the rest of the time - so if it took the game 8 ms to render, it sleeps for 8.

This causes a problem - if it sleeps too little, the next frame comes out too early. If it sleeps too much, next frame will be late.

The solution to this problem was the sleep margin - what happens is that the game sleeps for frame time minus sleep margin, and then sits on an infinite loop waiting for the perfect time to send the frame in.

So for example if your sleep margin is 1 and rendering took 8 ms, the game will sleep for 7 ms and then actively wait during the last 1 ms to sync perfectly.

upload_2017-1-29_5-9-31.png

Now you can probably guess why the default used to be set to 10 - it means whatever time is not spent on the game will be spent in an infinite loop. This maxes out your one core. Now, this increases fps because your cpu then detects high usage and jumps to a higher profile, because modern cpus actually slow down when they're not being used fully. So by artificially using more cpu you are causing your computer to speed up in an attempt to perform better. This is why you should not trust fps_real for benchmarks - if you need to test something like that, set room speed to 999 and use the actual fps as a reference.

It is general community consensus that the sleep margin should be left at 10 for Windows targets - performance on power-saving-heavy machines is sketchy otherwise.

upload_2017-1-29_17-53-32.png
 
Last edited:
L

Lonewolff

Guest
If that explanation is correct then the game loop in GMS is extremely badly implemented.

This would also open up a huge can of worms when vSync is enabled as the video card will be looking after this 'sleep' period via hardware as well. Two sets of 'sleep' periods, which will effective halve your 60 FPS framerate due to sync issues.

This explanation begs more questions than answers. You are saying by default, that GMS is incapable of rendering more than 1000 FPS because this default sleep margin on 1ms will mean that it is impossible to exceed this framerate.

Set room_speed to 9999 and measure FPS on an empty room. I guarantee that you'll be getting framerates far beyond 1000 'actual' FPS. ~6000 FPS on my system.

How is this possible if there is a 1ms sleep period going on?

There has got to be more to this 'sleep margin' thing than your explanation lets on.
 
R

renex

Guest
How is this possible if there is a 1ms sleep period going on?
Using a 0ms sleep margin. And the margin isn't spent if the game loop is using all the time. As I outlined on the schemes above, idle sleep and sleep margin are only used for the remainder of the step time. if the remainder is negative then the next frame starts immediately.

This is just a general guide though - I don't know the specific underlying mechanics. I just know how it works and how to use it.

This would also open up a huge can of worms when vSync is enabled as the video card will be looking after this 'sleep' period via hardware as well.
GM has a two frame lag at all times, and alternate sync makes that 3. Probably related.
 
L

Lonewolff

Guest
Using a 0ms sleep margin.

This is just a general guide though - I don't know the specific underlying mechanics. I just know how it works and how to use it.
If the default is 1ms (which it is) and unadjusted sleep margin can still achieve many thousand FPS.

Also, the recommendation by staff is to set this to 10ms when a game is choppy and by all reports 'fixes the problem'.

One example;

https://forum.yoyogames.com/index.php?threads/game-runs-at-half-framerate.18235/#post-117079

I can't understand how capping a framerate to a possible maximum 100 FPS can improve performance.
 

RangerX

Member
Its cool its 1 instead of 10 like before but I struggle to find good source as to "WHY" its better that way.

- When I set my game to 1, I encountered 2-3 computers with windows 10 in my surroundings that were getting a slow FPS (between 40 and 55 depending the computer). If I get 2-3 persons like that in my surroundings, just imagine the possible number in the world!!!

- So lately I did set back my game to 10. The people in my surroundings don't have FPS problems anymore but there is still a poster on this forum that had some.



So now I am wondering. What is truly the best setting? Why would it 1 and not 10 or vice-versa? Should it even be something else? Why GMS can't simply work 60fps fine on all compatible OSs ???
I struggle to decide what to do. I don't want to make a product "that mostly work".


:confused:
 
Last edited:
L

Lonewolff

Guest
It's not capping. Please read my edits.
Obviously 'my bad'. I must admit, I don't have a habit of re-reading threads from the start to see if something might have changed :p

So now I am wondering. What is truly the best setting? Why would it 1 and not 10 or vice-versa? Should it even be something else? Why GMS can't simply work 60fps fine on all compatible OSs ???
I struggle to decide what to do. I don't want to make a product "that mostly work".


:confused:
Exactly this! Why is 'sleep margin' even a thing?
 
Last edited by a moderator:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
How is this possible if there is a 1ms sleep period going on?
Sleep margin defines the minimum amount of time that the game has to wait before it switches from idle loop to sleep() function.
Idle loop is warranted to be precise, but uses CPU.
Sleep() function does not use CPU, but precision depends on many factors, and it has additional caveats.

As it was mentioned, on select systems excessive use of Sleep() results in OS crippling the application (supposedly because it doesn't look power-intensive to it). Mysteriously enough, opening a video player, browser, or an additional game returns things to normal.

Overall it is hard to pick the right value for the setting, and I stand by my opinion that it should have been a function instead (so that an option could be added in-game or automatic balancing could be performed).
 

Mike

nobody important
GMC Elder
It really isn't that complicated guys...

if you set the FPS to 60 and the game runs much faster, there's an amount of time it has to wait for the next frame. Normally you want to issue the OS "Sleep()" command for the time remaining so the OS gets that time back and can save power, battery and run other things.

However... Sleep() isn't very accurate (to say the least - on windows at least). So this value lets you tell us the minimum value it'll ever try to sleep for - in ms. If the time left is >1ms (if that's whats set in the options) then it'll try to use a sleep() for that amount of time. This gives the maximum time back to the OS,

However....If it spends most of it's time sleeping, with Power Saving etc being what it is these days, windows suddenly thinks the machine isn't that busy and so starts to power down the CPU and graphics card. By raising this number (to 10 for example), this means it'll spend more of it's time in a tight loop waiting for the next frame. This will give a more accurate timing, but will also burn more CPU time and power. This will satisfy Power Saving (as you're machine is now doing something most of the time), but on machines with batteries - laptops and mobiles, this can cause fans to come on, and power to drain quicker than normal. This is why the value was made user controllable. The higher the number, the more time it'll spend spinning and waiting, but the more accurate it'll be.

We will be looking to add more ways of syncing in, but frame syncing is not a simple issue, especially cross platform.


So... for the record. Sleep() is great if its reliable, but in general (on windows) anything less than 15ms is unreliable and can cause longer or shorter sleep times.
Spinning around a loop and waiting for the time to pass is much more accurate but burns battery and powers up fans.

In today's modern computer it's a battle between one or the other. Personally...I set this to 10 or 15 (for desktop) and just let it spin. I figure you're playing a game so expect higher usage than normal - but it's up to you, set this value to what works for you.
 

RangerX

Member
Ok so I understand I should put it to 15 since I am making a windows game.
'Cause really, my concern is that I just want the less trouble possible for anyone to keep a rock solid 60fps. Their PCs are strong enough :p
 
N

NPT

Guest
Overall it is hard to pick the right value for the setting, and I stand by my opinion that it should have been a function instead (so that an option could be added in-game or automatic balancing could be performed).
This.

On my machine a 1ms sleep margin works just fine.

But, we know that there are many machines in the wild that simply won't work with a sleep marging of 1 ms. Therefore any game released to the public has to be set to 10 (what used to be the unexposed default).

By exposing this option to be set programatically, apps can be released that allow the end-user to optimise performance in an options screen.

As it stands now, one would be foolish to release a (Windows) app with any setting less than 10.
 
Last edited by a moderator:

chance

predictably random
Forum Staff
Moderator
Personally...I set this to 10 or 15 (for desktop) and just let it spin. I figure you're playing a game so expect higher usage than normal - but it's up to you, set this value to what works for you.
This seems to be the consensus coming from a few different YYG staff. I seem to remember Noc and Russell mentioning this. And I'm glad they did, because it caught me off guard when my games suddenly started sputtering after a Studio 1 version upgrade (with a default sleep margin changed to 1).

I'm curious why the default value was changed in the latest Studio 1 and 2 versions. Seems like it'd be safer to leave it at 10, and give users an option to change it.

EDIT: clarity
 
R

renex

Guest
Thanks for the explanations. I'm going to edit the op with the new information.
 
Last edited:
M

Mobie

Guest
Does Windows 10 have any special issues with sleep margin? On my Windows 10 laptop and on our networked, Windows 10 lab computers at the school where I work, my games need a sleep margin of 15 to 25 to run smoothly.

Sorry for the thread necromancy here. This one has been dead for awhile.
 
Last edited by a moderator:

Tornado

Member
I struggle to understand this in the original posting:
"what happens is that the game sleeps for frame time minus sleep margin..."

shouldn't it be:
"what happens is that the game sleeps for THE REST OF THE frame time minus sleep margin..." ?
 
R

renex

Guest
I struggle to understand this in the original posting:
"what happens is that the game sleeps for frame time minus sleep margin..."

shouldn't it be:
"what happens is that the game sleeps for THE REST OF THE frame time minus sleep margin..." ?
thats the same thing

Can you talk about this? (or point me towards reading about it) I'm interested in having the lowest input lag possible.
i've had issues with this recently and after some investigation it appears to have been a problem ever since classic game maker. the game takes two frames to update its internal keyboard states, and as such it's impossible to get frame-accurate inputs unless you use an input dll. this isnt dependant on how you strucutre your code either, it really takes two frames to do it for some insane reason.
 

hogwater

Member
thats the same thing


i've had issues with this recently and after some investigation it appears to have been a problem ever since classic game maker. the game takes two frames to update its internal keyboard states, and as such it's impossible to get frame-accurate inputs unless you use an input dll. this isnt dependant on how you strucutre your code either, it really takes two frames to do it for some insane reason.
How did you test/check this? While two frames is a big deal, it depends on the total input "lag." For example certain arcade games (i believe Super Turbo is one) are known to have 4 frames of delay between pressing a button and the game displaying the result. So even "lagless" setups have a delay.
 

GMWolf

aka fel666
Its quite common to have a couple frames of input lag in games.
From what i can gather its due to the syncing of the polling thread and the main game thread. But dont quote me on that...
 
Top