-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing FILENAME_MAX in runtime-directory #17059
Changes from all commits
9d841d9
bba606e
2e63718
84b75d6
820aff0
021170e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ static char* nodelist = NULL; | |
static char* partition = NULL; | ||
static char* exclude = NULL; | ||
|
||
char slurmFilename[FILENAME_MAX]; | ||
char* slurmFilename = NULL; | ||
|
||
/* copies of binary to run per node */ | ||
#define procsPerNode 1 | ||
|
@@ -142,14 +142,13 @@ static int getCoresPerLocale(int nomultithread) { | |
|
||
return numCores; | ||
} | ||
#define MAX_COM_LEN (FILENAME_MAX + 128) | ||
// create the command that will actually launch the program and | ||
// create any files needed for the launch like the batch script | ||
static char* chpl_launch_create_command(int argc, char* argv[], | ||
int32_t numLocales) { | ||
int i; | ||
int size; | ||
char baseCommand[MAX_COM_LEN]; | ||
char* baseCommand=NULL; | ||
char* command; | ||
FILE* slurmFile; | ||
char* account = getenv("CHPL_LAUNCHER_ACCOUNT"); | ||
|
@@ -183,9 +182,9 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
// because they haven't been initialized yet | ||
char* bufferStdout = getenv("CHPL_LAUNCHER_SLURM_BUFFER_STDOUT"); | ||
const char* tmpDir = getTmpDir(); | ||
char stdoutFile [MAX_COM_LEN]; | ||
char stdoutFileNoFmt [MAX_COM_LEN]; | ||
char tmpStdoutFileNoFmt [MAX_COM_LEN]; | ||
char* stdoutFile = NULL; | ||
char* stdoutFileNoFmt = NULL; | ||
char* tmpStdoutFileNoFmt = NULL; | ||
|
||
// command line walltime takes precedence over env var | ||
if (!walltime) { | ||
|
@@ -244,6 +243,10 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
|
||
// if were running a batch job | ||
if (getenv("CHPL_LAUNCHER_USE_SBATCH") != NULL || generate_sbatch_script) { | ||
slurmFilename=(char *)chpl_mem_allocMany((strlen(baseSBATCHFilename) + | ||
snprintf(NULL, 0, "%d", (int)mypid) | ||
+ 1), sizeof(char), | ||
CHPL_RT_MD_FILENAME, -1, 0); | ||
// set the sbatch filename | ||
sprintf(slurmFilename, "%s%d", baseSBATCHFilename, (int)mypid); | ||
|
||
|
@@ -305,14 +308,25 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
// set the output file name to either the user specified | ||
// name or to the binaryName.<jobID>.out if none specified | ||
if (outputfn != NULL) { | ||
stdoutFile=(char *)chpl_mem_allocMany((strlen(outputfn) + 1), | ||
sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); | ||
sprintf(stdoutFile, "%s", outputfn); | ||
stdoutFileNoFmt=(char *)chpl_mem_allocMany((strlen(outputfn) + 1), | ||
sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); | ||
sprintf(stdoutFileNoFmt, "%s", outputfn); | ||
} | ||
else { | ||
sprintf(stdoutFile, "%s.%s.out", argv[0], "%j"); | ||
sprintf(stdoutFileNoFmt, "%s.%s.out", argv[0], "$SLURM_JOB_ID"); | ||
char* format="%s.%s.out"; | ||
int stdoutFileLen = strlen(format) + strlen(argv[0])+ strlen("%j"); | ||
stdoutFile=(char *)chpl_mem_allocMany(stdoutFileLen, sizeof(char), | ||
CHPL_RT_MD_FILENAME, -1, 0); | ||
sprintf(stdoutFile, format, argv[0], "%j"); | ||
char* tempArg = "$SLURM_JOB_ID"; | ||
int stdoutFileNoFmtLen = strlen(format) + strlen(argv[0]) + strlen(tempArg); | ||
stdoutFileNoFmt=(char *)chpl_mem_allocMany(stdoutFileNoFmtLen, | ||
sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); | ||
sprintf(stdoutFileNoFmt, format, argv[0], tempArg); | ||
} | ||
|
||
// We have slurm use the real output file to capture slurm errors/timeouts | ||
// We only redirect the program output to the tmp file | ||
fprintf(slurmFile, "#SBATCH --output=%s\n", stdoutFile); | ||
|
@@ -324,7 +338,13 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
// If we're buffering the output, set the temp output file name. | ||
// It's always <tmpDir>/binaryName.<jobID>.out. | ||
if (bufferStdout != NULL) { | ||
sprintf(tmpStdoutFileNoFmt, "%s/%s.%s.out", tmpDir, argv[0], "$SLURM_JOB_ID"); | ||
char* format = "%s/%s.%s.out"; | ||
char* tempArg = "$SLURM_JOB_ID"; | ||
int tmpStdoutFileNoFmtLen = strlen(format) + strlen(tmpDir) + | ||
strlen(argv[0]) + strlen(tempArg); | ||
tmpStdoutFileNoFmt = (char *)chpl_mem_allocMany(tmpStdoutFileNoFmtLen, | ||
sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); | ||
sprintf(tmpStdoutFileNoFmt, format, tmpDir, argv[0], tempArg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In cases like this where you've computed and stored the buffer length, wouldn't it be safer / more future-proof to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I kept the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your rationale makes sense, but if you're willing to make that change while here, I think that'd be great, thank you! |
||
} | ||
|
||
// add the srun command and the (possibly wrapped) binary name. | ||
|
@@ -348,6 +368,9 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
if (bufferStdout != NULL) { | ||
fprintf(slurmFile, "cat %s >> %s\n", tmpStdoutFileNoFmt, stdoutFileNoFmt); | ||
fprintf(slurmFile, "rm %s &> /dev/null\n", tmpStdoutFileNoFmt); | ||
//tmpStdoutFileNoFmt is only allocated memory if bufferStdout!=NULL | ||
//Hence free up inside this condition. | ||
chpl_mem_free(tmpStdoutFileNoFmt); | ||
} | ||
|
||
// close the batch file and change permissions | ||
|
@@ -360,7 +383,11 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
|
||
// the baseCommand is what will call the batch file | ||
// that was just created | ||
sprintf(baseCommand, "sbatch %s\n", slurmFilename); | ||
char* format = "sbatch %s\n"; | ||
int baseCommandLen = strlen(slurmFilename) + strlen(format); | ||
baseCommand=(char *)chpl_mem_allocMany(baseCommandLen, sizeof(char), | ||
CHPL_RT_MD_COMMAND_BUFFER, -1, 0); | ||
sprintf(baseCommand, format, slurmFilename); | ||
} | ||
// else we're running an interactive job | ||
else { | ||
|
@@ -431,15 +458,22 @@ static char* chpl_launch_create_command(int argc, char* argv[], | |
for (i=1; i<argc; i++) { | ||
len += sprintf(iCom+len, "%s ", argv[i]); | ||
} | ||
|
||
char* format = "srun %s "; | ||
int baseCommandLen = strlen(format) + len; | ||
baseCommand=(char *)chpl_mem_allocMany(baseCommandLen, sizeof(char), | ||
CHPL_RT_MD_COMMAND_BUFFER, -1, 0); | ||
// launch the job using srun | ||
sprintf(baseCommand, "srun %s ", iCom); | ||
sprintf(baseCommand, format, iCom); | ||
} | ||
|
||
// copy baseCommand into command and return it | ||
size = strlen(baseCommand) + 1; | ||
command = chpl_mem_allocMany(size, sizeof(char), CHPL_RT_MD_COMMAND_BUFFER, -1, 0); | ||
sprintf(command, "%s", baseCommand); | ||
//free dynamically allocated memory | ||
chpl_mem_free(baseCommand); | ||
chpl_mem_free(stdoutFile); | ||
chpl_mem_free(stdoutFileNoFmt); | ||
if (strlen(command)+1 > size) { | ||
chpl_internal_error("buffer overflow"); | ||
} | ||
|
@@ -460,7 +494,9 @@ static void chpl_launch_cleanup(void) { | |
// remove sbatch file unless it was explicitly generated by the user | ||
if (getenv("CHPL_LAUNCHER_USE_SBATCH") != NULL && !generate_sbatch_script) { | ||
if (unlink(slurmFilename)) { | ||
char msg[FILENAME_MAX + 128]; | ||
char* msg=(char *)chpl_mem_allocMany((strlen(slurmFilename) + | ||
strlen(strerror(errno)) + 36), | ||
sizeof(char), CHPL_RT_MD_FILENAME, -1, 0); | ||
snprintf(msg, sizeof(msg), "Error removing temporary file '%s': %s", | ||
slurmFilename, strerror(errno)); | ||
chpl_warning(msg, 0, 0); | ||
|
@@ -495,6 +531,7 @@ int chpl_launch(int argc, char* argv[], int32_t numLocales) { | |
|
||
chpl_launch_cleanup(); | ||
} | ||
chpl_mem_free(slurmFilename); | ||
return retcode; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had not seen this pattern before—clever. Given that this same expression appears three times in a row, how about storing it into an
int
and then re-using that int in these three malloc calls. I.e., something like: