Skip to contentSkip to conversation

About the Thread model in Qemu【 Is it one thread per vcpu?】

Qemu-devel 解壁伟 7 participants 10 messages Mar 12, 2013 – Apr 23, 2013

lore
Share & tools
lore

解壁伟

author
#0 lore raw

Hi there:

I've read this an article discribe the thread model in Qemu.
http://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html
Is there really one thread per vcpu in qemu?

When I run the qemu using "-smp 16", I can see only two threads using "ps -xH".

So I want to know that if qemu excuting the vcpu in one thread per vcpu way?

or, qemu simply excuting all the vcpus in one thread?

How can I trace the generation of the vcpu thread?

I'll be appreciate for your help~

puck
2013-3-12

#1 lore raw
Quoted 8 lines · 解壁伟

Hi there:

I've read this an article discribe the thread model in Qemu.
http://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html
Is there really one thread per vcpu in qemu?

When I run the qemu using "-smp 16", I can see only two threads using "ps -xH".

What ps is that? I'd try ps -efL.

Quoted 6 lines

So I want to know that if qemu excuting the vcpu in one thread per vcpu way?

or, qemu simply excuting all the vcpus in one thread?

How can I trace the generation of the vcpu thread?

qemu_init_vcpu() --> qemu_kvm_start_vcpu() [cpus.c]

#3 lore raw
In reply to 解壁伟
Quoted 8 lines · 解壁伟

Hi there:

I've read this an article discribe the thread model in Qemu.
http://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html
Is there really one thread per vcpu in qemu?

When I run the qemu using "-smp 16", I can see only two threads using "ps -xH".

"Note that TCG is not thread-safe so even under the iothread model it multiplexes vcpus across a single QEMU thread. Only KVM can take advantage of per-vcpu threads."

If you do not have -enable-kvm then you're probably using TCG.

Stefan

author
#4 lore raw

I read the article again, and I get the answer to my question in that paragraph.
Thank you very much.
As you've said, TCG's not thread-safe.
Is it only the shared data structure(e.g TB) that prevent TCG to be thread-safe? I'm seeking a method to parallize TCG in order to parallize Qemu.

And,how do the multi-thread in kvm mode Qemu synchronize to each other? For some threads maybe excute faster or slower than others, how do they synchronize the clock with each other? Using a huge lock or a global clock?

I'll be appreciate if you can give me some hints for further exploration.

For now, I need a emulator to emulate more cores efficiently.I want to analyze whether Qemu can be faster when it emulate more cores, while parallelism maybe a good choice.

puck

Quoted 17 lines · Stefan Hajnoczi
Quoted 8 lines · 解壁伟

Hi there:

I've read this an article discribe the thread model in Qemu.
http://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html
Is there really one thread per vcpu in qemu?

When I run the qemu using "-smp 16", I can see only two threads using "ps -xH".

"Note that TCG is not thread-safe so even under the iothread model it multiplexes vcpus across a single QEMU thread. Only KVM can take advantage of per-vcpu threads."

If you do not have -enable-kvm then you're probably using TCG.

Stefan

author
#2 lore raw
In reply to Laszlo Ersek

Thank you very much.

I've find the generation of thread in qemu, following your advice.

qemu_init_vcpu() --> qemu_kvm_start_vcpu() [cpus.c]

puck

Quoted 19 lines · Laszlo Ersek
Quoted 8 lines · 解壁伟

Hi there:

I've read this an article discribe the thread model in Qemu.
http://blog.vmsplice.net/2011/03/qemu-internals-overall-architecture-and.html
Is there really one thread per vcpu in qemu?

When I run the qemu using "-smp 16", I can see only two threads using "ps -xH".

What ps is that? I'd try ps -efL.

Quoted 5 lines

So I want to know that if qemu excuting the vcpu in one thread per vcpu way?

or, qemu simply excuting all the vcpus in one thread?

How can I trace the generation of the vcpu thread?

qemu_init_vcpu() --> qemu_kvm_start_vcpu() [cpus.c]

#5 lore raw
In reply to 解壁伟
Quoted 7 lines · 解壁伟

I read the article again, and I get the answer to my question in that paragraph.
Thank you very much.
As you've said, TCG's not thread-safe.
Is it only the shared data structure(e.g TB) that prevent TCG to be thread-safe? I'm seeking a method to parallize TCG in order to parallize Qemu.

Yes, the major problem is the non-thread-safe code generation data structures. (I fixed one set of non-threadsafe code recently but there are still a lot to do. Needs thought about the right design principles.)

Quoted 3 lines

For now, I need a emulator to emulate more cores efficiently.I want to analyze whether Qemu can be faster when it emulate more cores, while parallelism maybe a good choice.

There has been some other research work with QEMU in this area, you should probably look it up. However it has all been "we fixed enough of the big problems to be able to run some benchmarks and write our research paper", so none of this is actually in the QEMU sources. (The short answer I think is "probably but it will depend a lot on workload, and there's a lot of software engineering required to do it reliably".)

-- PMM

#6 lore raw

On Wed, Mar 13, 2013 at 10:51 AM, Peter Maydell
<[email protected]> wrote:
[...]

Quoted 11 lines
Quoted 3 lines

For now, I need a emulator to emulate more cores efficiently.I want to analyze whether Qemu can be faster when it emulate more cores, while parallelism maybe a good choice.

There has been some other research work with QEMU in this area, you should probably look it up. However it has all been "we fixed enough of the big problems to be able to run some benchmarks and write our research paper", so none of this is actually in the QEMU sources. (The short answer I think is "probably but it will depend a lot on workload, and there's a lot of software engineering required to do it reliably".)

I know of two such variants: COREMU and HQEMU, but as far as I know the latter isn't freely available.

Laurent

#7 lore raw

Il 13/03/2013 10:56, Laurent Desnogues ha scritto:

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

There has been some other research work with QEMU in this area, you should probably look it up. However it has all been "we fixed enough of the big problems to be able to run some benchmarks and write our research paper", so none of this is actually in the QEMU sources. (The short answer I think is "probably but it will depend a lot on workload, and there's a lot of software engineering required to do it reliably".)

I know of two such variants: COREMU and HQEMU, but as far as I know the latter isn't freely available.

COREMU had one code generator per VCPU, if I remember correctly.

Jan Kiszka also posted an RFC patch that removed the "lockstep" behavior of TCG, where the VCPU will always block the iothread even when it is not executing MMIO. Getting this upstream would be an even bigger step than making the code generator thread-safe.

Paolo

author
#8 lore raw

[Qemu-devel] Round robin of multi-vcpu in qemu

In reply to 解壁伟

Hi there:

I'm studying the execution details of multi-vcpu in TCG mode.

The vcpus in TCG mode are executed one by one in a sequencial way.

I read the function [tcg_exec_all] in Qemu 1.3.0 as bellow, but the implementation is not as I expected.

[tcg_exec_all] will finally call [cpu_exec] to excute a loop to excute the TBs in code cache

So,how does these functions control the running time of each VCPU?

That is,when will the execution of one VCPU return, in order to execute the next_cpu in the loop of [tcg_exec_all].

I've examined the alarm timmer, this timer is used to interrupt the execution of VCPUs to handle interruptions.

Does Qemu use a similar way (timer) to control the round robin of these VCPUs?

Or, does Qemu use the iCount to prevent a VCPU from running too long?

        staticvoidtcg_exec_all(void)
        {
        intr;

        /* Account partial waits to the vm_clock. */
        qemu_clock_warp(vm_clock);

        if(next_cpu==NULL){
        next_cpu=first_cpu;
        }
        for(;next_cpu!=NULL&&!exit_request;next_cpu=next_cpu->next_cpu){
        CPUArchState*env=next_cpu;
        CPUState*cpu=ENV_GET_CPU(env);

        qemu_clock_enable(vm_clock,
        (env->singlestep_enabled&SSTEP_NOTIMER)==0);

        if(cpu_can_run(cpu)){
        r=tcg_cpu_exec(env);
        if(r==EXCP_DEBUG){
        cpu_handle_guest_debug(env);
        break;
        }
        }elseif(cpu->stop||cpu->stopped){
        break;
        }
        }
        exit_request=0;
        }

Yours
Biwei

#9 lore raw

Re: [Qemu-devel] Round robin of multi-vcpu in qemu

Quoted 2 lines

I've examined the alarm timmer, this timer is used to interrupt the execution of VCPUs to handle interruptions.

IIRC, alarm timer will interrupt VCPU to execute the next one.
You can check where QEMU register the alarm timer, and see how it works.

Regards,
chenwj

Signature · 5 lines
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj