From 4210699949668655ee3142cd2a9a8997dfa83faa Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 29 Jun 2025 23:22:31 +0200 Subject: [PATCH] Don't use the after-gc-hook for monitoring garbage collection This seems to be happening not in the thread I expect, so avoid using the hook. --- scripts/guix-data-service.in | 45 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/scripts/guix-data-service.in b/scripts/guix-data-service.in index 0f97fae..44248cb 100644 --- a/scripts/guix-data-service.in +++ b/scripts/guix-data-service.in @@ -231,31 +231,34 @@ (set-thread-name "gc watcher") - (add-hook! - after-gc-hook - (let ((last-gc-time-taken - (assq-ref (gc-stats) 'gc-time-taken))) - (lambda () - (let* ((stats - (gc-stats)) - (gc-time-taken + (let loop ((last-gc-times + (or (assq-ref (gc-stats) 'times) 0)) + (last-gc-time-taken + (assq-ref (gc-stats) 'gc-time-taken))) + (let* ((stats (gc-stats)) + (gc-times (or (assq-ref stats 'gc-times) 0)) + (gc-time-taken (assq-ref stats 'gc-time-taken))) + (when (> gc-times last-gc-times) + (let ((gc-time-taken (assq-ref stats 'gc-time-taken)) (time-since-last (/ (- gc-time-taken last-gc-time-taken) - internal-time-units-per-second))) - (when (> time-since-last 0.1) - (format (current-error-port) - "after gc (additional time taken: ~f, times: ~d, heap-allocated-since-gc: ~d, heap size: ~d, heap free: ~d)\n" - time-since-last - (assq-ref stats 'gc-times) - (assq-ref stats 'heap-allocated-since-gc) - (assq-ref stats 'heap-size) - (assq-ref stats 'heap-free-size))) - (set! last-gc-time-taken - (assq-ref (gc-stats) 'gc-time-taken)))))) - (while #t - (usleep 100000)))) + internal-time-units-per-second)) + (gcs + (- gc-times + last-gc-times))) + (format (current-error-port) + "~d gc's (time taken: ~f, heap-allocated-since-gc: ~d, heap size: ~d, heap free: ~d)\n" + gcs + (/ time-since-last + gcs) + (assq-ref stats 'heap-allocated-since-gc) + (assq-ref stats 'heap-size) + (assq-ref stats 'heap-free-size)))) + (sleep 5) + (loop gc-times + gc-time-taken))))) ;; Provide some visual space between the startup output and the ;; server starting