← 返回命令列表

Linux command

sample 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Sample a process by PID

sample [pid]

Sample a process by name

sample [Safari] [30]

Sample with custom interval

sample [pid] [10] [5]

Wait for a process to start

sample [MyApp] [10] -wait

Save sample output to a file

sample [pid] [10] -file [output.txt]

Sample a process that may exit

sample [pid] [10] -mayDie

说明

sample is a macOS command-line profiler that captures the call stacks of all threads in a process at regular intervals. It suspends the process briefly during each sample, records the current stack trace, then resumes execution. The resulting report shows a condensed call tree indicating which functions were executing and how often they appeared on the stack. This sampling approach efficiently identifies hot spots and performance bottlenecks without the overhead of full instrumentation. Fast sampling rates (lower interval values) provide more data points and better coverage of short-lived function calls. The output can be analyzed as text or opened in Xcode Instruments for graphical visualization.

参数

pid | partial-name
Process ID or partial executable name to sample
duration
Sampling duration in seconds (default: 10)
interval
Sampling interval in milliseconds (default: 1)
-wait
Wait for the specified process to launch, then begin sampling
-mayDie
Immediately capture symbol locations in case the process exits during sampling
-fullPaths
Show full paths to source files in the output
-file _filename_
Write output to the specified file instead of stdout
-e
Open the output file in TextEdit when sampling completes

FAQ

What is the sample command used for?

sample is a macOS command-line profiler that captures the call stacks of all threads in a process at regular intervals. It suspends the process briefly during each sample, records the current stack trace, then resumes execution. The resulting report shows a condensed call tree indicating which functions were executing and how often they appeared on the stack. This sampling approach efficiently identifies hot spots and performance bottlenecks without the overhead of full instrumentation. Fast sampling rates (lower interval values) provide more data points and better coverage of short-lived function calls. The output can be analyzed as text or opened in Xcode Instruments for graphical visualization.

How do I run a basic sample example?

Run `sample [pid]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does pid | partial-name do in sample?

Process ID or partial executable name to sample