Skip to content

Commit

Permalink
Add timestamps to GC logs (ocaml-flambda#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
stedolan authored Mar 21, 2023
1 parent 7e92c02 commit 3f50ccc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ocaml/runtime/caml/osdeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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);

#ifdef _WIN32

extern int caml_win32_rename(const wchar_t *, const wchar_t *);
Expand Down
3 changes: 3 additions & 0 deletions ocaml/runtime/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ void caml_gc_message (int level, char *msg, ...)
if ((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);
Expand Down
31 changes: 31 additions & 0 deletions ocaml/runtime/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include "caml/config.h"
#if defined(SUPPORT_DYNAMIC_LINKING) && !defined(BUILDING_LIBCAMLRUNS)
#define WITH_DYNAMIC_LINKING
Expand Down Expand Up @@ -434,3 +436,32 @@ int caml_num_rows_fd(int fd)
return -1;
#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);
}
}
5 changes: 5 additions & 0 deletions ocaml/runtime/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,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)
{
Expand Down

0 comments on commit 3f50ccc

Please sign in to comment.