Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 88 additions & 27 deletions Lib/profile/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import _remote_debugging
import os
import pstats
import subprocess
import statistics
import sys
import sysconfig
Expand Down Expand Up @@ -542,46 +543,66 @@ def main():
parser = argparse.ArgumentParser(
description=(
"Sample a process's stack frames and generate profiling data.\n"
"Supports two output formats:\n"
" - pstats: Detailed profiling statistics with sorting options\n"
" - collapsed: Stack traces for generating flamegraphs\n"
"Supports the following target modes:\n"
" - -p PID: Profile an existing process by PID\n"
" - -m MODULE [ARGS...]: Profile a module as python -m module ... \n"
" - filename [ARGS...]: Profile the specified script by running it in a subprocess\n"
"\n"
"Examples:\n"
" # Profile process 1234 for 10 seconds with default settings\n"
" python -m profile.sample 1234\n"
" python -m profile.sample -p 1234\n"
"\n"
" # Profile a script by running it in a subprocess\n"
" python -m profile.sample myscript.py arg1 arg2\n"
"\n"
" # Profile a module by running it as python -m module in a subprocess\n"
" python -m profile.sample -m mymodule arg1 arg2\n"
"\n"
" # Profile with custom interval and duration, save to file\n"
" python -m profile.sample -i 50 -d 30 -o profile.stats 1234\n"
" python -m profile.sample -i 50 -d 30 -o profile.stats -p 1234\n"
"\n"
" # Generate collapsed stacks for flamegraph\n"
" python -m profile.sample --collapsed 1234\n"
" python -m profile.sample --collapsed -p 1234\n"
"\n"
" # Profile all threads, sort by total time\n"
" python -m profile.sample -a --sort-tottime 1234\n"
" python -m profile.sample -a --sort-tottime -p 1234\n"
"\n"
" # Profile for 1 minute with 1ms sampling interval\n"
" python -m profile.sample -i 1000 -d 60 1234\n"
" python -m profile.sample -i 1000 -d 60 -p 1234\n"
"\n"
" # Show only top 20 functions sorted by direct samples\n"
" python -m profile.sample --sort-nsamples -l 20 1234\n"
" python -m profile.sample --sort-nsamples -l 20 -p 1234\n"
"\n"
" # Profile all threads and save collapsed stacks\n"
" python -m profile.sample -a --collapsed -o stacks.txt 1234\n"
" python -m profile.sample -a --collapsed -o stacks.txt -p 1234\n"
"\n"
" # Profile with real-time sampling statistics\n"
" python -m profile.sample --realtime-stats 1234\n"
" python -m profile.sample --realtime-stats -p 1234\n"
"\n"
" # Sort by sample percentage to find most sampled functions\n"
" python -m profile.sample --sort-sample-pct 1234\n"
" python -m profile.sample --sort-sample-pct -p 1234\n"
"\n"
" # Sort by cumulative samples to find functions most on call stack\n"
" python -m profile.sample --sort-nsamples-cumul 1234"
" python -m profile.sample --sort-nsamples-cumul -p 1234\n"
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)

# Required arguments
parser.add_argument("pid", type=int, help="Process ID to sample")
# Target selection
target_group = parser.add_mutually_exclusive_group(required=True)
target_group.add_argument(
"-p", "--pid", type=int, help="Process ID to sample"
)
target_group.add_argument(
"-m", "--module",
nargs=argparse.REMAINDER,
help="Run and profile a module as python -m module [ARGS...]"
)
target_group.add_argument(
"script",
nargs=argparse.REMAINDER,
help="Script to run and profile, with optional arguments"
)

# Sampling options
sampling_group = parser.add_argument_group("Sampling configuration")
Expand Down Expand Up @@ -712,19 +733,59 @@ def main():

sort_value = args.sort if args.sort is not None else 2

sample(
args.pid,
sample_interval_usec=args.interval,
duration_sec=args.duration,
filename=args.outfile,
all_threads=args.all_threads,
limit=args.limit,
sort=sort_value,
show_summary=not args.no_summary,
output_format=args.format,
realtime_stats=args.realtime_stats,
)
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."
)
Comment on lines +736 to +739
Copy link
Preview

Copilot AI Jul 29, 2025

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.

Suggested change
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.


if args.pid:
sample(
args.pid,
sample_interval_usec=args.interval,
duration_sec=args.duration,
filename=args.outfile,
all_threads=args.all_threads,
limit=args.limit,
sort=sort_value,
show_summary=not args.no_summary,
output_format=args.format,
realtime_stats=args.realtime_stats,
)
elif args.module or args.script:
if args.module:
cmd = [sys.executable, "-m", *args.module]
else:
cmd = [sys.executable, *args.script]

process = subprocess.Popen(cmd)

try:
exit_code = process.wait(timeout=0.1)
Copy link
Preview

Copilot AI Jul 29, 2025

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.

Suggested change
exit_code = process.wait(timeout=0.1)
exit_code = process.wait(timeout=DEFAULT_PROCESS_WAIT_TIMEOUT)

Copilot uses AI. Check for mistakes.

Copy link
Member

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?

sys.exit(exit_code)
except subprocess.TimeoutExpired:
pass

try:
sample(
process.pid,
sort=sort_value,
sample_interval_usec=args.interval,
duration_sec=args.duration,
filename=args.outfile,
all_threads=args.all_threads,
limit=args.limit,
show_summary=not args.no_summary,
output_format=args.format,
realtime_stats=args.realtime_stats,
)
finally:
if process.poll() is None:
process.terminate()
try:
process.wait(timeout=2)
Copy link
Preview

Copilot AI Jul 29, 2025

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.

Suggested change
process.wait(timeout=2)
process.wait(timeout=GRACEFUL_TERMINATION_TIMEOUT_SEC)

Copilot uses AI. Check for mistakes.

except subprocess.TimeoutExpired:
process.kill()
process.wait()

if __name__ == "__main__":
main()
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy