Skip to content

Commit 7b53fe9

Browse files
interwqjasone
authored andcommitted
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. This resolves jemalloc#616.
1 parent 7c12483 commit 7b53fe9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/stats.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque,
136136
availregs = nregs * curruns;
137137
milli = (availregs != 0) ? (1000 * curregs) / availregs
138138
: 1000;
139-
assert(milli <= 1000);
140-
if (milli < 10) {
139+
140+
if (milli > 1000) {
141+
/*
142+
* Race detected: the counters were read in
143+
* separate mallctl calls and concurrent
144+
* operations happened in between. In this case
145+
* no meaningful utilization can be computed.
146+
*/
147+
malloc_snprintf(util, sizeof(util), " race");
148+
} else if (milli < 10) {
141149
malloc_snprintf(util, sizeof(util),
142150
"0.00%zu", milli);
143151
} else if (milli < 100) {
@@ -146,8 +154,10 @@ stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque,
146154
} else if (milli < 1000) {
147155
malloc_snprintf(util, sizeof(util), "0.%zu",
148156
milli);
149-
} else
157+
} else {
158+
assert(milli == 1000);
150159
malloc_snprintf(util, sizeof(util), "1");
160+
}
151161

152162
if (config_tcache) {
153163
malloc_cprintf(write_cb, cbopaque,

0 commit comments

Comments
 (0)