kutil/queue: add a process name into a thread name
authorMarek Olšák <marek.olsak@amd.com>
Tue, 3 Jul 2018 18:48:16 +0000 (14:48 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Thu, 5 Jul 2018 01:54:39 +0000 (21:54 -0400)
v2: simplifications

Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com> (v1)
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com> (v1)
src/util/u_queue.c
src/util/u_queue.h

index da513fd9cc51effc0024d91ae9c19b710bfa2a07..72ce1fc00a689e34cda2e3fabac11f634d92da0c 100644 (file)
@@ -31,6 +31,7 @@
 #include "util/os_time.h"
 #include "util/u_string.h"
 #include "util/u_thread.h"
+#include "process.h"
 
 static void util_queue_killall_and_wait(struct util_queue *queue);
 
@@ -240,7 +241,8 @@ util_queue_thread_func(void *input)
 
    if (queue->name) {
       char name[16];
-      util_snprintf(name, sizeof(name), "%s:%i", queue->name, thread_index);
+      util_snprintf(name, sizeof(name), "%s%i", queue->name, thread_index);
+      puts(name);
       u_thread_setname(name);
    }
 
@@ -299,8 +301,34 @@ util_queue_init(struct util_queue *queue,
 {
    unsigned i;
 
+   /* Form the thread name from process_name and name, limited to 13
+    * characters. Characters 14-15 are reserved for the thread number.
+    * Character 16 should be 0. Final form: "process:name12"
+    *
+    * If name is too long, it's truncated. If any space is left, the process
+    * name fills it.
+    */
+   const char *process_name = util_get_process_name();
+   int process_len = process_name ? strlen(process_name) : 0;
+   int name_len = strlen(name);
+   const int max_chars = sizeof(queue->name) - 1;
+
+   name_len = MIN2(name_len, max_chars);
+
+   /* See if there is any space left for the process name, reserve 1 for
+    * the colon. */
+   process_len = MIN2(process_len, max_chars - name_len - 1);
+   process_len = MAX2(process_len, 0);
+
    memset(queue, 0, sizeof(*queue));
-   queue->name = name;
+
+   if (process_len) {
+      snprintf(queue->name, sizeof(queue->name), "%.*s:%s",
+               process_len, process_name, name);
+   } else {
+      snprintf(queue->name, sizeof(queue->name), "%s", name);
+   }
+
    queue->flags = flags;
    queue->num_threads = num_threads;
    queue->max_jobs = max_jobs;
index d702c4bce8df8f3e94eb9bf84885ff676b3ce51b..714d9243f00ed043d6d7eb3029751914af9e090c 100644 (file)
@@ -199,7 +199,7 @@ struct util_queue_job {
 
 /* Put this into your context. */
 struct util_queue {
-   const char *name;
+   char name[14]; /* 13 characters = the thread name without the index */
    mtx_t finish_lock; /* only for util_queue_finish */
    mtx_t lock;
    cnd_t has_queued_cond;