Skip to content

Implement heap walk API #327

Open
Open

Description

@rustyx

It is currently very hard to implement custom leak checking because it seems impossible to walk the jemalloc heap.
The leak checking of jemalloc itself is very slow and does not print debugging information.

Therefore there should be a public API to walk live objects per arena.

Activity

  1. thestinger commented on Feb 9, 2016

    @thestinger
    Contributor
  2. jasone commented on Feb 9, 2016

    @jasone
    Member

    I've implemented precise leak checking in multiple systems, and after long experience with them I no longer consider precise leak enumeration to be an important feature in malloc implementations. These mechanisms have a perfection-based false allure, but in practice they are extremely difficult to utilize.

    How do you tell the difference between useful allocations and leaks? Unless you programmatically walk/track the reference graph to distinguish live/dead objects, it's infeasible to recognize leaks for non-toy applications. Okay, so what does work well? I know of three systems that are powerful and practical:

    • Valgrind. It's slow, but it sure does a good job of detecting leaks (by tracking the reference graph), and it provides great diagnostics on where leaks originated.
    • AddressSanitizer. Although similarly powerful to Valgrind with regard to detecting memory errors, it has the advantage of much lower CPU overhead, and the disadvantage of being more invasive.
    • tcmalloc's and jemalloc's heap profiling. Both allocators implement the most critical aspects of this, though jemalloc has expanded on the original feature set of tcmalloc.

    Regarding your criticisms of jemalloc's leak checking, i.e. heap profiling:

    • Slow: It's only slow if you "sample" all allocations, but for non-toy applications it works very well to use a sample rate that has only a small performance impact.
    • Does not print debugging information: You need to use the jeprof script to post-process heap profiles, and the result is a detailed representation of call graphs associated with extant allocations.

    All that said, I'm not totally against enumeration mechanisms, and in fact I have other long term plans that might require this functionality (see GC-related discussion in #203). But, it's not a priority right now, and it's really tricky to provide an API that doesn't suffer from extreme fragility and bad interactions with allocator internals.

  3. jasone commented on Feb 9, 2016

    @jasone
    Member

    Re: the Android patch, it's a bit odd that the application has to prime iteration with addresses. But, jemalloc doesn't currently track non-huge chunks, because it would only need such tracking for live object iteration.

  4. thestinger commented on Feb 9, 2016

    @thestinger
    Contributor

    In Android, it's being used for a leak detector based on a tracing garbage collector so it's the API needed for tracing through allocations.

  5. rustyx commented on Feb 9, 2016

    @rustyx
    ContributorAuthor

    Well, for us heap walk has been vital. We work with >20GB of tiny objects and when certain arenas do not go back to 0, we know that a leak has occurred, but we don't know where.
    Of course global heap walk is imperfect as it gives many false positives, but it's quite easy to visually distinguish those from actual leaks, if one know from where in the code it was allocated (and especially if arena IDs are also reported).
    Jemalloc's profiling with sampling set to collect every allocation slows down our process (which is already slow) by a factor of 8, so that's impractical to use.
    We had a compile-time setting to add the location in the code where the allocation occurred to each block, and this performed relatively well. But without heap walk functionality we can't use this mechanism anymore.
    So to be clear, jemalloc's profiling almost solves our problem, but is too slow. My request is therefore to either extend the profiling API with a heap walk API or to provide hooks so I can supply custom call stack info (which is where I think the biggest performance impact is). Note - we are on Windows, if that matters. And if it doesn't report the arena, then to add that as well (haven't seen arenas being reported, but might have missed it).

  6. jasone commented on Mar 7, 2016

    @jasone
    Member

    @rustyx, I still don't understand why it's important in your use case to "sample" every allocation. In my experience, much lower sample rates suffice for detecting all nontrivial leaks, so I'm still wondering if you're making leak detection harder than it needs to be, or if there's something unusual about your operating environment.

  7. rustyx commented on Mar 7, 2016

    @rustyx
    ContributorAuthor

    I don't know if that's unusual but we have a combination of long-lived data cubes and short-lived requests in the same process. Data cubes are large and are updated just a few times a day. If there is a leak in there of just a single allocation (which we did have in the past), sampling would be highly inefficient in finding it. With a "sample" rate of 8, I would have to run my process 8 times to catch it, and even then there is no guarantee that I will catch it.

    Sampling just does not work for us. Live object enumeration on the other hand works very well; an automated process builds a "leak check" version of the software, runs the tests and generates a report by comparing the list of leaked blocks with a previous run.

    The check code is also very simple - just override new and delete and store the caller's address (e.g. via __builtin_return_address) inside each block (extending the block size by 8). At the end live objects are enumerated and each return address is resolved via debug symbols and printed out.

  8. added this to the 5.0.0 milestone on Mar 7, 2016
  9. modified the milestones: 5.1.0, 5.0.0 on Jan 16, 2017
  10. modified the milestones: 5.1.0, 5.2.0 on Apr 11, 2018
  11. santagada commented on Oct 26, 2018

    @santagada
    Contributor

    We also need to expose a heap walk as we have memory trackers on our malloc layer that adds tracking information (similar to the utrace structs) to each allocation so walking the heap would make jemalloc compatible with a big set of internal tools for us.

    If anyone has tips on how to expose a heap walk I'm going to try to implement it for us internally and if successful I could do a pull request for it.

  12. modified the milestones: 5.2.0, 6.0 on Apr 3, 2019
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions