Add libuv thread names support to FATAL log level (#9382)

* Add support for the fatal() family of calls to detect non-netdata thread names from the OS
This commit is contained in:
Markos Fountoulakis 2020-06-20 22:13:22 +03:00 committed by GitHub
parent ae944cd492
commit 431b1c8569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 4 deletions

View File

@ -265,6 +265,12 @@ LIBS="${PTHREAD_LIBS} ${LIBS}"
CFLAGS="${CFLAGS} ${PTHREAD_CFLAGS}"
CC="${PTHREAD_CC}"
AC_CHECK_LIB(
[pthread],
[pthread_getname_np],
[AC_DEFINE([HAVE_PTHREAD_GETNAME_NP], [1], [Is set if pthread_getname_np is available])]
)
# -----------------------------------------------------------------------------
# libm

View File

@ -796,6 +796,8 @@ void fatal_int( const char *file, const char *function, const unsigned long line
// save a copy of errno - just in case this function generates a new error
int __errno = errno;
va_list args;
const char *thread_tag;
char os_threadname[NETDATA_THREAD_NAME_MAX + 1];
if(error_log_syslog) {
va_start( args, fmt );
@ -803,14 +805,22 @@ void fatal_int( const char *file, const char *function, const unsigned long line
va_end( args );
}
thread_tag = netdata_thread_tag();
if (!netdata_thread_tag_exists()) {
os_thread_get_current_name_np(os_threadname);
if ('\0' != os_threadname[0]) { /* If it is not an empty string replace "MAIN" thread_tag */
thread_tag = os_threadname;
}
}
char date[LOG_DATE_LENGTH];
log_date(date, LOG_DATE_LENGTH);
log_lock();
va_start( args, fmt );
if(debug_flags) fprintf(stderr, "%s: %s FATAL : %s : (%04lu@%-10.10s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
else fprintf(stderr, "%s: %s FATAL : %s :", date, program_name, netdata_thread_tag());
if(debug_flags) fprintf(stderr, "%s: %s FATAL : %s : (%04lu@%-10.10s:%-15.15s): ", date, program_name, thread_tag, line, file, function);
else fprintf(stderr, "%s: %s FATAL : %s : ", date, program_name, thread_tag);
vfprintf( stderr, fmt, args );
va_end( args );
@ -823,7 +833,7 @@ void fatal_int( const char *file, const char *function, const unsigned long line
snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, __errno);
char action_result[60+1];
snprintfz(action_result, 60, "%s:%s", program_name, strncmp(netdata_thread_tag(), "STREAM_RECEIVER", strlen("STREAM_RECEIVER"))?netdata_thread_tag():"[x]");
snprintfz(action_result, 60, "%s:%s", program_name, strncmp(thread_tag, "STREAM_RECEIVER", strlen("STREAM_RECEIVER")) ? thread_tag : "[x]");
send_statistics("FATAL", action_result, action_data);
netdata_cleanup_and_exit(1);

View File

@ -18,8 +18,12 @@ typedef struct {
static __thread NETDATA_THREAD *netdata_thread = NULL;
inline int netdata_thread_tag_exists(void) {
return (netdata_thread && netdata_thread->tag && *netdata_thread->tag);
}
const char *netdata_thread_tag(void) {
return ((netdata_thread && netdata_thread->tag && *netdata_thread->tag)?netdata_thread->tag:"MAIN");
return (netdata_thread_tag_exists() ? netdata_thread->tag : "MAIN");
}
// ----------------------------------------------------------------------------
@ -151,6 +155,18 @@ void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
info("cannot set libuv thread name to %s. Err: %d", threadname, ret);
}
void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
{
int ret = 0;
threadname[0] = '\0';
#if defined(__FreeBSD__)
pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
#elif defined(HAVE_PTHREAD_GETNAME_NP) /* Linux & macOS */
(void)pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
#endif
}
static void *thread_start(void *ptr) {
netdata_thread = (NETDATA_THREAD *)ptr;

View File

@ -22,6 +22,7 @@ typedef pthread_t netdata_thread_t;
#define NETDATA_THREAD_TAG_MAX 100
extern const char *netdata_thread_tag(void);
extern int netdata_thread_tag_exists(void);
extern size_t netdata_threads_init(void);
extern void netdata_threads_init_after_fork(size_t stacksize);
@ -33,6 +34,7 @@ extern int netdata_thread_detach(pthread_t thread);
#define NETDATA_THREAD_NAME_MAX 15
extern void uv_thread_set_name_np(uv_thread_t ut, const char* name);
extern void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]);
#define netdata_thread_self pthread_self
#define netdata_thread_testcancel pthread_testcancel