Lower CPU Patch

Lower CPU Patch; support/discussion/questions

Moderator: thunderchero

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Lower CPU Patch

Post by QuasarDonkey » Thu Nov 17, 2011 4:40 pm

BotF Lower CPU Patch

The design of BotF (like most games) causes it to consume 100% of CPU, even when it doesn't need to, e.g. you'll notice your CPU running at 100% even on the opening screen when the mouse is not moving. This patch limits BotF to use a certain percentage of CPU. This patch is more of a hack than anything. Use at your own risk!

:!: Note this patch may impact performance, particularly in tactical combat, especially with a slow CPU or with really heavy mods.
You can check framerate in 3D by pressing T.

But if you're a laptop user with overheating problems, an ecowarrior, or perfectionist, I think you'll like it. I never play BotF without it!

Patch

Just change all these numbers to a number anywhere up to about 10 hex (16). Setting it higher than 10 hex may reduce performance.

Code: Select all

offset
========   ==    ==
0x00182a   00 -> 10
0x00189c   00 -> 10
0x1154ae   01 -> 10
0x115526   10 -> 10
0x11747d   00 -> 10
0x1176d1   00 -> 10
0x1179e8   00 -> 10
Background

BotF uses the Windows API command "sleep" to temporarily suspend execution of the program:
MSDN wrote:Sleep(milliseconds) suspends the execution of the current thread until the time-out interval elapses.
In most cases, BotF issues the sleep command for 0 milliseconds:
MSDN wrote:A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
This patch changes the milliseconds parameter. You can think of this as a framerate limiter.

Edit: see later discussions on this issue -- it does actually reduce framerate.
Last edited by QuasarDonkey on Fri Nov 25, 2011 7:19 am, edited 6 times in total.

User avatar
Tethys
Past Administrator
Past Administrator
Posts: 2065
Joined: Fri Jul 18, 2008 2:00 am
Location: Your mom's bed ;)
Contact:

Re: Lower CPU Patch

Post by Tethys » Thu Nov 17, 2011 4:45 pm

So then if I wanted 50 FPS, I could use 32 instead of 20? Where would I find the location to do this?
Not for the weak of heart...
Image
GALM <--- GALM/Galaxies Mod

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Re: Lower CPU Patch

Post by QuasarDonkey » Thu Nov 17, 2011 4:53 pm

Tethys wrote:So then if I wanted 50 FPS, I could use 32 instead of 20? Where would I find the location to do this?
No, not quite. The higher you set the sleep value (in milliseconds), the lower the framerate: 1000 / time = framerate.

So you want (1000 / time = 50) implying (time = 1000/50 = 20). So you want a hex value of 14 (20 decimal).

You need to set the same value at all the listed offsets.

User avatar
anjel
Past Administrator
Past Administrator
Posts: 666
Joined: Thu May 08, 2008 2:00 am
Location: Bs As - Argentina

Re: Lower CPU Patch

Post by anjel » Thu Nov 17, 2011 4:58 pm

Tethys wrote:So then if I wanted 50 FPS, I could use 32 instead of 20? Where would I find the location to do this?
And why would you need 50fps??? I'm just curious :)

edited

User avatar
Tethys
Past Administrator
Past Administrator
Posts: 2065
Joined: Fri Jul 18, 2008 2:00 am
Location: Your mom's bed ;)
Contact:

Re: Lower CPU Patch

Post by Tethys » Thu Nov 17, 2011 4:58 pm

Thanks :)

Well yes anjel my eyes operate at 60 FPS ;)
Not for the weak of heart...
Image
GALM <--- GALM/Galaxies Mod

User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 2546
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Lower CPU Patch

Post by Flocke » Thu Nov 17, 2011 5:00 pm

depending on os and system, the windows sleep call can be quite inaccurate (and values below 20 might not matter at all)
for low fps it's sufficient but for 3D space combat or egoshooters 30 fps isn't a recommended framerate.
For a smoth experience you should have 60fps or more, that's cause other than movies, the render time of each frame can vary.
If it's below 60fps (=60Hz which was commonly the default monitor frequency, not 50Hz), you might also experience some hiccups.
It's especially noticable on fast movement, often sideways/up/down. I've not tested this with botf, this is just my general experience.
So this patch should be used with care.

For C++ coders: If you want a better 'sleep' accuracy with windows, have a look at waitable timers. ;)

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Re: Lower CPU Patch

Post by QuasarDonkey » Thu Nov 17, 2011 5:14 pm

Flocke wrote:depending on os and system, the windows sleep call can be quite inaccurate (and values below 20 might not matter at all)
I haven't noticed this on any modern systems, but:
MSDN wrote:If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time.
That's why I picked the sleep value of 20h (32) milliseconds.
Flocke wrote:the render time of each frame can vary
This doesn't matter if the system is fast enough enough.

On my 733 MHz machine, the value of 20h works perfectly smooth, even for 3D combat. I've noticed no hiccups at all, even with battles of 40+ ships. And the game always runs at less than 50% CPU for me, so I think it's perfectly safe. If you're really worried, a value of 10h (16) should be fine (but wastes more CPU :x).

Also note that this does not effect turn processing, that still uses 100% CPU (which it should).

User avatar
Tethys
Past Administrator
Past Administrator
Posts: 2065
Joined: Fri Jul 18, 2008 2:00 am
Location: Your mom's bed ;)
Contact:

Re: Lower CPU Patch

Post by Tethys » Thu Nov 17, 2011 5:18 pm

Interesting. Is there any plans to perhaps patch in multi-core support, at least for the turn processing? This may increase speed a little?
Not for the weak of heart...
Image
GALM <--- GALM/Galaxies Mod

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Re: Lower CPU Patch

Post by QuasarDonkey » Thu Nov 17, 2011 5:29 pm

Tethys wrote:Interesting. Is there any plans to perhaps patch in multi-core support, at least for the turn processing? This may increase speed a little?
That's an understatement, it would increase speed a lot. It would be complicated, and I don't know of any plans to do this.

I believe BotF does use multi-threading to a certain extent, for example the sound runs in a separate thread. Also, the game crashed out on me before while I was debugging, but the graphics system was still running and I could still move the mouse.

:?: If you want to test how many cores BotF uses, just run task manager, and start BotF. Play for a bit and quit. Check the task manager's CPU panes, and see how many cores registered CPU usage. It might happen that BotF already uses multi-core processing.

It's pretty odd, but I use Linux and I recently upgraded the wine subsystem (for running Windows software in Linux). Now the game is basically twice as fast -- no more map lag :D. I'm not sure what they did, but it runs faster than it does in Windows for me. If I were to randomly guess, I would say it's graphics related. Maybe BotF/MPR use VSYNC -- disabling VSYNC can often result in 2x speed-up. We could investigate this further.

User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 2546
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: Lower CPU Patch

Post by Flocke » Thu Nov 17, 2011 5:40 pm

did you test with higher resolution models than vanilla botf QD? (on a note, 40 ships is nothing)
with basic vanilla ships I'm not surprised the rendering is fast enough you don't notice rendering variation
I just wanted to note this shouldn't be a general patch, it's useful to decrease cpu usage though. ;)

Btw, BotF uses a second thread for video playback. (and eventually another for rotating cursors)
multicore support for turn processing eventually could be done for the different empires, but I guess it would mean too much work to get it done with old ai. Maybe it could be done by implementing a new ai. :mrgreen:

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Re: Lower CPU Patch

Post by QuasarDonkey » Thu Nov 17, 2011 6:06 pm

Flocke wrote:Btw, BotF uses a second thread for video playback. (and eventually another for rotating cursors)
This explains why I could still move the spinning cursor after one thread crashed out.
Flocke wrote:multicore support for turn processing eventually could be done for the different empires
A very good idea. All the empires should be largely independent, but we'd have to verify there are no shared resources.
Flocke wrote:did you test with higher resolution models than vanilla botf QD?
:? Which mods use high-res models?

We're really debating non-issues here. BotF is a fairly old game, and modern CPUs are very fast. Like I said, even my 12+ year old laptop (which came with Windows 98 installed :D) handles it perfectly with the sleep patch.

To be clear: If the game needs more CPU, it will use it, i.e. for heavy graphics processing, the game might run at near 100% CPU, but for simple GUI stuff if will run at about 20% or less. The game just wastes CPU running faster than the eye can see. If anything, the sleep delay actually means that BotF will run at a more consistent framerate.

Flocke, if you're really pedantic, I could try code up a replacement sleep routine, using variable time delay based on frametime, to guarantee constant framerate. Maybe I'll do it as a future project.

Or you could test the patch before complaining so much :o

KrazeeXXL
BORG Trouble Maker
BORG Trouble Maker
Posts: 2263
Joined: Sat Jan 03, 2009 3:00 am
Location: the 36th Chamber

Re: Lower CPU Patch

Post by KrazeeXXL » Fri Nov 18, 2011 5:42 am

The 100% CPU usage always bugged me! What a major flaw finally solved!

Thank you very much for this QD! Kudos!

In one word: awesome! :D

User avatar
anjel
Past Administrator
Past Administrator
Posts: 666
Joined: Thu May 08, 2008 2:00 am
Location: Bs As - Argentina

Re: Lower CPU Patch

Post by anjel » Fri Nov 18, 2011 7:30 am

i totally agre with krazee "AWESOME"

BTW: Krazee your signature animations always make me laugh :lol:
Live long and propser

User avatar
QuasarDonkey
Code Analyst
Code Analyst
Posts: 433
Joined: Tue Jul 26, 2011 8:29 pm
Location: Ireland

Re: Lower CPU Patch

Post by QuasarDonkey » Fri Nov 18, 2011 11:42 am

Given Flocke's concerns, I think I should note that it could decrease performance, if you have a slow CPU, or you're running a really heavy mod. I have no trouble, but I'd like to hear feedback from anyone who actually tested it and has trouble.

For me: Linux 733 MHz CPU, vanilla/ECM/UM4 work perfectly on standard large galaxy, 40+ ship battles, %CPU ranges from 10-90 depending on what screen you're at / what mod is used.

User avatar
Spocks-cuddly-tribble
Code Master
Code Master
Posts: 723
Joined: Sun Apr 27, 2008 2:00 am

Re: Lower CPU Patch

Post by Spocks-cuddly-tribble » Fri Nov 18, 2011 2:52 pm

Great job as always. It's now on the top of the fixes section in the modding index. :)

QuasarDonkey wrote:Given Flocke's concerns, I think I should note that it could decrease performance, if you have a slow CPU, or you're running a really heavy mod.
Just a shot in the dark, but may be the secret fear behind Flocke's comments is the patch could bring on colds for those BotF players, who're utilising the synergy effect of the CPU bug for home heating in winter^^?
On the verge of a nervous breakdown? Try the relaxing tribble sounds.

Post Reply

Return to “Lower CPU Patch”

Who is online

Users browsing this forum: No registered users