Skip to content

Commit 04d3a14

Browse files
authored
Add timestamp to GC log (#2032)
1 parent 89a692e commit 04d3a14

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

ocaml/runtime/caml/osdeps.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ extern char_os *caml_secure_getenv(char_os const *var);
103103
cannot be determined, return -1. */
104104
extern int caml_num_rows_fd(int fd);
105105

106+
/* Print a timestamp for verbose GC logs */
107+
extern void caml_print_timestamp(FILE* channel, int formatted);
108+
106109
/* Memory management platform-specific operations */
107110

108111
void *caml_plat_mem_map(uintnat, uintnat, int);

ocaml/runtime/misc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ void caml_gc_message (int level, char *msg, ...)
9797
if ((atomic_load_relaxed(&caml_verb_gc) & level) != 0){
9898
va_list ap;
9999
va_start(ap, msg);
100+
if (caml_verb_gc & 0x1000) {
101+
caml_print_timestamp(stderr, caml_verb_gc & 0x2000);
102+
}
100103
vfprintf (stderr, msg, ap);
101104
va_end(ap);
102105
fflush (stderr);

ocaml/runtime/unix.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
#ifdef HAS_UNISTD
4444
#include <unistd.h>
4545
#endif
46-
#ifdef HAS_POSIX_MONOTONIC_CLOCK
4746
#include <time.h>
48-
#elif HAS_MACH_ABSOLUTE_TIME
47+
#ifdef HAS_MACH_ABSOLUTE_TIME
4948
#include <mach/mach_time.h>
5049
#endif
5150
#ifdef HAS_DIRENT
@@ -476,6 +475,35 @@ int caml_num_rows_fd(int fd)
476475
#endif
477476
}
478477

478+
void caml_print_timestamp(FILE* channel, int formatted)
479+
{
480+
struct timeval tv;
481+
gettimeofday(&tv, NULL);
482+
if (!formatted) {
483+
fprintf(channel, "%ld.%06d ", (long)tv.tv_sec, (int)tv.tv_usec);
484+
} else {
485+
struct tm tm;
486+
char tz[10] = "Z";
487+
localtime_r(&tv.tv_sec, &tm);
488+
if (tm.tm_gmtoff != 0) {
489+
long tzhour = tm.tm_gmtoff / 60 / 60;
490+
long tzmin = (tm.tm_gmtoff / 60) % 60;
491+
if (tzmin < 0) {tzmin += 60; tzhour--;}
492+
sprintf(tz, "%+03ld:%02ld", tzhour, tzmin);
493+
}
494+
fprintf(channel,
495+
"[%04d-%02d-%02d %02d:%02d:%02d.%06d%s] ",
496+
1900 + tm.tm_year,
497+
tm.tm_mon + 1,
498+
tm.tm_mday,
499+
tm.tm_hour,
500+
tm.tm_min,
501+
tm.tm_sec,
502+
(int)tv.tv_usec,
503+
tz);
504+
}
505+
}
506+
479507
void caml_init_os_params(void)
480508
{
481509
caml_plat_mmap_alignment = caml_plat_pagesize = sysconf(_SC_PAGESIZE);

ocaml/runtime/win32.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,11 @@ int caml_num_rows_fd(int fd)
11071107
return -1;
11081108
}
11091109

1110+
void caml_print_timestamp(FILE* channel, int formatted)
1111+
{
1112+
/* unimplemented */
1113+
}
1114+
11101115
/* UCRT clock function returns wall-clock time */
11111116
CAMLexport clock_t caml_win32_clock(void)
11121117
{

0 commit comments

Comments
 (0)