Skip to content

Conversation

@corona10
Copy link
Member

@corona10 corona10 commented Nov 29, 2025

@corona10
Copy link
Member Author

corona10 commented Nov 29, 2025

Annotation

Screenshot 2025-11-29 at 6 10 03 PM

Without annotation

Screenshot 2025-11-29 at 6 23 31 PM

I verified that this annotation actually works, and I believe it will be very helpful for debugging when needed.

@corona10
Copy link
Member Author

cc @picnixz

@corona10
Copy link
Member Author

FYI, Fedora does not support yet even if the kernel version itself already support it.
See: https://bugzilla.redhat.com/show_bug.cgi?id=2302746

cc @vstinner @encukou

@corona10 corona10 requested a review from picnixz November 29, 2025 10:13
@SpecLad
Copy link
Contributor

SpecLad commented Nov 29, 2025

FWIW, it would be useful to have an option to enable this in release builds too.

@corona10
Copy link
Member Author

@SpecLad Thanks for the feedback, I will create a separate PR :)

@corona10
Copy link
Member Author

@SpecLad Just out of curiosity, passing the build flag through ./configure --enable-annotate-mmap would be fine to you?
I would like to follow Ruby's approach but disable this flag by default unless many disto actually want this flag.

@SpecLad
Copy link
Contributor

SpecLad commented Nov 29, 2025

To be clear, by "option" I really mean a runtime option. 🙂 It would be pretty annoying to have to rebuild Python just for this. If you compare with other implementations, none of them require a compile-time option:

  • C (glibc) - off by default, can be enabled with environment variable;
  • Go - on by default, can be disabled with environment variable;
  • Ruby - on permanently (unless I missed something).

TBH, I don't see why Python shouldn't also permanently enable this, but I would settle for "off by default, enabled with environment variable or -X option".

@picnixz
Copy link
Member

picnixz commented Nov 29, 2025

It's a bit annoying that this would only be supported on Linux. Do we have an option that isn't supported everywhere?

@corona10 corona10 requested a review from a team as a code owner December 3, 2025 18:29
@corona10 corona10 changed the title gh-141770: Annotate anonymous mmap usage in debug builds only gh-141770: Annotate anonymous mmap usage if "-X dev" is used Dec 3, 2025
# include <sys/prctl.h>
#endif

static inline int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this isn't worth putting into a macro? As a regular function, it will be fully internal, and PGO can inline the parts of it that are appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer static inline functions. I don't see the advantage of using a macro. Inlining or not shouldn't make any difference on performance.

@corona10 corona10 requested a review from zooba December 4, 2025 12:08
@corona10
Copy link
Member Author

corona10 commented Dec 4, 2025

@zooba I've updated PR. PTAL :)

#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__)
# define _PyAnnotateMemoryMap(addr, size, name) \
do { \
if (_Py_GetConfig()->dev_mode) { \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood that it should always be enabled if Python is built in debug mode (if Py_DEBUG macro is defined).

Copy link
Member Author

@corona10 corona10 Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer consistent behavior

I considered the following options:

  1. Always enable it for both debug and release builds (as long as the build flag is enabled).
  2. Enable it only when -X dev is passed. (current choice)

To me, any other combination feels weird (e.g enabling it unconditionally in debug builds but requiring -X dev for release build)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another code paths which is enabled if the Development Mode is enabled or Python is built in debug mode: checking errors argument in str.encode(encoding, errors), PyUnicode_Decode() and PyUnicode_FromEncodedObject().

In the past, a similar thing was done for errors on closing a file (io_finalize() in Modules/_io/iobase.c). Since Python 3.13, close errors on I/O are now always logged as unraisable exceptions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted the suggestion :)

@corona10 corona10 requested a review from vstinner December 4, 2025 18:43
#endif

#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__) && defined(Py_DEBUG)
# define _PyAnnotateMemoryMap(addr, size, name) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need macros? it is just to avoid the (unsigned long) cast? If so, I would suggest having a static inline function (but you can have a macro for the no-op case to avoid an empty function) and leave the rest to the compiler. Or if you want to keep a macro, please align the \ (see PEP-7).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer a static inline function for cleaner code.

#endif

#if defined(HAVE_PR_SET_VMA_ANON_NAME) && defined(__linux__) && defined(Py_DEBUG)
# define _PyAnnotateMemoryMap(addr, size, name) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer a static inline function for cleaner code.

@corona10 corona10 requested a review from vstinner December 5, 2025 14:08
Copy link
Member Author

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner @picnixz I 've applied all your suggestion.

modules that are missing or packaged separately.
(Contributed by Stan Ulbrych and Petr Viktorin in :gh:`139707`.)

* Annotating anonymous mmap usage is now supported if Linux kernel supports ``PR_SET_VMA_ANON_NAME`` (Linux 5.17 or newer).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* Annotating anonymous mmap usage is now supported if Linux kernel
  supports :manpage:`PR_SET_VMA_ANON_NAME <PR_SET_VMA(2const)>`
  (Linux 5.17 or newer).

I don't know under which manpage entry it is, but if you locally run Sphinx, you should be able to find the correct one. That way, users would have a link to click on. If there isn't a manpage, nevermind.

}
#endif
assert(strlen(name) < 80);
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)(addr), size, name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to restore the previous errno maybe? (I actually don't know whether we're consistent in "ignoring" errnos).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants