LinuxCommandLibrary

sample

TLDR

Sample a process by PID for 10 seconds

$ sample [pid]
copy
Sample a process by name for a specific duration
$ sample [Safari] [30]
copy
Sample with custom interval (in milliseconds)
$ sample [pid] [10] [5]
copy
Wait for a process to start then sample it
$ sample [MyApp] [10] -wait
copy
Save sample output to a file
$ sample [pid] [10] -file [output.txt]
copy
Sample a process that may exit during sampling
$ sample [pid] [10] -mayDie
copy

SYNOPSIS

sample pid|partial-name [duration] [interval] [-wait] [-mayDie] [-fullPaths] [-file filename]

DESCRIPTION

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.

PARAMETERS

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

CAVEATS

Sampling only captures functions executing at sample points, so very fast functions may be missed. The process is briefly suspended during each sample, which can affect timing-sensitive applications. Requires appropriate permissions to sample processes owned by other users.

HISTORY

sample is part of Apple's developer tools suite for macOS, providing command-line access to the same sampling technology used by Instruments and Activity Monitor. It enables performance profiling without requiring the full Xcode IDE.

SEE ALSO

spindump(8), leaks(1), heap(1), instruments(1)

Copied to clipboard