Merge pull request #338 from denis2342/threadname small patch to add names to threads, should work for linux, freebsd and osx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
diff --git a/cgminer.c b/cgminer.c
index 8a84c20..2628b59 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3048,6 +3048,8 @@ static void *get_work_thread(void *userdata)
pthread_detach(pthread_self());
+ RenameThread("get_work");
+
applog(LOG_DEBUG, "Creating extra get work thread");
retry:
@@ -3231,6 +3233,8 @@ static void *submit_work_thread(void *userdata)
pthread_detach(pthread_self());
+ RenameThread("submit_work");
+
applog(LOG_DEBUG, "Creating extra submit work thread");
check_solve(work);
@@ -3674,6 +3678,8 @@ static void *stage_thread(void *userdata)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("stage");
+
while (ok) {
struct work *work = NULL;
@@ -4353,6 +4359,8 @@ static void *input_thread(void __maybe_unused *userdata)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("input");
+
if (!curses_active)
return NULL;
@@ -4389,6 +4397,8 @@ static void *workio_thread(void *userdata)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("work_io");
+
while (ok) {
struct workio_cmd *wc;
@@ -4428,6 +4438,8 @@ static void *api_thread(void *userdata)
pthread_detach(pthread_self());
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("api");
+
api(api_thr_id);
PTH(mythr) = 0L;
@@ -4674,6 +4686,8 @@ static void *stratum_thread(void *userdata)
pthread_detach(pthread_self());
+ RenameThread("stratum");
+
while (42) {
struct timeval timeout;
fd_set rd;
@@ -5507,6 +5521,10 @@ void *miner_thread(void *userdata)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ char threadname[20];
+ snprintf(threadname,20,"miner %d",thr_id);
+ RenameThread(threadname);
+
gettimeofday(&getwork_start, NULL);
if (api->thread_init && !api->thread_init(mythr)) {
@@ -5762,6 +5780,8 @@ static void *longpoll_thread(void *userdata)
char *lp_url;
int rolltime;
+ RenameThread("longpoll");
+
curl = curl_easy_init();
if (unlikely(!curl)) {
applog(LOG_ERR, "CURL initialisation failed");
@@ -5953,6 +5973,8 @@ static void *watchpool_thread(void __maybe_unused *userdata)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("watchpool");
+
while (42) {
struct timeval now;
int i;
@@ -6024,6 +6046,8 @@ static void *watchdog_thread(void __maybe_unused *userdata)
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ RenameThread("watchdog");
+
memset(&zero_tv, 0, sizeof(struct timeval));
gettimeofday(&rotate_tv, NULL);
diff --git a/util.c b/util.c
index 9262f64..c4b065f 100644
--- a/util.c
+++ b/util.c
@@ -1496,3 +1496,19 @@ void *realloc_strcat(char *ptr, char *s)
free(ptr);
return ret;
}
+
+void RenameThread(const char* name)
+{
+#if defined(PR_SET_NAME)
+ // Only the first 15 characters are used (16 - NUL terminator)
+ prctl(PR_SET_NAME, name, 0, 0, 0);
+#elif (defined(__FreeBSD__) || defined(__OpenBSD__))
+ pthread_set_name_np(pthread_self(), name);
+#elif defined(MAC_OSX)
+ pthread_setname_np(name);
+#else
+ // Prevent warnings for unused parameters...
+ (void)name;
+#endif
+}
+