📄 dbg_printf.c
字号:
{ MPIU_DBG_OTHER, "OTHER", "other" }, { MPIU_DBG_CH3_CONNECT, "CH3_CONNECT", "ch3_connect" }, { MPIU_DBG_CH3_DISCONNECT,"CH3_DISCONNECT","ch3_disconnect" }, { MPIU_DBG_CH3_PROGRESS, "CH3_PROGRESS", "ch3_progress" }, { MPIU_DBG_CH3_CHANNEL, "CH3_CHANNEL", "ch3_channel" }, { MPIU_DBG_CH3_MSG, "CH3_MSG", "ch3_msg" }, { MPIU_DBG_CH3_OTHER, "CH3_OTHER", "ch3_other" }, { MPIU_DBG_CH3, "CH3", "ch3" }, { MPIU_DBG_NEM_SOCK_FUNC, "NEM_SOCK_FUNC", "nem_sock_func"}, { MPIU_DBG_NEM_SOCK_DET, "NEM_SOCK_DET", "nem_sock_det"}, { MPIU_DBG_VC, "VC", "vc"}, { MPIU_DBG_REFCOUNT, "REFCOUNT", "refcount"}, { MPIU_DBG_ALL, "ALL", "all" }, { 0, 0, 0 }};/* Because the level values are simpler and are rarely changed, these use a simple set of parallel arrays */static const int MPIU_Levelvalues[] = { MPIU_DBG_TERSE, MPIU_DBG_TYPICAL, MPIU_DBG_VERBOSE, 100 };static const char *MPIU_Levelname[] = { "TERSE", "TYPICAL", "VERBOSE", 0 };static const char *MPIU_LCLevelname[] = { "terse", "typical", "verbose", 0 };/* * Initialize the DBG_MSG system. This is called during MPI_Init to process * command-line arguments as well as checking the MPICH_DBG environment * variables. The initialization is split into two steps: a preinit and an * init. This makes it possible to enable most of the features before calling * MPID_Init, where a significant amount of the initialization takes place. */static int MPIU_DBG_ProcessArgs( int *argc_p, char ***argv_p ){ int i, rc; /* Here's where we do the same thing with the command-line options */ if (argc_p) { for (i=1; i<*argc_p; i++) { if (strncmp((*argv_p)[i],"-mpich-dbg", 10) == 0) { char *s = (*argv_p)[i] + 10; /* Found a command */ if (*s == 0) { /* Just -mpich-dbg */ MPIU_DBG_MaxLevel = MPIU_DBG_TYPICAL; MPIU_DBG_ActiveClasses = MPIU_DBG_ALL; } else if (*s == '=') { /* look for file */ MPIU_DBG_MaxLevel = MPIU_DBG_TYPICAL; MPIU_DBG_ActiveClasses = MPIU_DBG_ALL; s++; if (strncmp( s, "file", 4 ) == 0) { filePattern = defaultFilePattern; } } else if (strncmp(s,"-level",6) == 0) { char *p = s + 6; if (*p == '=') { p++; rc = SetDBGLevel( p, MPIU_LCLevelname ); if (rc) MPIU_DBG_Usage( "-mpich-dbg-level", "terse, typical, verbose" ); } } else if (strncmp(s,"-class",6) == 0) { char *p = s + 6; if (*p == '=') { p++; rc = setDBGClass( p ); if (rc) MPIU_DBG_Usage( "-mpich-dbg-class", 0 ); } } else if (strncmp( s, "-filename", 9 ) == 0) { char *p = s + 9; if (*p == '=') { p++; /* A special case for a filepattern of "-default", use the predefined default pattern */ if (strcmp( p, "-default" ) == 0) { filePattern = defaultFilePattern; } else { filePattern = MPIU_Strdup( p ); } } } else if (strncmp( s, "-rank", 5 ) == 0) { char *p = s + 5; if (*p == '=' && p[1] != 0) { char *sOut; p++; whichRank = strtol( p, &sOut, 10 ); if (p == sOut) { MPIU_DBG_Usage( "-mpich-dbg-rank", 0 ); whichRank = -1; } } } else { MPIU_DBG_Usage( (*argv_p)[i], 0 ); } /* Eventually, should null it out and reduce argc value */ } } } return MPI_SUCCESS;}static int MPIU_DBG_ProcessEnv( void ){ char *s; int rc; s = getenv( "MPICH_DBG" ); if (s) { /* Set the defaults */ MPIU_DBG_MaxLevel = MPIU_DBG_TYPICAL; MPIU_DBG_ActiveClasses = MPIU_DBG_ALL; if (strncmp(s,"FILE",4) == 0) { filePattern = defaultFilePattern; } } s = getenv( "MPICH_DBG_LEVEL" ); if (s) { rc = SetDBGLevel( s, MPIU_Levelname ); if (rc) MPIU_DBG_Usage( "MPICH_DBG_LEVEL", "TERSE, TYPICAL, VERBOSE" ); } s = getenv( "MPICH_DBG_CLASS" ); rc = setDBGClass( s ); if (rc) MPIU_DBG_Usage( "MPICH_DBG_CLASS", 0 ); s = getenv( "MPICH_DBG_FILENAME" ); if (s) { filePattern = MPIU_Strdup( s ); } s = getenv( "MPICH_DBG_RANK" ); if (s) { char *sOut; whichRank = strtol( s, &sOut, 10 ); if (s == sOut) { MPIU_DBG_Usage( "MPICH_DBG_RANK", 0 ); whichRank = -1; } } return MPI_SUCCESS;}/* * Attempt to initialize the logging system. This works only if MPID_Init * is not responsible for updating the environment and/or command-line * arguments. */int MPIU_DBG_PreInit( int *argc_p, char ***argv_p ){ MPID_Time_t t; /* if the DBG_MSG system was already initialized, say by the device, then return immediately */ if (mpiu_dbg_initialized != 0) return 0; /* Check to see if any debugging was selected. The order of these tests is important, as they allow general defaults to be set, followed by more specific modifications */ /* First, the environment variables */ MPIU_DBG_ProcessEnv(); MPIU_DBG_ProcessArgs( argc_p, argv_p ); MPID_Wtime( &t ); MPID_Wtime_todouble( &t, &timeOrigin ); mpiu_dbg_initialized = 1; return MPI_SUCCESS;}int MPIU_DBG_Init( int *argc_p, char ***argv_p, int has_args, int has_env, int wrank ){ /* if the DBG_MSG system was already initialized, say by the device, then return immediately. Note that the device is then responsible for handling the file mode (e.g., reopen when the rank become available) */ if (mpiu_dbg_initialized == 2) return 0; /* Check to see if any debugging was selected. The order of these tests is important, as they allow general defaults to be set, followed by more specific modifications. */ /* Both of these may have already been set in the PreInit call; if the command line and/or environment variables are set before MPID_Init, then don't call the routines to check those values (as they were already handled in DBG_PreInit) */ /* First, the environment variables */ if (!has_env) MPIU_DBG_ProcessEnv(); /* Now the command-line arguments */ if (!has_args) MPIU_DBG_ProcessArgs( argc_p, argv_p ); worldRank = wrank; if (whichRank >= 0 && whichRank != wrank) { /* Turn off logging on this process */ MPIU_DBG_ActiveClasses = 0; } /* If the file has already been opened and we need to reopen it, do that now. We only need to close the file, since OutEvent will reopen it as necessary */ if (filemode == MPIU_DBG_REOPEN && filestate == MPIU_DBG_PRERANK) { fclose( MPIU_DBG_fp ); MPIU_DBG_fp = 0; } mpiu_dbg_initialized = 2; return 0;}/* Print the usage statement to stderr */static int MPIU_DBG_Usage( const char *cmd, const char *vals ){ if (vals) { fprintf( stderr, "Incorrect value for %s, should be one of %s\n", cmd, vals ); } else { fprintf( stderr, "Incorrect value for %s\n", cmd ); } fprintf( stderr, "Command line for debug switches\n\ -mpich-dbg-class=name[,name,...]\n\ -mpich-dbg-level=name (one of terse, typical, verbose)\n\ -mpich-dbg-filename=pattern (includes %%d for world rank, %%t for thread id\n\ -mpich-dbg-rank=val (only this rank in COMM_WORLD will be logged)\n\ -mpich-dbg (shorthand for -mpich-dbg-class=all -mpich-dbg-level=typical)\n\ -mpich-dbg=file (shorthand for -mpich-dbg -mpich-dbg-filename=%s)\n\Environment variables\n\ MPICH_DBG_CLASS=NAME[,NAME...]\n\ MPICH_DBG_LEVEL=NAME\n\ MPICH_DBG_FILENAME=pattern\n\ MPICH_DBG_RANK=val\n\ MPICH_DBG=YES or FILE\n", defaultFilePattern ); fflush(stderr); return 0;}#ifndef MAXPATHLEN#define MAXPATHLEN 1024#endif/* This routine can make no MPI calls, since it may be logging those calls. */static int MPIU_DBG_OpenFile( void ){ int withinMworld = 0, /* True if within an @W...@ */ withinMthread = 0; /* True if within an @T...@ */ /* FIXME: Need to know how many MPI_COMM_WORLDs are known */ int nWorld = 1;#ifdef MPICH_IS_THREADED int threadID = 0; int nThread = 2;#else int nThread = 1;#endif static char worldNumAsChar[10] = "0"; /* FIXME: This is a hack to handle the common case of two worlds */ if (MPIR_Process.comm_parent != NULL) { nWorld = 2; worldNumAsChar[0] = '1'; worldNumAsChar[1] = '\0'; } if (!filePattern || *filePattern == 0 || strcmp(filePattern, "-stdout-" ) == 0) { filestate = MPIU_DBG_OPEN; MPIU_DBG_fp = stdout; } else if (strcmp( filePattern, "-stderr-" ) == 0) { filestate = MPIU_DBG_OPEN; MPIU_DBG_fp = stderr; } else { char filename[MAXPATHLEN], *pDest, *p; p = filePattern; pDest = filename; *filename = 0; while (*p && (pDest-filename) < MAXPATHLEN) { /* There are two special cases that allow text to be optionally included. Those patterns are @T...@ (only if multi-threaded) and @W...@ (only if more than one MPI_COMM_WORLD) UNIMPLEMENTED/UNTESTED */ if (*p == '@') { /* Escaped @? */ if (p[1] == '@') { *pDest++ = *++p; continue; } /* If within an @...@, terminate it */ if (withinMworld) { withinMworld = 0; p++; } else if (withinMthread) { withinMthread = 0; p++; } else { /* Look for command */ p++; if (*p == 'W') { p++; withinMworld = 1; } else if (*p == 'T') { p++; withinMthread = 1; } else { /* Unrecognized char */ *pDest++ = *p++; } } } else if ( (withinMworld && nWorld == 1) || (withinMthread && nThread == 1) ) { /* Simply skip this character since we're not showing this string */ p++; } else if (*p == '%') { p++; if (*p == 'd') { char rankAsChar[20]; MPIU_Snprintf( rankAsChar, sizeof(rankAsChar), "%d", worldRank ); *pDest = 0; MPIU_Strnapp( filename, rankAsChar, MAXPATHLEN ); pDest += strlen(rankAsChar); } else if (*p == 't') {#ifdef MPICH_IS_THREADED char threadIDAsChar[20]; MPE_Thread_id_t tid; MPE_Thread_self(&tid); threadID = (int)tid; MPIU_Snprintf( threadIDAsChar, sizeof(threadIDAsChar), "%d", threadID ); *pDest = 0; MPIU_Strnapp( filename, threadIDAsChar, MAXPATHLEN ); pDest += strlen(threadIDAsChar);#else *pDest++ = '0';#endif } else if (*p == 'w') { /* FIXME: Get world number */ /* *pDest++ = '0'; */ *pDest = 0; MPIU_Strnapp( filename, worldNumAsChar, MAXPATHLEN ); pDest += strlen(worldNumAsChar); } else { *pDest++ = '%'; *pDest++ = *p; } p++; } else { *pDest++ = *p++; } } *pDest = 0; if (worldRank == -1) filestate = MPIU_DBG_PRERANK; else filestate = MPIU_DBG_OPEN; MPIU_DBG_fp = fopen( filename, "w" ); if (!MPIU_DBG_fp) { MPIU_Error_printf( "Could not open log file %s\n", filename ); } } return 0;}/* Support routines for processing mpich-dbg values *//* Update the GLOBAL variable MPIU_DBG_ActiveClasses with the bits corresponding to this name */static int setDBGClass( const char *s ){ int i; int slen = 0; int len = 0; if (s && *s) slen = strlen(s); while (s && *s) { for (i=0; MPIU_Classnames[i].LCName; i++) { /* The LCLen and UCLen *should* be the same, but just in case, we separate them */ int LClen = strlen(MPIU_Classnames[i].LCName); int UClen = strlen(MPIU_Classnames[i].UCName); int matchClass = 0; /* Allow the upper case and lower case in all cases */ if (slen >= LClen && strncmp(s,MPIU_Classnames[i].LCName, LClen) == 0 && (s[LClen] == ',' || s[LClen] == 0) ) { matchClass = 1; len = LClen; } else if (slen >= UClen && strncmp(s,MPIU_Classnames[i].UCName, UClen) == 0 && (s[UClen] == ',' || s[UClen] == 0) ) { matchClass = 1; len = UClen; } if (matchClass) { MPIU_DBG_ActiveClasses |= MPIU_Classnames[i].classbits; s += len; slen -= len; if (*s == ',') { s++; slen--; } /* If we found a name, we need to restart the for loop */ break; } } if (!MPIU_Classnames[i].LCName) { return 1; } } return 0;}/* Set the global MPIU_DBG_MaxLevel if there is a match with the known level names */static int SetDBGLevel( const char *s, const char *(names[]) ){ int i; for (i=0; names[i]; i++) { if (strcmp( names[i], s ) == 0) { MPIU_DBG_MaxLevel = MPIU_Levelvalues[i]; return 0; } } return 1;}#endif /* USE_DBG_LOGGING */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -