diff --git a/runtime/caml/osdeps.h b/runtime/caml/osdeps.h index c72ccf6ccb6..be6d192d5ef 100644 --- a/runtime/caml/osdeps.h +++ b/runtime/caml/osdeps.h @@ -103,6 +103,9 @@ extern char_os *caml_secure_getenv(char_os const *var); cannot be determined, return -1. */ extern int caml_num_rows_fd(int fd); +/* Print a timestamp for verbose GC logs */ +extern void caml_print_timestamp(FILE* channel, int formatted); + /* Memory management platform-specific operations */ void *caml_plat_mem_map(uintnat, uintnat, int); diff --git a/runtime/misc.c b/runtime/misc.c index 081447683d8..205da1265bf 100644 --- a/runtime/misc.c +++ b/runtime/misc.c @@ -97,6 +97,9 @@ void caml_gc_message (int level, char *msg, ...) if ((atomic_load_relaxed(&caml_verb_gc) & level) != 0){ va_list ap; va_start(ap, msg); + if (caml_verb_gc & 0x1000) { + caml_print_timestamp(stderr, caml_verb_gc & 0x2000); + } vfprintf (stderr, msg, ap); va_end(ap); fflush (stderr); diff --git a/runtime/unix.c b/runtime/unix.c index 0877ec37a0f..7fed45cb39e 100644 --- a/runtime/unix.c +++ b/runtime/unix.c @@ -43,9 +43,8 @@ #ifdef HAS_UNISTD #include #endif -#ifdef HAS_POSIX_MONOTONIC_CLOCK #include -#elif HAS_MACH_ABSOLUTE_TIME +#ifdef HAS_MACH_ABSOLUTE_TIME #include #endif #ifdef HAS_DIRENT @@ -476,6 +475,35 @@ int caml_num_rows_fd(int fd) #endif } +void caml_print_timestamp(FILE* channel, int formatted) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + if (!formatted) { + fprintf(channel, "%ld.%06d ", (long)tv.tv_sec, (int)tv.tv_usec); + } else { + struct tm tm; + char tz[10] = "Z"; + localtime_r(&tv.tv_sec, &tm); + if (tm.tm_gmtoff != 0) { + long tzhour = tm.tm_gmtoff / 60 / 60; + long tzmin = (tm.tm_gmtoff / 60) % 60; + if (tzmin < 0) {tzmin += 60; tzhour--;} + sprintf(tz, "%+03ld:%02ld", tzhour, tzmin); + } + fprintf(channel, + "[%04d-%02d-%02d %02d:%02d:%02d.%06d%s] ", + 1900 + tm.tm_year, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec, + (int)tv.tv_usec, + tz); + } +} + void caml_init_os_params(void) { caml_plat_mmap_alignment = caml_plat_pagesize = sysconf(_SC_PAGESIZE); diff --git a/runtime/win32.c b/runtime/win32.c index a2dcf920a1e..f1332822c24 100644 --- a/runtime/win32.c +++ b/runtime/win32.c @@ -1107,6 +1107,11 @@ int caml_num_rows_fd(int fd) return -1; } +void caml_print_timestamp(FILE* channel, int formatted) +{ + /* unimplemented */ +} + /* UCRT clock function returns wall-clock time */ CAMLexport clock_t caml_win32_clock(void) {