Skip to content

Many threads doing simultaneous huge frees can result in a very slow single-threaded free #2834

Open

Description

@orlp

I'm using jemalloc 5.3.0 with the following options:

dirty_decay_ms:500,muzzy_decay_ms:-1,background_thread:true

We've noticed in a profile of a long-running query that we spend large amounts of time in a single-threaded drop on a worker thread:

Image

This occurs when many different working threads finish at around the same time, freeing huge buffers they've been using. It seems that all the work of the free attempts get transferred to that single thread, despite it not being a jemalloc background thread.

After some debugging we've traced the issue down to the following condition being met in src/arena.c:

void arena_handle_deferred_work(tsdn_t *tsdn, arena_t *arena) {
    witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
        WITNESS_RANK_CORE, 0);

    if (decay_immediately(&arena->pa_shard.pac.decay_dirty)) {
        arena_decay_dirty(tsdn, arena, false, true); // This becomes a slow single-threaded free.
    }

    arena_background_thread_inactivity_check(tsdn, arena, false);
}

The reason that this condition can get met despite us setting dirty_decay_ms:500 is that our choice gets overridden here:

/*
    * Purge eagerly for huge allocations, because: 1) number of
    * huge allocations is usually small, which means ticker based
    * decay is not reliable; and 2) less immediate reuse is
    * expected for huge allocations.
    */
if (arena_dirty_decay_ms_default_get() > 0) {
    arena_decay_ms_set(tsd_tsdn(tsd), huge_arena,
        extent_state_dirty, 0);
}
if (arena_muzzy_decay_ms_default_get() > 0) {
    arena_decay_ms_set(tsd_tsdn(tsd), huge_arena,
        extent_state_muzzy, 0);
}

Now, I did notice this was fixed upstream: d131331, and this is no longer done when a background thread is enabled.

That is nice, but on e.g. MacOS we don't have a background thread, and in general I think it's concerning that "eager purge" doesn't just mean "purge this one big allocation" but gets translated to "purge allocations for all threads on this one thread". Could this logic be further refined to prevent this from happening entirely, regardless of whether background threads are available?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions