-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
[gh-135953] Profile a module or script with sampling profiler #136777
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
base: main
Are you sure you want to change the base?
Conversation
Add `-m` and `filename` arguments to the sampling profiler to launch the specified Python program in a subprocess and start profiling it. Previously only a PID was accepted, this can now be done by passing `-p PID`.
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit 1bf048a 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F136777%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
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.
Pull Request Overview
This PR extends the sampling profiler to support profiling modules and scripts by launching them in subprocesses, in addition to the existing PID-based profiling. The change improves the usability of the profiler by allowing users to profile Python programs from startup rather than only attaching to existing processes.
- Adds
-m
and script filename arguments as mutually exclusive alternatives to the existing-p PID
option - Updates argument parsing to handle the new target modes with proper validation
- Implements subprocess management with graceful termination handling
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
Lib/profile/sample.py | Adds argument parsing for module and script modes, implements subprocess launching and management |
Lib/test/test_sample_profiler.py | Updates existing CLI tests to use -p flag and adds comprehensive test coverage for new module/script functionality |
Comments suppressed due to low confidence (1)
Lib/test/test_sample_profiler.py:1637
- The test uses
contextlib.chdir()
to change directories for module discovery, but doesn't test the case where the module is not found or the directory change fails. Consider adding a test case for module resolution errors.
contextlib.chdir(tempdir.name),
if not(args.pid or args.module or args.script): | ||
parser.error( | ||
"You must specify either a process ID (-p), a module (-m), or a script to run." | ||
) |
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.
The condition not(args.pid or args.module or args.script)
is redundant since the mutually exclusive group is already marked as required=True
. The argparse library will automatically enforce that one of these options is provided.
if not(args.pid or args.module or args.script): | |
parser.error( | |
"You must specify either a process ID (-p), a module (-m), or a script to run." | |
) | |
# The mutually exclusive group already enforces that one of these arguments is required. |
Copilot uses AI. Check for mistakes.
process = subprocess.Popen(cmd) | ||
|
||
try: | ||
exit_code = process.wait(timeout=0.1) |
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.
The hardcoded timeout of 0.1 seconds is a magic number. Consider defining this as a named constant or making it configurable, as some programs may have longer startup times.
exit_code = process.wait(timeout=0.1) | |
exit_code = process.wait(timeout=DEFAULT_PROCESS_WAIT_TIMEOUT) |
Copilot uses AI. Check for mistakes.
if process.poll() is None: | ||
process.terminate() | ||
try: | ||
process.wait(timeout=2) |
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.
The hardcoded timeout of 2 seconds for graceful termination is a magic number. Consider defining this as a named constant for better maintainability.
process.wait(timeout=2) | |
process.wait(timeout=GRACEFUL_TERMINATION_TIMEOUT_SEC) |
Copilot uses AI. Check for mistakes.
process = subprocess.Popen(cmd) | ||
|
||
try: | ||
exit_code = process.wait(timeout=0.1) |
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.
Hummm, why is this? Just in case the process fails immediately?
Add
-m
andfilename
arguments to the sampling profiler to launch the specified Python program in a subprocess and start profiling it. Previously only a PID was accepted, this can now be done by passing-p PID
.