Browse Source

Set recognizable names on threads for debugging

rename_thr copied from bitcoind's new RenameThread
Luke Dashjr 13 years ago
parent
commit
91fdbfa06d
7 changed files with 48 additions and 0 deletions
  1. 4 0
      api.c
  2. 16 0
      cgminer.c
  3. 1 0
      configure.ac
  4. 1 0
      driver-opencl.c
  5. 2 0
      findnonce.c
  6. 1 0
      miner.h
  7. 23 0
      util.c

+ 4 - 0
api.c

@@ -3101,6 +3101,8 @@ popipo:
 
 static void *quit_thread(__maybe_unused void *userdata)
 {
+	rename_thr("bfg-rpc-quit");
+
 	// allow thread creator to finish whatever it's doing
 	mutex_lock(&quit_restart_lock);
 	mutex_unlock(&quit_restart_lock);
@@ -3115,6 +3117,8 @@ static void *quit_thread(__maybe_unused void *userdata)
 
 static void *restart_thread(__maybe_unused void *userdata)
 {
+	rename_thr("bfg-rpc-restart");
+
 	// allow thread creator to finish whatever it's doing
 	mutex_lock(&quit_restart_lock);
 	mutex_unlock(&quit_restart_lock);

+ 16 - 0
cgminer.c

@@ -2283,6 +2283,7 @@ static void *get_work_thread(void *userdata)
 	int failures = 0;
 
 	pthread_detach(pthread_self());
+	rename_thr("bfg-get_work");
 
 	applog(LOG_DEBUG, "Creating extra get work thread");
 
@@ -2422,6 +2423,7 @@ static void *submit_work_thread(void *userdata)
 	int failures = 0;
 
 	pthread_detach(pthread_self());
+	rename_thr("bfg-submit_work");
 
 	applog(LOG_DEBUG, "Creating extra submit work thread");
 
@@ -2786,6 +2788,7 @@ static void *stage_thread(void *userdata)
 	struct thr_info *mythr = userdata;
 	bool ok = true;
 
+	rename_thr("bfg-stage");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	while (ok) {
@@ -3404,6 +3407,7 @@ retry:
 
 static void *input_thread(void __maybe_unused *userdata)
 {
+	rename_thr("bfg-input");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	if (!curses_active)
@@ -3440,6 +3444,7 @@ static void *workio_thread(void *userdata)
 	struct thr_info *mythr = userdata;
 	bool ok = true;
 
+	rename_thr("bfg-workio");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	while (ok) {
@@ -3479,6 +3484,7 @@ static void *api_thread(void *userdata)
 	struct thr_info *mythr = userdata;
 
 	pthread_detach(pthread_self());
+	rename_thr("bfg-rpc");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	api(api_thr_id);
@@ -4126,6 +4132,12 @@ void *miner_thread(void *userdata)
 	struct cgminer_stats *pool_stats;
 	struct timeval getwork_start;
 
+	{
+		char thrname[16];
+		sprintf(thrname, "bfg-miner-%s%d", api->name, cgpu->device_id);
+		rename_thr(thrname);
+	}
+
 	/* Try to cycle approximately 5 times before each log update */
 	const long cycle = opt_log_interval / 5 ? : 1;
 	struct timeval tv_start, tv_end, tv_workstart, tv_lastupdate;
@@ -4411,6 +4423,8 @@ static void *longpoll_thread(void *userdata)
 	int failures = 0;
 	int rolltime;
 
+	rename_thr("bfg-longpoll");
+
 	curl = curl_easy_init();
 	if (unlikely(!curl)) {
 		applog(LOG_ERR, "CURL initialisation failed");
@@ -4531,6 +4545,7 @@ static void reap_curl(struct pool *pool)
 
 static void *watchpool_thread(void __maybe_unused *userdata)
 {
+	rename_thr("bfg-watchpool");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	while (42) {
@@ -4597,6 +4612,7 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 	const unsigned int interval = WATCHDOG_INTERVAL;
 	struct timeval zero_tv;
 
+	rename_thr("bfg-watchdog");
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 	memset(&zero_tv, 0, sizeof(struct timeval));

+ 1 - 0
configure.ac

@@ -57,6 +57,7 @@ gl_INIT
 dnl Checks for header files.
 AC_HEADER_STDC
 AC_CHECK_HEADERS(syslog.h)
+AC_CHECK_HEADERS([sys/prctl.h])
 
 AC_FUNC_ALLOCA
 

+ 1 - 0
driver-opencl.c

@@ -1141,6 +1141,7 @@ void *reinit_gpu(void *userdata)
 	int gpu;
 
 	pthread_detach(pthread_self());
+	rename_thr("bfg-reinit_gpu");
 
 select_cgpu:
 	cgpu = tq_pop(mythr->q, NULL);

+ 2 - 0
findnonce.c

@@ -17,6 +17,7 @@
 #include <string.h>
 
 #include "findnonce.h"
+#include "miner.h"
 #include "scrypt.h"
 
 const uint32_t SHA256_K[64] = {
@@ -241,6 +242,7 @@ static void *postcalc_hash(void *userdata)
 	int entry = 0, nonces = 0;
 
 	pthread_detach(pthread_self());
+	rename_thr("bfg-postcalchsh");
 
 	for (entry = 0; entry < FOUND; entry++) {
 		uint32_t nonce = pcd->res[entry];

+ 1 - 0
miner.h

@@ -440,6 +440,7 @@ extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*s
 extern void thr_info_cancel(struct thr_info *thr);
 extern void thr_info_freeze(struct thr_info *thr);
 extern void nmsleep(unsigned int msecs);
+extern void rename_thr(const char* name);
 
 struct string_elist {
 	char *string;

+ 23 - 0
util.c

@@ -1,6 +1,8 @@
 /*
  * Copyright 2011-2012 Con Kolivas
  * Copyright 2010 Jeff Garzik
+ * Copyright 2012 Giel van Schijndel
+ * Copyright 2012 Gavin Andresen
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -16,12 +18,19 @@
 #include <ctype.h>
 #include <stdarg.h>
 #include <string.h>
+#include <pthread.h>
 #include <jansson.h>
 #include <curl/curl.h>
 #include <time.h>
 #include <errno.h>
 #include <unistd.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_PRCTL_H
+# include <sys/prctl.h>
+#endif
+#if defined(__FreeBSD__) || defined(__OpenBSD__)
+# include <pthread_np.h>
+#endif
 #ifndef WIN32
 # include <sys/socket.h>
 # include <netinet/in.h>
@@ -705,3 +714,17 @@ void nmsleep(unsigned int msecs)
 		ret = nanosleep(&twait, &tleft);
 	} while (ret == -1 && errno == EINTR);
 }
+
+void rename_thr(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(__APPLE__)
+	pthread_setname_np(name);
+#elif defined(__FreeBSD__) || defined(__OpenBSD__)
+	pthread_set_name_np(pthread_self(), name);
+#else
+	// Prevent warnings for unused parameters...
+	(void)name;
+#endif
+}