Skip to content

Bug fix for prof_active switch - #1604

Merged
yinan1048576 merged 1 commit into
jemalloc:devfrom
yinan1048576:prof-active-bug
Aug 22, 2019
Merged

yinan1048576 merged 1 commit into
jemalloc:devfrom
yinan1048576:prof-active-bug

Conversation

@yinan1048576

Copy link
Copy Markdown
Contributor

The bug is subtle but critical: if application performs the following
three actions in sequence: (a) turn prof_active off, (b) make at
least one allocation that triggers the malloc slow path via the
if (unlikely(bytes_until_sample < 0)) path, and (c) turn
prof_active back on, then the application would never get another
sample (until a very very long time later).

The fix is to properly reset bytes_until_sample rather than
throwing it all the way to SSIZE_MAX.

A side minor change is to call prof_active_get_unlocked() rather
than directly grabbing the prof_active variable - it is the very
reason why we defined the prof_active_get_unlocked() function.

@yinan1048576

Copy link
Copy Markdown
Contributor Author

While working on fixing this bug, I was thinking: an important reason why this bug was not identified earlier was the lack of test coverage, in two aspects:

  • There's no test that performed the three actions (in my description) in sequence - I think it is hard to exhaust everything and there's no easy solution here.
  • The malloc fast path is not well covered by tests - it's actually an interesting dilemma: in debug build malloc always routes to slow path here:
    return malloc_default(size);
    so basically the whole body of the malloc fast path function is undebuggable. I think we should have a way to solve it - it feels like a simpler problem to solve than the first one.

Comment thread src/jemalloc.c Resolved
The bug is subtle but critical: if application performs the following
three actions in sequence: (a) turn `prof_active` off, (b) make at
least one allocation that triggers the malloc slow path via the
`if (unlikely(bytes_until_sample < 0))` path, and (c) turn
`prof_active` back on, then the application would never get another
sample (until a very very long time later).

The fix is to properly reset `bytes_until_sample` rather than
throwing it all the way to `SSIZE_MAX`.

A side minor change is to call `prof_active_get_unlocked()` rather
than directly grabbing the `prof_active` variable - it is the very
reason why we defined the `prof_active_get_unlocked()` function.
@yinan1048576

Copy link
Copy Markdown
Contributor Author

I'm now resetting bytes_until_sample = 1 << lg_prof_sample, so that the rate of falling to the slow path is the same as when prof_active was on. Further, I'm not really letting it fall to the slow path, but instead I'm letting the execution flow continue on the fast path, so that it's more a really fast "slow path". I believe this is correct - even if we were resetting bytes_until_sample = SSIZE_MAX, we should have continued on the fast path.

One other minor remark is that I'm directly resetting via tsd_bytes_until_sample_set(tsd, ((uint64_t)1U << lg_prof_sample)) rather than doing the more correct thing:

    prof_tdata_t *tdata = tsd_prof_tdata_get(tsd);
    if (tdata != NULL) {
        prof_sample_threshold_update(tdata);
    }

The only difference is that the reset is not randomized, but we probably don't need that anyway when prof_active is off. Also the prof_sample_threshold_update() call would take much longer.

@yinan1048576
yinan1048576 merged commit 9e031c1 into jemalloc:dev Aug 22, 2019
BusyJay pushed a commit to tikv/jemallocator that referenced this pull request Aug 5, 2021
This depends on tikv/jemalloc#2 , which is a cherry-pick of jemalloc/jemalloc#1604 .

See jemalloc/jemalloc#1605 for details of the bug that this fixes; basically, it makes profiling/sampling fail to work under some conditions. We hit this in practice at Materialize when attempting to switch to tikv/jemallocator.

Signed-off-by: Brennan Vincent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants