Skip to content

Commit

Permalink
gethostname fix (#3043)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachglick authored and loriab committed Oct 3, 2023
1 parent f49582a commit 6d3438e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
27 changes: 12 additions & 15 deletions psi4/src/psi4/libciomr/tstart.cc
Expand Up @@ -66,15 +66,15 @@ double user_stop, sys_stop;
*/
void PSI_API tstart() {
int error;
char *name;
struct tms total_tmstime;
const long clk_tck = sysconf(_SC_CLK_TCK);
times(&total_tmstime);

/// host name info, needed?
name = (char *)malloc(40 * sizeof(char));
error = gethostname(name, 40);
if (error != 0) strncpy(name, "nohostname", 11);
// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> name(65);
error = gethostname(name.data(), 65);
if (error != 0) strncpy(name.data(), "nohostname", 11);
if (name.back() != '\0') name.push_back('\0');

/// start a global timer
if (!running) {
Expand All @@ -89,10 +89,8 @@ void PSI_API tstart() {
user_start = ((double)total_tmstime.tms_utime) / clk_tck;
sys_start = ((double)total_tmstime.tms_stime) / clk_tck;

outfile->Printf("\n*** tstart() called on %s\n", name);
outfile->Printf("\n*** tstart() called on %s\n", name.data());
outfile->Printf("*** at %s\n", ctime(&time_start));

free(name);
}

/*!
Expand All @@ -105,12 +103,13 @@ void PSI_API tstop() {
std::time_t total_time;
std::time_t total_time_overall;
struct tms total_tmstime;
char *name;
double user_s, sys_s;

name = (char *)malloc(40 * sizeof(char));
error = gethostname(name, 40);
if (error != 0) strncpy(name, "nohostname", 11);
// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> name(65);
error = gethostname(name.data(), 65);
if (error != 0) strncpy(name.data(), "nohostname", 11);
if (name.back() != '\0') name.push_back('\0');

time_end = std::time(nullptr);
total_time = time_end - time_start;
Expand All @@ -124,7 +123,7 @@ void PSI_API tstop() {
user_s = user_stop - user_start;
sys_s = sys_stop - sys_start;

outfile->Printf("\n*** tstop() called on %s at %s", name, ctime(&time_end));
outfile->Printf("\n*** tstop() called on %s at %s", name.data(), ctime(&time_end));

/// print all module timings
outfile->Printf("Module time:\n");
Expand All @@ -141,7 +140,5 @@ void PSI_API tstop() {
outfile->Printf("\tsystem time = %10.2f seconds = %10.2f minutes\n", sys_s, sys_s / 60.0);
outfile->Printf("\ttotal time = %10d seconds = %10.2f minutes\n", (int)total_time_overall,
((double)total_time_overall) / 60.0);

free(name);
}
} // namespace psi
12 changes: 7 additions & 5 deletions psi4/src/psi4/libqt/timer.cc
Expand Up @@ -1005,17 +1005,19 @@ void timer_done() {
omp_set_lock(&lock_timer);
extern Timer_Structure root_timer;
root_timer.turn_off();
char *host;

host = (char *)malloc(40 * sizeof(char));
gethostname(host, 40);
// host name has up to HOST_NAME_MAX(==64) + 1 bytes, and must end in the null byte
std::vector<char> host(65);
int error = gethostname(host.data(), 65);
if (error != 0) strncpy(host.data(), "nohostname", 11);
if (host.back() != '\0') host.push_back('\0');

/* Dump the timing data to timer.dat and free the timers */
auto mode = std::ostream::app;
auto printer = std::make_shared<PsiOutStream>("timer.dat", mode);
printer->Printf("\n");
printer->Printf("Host: %s\n", host);
free(host);
printer->Printf("Host: %s\n", host.data());

printer->Printf("\n");
// NOTE: ctime is not thread-safe and could potentially cause overwriting of the return strings.
printer->Printf("Timers On : %s", ctime(&timer_start)); // lgtm[cpp/potentially-dangerous-function]
Expand Down

0 comments on commit 6d3438e

Please sign in to comment.