Change in osmo-trx[master]: Use pthread_setname_np to name threads

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Thu Sep 20 16:09:09 UTC 2018


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/11047


Change subject: Use pthread_setname_np to name threads
......................................................................

Use pthread_setname_np to name threads

osmo-trx can start a considerable amount of threads that can make
debugging it challenging at least. By using phtread_setname_np, the
system sets a meaningful name to the thread which can be seen while
debugging with gdb or by printing /proc/$pid/task/$tid/comm.

Now we also log system TID when setting the name so we can identify
different tasks in /proc even if pthread_setname_np fails.

Change-Id: I84711739c3e224cb383fd12b6db933785b28209e
---
M CommonLibs/Threads.cpp
M CommonLibs/Threads.h
M Transceiver52M/Transceiver.cpp
M Transceiver52M/device/uhd/UHDDevice.cpp
M Transceiver52M/radioInterface.cpp
M tests/CommonLibs/Makefile.am
6 files changed, 43 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/47/11047/1

diff --git a/CommonLibs/Threads.cpp b/CommonLibs/Threads.cpp
index de6520b..2988e12 100644
--- a/CommonLibs/Threads.cpp
+++ b/CommonLibs/Threads.cpp
@@ -24,11 +24,17 @@
 */
 
 
-
-
+#include <string.h>
+#include <sys/types.h>
 
 #include "Threads.h"
 #include "Timeval.h"
+#include "Logger.h"
+
+#ifndef gettid
+#include <sys/syscall.h>
+#define gettid() syscall(SYS_gettid)
+#endif
 
 
 using namespace std;
@@ -102,6 +108,19 @@
 	pthread_cond_timedwait(&mSignal,&wMutex.mMutex,&waitTime);
 }
 
+void set_selfthread_name(const char *name)
+{
+	pthread_t selfid = pthread_self();
+	pid_t tid = gettid();
+	if (pthread_setname_np(selfid, name) == 0) {
+		LOG(INFO) << "Thread "<< selfid << " (task " << tid << ") set name: " << name;
+	} else {
+		char buf[256];
+		int err = errno;
+		char* err_str = strerror_r(err, buf, sizeof(buf));
+		LOG(NOTICE) << "Thread "<< selfid << " (task " << tid << ") set name \"" << name << "\" failed: (" << err << ") " << err_str;
+	}
+}
 
 void Thread::start(void *(*task)(void*), void *arg)
 {
diff --git a/CommonLibs/Threads.h b/CommonLibs/Threads.h
index 47c7275..857c5d9 100644
--- a/CommonLibs/Threads.h
+++ b/CommonLibs/Threads.h
@@ -141,6 +141,8 @@
 #define START_THREAD(thread,function,argument) \
 	thread.start((void *(*)(void*))function, (void*)argument);
 
+void set_selfthread_name(const char *name);
+
 /** A C++ wrapper for pthread threads.  */
 class Thread {
 
diff --git a/Transceiver52M/Transceiver.cpp b/Transceiver52M/Transceiver.cpp
index cdfd79d..076db3e 100644
--- a/Transceiver52M/Transceiver.cpp
+++ b/Transceiver52M/Transceiver.cpp
@@ -1044,11 +1044,15 @@
 
 void *RxUpperLoopAdapter(TransceiverChannel *chan)
 {
+  char thread_name[16];
   Transceiver *trx = chan->trx;
   size_t num = chan->num;
 
   delete chan;
 
+  snprintf(thread_name, 16, "RxUpper%zu", num);
+  set_selfthread_name(thread_name);
+
   trx->setPriority(0.42);
 
   while (1) {
@@ -1060,6 +1064,8 @@
 
 void *RxLowerLoopAdapter(Transceiver *transceiver)
 {
+  set_selfthread_name("RxLower");
+
   transceiver->setPriority(0.45);
 
   while (1) {
@@ -1071,6 +1077,8 @@
 
 void *TxLowerLoopAdapter(Transceiver *transceiver)
 {
+  set_selfthread_name("TxLower");
+
   transceiver->setPriority(0.44);
 
   while (1) {
@@ -1082,11 +1090,15 @@
 
 void *ControlServiceLoopAdapter(TransceiverChannel *chan)
 {
+  char thread_name[16];
   Transceiver *trx = chan->trx;
   size_t num = chan->num;
 
   delete chan;
 
+  snprintf(thread_name, 16, "CtrlService%zu", num);
+  set_selfthread_name(thread_name);
+
   while (1) {
     trx->driveControl(num);
     pthread_testcancel();
@@ -1096,11 +1108,15 @@
 
 void *TxUpperLoopAdapter(TransceiverChannel *chan)
 {
+  char thread_name[16];
   Transceiver *trx = chan->trx;
   size_t num = chan->num;
 
   delete chan;
 
+  snprintf(thread_name, 16, "TxUpper%zu", num);
+  set_selfthread_name(thread_name);
+
   trx->setPriority(0.40);
 
   while (1) {
diff --git a/Transceiver52M/device/uhd/UHDDevice.cpp b/Transceiver52M/device/uhd/UHDDevice.cpp
index b7109a4..c30f3a7 100644
--- a/Transceiver52M/device/uhd/UHDDevice.cpp
+++ b/Transceiver52M/device/uhd/UHDDevice.cpp
@@ -322,6 +322,7 @@
 
 void *async_event_loop(uhd_device *dev)
 {
+	set_selfthread_name("UHDAsyncEvent");
 	dev->setPriority(0.43);
 
 	while (1) {
diff --git a/Transceiver52M/radioInterface.cpp b/Transceiver52M/radioInterface.cpp
index 7d6a03b..0f949d7 100644
--- a/Transceiver52M/radioInterface.cpp
+++ b/Transceiver52M/radioInterface.cpp
@@ -148,6 +148,7 @@
 /** synchronization thread loop */
 void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface)
 {
+  set_selfthread_name("AlignRadio");
   while (1) {
     sleep(60);
     radioInterface->alignRadio();
diff --git a/tests/CommonLibs/Makefile.am b/tests/CommonLibs/Makefile.am
index 4543c72..2a9a021 100644
--- a/tests/CommonLibs/Makefile.am
+++ b/tests/CommonLibs/Makefile.am
@@ -28,11 +28,11 @@
 
 InterthreadTest_SOURCES = InterthreadTest.cpp
 InterthreadTest_LDADD = $(COMMON_LA)
-InterthreadTest_LDFLAGS = -lpthread
+InterthreadTest_LDFLAGS = -lpthread $(AM_LDFLAGS)
 
 SocketsTest_SOURCES = SocketsTest.cpp
 SocketsTest_LDADD = $(COMMON_LA)
-SocketsTest_LDFLAGS = -lpthread
+SocketsTest_LDFLAGS = -lpthread $(AM_LDFLAGS)
 
 TimevalTest_SOURCES = TimevalTest.cpp
 TimevalTest_LDADD = $(COMMON_LA)

-- 
To view, visit https://gerrit.osmocom.org/11047
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I84711739c3e224cb383fd12b6db933785b28209e
Gerrit-Change-Number: 11047
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180920/e85115b6/attachment.htm>


More information about the gerrit-log mailing list