Skip to contentSkip to conversation

Profiling Qemu for speed?

Qemu-devel Joe Luser 6 participants 10 messages Apr 17, 2005 – Apr 18, 2005

lore
Share & tools
lore

Joe Luser

author
#0 lore raw

Hi folks,

I've been using Qemu on the Mac for a few days now; Several OSes running (including Windoze), and I'm impressed. The source looks pretty clean, too.

Has anyone done any profiling work to see where Qemu spends most of its processing time? It is fast running x86 code on the PPC, but I'd like to find ways to speed things up even more.

If you know of areas that will make a big difference in speed, please let me know.

-Nathaniel G H

__________________________________
Do you Yahoo!?
Make Yahoo! your home page
http://www.yahoo.com/r/hs

#1 lore raw
Quoted 10 lines · Joe Luser

Hi folks,

I've been using Qemu on the Mac for a few days now; Several OSes running (including Windoze), and I'm impressed. The source looks pretty clean, too.

Has anyone done any profiling work to see where Qemu spends most of its processing time? It is fast running x86 code on the PPC, but I'd like to find ways to speed things up even more.

Quoted 8 lines

If you know of areas that will make a big difference in speed, please let me know.

-Nathaniel G H

From what I've read the Cirrus SVGA emulation probably deserves some

attention. Read through the archives, there have been some recent discussions.

Beyond that what will always be left is continually tweaking the dynamic code generator with whatever heuristics and host platform specific stuff you can conjure up. Pretty much no end to that sort of thing.

QEMU takes executing machine instructions from one virtual computer and dynamically translates them to the working equivalent on another (the host), stores them in cache to save reprocessing time (that's the *big* time-saving heuristic over Bochs) and executes the dynamically generated code on the host.

Think of it like this: aside from speed, the best generator of dynamic code would be an expert assembly language programmer on the platform you are translating code from and to. As you can imagine QEMU has tricks and leverages GCC but it will never attain the nirvana of being as good as the assembly language programmer. That's an asymptote. So that's why I say there will always be lots of things you can do on the dynamic code generator to speed it up.

One thought would be to have a peephole optimizer that looks back over the just translated basic block (or a state machine that matches such sequences as an on-line algorithm) and match against common, known primitive sequences, and replaces them with optimized versions.

The kind of profiling you would want to do here is to run, say, windows and take a snapshot of the dynamic code cache, and look for common instruction sequences. Ideally, you could write some software to do this automatically.

Anyway, I'm sure there are lots of other ideas laying around.

-- John.

#2 lore raw
Quoted 4 lines · John R. Hogerhuis

One thought would be to have a peephole optimizer that looks back over the just translated basic block (or a state machine that matches such sequences as an on-line algorithm) and match against common, known primitive sequences, and replaces them with optimized versions.

Another thing I've thought about is checking what sequences of instructions often appear in x86 programs (such as e.g. "push %ebp; movl %esp, %ebp") and then creating C-functions which emulate such an antire block, so they can be optimized as a whole by gcc. That would give a similar performance gain on all supported targets, and not just on the one you created the peephole optimizer for (+ less work to debug).

The only possible downside is that you can't jump to a particular instruction in such a block (the same goes for several kinds of peephole optimizations though). I don't know yet how Qemu exactly keeps track of the translations it has already performed, whether it supports multiple existing translations of the same instruction and/or whether it can already automatically invalidate the old block in case it turns out it needs to be splitted and thus re-translated (I guess it should at least some of these things, since it theory an x86 could jump into the middle of an instruction in order to reinterpret the bytes as another instruction stream).

Jonas

#3 lore raw
Quoted 6 lines · Jonas Maebe
Quoted 4 lines · John R. Hogerhuis

One thought would be to have a peephole optimizer that looks back over the just translated basic block (or a state machine that matches such sequences as an on-line algorithm) and match against common, known primitive sequences, and replaces them with optimized versions.

Quoted 7 lines

Another thing I've thought about is checking what sequences of instructions often appear in x86 programs (such as e.g. "push %ebp; movl %esp, %ebp") and then creating C-functions which emulate such an antire block, so they can be optimized as a whole by gcc. That would give a similar performance gain on all supported targets, and not just on the one you created the peephole optimizer for (+ less work to debug).

Unfortunately it's not that simple. The push instruction may cause an exception. Whatever optimizations you apply you've got to make sure that the guest state is still consistent when the exception occurs.

Paul

#4 lore raw
Quoted 3 lines · Paul Brook

Unfortunately it's not that simple. The push instruction may cause an exception. Whatever optimizations you apply you've got to make sure that the guest state is still consistent when the exception occurs.

If we just concatenate the C code of the two procedures, won't gcc take care of that for us? Or could scheduling mess this up? Maybe there's a switch to avoid having it reschedule instructions in a way that side effects happen in a different order? (that would still give us the advantage of CSE and peephole optimizations)

Jonas

#5 lore raw
Quoted 4 lines · Paul Brook

Unfortunately it's not that simple. The push instruction may cause an exception. Whatever optimizations you apply you've got to make sure that the guest state is still consistent when the exception occurs.

I brought this up because I want to speed up the case of x86 code on the PPC. The biggest trouble with optimizing the code generated by Qemu is that the two architectures are as different as they are complex.

I was up until 3:00am studying Qemu, and I came to the conclusion that it doesn't make sense to try speeding up the output code, at least not yet. A peephole optimizer or hand-coded sequences made to handle common combinations of instructions would lead to the problems discussed here:
exceptions happening at the right time, self-modifying code, etc. Worse, the translator might have to spend so much time doing this that the result would actually be slower execution.

I have another idea: The next-best thing to making faster output is to make the same output, faster. In other words, speeding up the translator. Given that the bulk of the translator is in disas_insn() and all the gen_* functions it calls, this seems like a good place to begin.

Does anybody know how GCC generates a "switch" statement? disas_insn() and friends contain dozens of switch statements containing hundreds and hundreds of cases. I'm not familiar with GCC internals, but I do know that compilers I've used in the past actually produce the equivalent of "if.. else if.. else if" code for switch statements. Some compilers produce table-lookups, but only under certain circumstances, like when there aren't too many cases and the numbers being tested fit in one byte. I couldn't find this information on GCC, and unfortunately the code is so complicated that my initial look through it hasn't told me much.

Unless someone can show me that GCC produces table-lookups for the switches in disas_insn(), there's a good opportunity for increased speed by doing this manually. Do you agree with this assessment?

Please let me know if I'm on the right track. :-)

Kind regards,
Nathaniel G H

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

#6 lore raw
Quoted 5 lines · Nathaniel G H

I was up until 3:00am studying Qemu, and I came to the conclusion that it doesn't make sense to try speeding up the output code, at least not yet. A peephole optimizer or hand-coded sequences made to handle common combinations of instructions would lead to the problems discussed here:
exceptions happening at the right time, self-modifying code, etc.

Well yeah... I didn't say it would be easy :-)

Quoted 2 lines

Worse, the translator might have to spend so much time doing this that the result would actually be slower execution.

Not if you do it right. Remember. by and large you only incur a one-time hit, and after that you're going as fast as your dynamic translator is smart.

Here's a heuristic for you: optimize for the general case, not corner cases. You still have to handle the corner cases, but they are corner cases for a reason: you don't run into them as often.

Quoted 7 lines

I have another idea: The next-best thing to making faster output is to make the same output, faster. In other words, speeding up the translator. Given that the bulk of the translator is in disas_insn() and all the gen_* functions it calls, this seems like a good place to begin.

Why would that be faster? Most of the time you only dynamically translate code once. Self modifying code, exceptions, are, well, exceptions.

The lowest hanging fruit right now is probably the cirrus svga emulation. That will probably make a huge, noticeable difference.

Then like I said I think there is always room to make the dynamic code generator smarter.

-- John.

#7 lore raw
In reply to Nathaniel G H

The problem with table lookups (I'm assuming you're talking about function pointer vectors) is that they *destroy* spatial locality of reference that you could otherwise attain by having series of if-then-else instructions and some clever instruction prefetching mechanism on modern processors... Not to mention the function call overhead.

However, for puely aesthetical reasons:
(Disclaimer: I'm not familiar (lack of time, sorry) with what disas_insn does apart from what is obviously implied by its name, so maybe the following may make any sense or not...)

If it happens to be the case that disas_insn decodes the instruction and then proceeds to the native implementation of the opcode in question, if you used the table lookup only to find the function entry point and then paste its CONTENT on a code buffer and then concatenate this with the rest of the translated code, and the whole basic block would be run from there from that part on (caveat self-modifying code), then it would really clean up that piece of code. But I suspect it wouldn't be any faster than the current approach.

Signature · 5 lines
"A year spent in artificial intelligence is enough to make one believe
in God"-Alan J. Perlis


2005/4/17, Nathaniel G H <[email protected]>:
Quoted 5 lines

Unless someone can show me that GCC produces table-lookups for the switches in disas_insn(), there's a good opportunity for increased speed by doing this manually. Do you agree with this assessment?

Please let me know if I'm on the right track. :-)

#8 lore raw

Ideally, we could force gcc to implement switch statements as indirect jumps with jump tables inline with the code. However, this may not be possible.

I think Nathaniel was just saying that gcc is likely generating several hundred sequential if-else blocks for large switch statements.
This gives you O(n) runtime, whereas a function pointer array gives
you O(1) runtime. (This assumes each case ends with a break... I haven't looked at the code to which Nathaniel is referring.) Therefore, for sufficiently large switch statements, sequential if-else statements will be slower than a function pointer array. The question is: is n=~ 200 sufficiently large? I think only empirical testing has a chance of answering this question in everyone's mind.

If you smartly nest your if-else statements, you can at least get O(log n) runtime, with negligible difference in the leading constant as compared to the sequential if-else statements.

There's also the possibility of (at startup) dynamically generating an indirect jump along with a jump table inline with the code (in order to minimize page faults, and perhaps utilize the prefectch to get the first few jump addresses in the L2 cache).

Of course, as pointed out elsewhere, the dynamic code generator is hopefully only invoked rarely, so speeding up the switch statements may not have a noticeable effect on emulation speed.

-Karl

Quoted 6 lines · André Braga

The problem with table lookups (I'm assuming you're talking about function pointer vectors) is that they *destroy* spatial locality of reference that you could otherwise attain by having series of if-then-else instructions and some clever instruction prefetching mechanism on modern processors... Not to mention the function call overhead.

#9 lore raw
In reply to Joe Luser
Quoted 2 lines · Joe Luser

If you know of areas that will make a big difference in speed, please let me know.

IMHO the most effective speedup for most users is to write custom guest device drivers for IDE/network/video that talk directly to qemu using some low overhead mechanism. Basically you want the guest to do as little as possible. The problem is that to be really useful you need to write open source drivers for the common operating systems (win9x, winnt/2k/xp, Linux, *BSD)

Paul