Skip to contentSkip to conversation

erofs-utils: lib: name worker threads erofs_compress

PATCH Superseded Nithurshen 2 participants 11 messages Mar 16 Library code

lore
Share & tools
lore

Nithurshen

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: name worker threads erofs_compress

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like \top\, \htop\, or \ps\.

This utilizes \prctl(PR_SET_NAME)\ on Linux and \pthread_setname_np\ on macOS to explicitly label these threads as \"erofs_compress\" upon initialization.

---
 lib/workqueue.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/workqueue.c +9 -0
index 18ee0f9..860e403 100644
@@ -2,6 +2,9 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include "erofs/workqueue.h"
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
 
 static void *worker_thread(void *arg)
 {
@@ -9,6 +12,12 @@ static void *worker_thread(void *arg)
 	struct erofs_work *work;
 	void *tlsp = NULL;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif
+
 	if (wq->on_start)
 		tlsp = (wq->on_start)(wq, NULL);
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines · Nithurshen

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like \top\, \htop\, or \ps\.

Please just use top, htop, or ps here.

Quoted 4 lines

This utilizes \prctl(PR_SET_NAME)\ on Linux and \pthread_setname_np\ on macOS to explicitly label these threads as \"erofs_compress\" upon initialization.

Why not just calling erofs_compressor, since those worker are really compressor.

Quoted 21 lines
---
  lib/workqueue.c | 9 +++++++++
  1 file changed, 9 insertions(+)
lib/workqueue.c +3 -0
index 18ee0f9..860e403 100644
@@ -2,6 +2,9 @@
  #include <pthread.h>
  #include <stdlib.h>
  #include "erofs/workqueue.h"
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
 
  static void *worker_thread(void *arg)
  {

lib/workqueue.c static void *worker_thread(void *arg) · line 19

Quoted 8 lines
lib/workqueue.c +5 -0
@@ -9,6 +12,12 @@ static void *worker_thread(void *arg)
  	struct erofs_work *work;
  	void *tlsp = NULL;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif

I don't think they should be added here, you could add in .on_start() of lib/compress.c instead.

Thanks,
Gao Xiang

Quoted 4 lines
lib/workqueue.c +1 -0
+
  	if (wq->on_start)
  		tlsp = (wq->on_start)(wq, NULL);
 
author
#2 lore raw

PATCH v2 1/1 erofs-utils: lib: name worker threads erofs_compress

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like top, htop, or ps.

This utilizes prctl(PR_SET_NAME) on Linux and pthread_setname_np on macOS to explicitly label these threads as "erofs_compress" upon initialization.

---
 lib/workqueue.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/workqueue.c +9 -0
index 18ee0f9..860e403 100644
@@ -2,6 +2,9 @@
 #include <pthread.h>
 #include <stdlib.h>
 #include "erofs/workqueue.h"
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
 
 static void *worker_thread(void *arg)
 {
@@ -9,6 +12,12 @@ static void *worker_thread(void *arg)
 	struct erofs_work *work;
 	void *tlsp = NULL;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif
+
 	if (wq->on_start)
 		tlsp = (wq->on_start)(wq, NULL);
 
Signature · 1 line
2.51.0
#3 lore raw

On 2026/3/17 00:54, Nithurshen wrote:

..

lib/workqueue.c static void *worker_thread(void *arg) · line 19

Quoted 11 lines
  static void *worker_thread(void *arg)
  {
lib/workqueue.c +5 -0
@@ -9,6 +12,12 @@ static void *worker_thread(void *arg)
  	struct erofs_work *work;
  	void *tlsp = NULL;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif

I'm not happy with this part, please read the v1 reply.

author
#4 lore raw

PATCH v3 1/1 erofs-utils: lib: name worker threads erofs_compress

In reply to Gao Xiang

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like top, htop, or ps.

This utilizes prctl(PR_SET_NAME) on Linux and pthread_setname_np on macOS to explicitly label these threads as "erofs_compressor" upon initialization in the workqueue's .on_start() callback.

---
 lib/compress.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/compress.c +9 -0
index 4a0d890..66b3b8b 100644
@@ -26,6 +26,9 @@
 #include "liberofs_compress.h"
 #include "liberofs_fragments.h"
 #include "liberofs_metabox.h"
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
 
 #define Z_EROFS_DESTBUF_SZ	(Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
 
@@ -1427,6 +1430,12 @@ void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
 {
 	struct erofs_compress_wq_tls *tls;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif
+
 	tls = calloc(1, sizeof(*tls));
 	if (!tls)
 		return NULL;
Signature · 1 line
2.51.0
#5 lore raw

On 2026/3/17 01:02, Nithurshen wrote:

..

lib/compress.c void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr) · line 1437

Quoted 9 lines
lib/compress.c +5 -0
@@ -1427,6 +1430,12 @@ void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
  {
  	struct erofs_compress_wq_tls *tls;
 
+#if defined(__linux__)
+	prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+	pthread_setname_np("erofs_compress");
+#endif

Why not call them as erofs_compressor?

and could we wrap it up as a new function like:

erofs_set_thread_name() somewhere instead so that
users can use it easier instead?

Thanks,
Gao Xiang

Quoted 4 lines
lib/compress.c +1 -0
+
  	tls = calloc(1, sizeof(*tls));
  	if (!tls)
  		return NULL;
#6 lore raw
In reply to Gao Xiang

Hi Xiang,

Sorry for the confusion. I sent the v2 patch before reading your v1 reply, as I noticed the quotes formatting was off in my first submission and wanted to fix it.

Quoted 11 lines · Gao Xiang
Quoted 7 lines · Nithurshen

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like \top\, \htop\, or \ps\.

Please just use top, htop, or ps here.

Yes, I have taken care of this in the updated commit message.

Quoted 6 lines
Quoted 3 lines

This utilizes \prctl(PR_SET_NAME)\ on Linux and \pthread_setname_np\ on macOS to explicitly label these threads as \"erofs_compress\" upon initialization.

Why not just calling erofs_compressor, since those worker are really compressor.

I initially tested using "erofs_compressor", but the OS thread naming API (prctl with PR_SET_NAME) has a strict 16-byte limit, which includes the null terminator.

Because "erofs_compressor" is exactly 16 characters long, the null byte pushes it to 17 bytes. As a result, the kernel truncates it, and it actually shows up as "erofs_compresso" in ps and top.

To keep the output looking clean and intentional, I chose to use "erofs_compress" (14 chars) instead. Please let me know if you are okay with this, or if you'd prefer a different abbreviation.

Quoted 8 lines

lib/workqueue.c static void *worker_thread(void *arg) · line 19

Quoted 5 lines
lib/workqueue.c +5 -0
+#if defined(__linux__)
+     prctl(PR_SET_NAME, "erofs_compress");
+#elif defined(__APPLE__)
+     pthread_setname_np("erofs_compress");
+#endif

I don't think they should be added here, you could add in .on_start() of lib/compress.c instead.

Yes, it has been moved to lib/compress.c

Thanks,
Nithurshen

#7 lore raw

Hi Nithurshen,

Quoted 2 lines · Nithurshen Karthikeyan

Hi Xiang,

...

Quoted 8 lines
Quoted 3 lines

Why not just calling erofs_compressor, since those worker are really compressor.

I initially tested using "erofs_compressor", but the OS thread naming API (prctl with PR_SET_NAME) has a strict 16-byte limit, which includes the null terminator.

Quoted 7 lines

Because "erofs_compressor" is exactly 16 characters long, the null byte pushes it to 17 bytes. As a result, the kernel truncates it, and it actually shows up as "erofs_compresso" in ps and top.

To keep the output looking clean and intentional, I chose to use "erofs_compress" (14 chars) instead. Please let me know if you are okay with this, or if you'd prefer a different abbreviation.

Ok, or just call it as erofscompressor?
erofs_compress is really awkward to me.

Thanks,
Gao Xiang

author
#8 lore raw

PATCH v4 1/1 erofs-utils: lib: name worker threads erofscompressor

In reply to Gao Xiang

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like top, htop, or ps.

This introduces a new OS-independent wrapper, erofs_set_thread_name(), which utilizes prctl(PR_SET_NAME) on Linux and pthread_setname_np on macOS. This helper is now called during the workqueue's .on_start() callback in compress.c to explicitly label the worker threads as "erofscompressor".

---
 include/erofs/workqueue.h |  2 ++
 lib/compress.c            |  2 ++
 lib/workqueue.c           | 13 +++++++++++++
 3 files changed, 17 insertions(+)
include/erofs/workqueue.h +2 -0
index 36037c3..27d3ba4 100644
@@ -31,4 +31,6 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 			  erofs_wq_func_t on_exit);
 int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work);
 int erofs_destroy_workqueue(struct erofs_workqueue *wq);
+
+void erofs_set_thread_name(const char *name);
 #endif
lib/compress.c +2 -0
index 4a0d890..65971c9 100644
@@ -1427,6 +1427,8 @@ void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
 {
 	struct erofs_compress_wq_tls *tls;
 
+	erofs_set_thread_name("erofscompressor");
+
 	tls = calloc(1, sizeof(*tls));
 	if (!tls)
 		return NULL;
lib/workqueue.c +13 -0
index 18ee0f9..c924c1b 100644
@@ -3,6 +3,19 @@
 #include <stdlib.h>
 #include "erofs/workqueue.h"
 
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+
+void erofs_set_thread_name(const char *name)
+{
+	#if defined(__linux__)
+		prctl(PR_SET_NAME, name);
+	#elif defined(__APPLE__)
+		pthread_setname_np(name);
+	#endif
+}
+
 static void *worker_thread(void *arg)
 {
 	struct erofs_workqueue *wq = arg;
Signature · 1 line
2.51.0
#9 lore raw

Re: [PATCH v4] erofs-utils: lib: name worker threads erofscompressor

On 2026/3/17 01:25, Nithurshen wrote:

..

Quoted 5 lines
lib/workqueue.c +0 -0
index 18ee0f9..c924c1b 100644

lib/workqueue.c line 12

Quoted 10 lines
lib/workqueue.c +7 -0
@@ -3,6 +3,19 @@
  #include <stdlib.h>
  #include "erofs/workqueue.h"
 
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+
+void erofs_set_thread_name(const char *name)
+{
+	#if defined(__linux__)

nit: no indentation for all C macros. Since you'd like to contribute more, I hope you could read the kernel code style too.

Thanks,
Gao Xiang

Quoted 9 lines
lib/workqueue.c +6 -0
+		prctl(PR_SET_NAME, name);
+	#elif defined(__APPLE__)
+		pthread_setname_np(name);
+	#endif
+}
+
  static void *worker_thread(void *arg)
  {
  	struct erofs_workqueue *wq = arg;
author
#10 lore raw

PATCH v5 1/1 erofs-utils: lib: name worker threads erofscompressor

In reply to Gao Xiang

Set a specific thread name for the multi-threaded workqueue workers to make debugging, profiling, and process monitoring significantly easier.

Previously, worker threads inherited the name of the parent process (e.g., mkfs.erofs), making it difficult to distinguish them from the main thread in tools like top, htop, or ps.

This introduces a new OS-independent wrapper, erofs_set_thread_name(), which utilizes prctl(PR_SET_NAME) on Linux and pthread_setname_np on macOS. This helper is now called during the workqueue's .on_start() callback in compress.c to explicitly label the worker threads as "erofscompressor".

---
 include/erofs/workqueue.h |  2 ++
 lib/compress.c            |  2 ++
 lib/workqueue.c           | 13 +++++++++++++
 3 files changed, 17 insertions(+)
include/erofs/workqueue.h +2 -0
index 36037c3..27d3ba4 100644
@@ -31,4 +31,6 @@ int erofs_alloc_workqueue(struct erofs_workqueue *wq, unsigned int nworker,
 			  erofs_wq_func_t on_exit);
 int erofs_queue_work(struct erofs_workqueue *wq, struct erofs_work *work);
 int erofs_destroy_workqueue(struct erofs_workqueue *wq);
+
+void erofs_set_thread_name(const char *name);
 #endif
lib/compress.c +2 -0
index 4a0d890..65971c9 100644
@@ -1427,6 +1427,8 @@ void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
 {
 	struct erofs_compress_wq_tls *tls;
 
+	erofs_set_thread_name("erofscompressor");
+
 	tls = calloc(1, sizeof(*tls));
 	if (!tls)
 		return NULL;
lib/workqueue.c +13 -0
index 18ee0f9..1811c9c 100644
@@ -3,6 +3,19 @@
 #include <stdlib.h>
 #include "erofs/workqueue.h"
 
+#if defined(__linux__)
+#include <sys/prctl.h>
+#endif
+
+void erofs_set_thread_name(const char *name)
+{
+#if defined(__linux__)
+	prctl(PR_SET_NAME, name);
+#elif defined(__APPLE__)
+	pthread_setname_np(name);
+#endif
+}
+
 static void *worker_thread(void *arg)
 {
 	struct erofs_workqueue *wq = arg;
Signature · 1 line
2.51.0