Skip to content

Add timestamps to GC logs #1229

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

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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