[Libguestfs] [PATCH 2/2] Add the thread identifier to various multi-threaded programs and tests.

Richard W.M. Jones rjones at redhat.com
Wed Oct 7 17:29:33 UTC 2015


---
 df/parallel.c                                 | 6 ++++++
 src/guestfs.pod                               | 3 +++
 tests/mount-local/test-parallel-mount-local.c | 2 ++
 tests/parallel/test-parallel.c                | 6 ++++++
 4 files changed, 17 insertions(+)

diff --git a/df/parallel.c b/df/parallel.c
index 2d1d1d3..114cc27 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 248a4d0..349aa8d 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</guestfs_set_identifier> 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 0210330..873d20a 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 d018ec7..85182f2 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,
-- 
2.5.0




More information about the Libguestfs mailing list