wingo pushed a commit to branch wip-whippet
in repository guile.

commit 1267f77de3584392b5a5a52ec1d77873fbde87b8
Author: Andy Wingo <wi...@igalia.com>
AuthorDate: Sun Dec 10 21:23:44 2023 +0100

    Basic stats records latency readings in a histogram, prints p95
---
 api/gc-basic-stats.h | 16 +++++++++-
 api/gc-histogram.h   | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/api/gc-basic-stats.h b/api/gc-basic-stats.h
index 14cac492a..af8cd4243 100644
--- a/api/gc-basic-stats.h
+++ b/api/gc-basic-stats.h
@@ -2,6 +2,7 @@
 #define GC_BASIC_STATS_H
 
 #include "gc-event-listener.h"
+#include "gc-histogram.h"
 
 #include <inttypes.h>
 #include <stdint.h>
@@ -9,6 +10,8 @@
 #include <string.h>
 #include <sys/time.h>
 
+GC_DEFINE_HISTOGRAM(gc_latency, 25, 4);
+
 struct gc_basic_stats {
   uint64_t major_collection_count;
   uint64_t minor_collection_count;
@@ -18,6 +21,7 @@ struct gc_basic_stats {
   size_t heap_size;
   size_t max_heap_size;
   size_t max_live_data_size;
+  struct gc_latency pause_times;
 };
 
 static inline uint64_t gc_basic_stats_now(void) {
@@ -58,7 +62,9 @@ static inline void gc_basic_stats_ephemerons_traced(void 
*data) {}
 static inline void gc_basic_stats_restarting_mutators(void *data) {
   struct gc_basic_stats *stats = data;
   uint64_t now = gc_basic_stats_now();
-  stats->elapsed_collector_usec += now - stats->last_time_usec;
+  uint64_t pause_time = now - stats->last_time_usec;
+  stats->elapsed_collector_usec += pause_time;
+  gc_latency_record(&stats->pause_times, pause_time);
   stats->last_time_usec = now;
 }
 
@@ -120,6 +126,14 @@ static inline void gc_basic_stats_print(struct 
gc_basic_stats *stats, FILE *f) {
   fprintf(f, "%" PRIu64 ".%.3" PRIu64 " ms total time "
           "(%" PRIu64 ".%.3" PRIu64 " stopped).\n",
           elapsed / ms, elapsed % ms, stopped / ms, stopped % ms);
+  uint64_t pause_median = gc_latency_median(&stats->pause_times);
+  uint64_t pause_p95 = gc_latency_percentile(&stats->pause_times, 0.95);
+  uint64_t pause_max = gc_latency_max(&stats->pause_times);
+  fprintf(f, "%" PRIu64 ".%.3" PRIu64 " ms median pause time, "
+          "%" PRIu64 ".%.3" PRIu64 " p95, "
+          "%" PRIu64 ".%.3" PRIu64 " max.\n",
+          pause_median / ms, pause_median % ms, pause_p95 / ms, pause_p95 % ms,
+          pause_max / ms, pause_max % ms);
   double MB = 1e6;
   fprintf(f, "Heap size is %.3f MB (max %.3f MB); peak live data %.3f MB.\n",
           stats->heap_size / MB, stats->max_heap_size / MB,
diff --git a/api/gc-histogram.h b/api/gc-histogram.h
new file mode 100644
index 000000000..be5702e5c
--- /dev/null
+++ b/api/gc-histogram.h
@@ -0,0 +1,89 @@
+#ifndef GC_HISTOGRAM_H
+#define GC_HISTOGRAM_H
+
+#include "gc-assert.h"
+
+#include <stdint.h>
+
+static inline size_t gc_histogram_bucket(uint64_t max_value_bits,
+                                         uint64_t precision,
+                                         uint64_t val) {
+  uint64_t major = val < (1ULL << precision)
+    ? 0ULL
+    : 64ULL - __builtin_clzl(val) - precision;
+  uint64_t minor = val < (1 << precision)
+    ? val
+    : (val >> (major - 1ULL)) & ((1ULL << precision) - 1ULL);
+  uint64_t idx = (major << precision) | minor;
+  if (idx >= (max_value_bits << precision))
+    idx = max_value_bits << precision;
+  return idx;
+}
+
+static inline uint64_t gc_histogram_bucket_min_val(uint64_t precision,
+                                                   size_t idx) {
+  uint64_t major = idx >> precision;
+  uint64_t minor = idx & ((1ULL << precision) - 1ULL);
+  uint64_t min_val = major
+    ? ((1ULL << precision) | minor) << (major - 1ULL)
+    : minor;
+  return min_val;
+}
+
+static inline void gc_histogram_record(uint32_t *buckets,
+                                       uint64_t max_value_bits,
+                                       uint64_t precision,
+                                       uint64_t val) {
+  buckets[gc_histogram_bucket(max_value_bits, precision, val)]++;
+}
+
+#define GC_DEFINE_HISTOGRAM(name, max_value_bits, precision)            \
+  struct name { uint32_t buckets[((max_value_bits) << (precision)) + 1]; }; \
+  static inline size_t name##_size(void) {                              \
+    return ((max_value_bits) << (precision)) + 1;                       \
+  }                                                                     \
+  static inline uint64_t name##_bucket_min_val(size_t idx) {            \
+    GC_ASSERT(idx < name##_size());                                     \
+    return gc_histogram_bucket_min_val((precision), idx);               \
+  }                                                                     \
+  static inline struct name make_##name(void) {                         \
+    return (struct name) { { 0, }};                                     \
+  }                                                                     \
+  static inline void name##_record(struct name *h, uint64_t val) {      \
+    h->buckets[gc_histogram_bucket((max_value_bits), (precision), val)]++; \
+  }                                                                     \
+  static inline uint64_t name##_ref(struct name *h, size_t idx) {       \
+    GC_ASSERT(idx < name##_size());                                     \
+    return h->buckets[idx];                                             \
+  }                                                                     \
+  static inline uint64_t name##_min(struct name *h) {                   \
+    for (size_t bucket = 0; bucket < name##_size(); bucket++)           \
+      if (h->buckets[bucket]) return name##_bucket_min_val(bucket);     \
+    return -1;                                                          \
+  }                                                                     \
+  static inline uint64_t name##_max(struct name *h) {                   \
+    if (h->buckets[name##_size()-1]) return -1LL;                       \
+    for (ssize_t bucket = name##_size() - 1; bucket >= 0; bucket--)     \
+      if (h->buckets[bucket]) return name##_bucket_min_val(bucket+1);   \
+    return 0;                                                           \
+  }                                                                     \
+  static inline uint64_t name##_count(struct name *h) {                 \
+    uint64_t sum = 0;                                                   \
+    for (size_t bucket = 0; bucket < name##_size(); bucket++)           \
+      sum += h->buckets[bucket];                                        \
+    return sum;                                                         \
+  }                                                                     \
+  static inline uint64_t name##_percentile(struct name *h, double p) {  \
+    uint64_t n = name##_count(h) * p;                                   \
+    uint64_t sum = 0;                                                   \
+    for (size_t bucket = 0; bucket + 1 < name##_size(); bucket++) {     \
+      sum += h->buckets[bucket];                                        \
+      if (sum >= n) return name##_bucket_min_val(bucket+1);             \
+    }                                                                   \
+    return -1ULL;                                                       \
+  }                                                                     \
+  static inline uint64_t name##_median(struct name *h) {                \
+    return name##_percentile(h, 0.5);                                   \
+  }
+
+#endif // GC_HISTOGRAM_H

Reply via email to