-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Tracemalloc traces do not include original stack trace length #82142
Comments
When using the tracemalloc module, the maximum number of frames that are captured is specified at startup via a value to the However, if the number of frames is truncated, there's no way to know the original length of the stack traces. |
PR 15545 makes each trace 4 bytes (sizeof int) larger. Would it be enough for you to only know if the traceback is truncated? tracemalloc is already "memory heavy", so I don't think that making trace_t larger is a blocker issue :-) |
That's a good question. Considering that Py_DEFAULT_RECURSION_LIMIT is 1000, we could probably settle on 2 bytes by using an uint16_t which ought to be enough unless people regularly trace Python stack traces bigger that are bigger than 2^16. |
I'm fine with limiting MAX_NFRAME to USHRT_MAX and change traceback_t.nframe type to unsigned short (16 bits). Storing 65536 should be way enough. It would be great if you manage to add your new field without making traceback_t larger. In fact, I didn't check: maybe traceback_t size is not change by your PR, because of memory aligment and padding. By the way, first I taught that your modified trace_t: no, you modified traceback_t. _tracemalloc ensures that a traceback_t instance is allocated exactly once in the heap memory, to reduce the memory footprint. So making traceback_t larger should impact less the memory than making trace_t larger. |
It is changed. The current size is should be determined by sizeof(Py_uhash_t + int + int) which is 4 + 4 + 4, so 12 aligned/padded. Using a short will add 2 bytes and potentially some space for 2 more bytes in the future. :) |
With the following change, traceback_t size changes from 24 bytes to 32 bytes: diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index ee32ac29b7..8a820db907 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -79,6 +79,7 @@ __attribute__((packed))
typedef struct {
Py_uhash_t hash;
int nframe;
+ int length;
frame_t frames[1];
} traceback_t;
@@ -1640,6 +1641,8 @@ PyInit__tracemalloc(void)
if (tracemalloc_init() < 0)
return NULL;
+ printf("sizeof(traceback_t) = %zu bytes\n", sizeof(traceback_t));
+
return m;
} The following structure size is 24 bytes, so no change: typedef struct {
Py_uhash_t hash;
short nframe;
short length;
frame_t frames[1];
} traceback_t; The short approach is promising :-) |
Thanks for your contribution Julien, I merged your PR. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: