What is Starlark?
Starlark is a Python dialect designed for embedded scripting. It features:- Python-like syntax (loops, functions, lambdas)
- Immutable by default
- No imports, classes, or exceptions
- Deterministic and safe for sandboxed execution
while loops, top-level control flow, sets, and recursion.
Basic Usage
Execute inline code
Run a script file
Pass arguments
ARGS list:
Script Structure
A typical script:- Opens one or more profiles with
open() - Analyzes data using Profile methods
- Outputs results with
print()oremit() - Optionally exits with
fail()for CI checks
Core Types
The API provides specialized types for working with profile data:- Profile - A loaded profile scoped to one event type
- Stack - A single stack trace with metadata
- Bucket - A time bucket from timeline analysis
- Frame - A single stack frame with method/class/package info
- Method - Hot method entry with self/total percentages
- Thread - Thread metadata with sample distribution
- Diff - Comparison result between two profiles
Global Functions
See Functions for the complete list of global functions:open(path)- Load a profile (JFR, pprof, collapsed)diff(a, b)- Compare two profiles by self%emit(stack)- Write a stack in collapsed formatround(x, decimals)- Round floats for displayljust(value, width)/rjust(value, width)- String alignmentmatch(string, pattern)- Regex matchingfail(msg)- Print to stderr and exit 1warn(msg)- Print to stderr, continue
Starlark Notes
String Formatting
%s, %d, %f work but without width/padding/precision:
Reserved Keywords
from is reserved - use start/end kwargs:
Collections
Example Scripts
Hot methods
Filter and emit pipeline
ap-query hot for visualization:
Time window comparison
Group by thread pool
Next Steps
- Profile API - Profile type and methods
- Stack API - Stack filtering and navigation
- Functions - Global utility functions
- Bucket API - Timeline analysis