News for hackers. Stuff that matters.
Re:The Evolution of the QEMU Translator by saagarjha on Friday January 29, 2021 @01:34AM (#25954709)
It’ll be very interesting to see these efforts to make QEMU faster–until now it seems like flexibility and compatibility were the main goals, with performance being much lower priority, but with a good optimizing JIT it might become reasonably competitive.
Re:The Evolution of the QEMU Translator by thesz on Friday January 29, 2021 @03:12AM (#25955189)
https://en.wikipedia.org/wiki/Loongson#Hardware-assisted_x86...

"With added improvements in QEMU from ICT, Loongson-3 achieves an average of 70% the performance of executing native binaries when running x86 binaries from nine benchmarks."

Loongson is MIPS which executes x86 binaries at 70% speed of native execution. Using QEMU.

QEMU is not unperformant.

Re:The Evolution of the QEMU Translator by saagarjha on Friday January 29, 2021 @03:20AM (#25955223)
That quote literally comes from a section titled "Hardware-assisted x86 emulation", so I don't think it would necessarily be fair to attribute all the performance to QEMU. FWIW, getting results that good almost always requires hardware MMU support, which is usually the largest bottleneck in these kinds of things. Without that you'd be lucky to get within the same order of magnitude.

Furthermore, having worked with QEMU in several binary translation and emulation projects, I cannot say that it is designed with performance as its first priority–TCG's number one consideration seems to be portability and ease of supporting new architectures. If you look at TCG-generated code it's pretty "stupid"; almost no optimizations are applied at all. This isn't necessarily something I fault QEMU for, it's just clearly not a priority. Or hasn't been, I guess; but it seems like this might be changing and I'm very interested to seeing where it goes.

Re:The Evolution of the QEMU Translator by beagle3 on Friday January 29, 2021 @04:39AM (#25955643)
TCG is actually the 2nd generation backend. The 1st generation was much worse, but it was incredibly elegant: the operations were just written as functions in C, as you may do for an interpreter - and then memcpy’d one after the other.

So a compiler from one architecture to another which is as easy to write as an interpreter (but without interpreter overhead), is independent of the target architecture - and it just works. I remember being blown away by the elegance.

Re:The Evolution of the QEMU Translator by CodesInChaos on Friday January 29, 2021 @03:51PM (#25962347)
You might want to take a look at Partial evaluation, which is a similarly elegant technique.

https://en.wikipedia.org/wiki/Partial_evaluation

https://labs.oracle.com/pls/apex/f?p=LABS:0::APPLICATION_PRO...

Re:The Evolution of the QEMU Translator by thesz on Friday January 29, 2021 @05:01PM (#25963267)
Of course, hardware helps a lot. But I doubt that getting from typical 5%-10% of native performance to 70% (7-14 times speedup) can be attributed only to hardware assistance. MIPS is way too different from x86.
Re:The Evolution of the QEMU Translator by saagarjha on Saturday January 30, 2021 @05:54AM (#25968068)
5%-10% is typical performance for a pure interpreter; a good JIT can hit 30%-50%. After that the main issue is going to be the MMU that I mentioned, and having hardware support there gets you to nearly native.
Re:The Evolution of the QEMU Translator by thesz on Wednesday February 03, 2021 @08:18PM (#26021422)
"The good JIT" is a TCG at the time. Is it good for you?

MIPS needs about 20 to 40 cycles for page fault handling. For 500MHz MIPS (Verilog/VHDL implementation synthesized to 90nm - yes, I am that old) it translates to latency from 1us to 2us. [1] shows that typical x86 page fault latency is ~7.8us, about at least twice as much. [2] (best I've found, sorry) speculates that functions compute x86 status codes and allow 80-bit long double operations natively.

[1] https://makedist.com/posts/2016/10/10/measuring-userfaultfd-...

[2] https://news.ycombinator.com/item?id=15543718

Excuse my rant, but MIPS has just one mistake - delay branch slot. It complicates implementation immensely. Other than that, it is a fascinating architecture. They claimed that their CPU can be programmed using regular instructions as effective as others do with microcode and thus MIPS does not need that and my experience had shown that.

Re:The Evolution of the QEMU Translator by FeepingCreature on Friday January 29, 2021 @05:49AM (#25956027)
What if QEMU ends up faster than native? I could imagine a future where programs are built unoptimized or with `-Os` for download size, and optimized by the operating system before or during execution, with x86 or arm (or risc-v!) ending up as the default "portable executable instruction format" for a whole bunch of CPU architectures. Where the first thing the OS or even the BIOS loads is qemu...
Re:The Evolution of the QEMU Translator by pm215 on Friday January 29, 2021 @06:37AM (#25956258)
Not gonna happen, so it's a bit irrelevant. QEMU's architecture is fairly generic and it was never built for speed in the first place. Improving the speed of emulation is more about getting better than the current baseline of "10x or worse slower than native, and continuing to gradually get slower if we add features and don't actively try to work on performance". If we got to "3x slower than native" I would be really surprised.

Which isn't to say that you can't do better than 3x-slower under any circumstances, just that if you wanted performance you'd probably be better off starting from scratch with an emulator design that cared about performance and which was really clear about its use-cases -- eg "this is user-space only, not system emulation" and "this is only this very small set of host and guest architectures". QEMU does a lot of different things in one codebase, which makes it cumbersome to change anything and hard to put in optimisations or simplifications which might be valid for a specific host/target combination but not more widely.

Re:The Evolution of the QEMU Translator by jakogut on Friday January 29, 2021 @10:15AM (#25958032)
> if you wanted performance you'd probably be better off starting from scratch with an emulator design that cared about performance and which was really clear about its use-cases

See box86:

https://github.com/ptitSeb/box86

Re:The Evolution of the QEMU Translator by stsquad on Monday February 01, 2021 @04:29PM (#25994232)
AIUI that is mainly achieved by running the native library functions.Given how much time you spend in library functions I'm not surprised it has an edge over the full translation of the application.
Re:The Evolution of the QEMU Translator by jart on Friday January 29, 2021 @06:26AM (#25956211)
The GCC Runtime Exception v3 might forbid that. That's one of the reasons why Apple and Google blew a gasket when GNU changed licenses and then poured billions into Clang. They wanted to be able to recompile your app store binaries on their backend to optimize for any hardware changes they made.

One workaround for that restriction is to use Cosmopolitan Libc: https://justine.lol/cosmopolitan/index.html It bundles compiler runtime libraries that are fast and permissively licensed. It lets you keep using GCC without facing restrictions on things like code morphing.

Re:The Evolution of the QEMU Translator by gaul on Saturday January 30, 2021 @08:15AM (#25968787)
HP Dynamo is similar to what you suggest:

https://www.hpl.hp.com/techreports/1999/HPL-1999-78.html

Note that CPUs and compiler optimization have improved over the last 20 years and these results may not still hold.

Re:The Evolution of the QEMU Translator by saagarjha on Friday January 29, 2021 @06:39AM (#25956274)
This sounds much like the pitch behind the JVM.
Re:The Evolution of the QEMU Translator by saurik on Saturday January 30, 2021 @01:33PM (#25971576)
While true of course, this more directly sounds like the OG pitch behind the Low-Level Virtual Machine (aka, LLVM).
Re:The Evolution of the QEMU Translator by ec109685 on Friday January 29, 2021 @11:10AM (#25958762)
Isn’t that essentially what WASM is?

It’d be interesting to see the performance of QEMU if it used WASM as an intermediate representation, and then fed that into WASM engine so it could take advantage of all the browser’s optimizations.