Files
rocm-systems/src/debug.cc
T

211 wiersze
7.3 KiB
C++
Czysty Zwykły widok Historia

2019-11-19 14:57:39 -08:00
/*************************************************************************
2022-01-07 06:39:55 -08:00
* Copyright (c) 2016-2022, NVIDIA CORPORATION. All rights reserved.
2019-11-19 14:57:39 -08:00
*
* See LICENSE.txt for license information
************************************************************************/
#include "core.h"
#include "nccl_net.h"
#include <stdlib.h>
#include <stdarg.h>
2022-05-24 02:02:31 -07:00
#include <sys/syscall.h>
2023-09-26 05:47:28 -07:00
#include "param.h"
2019-11-19 14:57:39 -08:00
int ncclDebugLevel = -1;
2022-05-24 02:02:31 -07:00
static int pid = -1;
static char hostname[1024];
2019-11-19 14:57:39 -08:00
thread_local int ncclDebugNoWarn = 0;
2022-05-24 02:02:31 -07:00
char ncclLastError[1024] = ""; // Global string for the last error in human readable form
2023-02-02 12:52:47 -08:00
uint64_t ncclDebugMask = NCCL_INIT|NCCL_ENV; // Default debug sub-system mask is INIT and ENV
2019-11-19 14:57:39 -08:00
FILE *ncclDebugFile = stdout;
pthread_mutex_t ncclDebugLock = PTHREAD_MUTEX_INITIALIZER;
2022-05-24 02:02:31 -07:00
std::chrono::steady_clock::time_point ncclEpoch;
static __thread int tid = -1;
2019-11-19 14:57:39 -08:00
void ncclDebugInit() {
pthread_mutex_lock(&ncclDebugLock);
2020-05-12 14:40:18 -07:00
if (ncclDebugLevel != -1) { pthread_mutex_unlock(&ncclDebugLock); return; }
2023-09-26 05:47:28 -07:00
const char* nccl_debug = ncclGetEnv("NCCL_DEBUG");
2022-08-03 20:47:40 -07:00
int tempNcclDebugLevel = -1;
if (nccl_debug == NULL) {
tempNcclDebugLevel = NCCL_LOG_NONE;
} else if (strcasecmp(nccl_debug, "VERSION") == 0) {
tempNcclDebugLevel = NCCL_LOG_VERSION;
} else if (strcasecmp(nccl_debug, "WARN") == 0) {
tempNcclDebugLevel = NCCL_LOG_WARN;
} else if (strcasecmp(nccl_debug, "INFO") == 0) {
tempNcclDebugLevel = NCCL_LOG_INFO;
} else if (strcasecmp(nccl_debug, "ABORT") == 0) {
tempNcclDebugLevel = NCCL_LOG_ABORT;
} else if (strcasecmp(nccl_debug, "TRACE") == 0) {
tempNcclDebugLevel = NCCL_LOG_TRACE;
}
2019-11-19 14:57:39 -08:00
/* Parse the NCCL_DEBUG_SUBSYS env var
* This can be a comma separated list such as INIT,COLL
* or ^INIT,COLL etc
*/
2023-09-26 05:47:28 -07:00
const char* ncclDebugSubsysEnv = ncclGetEnv("NCCL_DEBUG_SUBSYS");
2019-11-19 14:57:39 -08:00
if (ncclDebugSubsysEnv != NULL) {
int invert = 0;
if (ncclDebugSubsysEnv[0] == '^') { invert = 1; ncclDebugSubsysEnv++; }
ncclDebugMask = invert ? ~0ULL : 0ULL;
char *ncclDebugSubsys = strdup(ncclDebugSubsysEnv);
char *subsys = strtok(ncclDebugSubsys, ",");
while (subsys != NULL) {
uint64_t mask = 0;
if (strcasecmp(subsys, "INIT") == 0) {
mask = NCCL_INIT;
} else if (strcasecmp(subsys, "COLL") == 0) {
mask = NCCL_COLL;
} else if (strcasecmp(subsys, "P2P") == 0) {
mask = NCCL_P2P;
} else if (strcasecmp(subsys, "SHM") == 0) {
mask = NCCL_SHM;
} else if (strcasecmp(subsys, "NET") == 0) {
mask = NCCL_NET;
} else if (strcasecmp(subsys, "GRAPH") == 0) {
mask = NCCL_GRAPH;
} else if (strcasecmp(subsys, "TUNING") == 0) {
mask = NCCL_TUNING;
2020-05-12 14:40:18 -07:00
} else if (strcasecmp(subsys, "ENV") == 0) {
mask = NCCL_ENV;
2021-05-11 18:16:30 -07:00
} else if (strcasecmp(subsys, "ALLOC") == 0) {
mask = NCCL_ALLOC;
2022-05-24 02:02:31 -07:00
} else if (strcasecmp(subsys, "CALL") == 0) {
mask = NCCL_CALL;
2023-04-03 05:32:07 -07:00
} else if (strcasecmp(subsys, "PROXY") == 0) {
mask = NCCL_PROXY;
2023-02-27 02:48:21 -08:00
} else if (strcasecmp(subsys, "NVLS") == 0) {
mask = NCCL_NVLS;
2019-11-19 14:57:39 -08:00
} else if (strcasecmp(subsys, "ALL") == 0) {
mask = NCCL_ALL;
}
if (mask) {
if (invert) ncclDebugMask &= ~mask; else ncclDebugMask |= mask;
}
subsys = strtok(NULL, ",");
}
free(ncclDebugSubsys);
}
2022-05-24 02:02:31 -07:00
// Cache pid and hostname
getHostName(hostname, 1024, '.');
pid = getpid();
2019-11-19 14:57:39 -08:00
/* Parse and expand the NCCL_DEBUG_FILE path and
* then create the debug file. But don't bother unless the
* NCCL_DEBUG level is > VERSION
*/
2023-09-26 05:47:28 -07:00
const char* ncclDebugFileEnv = ncclGetEnv("NCCL_DEBUG_FILE");
2022-08-03 20:47:40 -07:00
if (tempNcclDebugLevel > NCCL_LOG_VERSION && ncclDebugFileEnv != NULL) {
2019-11-19 14:57:39 -08:00
int c = 0;
char debugFn[PATH_MAX+1] = "";
char *dfn = debugFn;
while (ncclDebugFileEnv[c] != '\0' && c < PATH_MAX) {
if (ncclDebugFileEnv[c++] != '%') {
*dfn++ = ncclDebugFileEnv[c-1];
continue;
}
switch (ncclDebugFileEnv[c++]) {
case '%': // Double %
*dfn++ = '%';
break;
case 'h': // %h = hostname
dfn += snprintf(dfn, PATH_MAX, "%s", hostname);
break;
case 'p': // %p = pid
2022-05-24 02:02:31 -07:00
dfn += snprintf(dfn, PATH_MAX, "%d", pid);
2019-11-19 14:57:39 -08:00
break;
default: // Echo everything we don't understand
*dfn++ = '%';
*dfn++ = ncclDebugFileEnv[c-1];
break;
}
}
*dfn = '\0';
if (debugFn[0] != '\0') {
FILE *file = fopen(debugFn, "w");
2022-05-24 02:02:31 -07:00
if (file != nullptr) {
setbuf(file, nullptr); // disable buffering
2019-11-19 14:57:39 -08:00
ncclDebugFile = file;
}
}
}
2022-05-24 02:02:31 -07:00
ncclEpoch = std::chrono::steady_clock::now();
__atomic_store_n(&ncclDebugLevel, tempNcclDebugLevel, __ATOMIC_RELEASE);
2019-11-19 14:57:39 -08:00
pthread_mutex_unlock(&ncclDebugLock);
}
/* Common logging function used by the INFO, WARN and TRACE macros
* Also exported to the dynamically loadable Net transport modules so
* they can share the debugging mechanisms and output files
*/
void ncclDebugLog(ncclDebugLogLevel level, unsigned long flags, const char *filefunc, int line, const char *fmt, ...) {
2022-05-24 02:02:31 -07:00
if (__atomic_load_n(&ncclDebugLevel, __ATOMIC_ACQUIRE) == -1) ncclDebugInit();
2020-01-16 16:02:42 -08:00
if (ncclDebugNoWarn != 0 && level == NCCL_LOG_WARN) { level = NCCL_LOG_INFO; flags = ncclDebugNoWarn; }
2022-05-24 02:02:31 -07:00
// Save the last error (WARN) as a human readable string
if (level == NCCL_LOG_WARN) {
pthread_mutex_lock(&ncclDebugLock);
va_list vargs;
va_start(vargs, fmt);
(void) vsnprintf(ncclLastError, sizeof(ncclLastError), fmt, vargs);
va_end(vargs);
pthread_mutex_unlock(&ncclDebugLock);
}
2020-09-04 14:35:05 -07:00
if (ncclDebugLevel < level || ((flags & ncclDebugMask) == 0)) return;
2019-11-19 14:57:39 -08:00
2022-05-24 02:02:31 -07:00
if (tid == -1) {
tid = syscall(SYS_gettid);
}
2019-11-19 14:57:39 -08:00
int cudaDev;
2022-05-24 02:02:31 -07:00
if (!(level == NCCL_LOG_TRACE && flags == NCCL_CALL)) {
cudaGetDevice(&cudaDev);
}
2019-11-19 14:57:39 -08:00
char buffer[2048];
2019-11-19 14:57:39 -08:00
size_t len = 0;
2022-05-24 02:02:31 -07:00
if (level == NCCL_LOG_WARN) {
len = snprintf(buffer, sizeof(buffer), "\n%s:%d:%d [%d] %s:%d NCCL WARN ",
hostname, pid, tid, cudaDev, filefunc, line);
} else if (level == NCCL_LOG_INFO) {
len = snprintf(buffer, sizeof(buffer), "%s:%d:%d [%d] NCCL INFO ", hostname, pid, tid, cudaDev);
} else if (level == NCCL_LOG_TRACE && flags == NCCL_CALL) {
len = snprintf(buffer, sizeof(buffer), "%s:%d:%d NCCL CALL ", hostname, pid, tid);
} else if (level == NCCL_LOG_TRACE) {
auto delta = std::chrono::steady_clock::now() - ncclEpoch;
2019-11-19 14:57:39 -08:00
double timestamp = std::chrono::duration_cast<std::chrono::duration<double>>(delta).count()*1000;
2022-05-24 02:02:31 -07:00
len = snprintf(buffer, sizeof(buffer), "%s:%d:%d [%d] %f %s:%d NCCL TRACE ",
hostname, pid, tid, cudaDev, timestamp, filefunc, line);
2019-11-19 14:57:39 -08:00
}
2022-05-24 02:02:31 -07:00
2019-11-19 14:57:39 -08:00
if (len) {
va_list vargs;
va_start(vargs, fmt);
2022-05-24 02:02:31 -07:00
len += vsnprintf(buffer+len, sizeof(buffer)-len, fmt, vargs);
2019-11-19 14:57:39 -08:00
va_end(vargs);
2022-05-24 02:02:31 -07:00
buffer[len++] = '\n';
fwrite(buffer, 1, len, ncclDebugFile);
2019-11-19 14:57:39 -08:00
}
}
2022-01-07 06:39:55 -08:00
NCCL_PARAM(SetThreadName, "SET_THREAD_NAME", 0);
void ncclSetThreadName(pthread_t thread, const char *fmt, ...) {
// pthread_setname_np is nonstandard GNU extension
// needs the following feature test macro
#ifdef _GNU_SOURCE
if (ncclParamSetThreadName() != 1) return;
char threadName[NCCL_THREAD_NAMELEN];
va_list vargs;
va_start(vargs, fmt);
vsnprintf(threadName, NCCL_THREAD_NAMELEN, fmt, vargs);
va_end(vargs);
pthread_setname_np(thread, threadName);
#endif
}