Skip to contentSkip to conversation

Profiling Qemu for speed?

Qemu-devel Daniel J Guinan 6 participants 7 messages Apr 18, 2005

lore
Share & tools
lore
#0 lore raw

This conversation, below, is very interesting. It is precisely this part of QEMU that fascinates me and potentially holds the most promise for performance gains. I have even imagined using a genetic algorithm to discover optimal block-sizes and instruction re-ordering and whatnot. This could be done in order to generate translation tables of guest instruction sequences and host translated instruction sequences. Even if only a handful of very common sequences were translated in this fashion, the potential speedups are enormous.

Before even discussing the exotic possibilities, however, we need to figure out what is possible within the framework of the current QEMU translation system. Rewiring QEMU to support translating sequences (blocks of instructions) rather than single instructions may or may not be necessary. It should be rather simple to instrument QEMU to keep track of the most common sequences in order to figure out if there are, in fact, sequences that show up with a high enough frequency to make this endeavor worthwhile (I would think the answer would be yes). Then, someone skilled in machine code for the host and guest could take a stab at hand-coding the translation for the most common couple of sequences to see how the performance gains come out.

I would love to see some work in this direction and would be willing to help, although my skills are limited in x86 machine.

-Daniel

Quoted 69 lines

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.

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

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

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

#1 lore raw

There are some code sequences that are quite common, for example compare followed by branch. A threaded decoder tends to look like:

... // do some work
load <instruction>
mask out opcode
address_of_decoder = load decoder_lookup<opcode>
goto *address_of_decoder

but if you say compare and branch are common then possibly

compare_instruction:
... // do some work
load <instruction>
mask out opcode
if opcode == branch then goto branch_decoder
address_of_decoder = load decoder_lookup<opcode>
goto *address_of_decoder

is more optimal, as in the branch case you only have one load. So it seems you can use knowledge of common code sequences to remove one memory access. The overall effect of this is going to come down to the branch prediction hardware too - so a win isn't obvious. If you have longer code sequences then you can use specialization to create a speciailized and optimally laid out decoder.

I'm not sure if you can get GCC to generate code sequences like this, but you probably at least need to use the -fprofile-generate and -fprofile-use options
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

I'm doing this in Java and it appears to be worth upto a 10% speedup in an interpreter. So possibly this would save 10% in the translator.

Sorry if this is obvious. Regards,

Ian Rogers
-- http://www.binarytranslator.org/

Quoted 93 lines · Daniel J Guinan

This conversation, below, is very interesting. It is precisely this part of QEMU that fascinates me and potentially holds the most promise for performance gains. I have even imagined using a genetic algorithm to discover optimal block-sizes and instruction re-ordering and whatnot. This could be done in order to generate translation tables of guest instruction sequences and host translated instruction sequences. Even if only a handful of very common sequences were translated in this fashion, the potential speedups are enormous.

Before even discussing the exotic possibilities, however, we need to
figure out what is possible within the framework of the current QEMU
translation system. Rewiring QEMU to support translating sequences
(blocks of instructions) rather than single instructions may or may not
be necessary. It should be rather simple to instrument QEMU to keep
track of the most common sequences in order to figure out if there are,
in fact, sequences that show up with a high enough frequency to make
this endeavor worthwhile (I would think the answer would be yes). Then, someone skilled in machine code for the host and guest could take a stab at hand-coding the translation for the most common couple of
sequences to see how the performance gains come out.

I would love to see some work in this direction and would be willing to help, although my skills are limited in x86 machine.

-Daniel

Quoted 46 lines

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.

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

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

Quoted 9 lines

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

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#2 lore raw
Quoted 3 lines · Ian Rogers

I'm not sure if you can get GCC to generate code sequences like this, but you probably at least need to use the -fprofile-generate and -fprofile-use options
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Feedback optimisation (FDO) will not work for two reasons:
a) qemu itself is something like a realtime compiler so FDO will only speed up the compiler but not the generated code
b) FDO will only provide speed boosts if the feedback phase has a chance to analyse a representative work pattern that is hopefully also repetitive

After all FDO is mostly about making a tradeoff size/speed and rearranging code (mostly branches) to avoid branch mispredictions of the CPU.

Servus,
Daniel

#3 lore raw

I did months ago gcc/FDO with a xp/lite installation as a "repetitive task" :) I did not improve the timings after all the effort.

Christian

Quoted 30 lines · Daniel Egger
Quoted 4 lines · Ian Rogers

I'm not sure if you can get GCC to generate code sequences like this, but you probably at least need to use the -fprofile-generate and -fprofile-use options
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Feedback optimisation (FDO) will not work for two reasons:
a) qemu itself is something like a realtime compiler so FDO will only speed up the compiler but not the generated code
b) FDO will only provide speed boosts if the feedback phase has a chance to analyse a representative work pattern that is hopefully also repetitive

After all FDO is mostly about making a tradeoff size/speed and rearranging code (mostly branches) to avoid branch mispredictions of the CPU.

Servus,
Daniel

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

Signature · 1 line
Christian
#4 lore raw
Quoted 4 lines · Christian MICHON

I did months ago gcc/FDO with a xp/lite installation as a "repetitive task" :) I did not improve the timings after all the effort.

could this be down to the tables used to find the translators/generators? are they constant? is it possible to make them amenable to feedback directed analysis? x86 is also an odd case, it could be that load-store architectures with more registers notice FDO more as the x86 can access the L1 caches similarly to a large register file. If the problem is gcc's feedback analysis then you could always try Intel's compiler. Regards,

Ian Rogers
-- http://www.binarytranslator.org/

#5 lore raw
In reply to Daniel Egger

Sorry, I was responding to Karl Magdsick's point about the cost of switch statements relating to Nathaniel G.H.'s point about the cost of translation/generation. FDO works in the case of interpreters and translators from my experience as code sequences are pretty predictable things (you just don't tend to choose instructions at random). Improving dynamically generated code is the best way to improve performance, as that's where you spend your time. However, the time spent compiling means you only want to optimise hot spots. Well thought out simple just-in-time compilation is often very hard to beat with an optimising compiler, for example, knowing your target architecture well means you can get a really good static register mapping to the host architecture. These lessons are well known in the literature. I'd still be interested to know if the translator's profiled performance improved using FDO. I have results from writing things in Java, where FDO is par for the course, but they hardly apply to QEMU/GCC.

Regards,

Ian Rogers
-- http://www.binarytranslator.org/

Quoted 20 lines · Daniel Egger
Quoted 3 lines · Ian Rogers

I'm not sure if you can get GCC to generate code sequences like this, but you probably at least need to use the -fprofile-generate and -fprofile-use options
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Feedback optimisation (FDO) will not work for two reasons:
a) qemu itself is something like a realtime compiler so FDO will only speed up the compiler but not the generated code
b) FDO will only provide speed boosts if the feedback phase has a chance to analyse a representative work pattern that is hopefully also repetitive

After all FDO is mostly about making a tradeoff size/speed and rearranging code (mostly branches) to avoid branch mispredictions of the CPU.

Servus,
Daniel

#6 lore raw
In reply to Daniel Egger
Quoted 13 lines · Daniel Egger
Quoted 4 lines · Ian Rogers

I'm not sure if you can get GCC to generate code sequences like this, but you probably at least need to use the -fprofile-generate and -fprofile-use options
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Feedback optimisation (FDO) will not work for two reasons:
a) qemu itself is something like a realtime compiler so FDO will only speed up the compiler but not the generated code
b) FDO will only provide speed boosts if the feedback phase has a chance to analyse a representative work pattern that is hopefully also repetitive

Quoted 3 lines

After all FDO is mostly about making a tradeoff size/speed and rearranging code (mostly branches) to avoid branch mispredictions of the CPU.

IMHO Profile feedback (in fact any compiler optimizations) are unlikely to provide any benefit for qemu itself.

However profile feedback can be useful once we start making the dynamic translator smarter. In particular profile information can be used to determine how hard to try optimizing the generated code. For code that is executed rarely we want to generate it as quickly as possible, for code that is executed may times we can afford the extra overhead to make the resulting code faster. I've seen measuremets that indicate most code is either executed once, or is executed many many times.

With current qemu it doesn't make all that much difference as we only have the quick-and-dumb code generator.

Paul