From 1ec823bf5e55058f3ba3095b3b3305fb16318f66 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Tue, 16 Jun 2020 13:50:21 +0200 Subject: [PATCH] libc: support manual pthread configuration Fixes #3787 --- repos/libports/src/lib/libc/pthread_create.cc | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/repos/libports/src/lib/libc/pthread_create.cc b/repos/libports/src/lib/libc/pthread_create.cc index ed8c29c1d9..efb8f71cea 100644 --- a/repos/libports/src/lib/libc/pthread_create.cc +++ b/repos/libports/src/lib/libc/pthread_create.cc @@ -30,18 +30,45 @@ static Genode::Cpu_session * _cpu_session { nullptr }; -static bool _single_cpu { false }; +static unsigned _id_cpu_map[32]; +static bool _verbose { false }; void Libc::init_pthread_support(Genode::Cpu_session &cpu_session, Genode::Xml_node node) { _cpu_session = &cpu_session; + _verbose = node.attribute_value("verbose", false); + String<32> placement("all-cpus"); placement = node.attribute_value("placement", placement); - if (placement == "single-cpu") - _single_cpu = true; + if (placement == "single-cpu") { + return; + } + + if (placement == "manual") { + node.for_each_sub_node("thread", [](Xml_node &node) { + if (node.has_attribute("id") && node.has_attribute("cpu")) { + unsigned const id = node.attribute_value("id", 0U); + unsigned const cpu = node.attribute_value("cpu", 0U); + + if (id < sizeof(_id_cpu_map) / sizeof(_id_cpu_map[0])) { + _id_cpu_map[id] = cpu; + if (_verbose) + Genode::log("pthread.", id, " -> cpu ", cpu); + } else { + Genode::warning("pthread configuration ignored - " + "id=", id, " cpu=", cpu); + } + } + }); + return; + } + + /* all-cpus and unknown case */ + for (unsigned i = 0; i < sizeof(_id_cpu_map) / sizeof(_id_cpu_map[0]); i++) + _id_cpu_map[i] = i; } @@ -105,9 +132,14 @@ extern "C" using Genode::Affinity; unsigned const id { pthread_id() }; + unsigned const cpu = (id < sizeof(_id_cpu_map) / sizeof(_id_cpu_map[0])) ? _id_cpu_map[id] : 0; + Genode::String<32> const pthread_name { "pthread.", id }; Affinity::Space space { _cpu_session->affinity_space() }; - Affinity::Location location { space.location_of_index(_single_cpu ? 0 : id) }; + Affinity::Location location { space.location_of_index(cpu) }; + + if (_verbose) + Genode::log("create ", pthread_name, " -> cpu ", cpu); return Libc::pthread_create(thread, start_routine, arg, stack_size, pthread_name.string(), _cpu_session,