From bbff6ca6740c27737378f6c2dec3a13053a5a150 Mon Sep 17 00:00:00 2001 From: Qi Wang Date: Mon, 30 Jan 2017 15:54:16 -0800 Subject: [PATCH] Handle race in stats_arena_bins_print When multiple threads calling stats_print, race could happen as we read the counters in separate mallctl calls; and the removed assertion could fail when other operations happened in between the mallctl calls. For simplicity, output "race" in the utilization field in this case. --- src/stats.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/stats.c b/src/stats.c index 2a424a73..ae360e1b 100644 --- a/src/stats.c +++ b/src/stats.c @@ -133,8 +133,16 @@ stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque, availregs = nregs * curslabs; milli = (availregs != 0) ? (1000 * curregs) / availregs : 1000; - assert(milli <= 1000); - if (milli < 10) { + + if (milli > 1000) { + /* + * Race detected: the counters were read in + * separate mallctl calls and concurrent + * operations happened in between. In this case + * no meaningful utilization can be computed. + */ + malloc_snprintf(util, sizeof(util), " race"); + } else if (milli < 10) { malloc_snprintf(util, sizeof(util), "0.00%zu", milli); } else if (milli < 100) { @@ -144,6 +152,7 @@ stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque, malloc_snprintf(util, sizeof(util), "0.%zu", milli); } else { + assert(milli == 1000); malloc_snprintf(util, sizeof(util), "1"); }