1*8975f5c5SAndroid Build Coastguard Worker# Debugging Slow Builds 2*8975f5c5SAndroid Build Coastguard Worker 3*8975f5c5SAndroid Build Coastguard WorkerDid you know that Ninja writes a log to disk after each build? 4*8975f5c5SAndroid Build Coastguard Worker 5*8975f5c5SAndroid Build Coastguard WorkerTo see what kinds of files took the longest for your previous build: 6*8975f5c5SAndroid Build Coastguard Worker 7*8975f5c5SAndroid Build Coastguard Worker```sh 8*8975f5c5SAndroid Build Coastguard Workercd out/Default 9*8975f5c5SAndroid Build Coastguard Worker# Lives in depot_tools: 10*8975f5c5SAndroid Build Coastguard Workerpost_build_ninja_summary.py 11*8975f5c5SAndroid Build Coastguard Worker``` 12*8975f5c5SAndroid Build Coastguard Worker 13*8975f5c5SAndroid Build Coastguard WorkerBecause the build is highly parallelized the `elapsed time` values are usually 14*8975f5c5SAndroid Build Coastguard Workernot meaningful so the `weighted time` numbers are calculated to approximate 15*8975f5c5SAndroid Build Coastguard Workerthe impact of build steps on wall-clock time. 16*8975f5c5SAndroid Build Coastguard Worker 17*8975f5c5SAndroid Build Coastguard WorkerYou can also set `NINJA_SUMMARIZE_BUILD=1` to have this command run 18*8975f5c5SAndroid Build Coastguard Workerafter each `autoninja` invocation. Setting this environment variable also runs 19*8975f5c5SAndroid Build Coastguard Workerninja with `-d stats` which causes it to print out internal information such 20*8975f5c5SAndroid Build Coastguard Workeras StartEdge times, which measures the times to create processes, and it 21*8975f5c5SAndroid Build Coastguard Workermodifies the `NINJA_STATUS` environment variable to add information such as how 22*8975f5c5SAndroid Build Coastguard Workermany processes are running at any given time - both are useful for detecting 23*8975f5c5SAndroid Build Coastguard Workerslow process creation. You can get this last benefit on its own by setting 24*8975f5c5SAndroid Build Coastguard Worker`NINJA_STATUS=[%r processes, %f/%t @ %o/s : %es ] ` (trailing space is 25*8975f5c5SAndroid Build Coastguard Workerintentional). 26*8975f5c5SAndroid Build Coastguard Worker 27*8975f5c5SAndroid Build Coastguard WorkerTo generate a Chrome trace of your most recent build: 28*8975f5c5SAndroid Build Coastguard Worker 29*8975f5c5SAndroid Build Coastguard Worker```sh 30*8975f5c5SAndroid Build Coastguard Workergit clone https://github.com/nico/ninjatracing 31*8975f5c5SAndroid Build Coastguard Workerninjatracing/ninjatracing out/Default/.ninja_log > trace.json 32*8975f5c5SAndroid Build Coastguard Worker# Then open in https://ui.perfetto.dev/ 33*8975f5c5SAndroid Build Coastguard Worker``` 34*8975f5c5SAndroid Build Coastguard Worker 35*8975f5c5SAndroid Build Coastguard WorkerIf your build is stuck on a long-running build step you can see what it is by 36*8975f5c5SAndroid Build Coastguard Workerrunning `tools/buildstate.py`. 37*8975f5c5SAndroid Build Coastguard Worker 38*8975f5c5SAndroid Build Coastguard Worker## Slow Bot Builds 39*8975f5c5SAndroid Build Coastguard Worker 40*8975f5c5SAndroid Build Coastguard WorkerOur bots run `ninjatracing` and `post_build_ninja_summary.py` as well. 41*8975f5c5SAndroid Build Coastguard Worker 42*8975f5c5SAndroid Build Coastguard WorkerFind the trace at: `postprocess for reclient > gsutil upload ninja_log > ninja_log`: 43*8975f5c5SAndroid Build Coastguard Worker 44*8975f5c5SAndroid Build Coastguard Worker * _".ninja_log in table format (full)"_ is for `post_build_ninja_summary.py`. 45*8975f5c5SAndroid Build Coastguard Worker * _"trace viewer (sort_by_end)"_ is for `ninjatracing`. 46*8975f5c5SAndroid Build Coastguard Worker 47*8975f5c5SAndroid Build Coastguard Worker## Advanced(ish) Tips 48*8975f5c5SAndroid Build Coastguard Worker 49*8975f5c5SAndroid Build Coastguard Worker* Use `gn gen --tracelog trace.json` to create a trace for `gn gen`. 50*8975f5c5SAndroid Build Coastguard Worker* Many Android templates make use of 51*8975f5c5SAndroid Build Coastguard Worker [`md5_check.py`](https://cs.chromium.org/chromium/src/build/android/gyp/util/md5_check.py) 52*8975f5c5SAndroid Build Coastguard Worker to optimize incremental builds. 53*8975f5c5SAndroid Build Coastguard Worker * Set `PRINT_BUILD_EXPLANATIONS=1` to have these commands log which inputs 54*8975f5c5SAndroid Build Coastguard Worker changed. 55*8975f5c5SAndroid Build Coastguard Worker* If you suspect files are being rebuilt unnecessarily during incremental 56*8975f5c5SAndroid Build Coastguard Worker builds: 57*8975f5c5SAndroid Build Coastguard Worker * Use `ninja -n -d explain` to figure out why ninja thinks a target is dirty. 58*8975f5c5SAndroid Build Coastguard Worker * Ensure actions are taking advantage of ninja's `restat=1` feature by not 59*8975f5c5SAndroid Build Coastguard Worker updating timestamps on outputs when their contents do not change. 60*8975f5c5SAndroid Build Coastguard Worker * E.g. by using [`build_utils.AtomicOutput()`] 61*8975f5c5SAndroid Build Coastguard Worker 62*8975f5c5SAndroid Build Coastguard Worker[`build_utils.AtomicOutput()`]: https://source.chromium.org/search?q=symbol:AtomicOutput%20f:build 63