tcg: Pass max_threads not max_cpus to tcg_init

In effect, hoist the check for mttcg from tcg_n_regions()
to tcg_init_machine().

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2025-04-04 16:30:57 -07:00
parent 9638cb59ee
commit a9d107fa0e
5 changed files with 31 additions and 32 deletions

View file

@ -103,18 +103,20 @@ bool one_insn_per_tb;
static int tcg_init_machine(MachineState *ms) static int tcg_init_machine(MachineState *ms)
{ {
TCGState *s = TCG_STATE(current_accel()); TCGState *s = TCG_STATE(current_accel());
#ifdef CONFIG_USER_ONLY unsigned max_threads = 1;
unsigned max_cpus = 1;
#else
unsigned max_cpus = ms->smp.max_cpus;
#endif
tcg_allowed = true; tcg_allowed = true;
mttcg_enabled = s->mttcg_enabled; mttcg_enabled = s->mttcg_enabled;
page_init(); page_init();
tb_htable_init(); tb_htable_init();
tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_cpus);
#ifndef CONFIG_USER_ONLY
if (mttcg_enabled) {
max_threads = ms->smp.max_cpus;
}
#endif
tcg_init(s->tb_size * MiB, s->splitwx_enabled, max_threads);
#if defined(CONFIG_SOFTMMU) #if defined(CONFIG_SOFTMMU)
/* /*

View file

@ -29,12 +29,12 @@
* tcg_init: Initialize the TCG runtime * tcg_init: Initialize the TCG runtime
* @tb_size: translation buffer size * @tb_size: translation buffer size
* @splitwx: use separate rw and rx mappings * @splitwx: use separate rw and rx mappings
* @max_cpus: number of vcpus in system mode * @max_threads: number of vcpu threads in system mode
* *
* Allocate and initialize TCG resources, especially the JIT buffer. * Allocate and initialize TCG resources, especially the JIT buffer.
* In user-only mode, @max_cpus is unused. * In user-only mode, @max_threads is unused.
*/ */
void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus); void tcg_init(size_t tb_size, int splitwx, unsigned max_threads);
/** /**
* tcg_register_thread: Register this thread with the TCG runtime * tcg_register_thread: Register this thread with the TCG runtime

View file

@ -422,7 +422,7 @@ void tcg_region_reset_all(void)
tcg_region_tree_reset_all(); tcg_region_tree_reset_all();
} }
static size_t tcg_n_regions(size_t tb_size, unsigned max_cpus) static size_t tcg_n_regions(size_t tb_size, unsigned max_threads)
{ {
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
return 1; return 1;
@ -431,24 +431,25 @@ static size_t tcg_n_regions(size_t tb_size, unsigned max_cpus)
/* /*
* It is likely that some vCPUs will translate more code than others, * It is likely that some vCPUs will translate more code than others,
* so we first try to set more regions than max_cpus, with those regions * so we first try to set more regions than threads, with those regions
* being of reasonable size. If that's not possible we make do by evenly * being of reasonable size. If that's not possible we make do by evenly
* dividing the code_gen_buffer among the vCPUs. * dividing the code_gen_buffer among the vCPUs.
*
* Use a single region if all we have is one vCPU thread.
*/ */
/* Use a single region if all we have is one vCPU thread */ if (max_threads == 1) {
if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
return 1; return 1;
} }
/* /*
* Try to have more regions than max_cpus, with each region being >= 2 MB. * Try to have more regions than threads, with each region being >= 2 MB.
* If we can't, then just allocate one region per vCPU thread. * If we can't, then just allocate one region per vCPU thread.
*/ */
n_regions = tb_size / (2 * MiB); n_regions = tb_size / (2 * MiB);
if (n_regions <= max_cpus) { if (n_regions <= max_threads) {
return max_cpus; return max_threads;
} }
return MIN(n_regions, max_cpus * 8); return MIN(n_regions, max_threads * 8);
#endif #endif
} }
@ -731,11 +732,7 @@ static int alloc_code_gen_buffer(size_t size, int splitwx, Error **errp)
* and then assigning regions to TCG threads so that the threads can translate * and then assigning regions to TCG threads so that the threads can translate
* code in parallel without synchronization. * code in parallel without synchronization.
* *
* In system-mode the number of TCG threads is bounded by max_cpus, so we use at * In system-mode the number of TCG threads is bounded by max_threads,
* least max_cpus regions in MTTCG. In !MTTCG we use a single region.
* Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
* must have been parsed before calling this function, since it calls
* qemu_tcg_mttcg_enabled().
* *
* In user-mode we use a single region. Having multiple regions in user-mode * In user-mode we use a single region. Having multiple regions in user-mode
* is not supported, because the number of vCPU threads (recall that each thread * is not supported, because the number of vCPU threads (recall that each thread
@ -749,7 +746,7 @@ static int alloc_code_gen_buffer(size_t size, int splitwx, Error **errp)
* in practice. Multi-threaded guests share most if not all of their translated * in practice. Multi-threaded guests share most if not all of their translated
* code, which makes parallel code generation less appealing than in system-mode * code, which makes parallel code generation less appealing than in system-mode
*/ */
void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus) void tcg_region_init(size_t tb_size, int splitwx, unsigned max_threads)
{ {
const size_t page_size = qemu_real_host_page_size(); const size_t page_size = qemu_real_host_page_size();
size_t region_size; size_t region_size;
@ -787,7 +784,7 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus)
* As a result of this we might end up with a few extra pages at the end of * As a result of this we might end up with a few extra pages at the end of
* the buffer; we will assign those to the last region. * the buffer; we will assign those to the last region.
*/ */
region.n = tcg_n_regions(tb_size, max_cpus); region.n = tcg_n_regions(tb_size, max_threads);
region_size = tb_size / region.n; region_size = tb_size / region.n;
region_size = QEMU_ALIGN_DOWN(region_size, page_size); region_size = QEMU_ALIGN_DOWN(region_size, page_size);

View file

@ -34,7 +34,7 @@ extern TCGContext **tcg_ctxs;
extern unsigned int tcg_cur_ctxs; extern unsigned int tcg_cur_ctxs;
extern unsigned int tcg_max_ctxs; extern unsigned int tcg_max_ctxs;
void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus); void tcg_region_init(size_t tb_size, int splitwx, unsigned max_threads);
bool tcg_region_alloc(TCGContext *s); bool tcg_region_alloc(TCGContext *s);
void tcg_region_initial_alloc(TCGContext *s); void tcg_region_initial_alloc(TCGContext *s);
void tcg_region_prologue_set(TCGContext *s); void tcg_region_prologue_set(TCGContext *s);

View file

@ -1499,7 +1499,7 @@ static void process_constraint_sets(void);
static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
TCGReg reg, const char *name); TCGReg reg, const char *name);
static void tcg_context_init(unsigned max_cpus) static void tcg_context_init(unsigned max_threads)
{ {
TCGContext *s = &tcg_init_ctx; TCGContext *s = &tcg_init_ctx;
int n, i; int n, i;
@ -1538,15 +1538,15 @@ static void tcg_context_init(unsigned max_cpus)
* In user-mode we simply share the init context among threads, since we * In user-mode we simply share the init context among threads, since we
* use a single region. See the documentation tcg_region_init() for the * use a single region. See the documentation tcg_region_init() for the
* reasoning behind this. * reasoning behind this.
* In system-mode we will have at most max_cpus TCG threads. * In system-mode we will have at most max_threads TCG threads.
*/ */
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
tcg_ctxs = &tcg_ctx; tcg_ctxs = &tcg_ctx;
tcg_cur_ctxs = 1; tcg_cur_ctxs = 1;
tcg_max_ctxs = 1; tcg_max_ctxs = 1;
#else #else
tcg_max_ctxs = max_cpus; tcg_max_ctxs = max_threads;
tcg_ctxs = g_new0(TCGContext *, max_cpus); tcg_ctxs = g_new0(TCGContext *, max_threads);
#endif #endif
tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0)); tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
@ -1554,10 +1554,10 @@ static void tcg_context_init(unsigned max_cpus)
tcg_env = temp_tcgv_ptr(ts); tcg_env = temp_tcgv_ptr(ts);
} }
void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus) void tcg_init(size_t tb_size, int splitwx, unsigned max_threads)
{ {
tcg_context_init(max_cpus); tcg_context_init(max_threads);
tcg_region_init(tb_size, splitwx, max_cpus); tcg_region_init(tb_size, splitwx, max_threads);
} }
/* /*