Skip to content

Commit

Permalink
linux-user: constrain execution on a single core
Browse files Browse the repository at this point in the history
QEMU doesn't behave well when a multi-threaded binary is emulated and
the various threads are scheduled on different cores by Linux. The
problem manifests as a deadlock with the all the threads blocking on
futex().

This commit uses a workaround which is to select a random core at the
beginning of the initial thread and set the CPU affinity to it. Since
the vast majority of binaries don't change the CPU affinity themselves
and on Linux the affinity is inherited by child threads, this persists
across all threads of a multi-threaded application.

Signed-off-by: Petros Angelatos <[email protected]>
  • Loading branch information
petrosagg committed Aug 13, 2018
1 parent 5d3e831 commit 6891d93
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions linux-user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,23 @@ int main(int argc, char **argv, char **envp)

module_call_init(MODULE_INIT_TRACE);
qemu_init_cpu_list();

cpu_set_t cpuset;
long ncpus;
int sched_cpu;

srand(time(NULL) + getpid());

ncpus = sysconf(_SC_NPROCESSORS_ONLN);
sched_cpu = rand() % ncpus;

CPU_ZERO(&cpuset);
CPU_SET(sched_cpu, &cpuset);

if (sched_setaffinity(getpid(), sizeof(cpuset), &cpuset)) {
fprintf(stderr, "Warning: Unable to set CPU affinity\n");
}

module_call_init(MODULE_INIT_QOM);

envlist = envlist_create();
Expand Down

0 comments on commit 6891d93

Please sign in to comment.