Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active defrag deficiency #26

Open
joehu21 opened this issue Dec 11, 2024 · 0 comments
Open

Active defrag deficiency #26

joehu21 opened this issue Dec 11, 2024 · 0 comments

Comments

@joehu21
Copy link
Collaborator

joehu21 commented Dec 11, 2024

The defrag callback is implemented as "copy + free" instead of calling the VM_DefragAlloc API. The original intention is for easy implementation. Each JSON document is a RapidJSON document tree that may have lots of small allocations. Without using "copy + free", we would have to traverse the JSON tree to find every pointer and call the VM_DefragAlloc for each pointer, which would be very complicated.

However, the problem with this approach is that it is very likely we will reallocate the exact same pointer we just freed, because the memory will be freed to tcache and it will be used for the next allocation. The way the defrag avoids that is that it uses API to explicitly tell the allocator to skip the tcache when doing alloc & free for defrag.

To fix the issue, there are several options:

  1. Calling VM_DefragAlloc: Walk through the JSON tree, for every malloc'ed chunk, call VM_DefragAlloc.

  2. Redirect alloc/free:

  • defrag:
    1. redirect alloc/free to zmalloc_no_tcache/zfree_no_tcache
    2. copy json object
    3. free the old json object
    4. redirect alloc/free to back to RedisModule_Alloc/RedisModule_Free
  • Expose zmalloc_no_tcache/zfree_no_tcache as module APIs.
  1. Flush tcache:
    *defrag:
    1. flush tcache
    2. copy json object
    3. free the old json object
  • Add a module API for flushing tcache.
  1. disable tcache:
  • defrag:
    1. VM_DisableTcache (a new api)
    2. copy json object
    3. free the old json object
    4. VM_EnableTcache (a new api)
  • Add module API VM_DisableTcache and VM_EnableTcache
  • Modify VM_Alloc/Free to check if tcache is disabled. If yes, use zmalloc_no_cache/zfree_no_cache.
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

No branches or pull requests

1 participant