udo-munk / z80pack

A Zilog Z80 and Intel 8080 systems emulation
MIT License
168 stars 37 forks source link

CPU utilisation is high because CP/M busy-waits on serial I/O (IMSAI) #15

Closed dmcnaugh closed 6 years ago

dmcnaugh commented 6 years ago

I've noticed that CPU utilisation is high even when the simulator is doing nothing but waiting on keyboard input (in running state) so I thought I'd investigate. On OSX running on an i7 its almost 50% , and on Debian on an Intel Atom its 100%, and that is when running the IMSAI machine simulation at only 2MHz. The cause appears to be triggered by the fact that CP/M busy-waits on serial I/O, and the single biggest user of this time is the poll() function call in imsai-sio2.c The poll() seems to be an "expensive" function with respect to CPU utilisation. And it gets called twice for each character. This high CPU utilisation isn't good for battery consumption on any of my laptops (i7 MacBook Pro, or Intel Atom Asus Netbook). I have a proposed solution that I have been using successfully that I will post as a Pull-Request.

dmcnaugh commented 6 years ago

Pull-request #16 reduces the CPU utilisation on my i7 running OSX to below 20%, sometimes as good as 12%. Additionally its a nice effect to lower the baudrate to 9600 and see how these machines really performed. Running WordStar or SuperCalc2 at this speed is an eye-opener.

udo-munk commented 6 years ago

load from my system running CP/M 2.2 idle at the prompt before applying #16 Load Avg: 1.32, 1.38, 1.30 CPU usage: 7.48% user, 10.35% sys, 82.15% id

udo-munk commented 6 years ago

load under same conditions after applying #16 Load Avg: 0.81, 1.11, 1.20 CPU usage: 6.84% user, 4.41% sys, 88.74% idle

Reduces %sys a bit where poll() spends most time

dmcnaugh commented 6 years ago

poll() under OSX must be much less efficient than in your environment, but I saw a similar improvement under Debian on the Intel Atom. What OS/CPU combination are you using? As I said in the note on the PR, its nice just to be able to play with the simulated baudrate in addition to any CPU performance benefits.

udo-munk commented 6 years ago

This is on OSX 10.9.5 MacBook Pro 2.4 GHz Core i5. For Debian and Ubuntu running on Core i7 CPU’s it looks similar. So never saw the problem and playing with the baud rate sure is a nice addition. There will be a small change not using timersub(), that one is not in POSIX.1 and my own time_diff() already eliminates the seconds.

On 03 Jul 2018, at 01:15, dmcnaugh notifications@github.com wrote:

poll() under OSX must be much less efficient than in your environment, but I saw a similar improvement under Debian on the Intel Atom. What OS/CPU combination are you using? As I said in the note on the PR, its nice just to be able to play with the simulated baudrate in addition to any CPU performance benefits.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

udo-munk commented 6 years ago

Ops, doesn't work with time_diff().

udo-munk commented 6 years ago

Fixed, pretty good idea, thanks.