Files
rocm-systems/src/include/debug.h
T

140 строки
4.3 KiB
C++
Исходник Обычный вид История

2018-09-24 16:06:59 -07:00
/*************************************************************************
2019-03-14 19:39:20 -07:00
* Copyright (c) 2015-2019, NVIDIA CORPORATION. All rights reserved.
2018-09-24 16:06:59 -07:00
*
* See LICENSE.txt for license information
************************************************************************/
#ifndef NCCL_DEBUG_H_
#define NCCL_DEBUG_H_
#include <pthread.h>
#include <stdio.h>
#include <chrono>
#include <unistd.h>
#include <sys/syscall.h>
#include <limits.h>
#include <string.h>
#include "nccl.h"
2018-11-13 10:37:20 -08:00
#include "nccl_net.h"
2018-09-24 16:06:59 -07:00
#define gettid() (pid_t) syscall(SYS_gettid)
2018-11-13 10:37:20 -08:00
extern int ncclDebugLevel;
2018-09-24 16:06:59 -07:00
extern uint64_t ncclDebugMask;
extern pthread_mutex_t ncclDebugOutputLock;
extern FILE *ncclDebugFile;
2019-03-14 19:39:20 -07:00
extern ncclResult_t getHostName(char* hostname, int maxlen, const char delim);
2018-12-13 15:56:12 -08:00
extern ncclResult_t getNvmlDevice(int cudaDev, int *nvmlDev);
2018-09-24 16:06:59 -07:00
2018-11-13 10:37:20 -08:00
extern void ncclDebugLog(ncclDebugLogLevel level, unsigned long flags, const char *filefunc, int line, const char *fmt, ...);
2018-09-24 16:06:59 -07:00
2018-11-13 10:37:20 -08:00
#define WARN(...) ncclDebugLog(NCCL_LOG_WARN, NCCL_ALL, __FILE__, __LINE__, __VA_ARGS__)
#define INFO(FLAGS, ...) ncclDebugLog(NCCL_LOG_INFO, (FLAGS), __func__, __LINE__, __VA_ARGS__)
2018-09-24 16:06:59 -07:00
#ifdef ENABLE_TRACE
2018-11-13 10:37:20 -08:00
#define TRACE(FLAGS, ...) ncclDebugLog(NCCL_LOG_TRACE, (FLAGS), __func__, __LINE__, __VA_ARGS__)
2018-09-24 16:06:59 -07:00
extern std::chrono::high_resolution_clock::time_point ncclEpoch;
#else
#define TRACE(...)
#endif
#include <stdlib.h>
static inline void initDebug() {
const char* nccl_debug = getenv("NCCL_DEBUG");
if (nccl_debug == NULL) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_NONE;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(nccl_debug, "VERSION") == 0) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_VERSION;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(nccl_debug, "WARN") == 0) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_WARN;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(nccl_debug, "INFO") == 0) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_INFO;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(nccl_debug, "ABORT") == 0) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_ABORT;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(nccl_debug, "TRACE") == 0) {
2018-11-13 10:37:20 -08:00
ncclDebugLevel = NCCL_LOG_TRACE;
2018-09-24 16:06:59 -07:00
}
/* Parse the NCCL_DEBUG_SUBSYS env var
* This can be a comma separated list such as INIT,COLL
* or ^INIT,COLL etc
*/
char* nccl_debug_subsys = getenv("NCCL_DEBUG_SUBSYS");
if (nccl_debug_subsys != NULL) {
char *subsys = strtok(nccl_debug_subsys, ",");
while (subsys != NULL) {
int invert = 0;
uint64_t mask = 0;
if (subsys[0] == '^') { invert = 1; subsys++; }
if (strcasecmp(subsys, "INIT") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_INIT;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(subsys, "COLL") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_COLL;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(subsys, "P2P") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_P2P;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(subsys, "SHM") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_SHM;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(subsys, "NET") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_NET;
2018-09-24 16:06:59 -07:00
} else if (strcasecmp(subsys, "ALL") == 0) {
2018-11-13 10:37:20 -08:00
mask = NCCL_ALL;
2018-09-24 16:06:59 -07:00
}
if (mask) {
if (invert) ncclDebugMask &= ~mask; else ncclDebugMask |= mask;
}
subsys = strtok(NULL, ",");
}
}
/* 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
*/
const char* nccl_debug_file = getenv("NCCL_DEBUG_FILE");
2018-11-13 10:37:20 -08:00
if (ncclDebugLevel > NCCL_LOG_VERSION && nccl_debug_file != NULL) {
2018-09-24 16:06:59 -07:00
int c = 0;
char debug_fn[PATH_MAX+1] = "";
char *dfn = debug_fn;
while (nccl_debug_file[c] != '\0' && c < PATH_MAX) {
if (nccl_debug_file[c++] != '%') {
*dfn++ = nccl_debug_file[c-1];
continue;
}
switch (nccl_debug_file[c++]) {
case '%': // Double %
*dfn++ = '%';
break;
case 'h': // %h = hostname
char hostname[1024];
2019-03-14 19:39:20 -07:00
getHostName(hostname, 1024, '.');
2018-09-24 16:06:59 -07:00
dfn += snprintf(dfn, PATH_MAX, "%s", hostname);
break;
case 'p': // %p = pid
dfn += snprintf(dfn, PATH_MAX, "%d", getpid());
break;
default: // Echo everything we don't understand
*dfn++ = '%';
*dfn++ = nccl_debug_file[c-1];
break;
}
}
*dfn = '\0';
if (debug_fn[0] != '\0') {
FILE *file = fopen(debug_fn, "w");
if (file != NULL) {
2018-11-13 10:37:20 -08:00
INFO(NCCL_ALL,"DEBUG file is '%s'", debug_fn);
2018-09-24 16:06:59 -07:00
ncclDebugFile = file;
}
}
}
pthread_mutex_init(&ncclDebugOutputLock, NULL);
#ifdef ENABLE_TRACE
ncclEpoch = std::chrono::high_resolution_clock::now();
#endif
}
#endif