Skip to contentSkip to conversation

TCG: Convert global variables to be TLS.

PATCH In review Evgeny Voevodin 4 participants 16 messages Feb 27, 2012 – Mar 2, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 TCG: Convert global variables to be TLS.

I suggest to send this patch to mailing list. Approve?

This patch converts some TCG data to be TLS on the way to make
TCG multithreaded. This work was made in assumption that qemu-tls.h defines a general direction to let each VCPU run in its own thread wile being processed by TCG.

Evgeny Voevodin (1):
TCG: Convert global variables to be TLS.

 bsd-user/main.c    |    1 +
 cpus.c             |    2 +
 darwin-user/main.c |    1 +
 exec.c             |  121 ++++++++++++++++++++++++++++++---------------------
 linux-user/main.c  |    1 +
 qemu-common.h      |    1 +
 6 files changed, 77 insertions(+), 50 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/1 TCG: Convert global variables to be TLS.

This commit converts code_gen_buffer, code_gen_ptr, tbs, nb_tbs to TLS. We need this if we want TCG to become multithreaded.

Initialization of code_gen_buffer and code_gen_ptr is moved to new tcg_gen_buffer_init() function. This is done because we do not need to allocate and initialize TCG buffers for IO thread. Initialization is now done in qemu_tcg_cpu_thread_fn() by each HW thread individually.

Also tcg_enabled() returns a variable instead of (code_gen_buffer != NULL) since if called from IO thread, this will always return FALSE.

Also some code format changes.

---
 bsd-user/main.c    |    1 +
 cpus.c             |    2 +
 darwin-user/main.c |    1 +
 exec.c             |  121 ++++++++++++++++++++++++++++++---------------------
 linux-user/main.c  |    1 +
 qemu-common.h      |    1 +
 6 files changed, 77 insertions(+), 50 deletions(-)
bsd-user/main.c +1 -0
index cc7d4a3..11e4540 100644
@@ -906,6 +906,7 @@ int main(int argc, char **argv)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
cpus.c +2 -0
index f45a438..6190250 100644
@@ -746,6 +746,8 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
 {
     CPUState *env = arg;
 
+    tcg_gen_buffer_init();
+
     qemu_tcg_init_cpu_signals();
     qemu_thread_get_self(env->thread);
 
darwin-user/main.c +1 -0
index 9b57c20..8618a52 100644
@@ -851,6 +851,7 @@ int main(int argc, char **argv)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
exec.c +71 -50

267 lines not on this page · raw message

linux-user/main.c +1 -0
index 14bf5f0..483482f 100644
@@ -3364,6 +3364,7 @@ int main(int argc, char **argv, char **envp)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
qemu-common.h +1 -0
index c5e9cad..13d45e0 100644
@@ -258,6 +258,7 @@ typedef enum LostTickPolicy {
     LOST_TICK_MAX
 } LostTickPolicy;
 
+void tcg_gen_buffer_init(void);
 void tcg_exec_init(unsigned long tb_size);
 bool tcg_enabled(void);
 
Signature · 1 line
1.7.5.4
#2 lore raw
In reply to Evgeny Voevodin
Quoted 9 lines · Evgeny Voevodin

This patch converts some TCG data to be TLS on the way to make
TCG multithreaded. This work was made in assumption that qemu-tls.h defines a general direction to let each VCPU run in its own thread wile being processed by TCG.

Evgeny Voevodin (1):
TCG: Convert global variables to be TLS.

Quoted 8 lines
  bsd-user/main.c    |    1 +
  cpus.c             |    2 +
  darwin-user/main.c |    1 +
  exec.c             |  121 ++++++++++++++++++++++++++++++---------------------
  linux-user/main.c  |    1 +
  qemu-common.h      |    1 +
  6 files changed, 77 insertions(+), 50 deletions(-)

Just realised that the patch is buggy and does not work for -smp 1. Anyway, it is useful to get any suggestions on the general approach.

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]
#3 lore raw

PATCH v2 1/1 TCG: Convert global variables to be TLS.

In reply to Evgeny Voevodin

This commit converts code_gen_buffer, code_gen_ptr, tbs, nb_tbs to TLS. We need this if we want TCG to become multithreaded.

Initialization of code_gen_buffer and code_gen_ptr is moved to new tcg_gen_buffer_init() function. This is done because we do not need to allocate and initialize TCG buffers for IO thread. Initialization is now done in qemu_tcg_cpu_thread_fn() by each HW thread individually.

Also tcg_enabled() returns a variable instead of (code_gen_buffer != NULL) since if called from IO thread, this will always return FALSE.

Also some code format changes.

---
 bsd-user/main.c    |    1 +
 cpus.c             |    2 +
 darwin-user/main.c |    1 +
 exec.c             |  123 +++++++++++++++++++++++++++++++---------------------
 linux-user/main.c  |    1 +
 qemu-common.h      |    1 +
 6 files changed, 79 insertions(+), 50 deletions(-)
bsd-user/main.c +1 -0
index cc7d4a3..11e4540 100644
@@ -906,6 +906,7 @@ int main(int argc, char **argv)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
cpus.c +2 -0
index f45a438..6190250 100644
@@ -746,6 +746,8 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
 {
     CPUState *env = arg;
 
+    tcg_gen_buffer_init();
+
     qemu_tcg_init_cpu_signals();
     qemu_thread_get_self(env->thread);
 
darwin-user/main.c +1 -0
index 9b57c20..8618a52 100644
@@ -851,6 +851,7 @@ int main(int argc, char **argv)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
exec.c +73 -50

273 lines not on this page · raw message

linux-user/main.c +1 -0
index 14bf5f0..483482f 100644
@@ -3364,6 +3364,7 @@ int main(int argc, char **argv, char **envp)
 #endif
     }
     tcg_exec_init(0);
+    tcg_gen_buffer_init();
     cpu_exec_init_all();
     /* NOTE: we need to init the CPU at this stage to get
        qemu_host_page_size */
qemu-common.h +1 -0
index c5e9cad..13d45e0 100644
@@ -258,6 +258,7 @@ typedef enum LostTickPolicy {
     LOST_TICK_MAX
 } LostTickPolicy;
 
+void tcg_gen_buffer_init(void);
 void tcg_exec_init(unsigned long tb_size);
 bool tcg_enabled(void);
 
Signature · 1 line
1.7.5.4
#4 lore raw
Quoted 2 lines · Evgeny Voevodin

This commit converts code_gen_buffer, code_gen_ptr, tbs, nb_tbs to TLS. We need this if we want TCG to become multithreaded.

I'm sceptical about doing this kind of thing as a change on its own. A true multithreaded TCG is a large project, and unless we're going to commit to doing that I don't see much value in making some variables per-thread when we might instead need to do larger refactorings (properly encapsulating the codegen caches as qom objects, maybe?).

-- PMM

#5 lore raw
Quoted 12 lines · Peter Maydell
Quoted 2 lines · Evgeny Voevodin

This commit converts code_gen_buffer, code_gen_ptr, tbs, nb_tbs to TLS. We need this if we want TCG to become multithreaded.

I'm sceptical about doing this kind of thing as a change on its own. A true multithreaded TCG is a large project, and unless we're going to commit to doing that I don't see much value in making some variables per-thread when we might instead need to do larger refactorings (properly encapsulating the codegen caches as qom objects, maybe?).

-- PMM

I wanted to get some feedback and points to show up a direction to move in this field. And qomification of translation caches is an interesting suggestion I think.

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]
#6 lore raw
Quoted 3 lines · Evgeny Voevodin

I wanted to get some feedback and points to show up a direction to move in this field.
And qomification of translation caches is an interesting suggestion I think.

If you're serious about multithreading TCG then I think the first steps are:
* fix existing race conditions
* think very hard
* come up with an overall design for what you're proposing

You won't get there by incremental steps unless you know where you're going...

-- PMM

#7 lore raw
Quoted 5 lines · Peter Maydell
Quoted 3 lines · Evgeny Voevodin

I wanted to get some feedback and points to show up a direction to move in this field.
And qomification of translation caches is an interesting suggestion I think.

Quoted 8 lines

If you're serious about multithreading TCG then I think the first steps are:
* fix existing race conditions
* think very hard
* come up with an overall design for what you're proposing

You won't get there by incremental steps unless you know where you're going...

Would the paper "PQEMU: A Parallel System Emulator Based on QEMU " help on this?

Regards,
chenwj

[1] http://www.cs.nthu.edu.tw/~ychung/conference/ICPADS2011.pdf

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
#8 lore raw
Quoted 11 lines · 陳韋任
Quoted 10 lines · Peter Maydell
Quoted 3 lines · Evgeny Voevodin

I wanted to get some feedback and points to show up a direction to move in this field.
And qomification of translation caches is an interesting suggestion I think.

If you're serious about multithreading TCG then I think the first steps are:
* fix existing race conditions
* think very hard
* come up with an overall design for what you're proposing

Quoted 9 lines
Quoted 2 lines

You won't get there by incremental steps unless you know where you're going...

Would the paper "PQEMU: A Parallel System Emulator Based on QEMU " help on this?

Regards,
chenwj

[1] http://www.cs.nthu.edu.tw/~ychung/conference/ICPADS2011.pdf

Certainly would :) Also I've studied COREMU: http://ppi.fudan.edu.cn/_media/publications%3Bcoremu-ppopp11.pdf
But they are based on v0.14 as I can remember and seems that this project is not going to come upstream. Anyway, thee are a lot of useful approaches they done while facing different problems on the way of paralleling the TCG. I'm sure that those approaches should be used in future work.

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]
#9 lore raw
Quoted 7 lines

Certainly would :) Also I've studied COREMU:
http://ppi.fudan.edu.cn/_media/publications%3Bcoremu-ppopp11.pdf
But they are based on v0.14 as I can remember and seems that this project is not going to come upstream.
Anyway, thee are a lot of useful approaches they done while facing different problems on the way of paralleling the TCG. I'm sure that those approaches should be used in future work.

FWIW, COREMU maintainer tends to upstream their work but they have
another project to do right now, so ... ;)

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
#10 lore raw
Quoted 7 lines · 陳韋任
Quoted 7 lines

Certainly would :) Also I've studied COREMU:
http://ppi.fudan.edu.cn/_media/publications%3Bcoremu-ppopp11.pdf
But they are based on v0.14 as I can remember and seems that this project is not going to come upstream.
Anyway, thee are a lot of useful approaches they done while facing different problems on the way of paralleling the TCG. I'm sure that those approaches should be used in future work.

Quoted 6 lines

FWIW, COREMU maintainer tends to upstream their work but they have
another project to do right now, so ... ;)

Regards,
chenwj

Their git tree was not updated for more then a year and they are based on v0.14, in which one thread was used for HW and IO. Also their code is splited into coremu lib and modified qemu from which coremu interfaces are called.

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]
#11 lore raw
In reply to Peter Maydell
Quoted 5 lines

If you're serious about multithreading TCG then I think the first steps are:
* fix existing race conditions
* think very hard
* come up with an overall design for what you're proposing

As COREMU [1] point out, current QEMU atomic instruction emulation approach is
problematic. For example, guest application might use x86 xchg instruction to implement spin lock/unlock (addr is a shared memory space).

      spin_unlock:                   spin_lock:
                                     
                                     try:
                                       r10 = 1;
                                       xchg addr, r10;
                                       if (r10 == 0)
                                         goto success;
      *addr = 0;                     fail:
                                       pause;
                                       if (*addr != 0)
                                         goto fail;

                                       goto try;

                                     success:

After QEMU translation, guest xchg instruction becomes

      spin_unlock:                   spin_lock:

                                     helper_lock;

      *addr = 0;                     T0 = r10;
                                     T1 = *addr;
                                     *addr = T0;
                                     r10 = T1;

                                     helper_unlock;

You can the see the atomicity on which spin lock/unlock rely is broken.
"*addr = 0" can happened in the between of helper_lock/helper_unlock. COREMU solve this by using a lightway software transaction memory to emulate atomic instructions. I think this issue is quite important if we want to make TCG multithreaded, right? Is there a better way to solve this?

Regards,
chenwj

[1]
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.187.6011&rep=rep1&type=pdf

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
#12 lore raw
Quoted 47 lines · 陳韋任
Quoted 5 lines

If you're serious about multithreading TCG then I think the first steps are:
* fix existing race conditions
* think very hard
* come up with an overall design for what you're proposing

As COREMU [1] point out, current QEMU atomic instruction emulation approach is
problematic. For example, guest application might use x86 xchg instruction to implement spin lock/unlock (addr is a shared memory space).

       spin_unlock:                   spin_lock:

                                      try:
                                        r10 = 1;
                                        xchg addr, r10;
                                        if (r10 == 0)
                                          goto success;
       *addr = 0;                     fail:
                                        pause;
                                        if (*addr != 0)
                                          goto fail;

                                        goto try;

                                      success:

After QEMU translation, guest xchg instruction becomes

       spin_unlock:                   spin_lock:

                                      helper_lock;

       *addr = 0;                     T0 = r10;
                                      T1 = *addr;
                                      *addr = T0;
                                      r10 = T1;

                                      helper_unlock;

You can the see the atomicity on which spin lock/unlock rely is broken.
"*addr = 0" can happened in the between of helper_lock/helper_unlock. COREMU solve this by using a lightway software transaction memory to emulate atomic instructions. I think this issue is quite important if we want to make TCG multithreaded, right? Is there a better way to solve this?

Quoted 6 lines

Regards,
chenwj

[1]
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.187.6011&rep=rep1&type=pdf

In COREMU implementation they rely on support of single-word CAS instructions by the host architecture. And if such support presents, we can use CASN algorithm if we need multiple-word CAS. So, this approach limits supported host architectures. The general question - is there some host which QEMU can run on and which doesn't support CAS?

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]
#13 lore raw
In reply to Evgeny Voevodin

Am 28.02.2012 04:13, schrieb Evgeny Voevodin:

Quoted 9 lines · Peter Maydell
Quoted 5 lines

A true multithreaded TCG is a large project, and unless we're going to commit to doing that I don't see much value in making some variables per-thread when we might instead need to do larger refactorings (properly encapsulating the codegen caches as qom objects, maybe?).

[...] qomification of translation caches is an interesting suggestion I think.

While I have come to like QOM and am using it for the CPUState, I don't see the benefit in using it for these secondary structures. There are already dedicated monitor commands to inspect them, no?

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#14 lore raw
Quoted 8 lines · Andreas Färber

Am 28.02.2012 04:13, schrieb Evgeny Voevodin:

Quoted 7 lines · Peter Maydell
Quoted 5 lines

A true multithreaded TCG is a large project, and unless we're going to commit to doing that I don't see much value in making some variables per-thread when we might instead need to do larger refactorings (properly encapsulating the codegen caches as qom objects, maybe?).

Quoted 6 lines
Quoted 2 lines

[...] qomification of translation caches is an interesting suggestion I think.

While I have come to like QOM and am using it for the CPUState, I don't see the benefit in using it for these secondary structures. There are already dedicated monitor commands to inspect them, no?

Mostly I was thinking about the encapsulation of knowing which data structures are associated with a translation cache and letting you have more than one of them. You could do that with a plain struct but since we have this OO infrastructure now why not use it?

-- PMM

#15 lore raw
Quoted 13 lines · Peter Maydell
Quoted 7 lines · Andreas Färber

Am 28.02.2012 04:13, schrieb Evgeny Voevodin:

Quoted 6 lines · Peter Maydell
Quoted 5 lines

A true multithreaded TCG is a large project, and unless we're going to commit to doing that I don't see much value in making some variables per-thread when we might instead need to do larger refactorings (properly encapsulating the codegen caches as qom objects, maybe?).

Quoted 5 lines
Quoted 2 lines

[...] qomification of translation caches is an interesting suggestion I think.

While I have come to like QOM and am using it for the CPUState, I don't see the benefit in using it for these secondary structures. There are already dedicated monitor commands to inspect them, no?

Quoted 7 lines

Mostly I was thinking about the encapsulation of knowing which data structures are associated with a translation cache and letting you have more than one of them. You could do that with a plain struct but since we have this OO infrastructure now why not use it?

-- PMM

Actually, I didn't dive deep enough in QOM and can't see any benefits or disadvantages in such encapsulation. As stands to me now, QOM is mostly an interface, but internal things are still structs :) And if we implement appropriate model for multithreading TCG, I believe, that it could be easily wrapped with QOM, if needed.
Also, there are at least two approaches for cache - unified for all VCPUs and exclusive for each VCPU. First is better when a lot of different threads run in the target, since each cache holds unique code and thread communication is small.
Second is better when a lot of identical threads running since no excessive translation of identical code is made by each VCPU thread, but communication between threads on accessing the cache is high. So, what I'm talking about is if we use unified cache, we may not need to have more than one cache instance.

Signature · 5 lines
Kind regards,
Evgeny Voevodin,
Leading Software Engineer,
ASWG, Moscow R&D center, Samsung Electronics
e-mail: [email protected]