-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-141770: Annotate anonymous mmap usage if "-X dev" is used #142079
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
Changes from 9 commits
0e3933d
8b7023e
816d962
979ce02
b3d8547
0c5a495
e757dd9
cb9509b
855742b
32cc007
49b4f95
1b88b75
e2eedbc
a680677
8ec4d73
c0e08b6
e66e650
b0ca751
c9138de
8f77e0d
8706fce
8ce8281
d71a744
4dd1ee4
fee757e
a240b3c
805c539
795d31c
be4d550
290864a
b287e0c
1619f95
a1ad592
e4e4835
0429a0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Annotate anonymous mmap usage in debug builds only when supported by the | ||
| Linux kernel. Patch by Donghee Na. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||
| # endif | ||||||
| #endif | ||||||
| #include "ctypes.h" | ||||||
| #include "pycore_obmalloc.h" // _PyAnnotateMemoryMap() | ||||||
|
|
||||||
| /* BLOCKSIZE can be adjusted. Larger blocksize will take a larger memory | ||||||
| overhead, but allocate less blocks from the system. It may be that some | ||||||
|
|
@@ -74,14 +75,16 @@ static void more_core(void) | |||||
| if (item == NULL) | ||||||
| return; | ||||||
| #else | ||||||
| size_t mem_size = count * sizeof(ITEM); | ||||||
| item = (ITEM *)mmap(NULL, | ||||||
| count * sizeof(ITEM), | ||||||
| mem_size, | ||||||
| PROT_READ | PROT_WRITE | PROT_EXEC, | ||||||
| MAP_PRIVATE | MAP_ANONYMOUS, | ||||||
| -1, | ||||||
| 0); | ||||||
| if (item == (void *)MAP_FAILED) | ||||||
| return; | ||||||
| _PyAnnotateMemoryMap(item, mem_size, "cpython:more_core"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(item, mem_size, "cpython:more_core"); | |
| _PyAnnotateMemoryMap(item, mem_size, "cpython:ctypes:malloc_closure"); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |||||
| #include "pycore_abstract.h" // _Py_convert_optional_to_ssize_t() | ||||||
| #include "pycore_bytesobject.h" // _PyBytes_Find() | ||||||
| #include "pycore_fileutils.h" // _Py_stat_struct | ||||||
| #include "pycore_obmalloc.h" // _PyAnnotateMemoryMap() | ||||||
corona10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() | ||||||
|
|
||||||
| #include <stddef.h> // offsetof() | ||||||
|
|
@@ -1951,6 +1952,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) | |||||
| PyErr_SetFromErrno(PyExc_OSError); | ||||||
| return NULL; | ||||||
| } | ||||||
| _PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:new_mmap_object"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:new_mmap_object"); | |
| _PyAnnotateMemoryMap(m_obj->data, map_size, "cpython:mmap"); |
(If we allow users to set an annotation: this could be cpython:mmap:xxx?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think _PyAnnotateMemoryMap should be used in this case at all:
- This function creates a memory mapping on behalf of the caller, not Python itself. So it's not really correct to attribute the memory region to Python.
- You can only name anonymous memory areas, but this region may not be anonymous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to annotate this as allocated with the Python mmap module, rather than by something else (within CPython, or another library).
If the mmap is not anonymous, the annotation fails. Good.
If in the future, the kernel somehow allows annotating non-anonymous areas, this annotation would also work.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -467,6 +467,7 @@ _PyMem_ArenaAlloc(void *Py_UNUSED(ctx), size_t size) | |||||
| if (ptr == MAP_FAILED) | ||||||
| return NULL; | ||||||
| assert(ptr != NULL); | ||||||
| _PyAnnotateMemoryMap(ptr, size, "cpython:PyMem_ArenaAlloc"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(ptr, size, "cpython:PyMem_ArenaAlloc"); | |
| _PyAnnotateMemoryMap(ptr, size, "cpython:obmalloc"); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -71,6 +71,9 @@ jit_alloc(size_t size) | |||||
| int prot = PROT_READ | PROT_WRITE; | ||||||
| unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); | ||||||
| int failed = memory == MAP_FAILED; | ||||||
| if (!failed) { | ||||||
| _PyAnnotateMemoryMap(memory, size, "cpython:jit_alloc"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(memory, size, "cpython:jit_alloc"); | |
| _PyAnnotateMemoryMap(memory, size, "cpython:jit"); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -61,6 +61,7 @@ | |||||
| #include "pycore_ceval.h" // _PyPerf_Callbacks | ||||||
| #include "pycore_frame.h" | ||||||
| #include "pycore_interp.h" | ||||||
| #include "pycore_obmalloc.h" // _PyAnnotateMemoryMap() | ||||||
| #include "pycore_runtime.h" // _PyRuntime | ||||||
|
|
||||||
| #ifdef PY_HAVE_PERF_TRAMPOLINE | ||||||
|
|
@@ -1085,6 +1086,7 @@ static void* perf_map_jit_init(void) { | |||||
| close(fd); | ||||||
| return NULL; // Memory mapping failed | ||||||
| } | ||||||
| _PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:perf_map_jit_init"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:perf_map_jit_init"); | |
| _PyAnnotateMemoryMap(perf_jit_map_state.mapped_buffer, page_size, "cpython:jit:perf_map"); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -132,6 +132,7 @@ any DWARF information available for them). | |||||
| #include "Python.h" | ||||||
| #include "pycore_ceval.h" // _PyPerf_Callbacks | ||||||
| #include "pycore_interpframe.h" // _PyFrame_GetCode() | ||||||
| #include "pycore_obmalloc.h" // _PyAnnotateMemoryMap() | ||||||
| #include "pycore_runtime.h" // _PyRuntime | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -290,6 +291,7 @@ new_code_arena(void) | |||||
| perf_status = PERF_STATUS_FAILED; | ||||||
| return -1; | ||||||
| } | ||||||
| _PyAnnotateMemoryMap(memory, mem_size, "cpython:new_code_arena"); | ||||||
|
||||||
| _PyAnnotateMemoryMap(memory, mem_size, "cpython:new_code_arena"); | |
| _PyAnnotateMemoryMap(memory, mem_size, "cpython:perf_trampoline"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.