Repository navigation
gh-135953: Implement sampling tool under profile.sample #135998
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
82092dd
Move core profiling module into a package
lkollar cd5c814
Add sampling profiler
lkollar b26d1fa
Add pstats.SampledStats to display sample results
lkollar 2e8e0a9
Add collapsed sample profiler output format
lkollar bf9e3fa
Add tests for sampling profiler
lkollar aeca768
fixup! Add tests for sampling profiler
lkollar 7a76f68
Improve CLI
pablogsal 97ba97e
Add more tests
pablogsal 543b13d
Format files
pablogsal a8f1bdd
Formatting improvementts
pablogsal 0440856
Fix small issues
pablogsal 65d60e9
Add news entry
pablogsal bf15570
Moar fixes
pablogsal 219670e
Use the new gil sampling
pablogsal f3dc377
Correct NEWS entry
pablogsal 3002ab8
Add docs
pablogsal 59137e4
Add what's new
pablogsal fe12677
Add what's new 2
pablogsal c28b0e0
Add what's new 2
pablogsal 0c7b1f1
fixup! Add docs
lkollar d7706e6
fixup! fixup! Add docs
lkollar 5c8d939
fixup! fixup! fixup! Add docs
lkollar b75aee1
fixup! Add what's new 2
lkollar acace5b
Fix free threading
pablogsal f50cfd7
Add sample.profile module reference
lkollar 8d5dc18
Merge branch 'main' into sampling-profiler
lkollar d534f1e
Fix whatsnew
lkollar 0360a72
Change profile.stack_collectors to singular
lkollar 4fa0832
Remove redundant len check
lkollar b42812e
Handle process exiting during sampling
lkollar 8d9cbfe
fixup! Handle process exiting during sampling
lkollar 260d934
Protect against permission errors
pablogsal f0de45a
Sync processes in test_sample_profiler
pablogsal dbe2c0a
More fixes
pablogsal bc43ec7
Skip unsupported platforms in profiler tests
lkollar 85f12d0
Add profile directory to Makefile.pre.in
lkollar a33d166
Raise on unsupported platforms in GetPyRuntimeAddress
lkollar 0235127
fixup! Skip unsupported platforms in profiler tests
lkollar 90260a6
Require subprocess support
pablogsal 5683b76
fixup! fixup! Skip unsupported platforms in profiler tests
lkollar 5a83439
Skip test on Android
lkollar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Move core profiling module into a package
This allows adding a new 'sample' submodule and enables invoking the sampling profiler through 'python -m profile.sample', while retaining backwards compatibility when using 'profile'.
- Loading branch information
commit 82092ddacfa35195f7be4625f125db6e54902dbe
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from .profile import run | ||
| from .profile import runctx | ||
| from .profile import Profile | ||
| from .profile import _Utils | ||
|
|
||
| __all__ = ['run', 'runctx', 'Profile'] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import io | ||
| import importlib.machinery | ||
| import os | ||
| import sys | ||
| from optparse import OptionParser | ||
|
|
||
| from .profile import runctx | ||
|
|
||
|
|
||
| def main(): | ||
|
|
||
| usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..." | ||
| parser = OptionParser(usage=usage) | ||
| parser.allow_interspersed_args = False | ||
| parser.add_option('-o', '--outfile', dest="outfile", | ||
| help="Save stats to <outfile>", default=None) | ||
| parser.add_option('-m', dest="module", action="store_true", | ||
| help="Profile a library module.", default=False) | ||
| parser.add_option('-s', '--sort', dest="sort", | ||
| help="Sort order when printing to stdout, based on pstats.Stats class", | ||
| default=-1) | ||
|
|
||
| if not sys.argv[1:]: | ||
| parser.print_usage() | ||
| sys.exit(2) | ||
|
|
||
| (options, args) = parser.parse_args() | ||
| sys.argv[:] = args | ||
|
|
||
| # The script that we're profiling may chdir, so capture the absolute path | ||
| # to the output file at startup. | ||
| if options.outfile is not None: | ||
| options.outfile = os.path.abspath(options.outfile) | ||
|
|
||
| if len(args) > 0: | ||
| if options.module: | ||
| import runpy | ||
| code = "run_module(modname, run_name='__main__')" | ||
| globs = { | ||
| 'run_module': runpy.run_module, | ||
| 'modname': args[0] | ||
| } | ||
| else: | ||
| progname = args[0] | ||
| sys.path.insert(0, os.path.dirname(progname)) | ||
| with io.open_code(progname) as fp: | ||
| code = compile(fp.read(), progname, 'exec') | ||
| spec = importlib.machinery.ModuleSpec(name='__main__', loader=None, | ||
| origin=progname) | ||
| globs = { | ||
| '__spec__': spec, | ||
| '__file__': spec.origin, | ||
| '__name__': spec.name, | ||
| '__package__': None, | ||
| '__cached__': None, | ||
| } | ||
| try: | ||
| runctx(code, globs, None, options.outfile, options.sort) | ||
| except BrokenPipeError as exc: | ||
| # Prevent "Exception ignored" during interpreter shutdown. | ||
| sys.stdout = None | ||
| sys.exit(exc.errno) | ||
| else: | ||
| parser.print_usage() | ||
| return parser | ||
|
|
||
| # When invoked as main program, invoke the profiler on a script | ||
| if __name__ == '__main__': | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: note that this isn't exactly enough to satisfy "backwards compatibility". Users that monkey-patched
profile.runctxwill now still be patching it but it won't be very effective.