MCPcopy Index your code
hub / github.com/python/cpython / _handle_replay

Function _handle_replay

Lib/profiling/sampling/cli.py:1199–1244  ·  view source on GitHub ↗

Handle the 'replay' command - convert binary profile to another format.

(args)

Source from the content-addressed store, hash-verified

1197
1198
1199def _handle_replay(args):
1200 """Handle the 'replay' command - convert binary profile to another format."""
1201 if not os.path.exists(args.input_file):
1202 sys.exit(f"Error: Input file not found: {args.input_file}")
1203
1204 with BinaryReader(args.input_file) as reader:
1205 info = reader.get_info()
1206 interval = info['sample_interval_us']
1207
1208 print(f"Replaying {info['sample_count']} samples from {args.input_file}")
1209 print(f" Sample interval: {interval} us")
1210 print(f" Compression: {'zstd' if info.get('compression_type', 0) == 1 else 'none'}")
1211
1212 collector = _create_collector(
1213 args.format, interval, skip_idle=False,
1214 diff_baseline=args.diff_baseline
1215 )
1216
1217 def progress_callback(current, total):
1218 if total > 0:
1219 pct = current / total
1220 bar_width = 40
1221 filled = int(bar_width * pct)
1222 bar = '█' * filled + '░' * (bar_width - filled)
1223 print(f"\r [{bar}] {pct*100:5.1f}% ({current:,}/{total:,})", end="", flush=True)
1224
1225 count = reader.replay_samples(collector, progress_callback)
1226 print()
1227
1228 if args.format == "pstats":
1229 if args.outfile:
1230 collector.export(args.outfile)
1231 else:
1232 sort_choice = args.sort if args.sort is not None else "nsamples"
1233 limit = args.limit if args.limit is not None else 15
1234 sort_mode = _sort_to_mode(sort_choice)
1235 collector.print_stats(sort_mode, limit, not args.no_summary, PROFILING_MODE_WALL)
1236 else:
1237 filename = args.outfile or _generate_output_filename(args.format, os.getpid())
1238 collector.export(filename)
1239
1240 # Auto-open browser for HTML output if --browser flag is set
1241 if args.format in ('flamegraph', 'diff_flamegraph', 'heatmap') and getattr(args, 'browser', False):
1242 _open_in_browser(filename)
1243
1244 print(f"Replayed {count} samples")
1245
1246
1247if __name__ == "__main__":

Callers

nothing calls this directly

Calls 12

BinaryReaderClass · 0.85
_create_collectorFunction · 0.85
_sort_to_modeFunction · 0.85
_open_in_browserFunction · 0.85
replay_samplesMethod · 0.80
existsMethod · 0.45
exitMethod · 0.45
get_infoMethod · 0.45
getMethod · 0.45
exportMethod · 0.45
print_statsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…