GH-148937: fix for free-threaded GC (RSS based defer)#148940
GH-148937: fix for free-threaded GC (RSS based defer)#148940nascheme wants to merge 5 commits intopython:mainfrom
Conversation
|
Note that this adds two extra stop/start-the-world points. We need STW to call the mimalloc APIs to compute the memory usage (iterating through arenas). We could likely consolidate one or both of these with existing STW points but I think it makes the code more complex. So I decided to keep it simple for now. I think we should backport this change to 3.14. |
Asking the OS for the process memory usage doesn't work will given how mimalloc works. It does not promptly return memory to the OS and so the memory doesn't drop after cyclic trash is freed. Instead of asking the OS, use mimalloc APIs to compute how much memory is being used by all mimalloc arenas. We need to stop-the-world to do this but usually we can avoid doing a collection. So, from a performance perspective, this is worth it.
It's probably better to call this inside of gc_collect_main(). That way, we are not doing the STW from inside _PyObject_GC_Link() function. This should have no significant performance impact since we hit this only after the young object count hits the threshold.
This avoids using STW in exchange for less accurate memory usage estimates.
ac09833 to
a853c00
Compare
Documentation build overview
5 files changed ·
|
|
Based on a suggestion from Sam, I changed it to instead estimate mimalloc memory use by counting full mimalloc pages. This requires a couple of changes to mimalloc itself but avoids the STW blocks and so should perform better. The accounting happens when a page transitions from non-full->full and so should have minimal performance overhead. |
|
Benchmark results from cyclotron. First compares 3.14.3t to this PR. Note that r-trash are mostly small. Table below this compared 3.13 (GIL, generational GC) with this PR. base=./py-3.14t/bin/python vs new=/home/nas/src/cpython/python
Uniform columns omitted: Legend (base vs new, matched by wl/cycle/extra/live/cyc%): base=/usr/bin/python3 vs new=/home/nas/src/cpython/python
|
Asking the OS for the process memory usage doesn't work well given how mimalloc works. It does not promptly return memory to the OS and so the memory doesn't drop after cyclic trash is freed.
Instead of asking the OS, use mimalloc APIs to compute how much memory is being used by all mimalloc arenas. We need to stop-the-world to do this but usually we can avoid doing a collection. So, from a performance perspective, this is worth it.
Tim Peters has a GC stress tester that quickly shows the issue, linked below. Before this fix, when I run this, the process RSS quickly goes up to 1 GB. After the fix, the RSS stays at about 100 MB. For comparision, the 3.13 GC keeps RSS at about 200 MB.
tim-gc-test.py
Benchmark results