From f291411f76352df47f7741e2e135403ecba1f2ea Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 7 Oct 2015 18:01:09 +0100 Subject: [PATCH] Add the thread identifier to various multi-threaded programs and tests. --- df/parallel.c | 6 ++++++ src/guestfs.pod | 3 +++ tests/mount-local/test-parallel-mount-local.c | 2 ++ tests/parallel/test-parallel.c | 6 ++++++ tests/qemu/qemu-boot.c | 4 ++++ 5 files changed, 21 insertions(+) diff --git a/df/parallel.c b/df/parallel.c index 2d1d1d330..114cc2765 100644 --- a/df/parallel.c +++ b/df/parallel.c @@ -149,6 +149,7 @@ worker_thread (void *thread_data_vp) size_t output_len = 0; guestfs_h *g; int err; + char id[64]; /* Take the next domain from the list. */ if (thread_data->verbose) @@ -191,6 +192,11 @@ worker_thread (void *thread_data_vp) return &thread_data->r; } + /* Set the handle identifier so we can tell threads apart. */ + snprintf (id, sizeof id, "thread_%zu_domain_%zu", + thread_data->thread_num, i); + guestfs_set_identifier (g, id); + /* Copy some settings from the options guestfs handle. */ guestfs_set_trace (g, thread_data->trace); guestfs_set_verbose (g, thread_data->verbose); diff --git a/src/guestfs.pod b/src/guestfs.pod index 248a4d0d2..349aa8d02 100644 --- a/src/guestfs.pod +++ b/src/guestfs.pod @@ -1328,6 +1328,9 @@ safe to be called from multiple threads without a mutex. See the graphical program guestfs-browser for one possible architecture for multithreaded programs using libvirt and libguestfs. +Use L to make it simpler to identify threads +in trace output. + =head2 PATH Libguestfs needs a supermin appliance, which it finds by looking along diff --git a/tests/mount-local/test-parallel-mount-local.c b/tests/mount-local/test-parallel-mount-local.c index 021033011..873d20a99 100644 --- a/tests/mount-local/test-parallel-mount-local.c +++ b/tests/mount-local/test-parallel-mount-local.c @@ -177,6 +177,8 @@ start_thread (void *statevp) pthread_exit (&state->exit_status); } + guestfs_set_identifier (g, state->mp); + if (guestfs_add_drive_scratch (g, 512*1024*1024, -1) == -1) goto error; if (guestfs_launch (g) == -1) diff --git a/tests/parallel/test-parallel.c b/tests/parallel/test-parallel.c index d018ec7d1..85182f285 100644 --- a/tests/parallel/test-parallel.c +++ b/tests/parallel/test-parallel.c @@ -43,6 +43,7 @@ #define NR_THREADS 5 struct thread_state { + size_t thread_num; /* Thread number. */ pthread_t thread; /* Thread handle. */ int exit_status; /* Thread exit status. */ }; @@ -89,6 +90,7 @@ main (int argc, char *argv[]) sigaction (SIGQUIT, &sa, NULL); for (i = 0; i < NR_THREADS; ++i) { + threads[i].thread_num = i; /* Start the thread. */ r = pthread_create (&threads[i].thread, NULL, start_thread, &threads[i]); @@ -117,6 +119,7 @@ start_thread (void *statevp) struct thread_state *state = statevp; guestfs_h *g; time_t start_t, t; + char id[64]; time (&start_t); @@ -133,6 +136,9 @@ start_thread (void *statevp) pthread_exit (&state->exit_status); } + snprintf (id, sizeof id, "%zu", state->thread_num); + guestfs_set_identifier (g, id); + if (guestfs_add_drive_opts (g, "/dev/null", GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, diff --git a/tests/qemu/qemu-boot.c b/tests/qemu/qemu-boot.c index e9683eb36..1358a73d3 100644 --- a/tests/qemu/qemu-boot.c +++ b/tests/qemu/qemu-boot.c @@ -199,6 +199,7 @@ start_thread (void *thread_data_vp) size_t i; guestfs_h *g; unsigned errors = 0; + char id[64]; for (;;) { /* Take the next process. */ @@ -237,6 +238,9 @@ start_thread (void *thread_data_vp) goto error; } + snprintf (id, sizeof id, "%zu", i); + guestfs_set_identifier (g, id); + guestfs_set_trace (g, trace); guestfs_set_verbose (g, verbose);