Best Way To Sleep An Empty Server

Discuss programming topics for the various GPL'd game engine sources.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Best Way To Sleep An Empty Server

Post by Baker »

Any ideas on the best way to put an empty server into sleep in a way it uses virtually no CPU ever until someone connects?

[But will still allow typing in the terminal if needed?]

A thought I had to was to put the sockets into blocking mode if no players, so it just waits. But that will prevent typing in the console/terminal ...
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Best Way To Sleep An Empty Server

Post by Spike »

unix? select. add both stdin+sockets to your fd list (use poll or epoll if you have many many sockets).
windows? ahahahaha... select socket+timeout is the easiest way. this can be done 'properly' using WaitForMultipleObjects + WSAEventSelect, but is horribly non-portable.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Best Way To Sleep An Empty Server

Post by Baker »

Spike wrote:unix? select. add both stdin+sockets to your fd list
Sounds a bit different than how I think, but will be fun to setup.

I tend to think of a select as being for sockets, but I know on Unix an fd is an fd.
Spike wrote:WaitForMultipleObjects
I was trying to think of something that would get that effect on non-Windows, actually.

I was watching the CPU for the server loop idle around 3%. I want it to be 0%. :D
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
dimman
Posts: 7
Joined: Wed Jan 29, 2014 1:58 pm

Re: Best Way To Sleep An Empty Server

Post by dimman »

Lol spike, poll/epoll for say 10-20 sockets? Nah simple select does just fine and is way simpler :)

Baker: It's straight forward (in theory atleast). As Spike says, just add stdin fd to the fd set that select listens on (for console input) along with the listen socket. If you get input on console then handle that then back to the select, if player/spec then go into normal mode and when player/spec count drops to 0 back to select?

Quake sucks as inspiration source tbh, making things seem a thousand times more complicates than it really is :)
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Best Way To Sleep An Empty Server

Post by Baker »

Quake sucks as inspiration source tbh, making things seem a thousand times more complicates than it really is :)
Absolutely. :D :D I think in the last 2 years, I've ripped maybe 1/3 of the "Quake" out my engine because I can't stand looking at it. :lol:
dimman wrote:Lol spike, poll/epoll for say 10-20 sockets? Nah simple select does just fine and is way simpler :)
Spike uses provides horizon expanding information and probably thinks in terms of QTV and 1000 connections.

Which is quite fine by me :D Listening to Spike never leaves one without thoughts of the future.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Best Way To Sleep An Empty Server

Post by Spike »

you should always know the best way to implement things.

and the easiest way too. and you should pick the easiest way and ignore the best because frankly the best way is too much like hard work, you'll get bored before its finished, and it'll end up never working properly. if you can leave your options open so that you can switch to the best way when the easiest way demonstrates itself to be non-descript mamalian excrement, you can have the best of both worlds.

I did put some emphasis on 'many' when I mentioned poll/epoll...
just use select. you won't notice any cpu use from polling stdin with a small timeout, and few people bother with dedicated consoles anyway - at least when rcon works properly. you might want to run a frame every now and then anyway so that the server doesn't have huge jumbo frames when a player does finally connect - that sort of thing can really mess with physics (or you could just pause the server, but then that can be weird too, especially if you're depending on the mod's timelimit to prevent Sys_Milliseconds overflows or other sorts of timing precision loss).
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Best Way To Sleep An Empty Server

Post by Baker »

Spike wrote:you should always know the best way to implement things.

and the easiest way too. and you should pick the easiest way and ignore the best because frankly the best way is too much like hard work, you'll get bored before its finished
Sometimes it is a bit irritating that with Quake you usually have to break 17 barrier to do something simple.

I have some fun getting close-like to being done. And I did the hard work already.

Threads, sockets, validation, mechanisms for doing things thread safe by switching to events.

Then you discover stupid stuff. Like 3 or 4 clumps of it. Nothing that will stop you.

But more quirky oddball stuff to deal with. Case in point, what is a thread-safe way to determine if a file is in a pak without assuming any particular gamedir is available since it could change while? First thought, check the precache. Sure, but half that stuff just points into progs strings so isn't like you can expand it and add a field.

(Which in itself wouldn't be thread-safe, but regenerating a list on map load that is thread safe would be.)
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Best Way To Sleep An Empty Server

Post by Spike »

Baker wrote: But more quirky oddball stuff to deal with. Case in point, what is a thread-safe way to determine if a file is in a pak without assuming any particular gamedir is available since it could change while?
Have your main thread sync all other threads, so you're sure that none of them are running when you switch out pak files.
Even if you did do it with mutexes, you still have a race condition over whether the thread read its data before or after the pak changed.
Of course, if you're using fseek+fread to read data from the same file handle on multiple threads, you'll need a mutex there so that another thread can't fseek and mess up your offsets.
It should be noted that NQ just fopens the pak again with a different file handle, in which case you don't need that mutex (because the OS will be doing all of that already anyway). As I already said, this won't solve your race conditions, but it will avoid crashes/corruption.


You'll likely find that GetResource type functions cannot be done without a mutex of some kind (or rather that they can, but only if you're willing to accept dupes).
While you can do many things with atomics, you'll likely find that doing so will just make you pull your hair out. A well placed mutex will save you a lot of pain (a spin lock around a hashtable lookup should be fine, performance-wise - note that windows' critical sections can do some spinning for you so long as the other thread doesn't hold the lock for too many instructions).

You can also use some sort of read/write lock, where multiple readers can all hold a lock simultaneously, but a single writer will lock them all out. These are still awkward to destroy when other threads might want to lock them at any time.

Anyway, the easy option is to just say that a gamedir change is going to invalidate everything loaded from disk anyway, so you might as well sync threads, switch gamedirs, then resume the threads allowing them to flush+reload anything they need.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Best Way To Sleep An Empty Server

Post by Baker »

Spike wrote:Have your main thread sync all other threads, so you're sure that none of them are running when you switch out pak files.
I'm going to have the clients ask for files with the gamedir/ as part of the request, so if a client happens to ask for something after a gamedir change, it will still go fine.

I have my pak reader/writer that can open paks independently of the Quake file system, so Quake doesn't need the pak opened.

Which is fortunate, because trying to make the Quake filesystem thread-safe would be very difficult and painful. :D Not that would have attempted such a thing, but you pointed out a few extra things that would make doing such especially painful. I hadn't thought for fseek issue, for instance.

[And I expect you've probably done exactly that, considering what you said about FTE being able to run multiple maps at the same time.]
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
Post Reply