Reading .ninja_log & ninja -t
Before you optimise a build you need its shape: which outputs exist, which commands produce them, what each one depends on, and how long each one took last time. Ninja records all of that, and its built-in -t tools let you query it without any extra software. (The basics of the log were introduced in The Build Log & -d explain; this lesson uses it for performance work.)
The log as a profile
.ninja_log has one tab-separated line per output: start and end time in milliseconds since the build began, the output's mtime, the output path, and a hash of the command. The times change on every run, so this session prints only the stable columns, and counts what the log contains after a clean build and after an incremental one:
mkdir -p prof/src && cd prof
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Prof CXX)
add_library(core STATIC src/lexer.cpp src/parser.cpp src/eval.cpp)
add_executable(calc src/main.cpp)
target_link_libraries(calc PRIVATE core)
CM
for f in lexer parser eval; do echo "int $f() { return 1; }" > src/$f.cpp; done
echo 'int lexer(); int parser(); int eval(); int main() { return lexer() + parser() + eval() - 3; }' > src/main.cpp
cmake -S . -B build -G Ninja > /dev/null
cmake --build build > /dev/null
cd build
echo "entries after a clean build: $(grep -vc '^#' .ninja_log)"
awk -F'\t' 'NR > 1 { print $4 }' .ninja_log
sleep 1 && touch ../src/parser.cpp
ninja > /dev/null
echo "entries after editing parser.cpp: $(grep -vc '^#' .ninja_log)"
awk -F'\t' 'NR > 1 { n[$4]++ } END { for (o in n) if (n[o] > 1) print "logged twice:", o }' .ninja_log | sortentries after a clean build: 6
CMakeFiles/core.dir/src/lexer.cpp.o
CMakeFiles/core.dir/src/parser.cpp.o
CMakeFiles/core.dir/src/eval.cpp.o
libcore.a
CMakeFiles/calc.dir/src/main.cpp.o
calc
entries after editing parser.cpp: 9
logged twice: CMakeFiles/core.dir/src/parser.cpp.o
logged twice: calc
logged twice: libcore.a
Ninja appends a line every time it runs a command, so after the edit the log holds a second entry for the three outputs that were rebuilt: the object, the archive and the executable. (Ninja recompacts the log from time to time and keeps only the newest entry per output; ninja -t recompact does it on demand.) Tools that read the log, such as the ninjatracing script that converts it into a Chrome/Perfetto trace, take the most recent entry per output.
With durations, the same file answers "which steps are slowest?" and, combined with the dependency graph, "what is the critical path?". You'll compute that from a fixed log in the next lesson.
ninja -t: questions about the graph
| Tool | Answers |
|---|---|
ninja -t targets all |
every output and the rule that builds it |
ninja -t query X |
X's direct inputs and the outputs that use it |
ninja -t inputs X |
all inputs of X, recursively |
ninja -t commands X |
every command needed to build X, in order |
ninja -t deps X.o |
the headers recorded for an object (from -MD) |
ninja -t compdb |
compile_commands.json for tools |
ninja -t graph X |
Graphviz dot source of the graph |
ninja -n |
a dry run: what would run now |
mkdir -p q/src && cd q
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Q CXX)
add_library(core STATIC src/lexer.cpp src/parser.cpp)
target_include_directories(core PUBLIC src)
add_executable(calc src/main.cpp)
target_link_libraries(calc PRIVATE core)
CM
printf '#pragma once\nint lexer();\nint parser();\n' > src/core.hpp
printf '#include "core.hpp"\n#include <string>\nint lexer() { return std::string("x").size(); }\n' > src/lexer.cpp
printf '#include "core.hpp"\nint parser() { return lexer(); }\n' > src/parser.cpp
printf '#include "core.hpp"\nint main() { return parser() - 1; }\n' > src/main.cpp
cmake -S . -B build -G Ninja > /dev/null && cmake --build build > /dev/null
cd build
echo "--- query calc"; ninja -t query calc
echo "--- compile commands needed for calc: $(ninja -t commands calc | grep -c ' -c ')"
echo "--- headers per object (system headers counted, not listed)"
ninja -t deps | awk '/#deps/ { obj = ($1 ~ /\.o:$/) ? $1 : "" } /^ / && obj { n[obj]++ } END { for (o in n) print n[o], o }' | sort -k2
echo "--- dry run after touching core.hpp"
sleep 1 && touch ../src/core.hpp
ninja -n | sed 's/^\[[0-9]*\/[0-9]*\] //'--- query calc
calc:
input: CXX_EXECUTABLE_LINKER__calc_
CMakeFiles/calc.dir/src/main.cpp.o
| libcore.a
|| libcore.a
outputs:
all
--- compile commands needed for calc: 3
--- headers per object (system headers counted, not listed)
3 CMakeFiles/calc.dir/src/main.cpp.o:
140 CMakeFiles/core.dir/src/lexer.cpp.o:
3 CMakeFiles/core.dir/src/parser.cpp.o:
--- dry run after touching core.hpp
Building CXX object CMakeFiles/core.dir/src/lexer.cpp.o
Building CXX object CMakeFiles/core.dir/src/parser.cpp.o
Building CXX object CMakeFiles/calc.dir/src/main.cpp.o
Linking CXX static library libcore.a
Linking CXX executable calc
In the query output, | marks an implicit input and || an order-only one: CMake lists the library both as a link input and as something that must exist before calc's objects are compiled. The deps count is a quick header-weight indicator: lexer.cpp pulls in well over a hundred files because it includes <string>, the others only their own header and stdc-predef.h. ninja -n shows the blast radius of an edit before you make it: touching core.hpp means five commands.
Beyond Ninja
- CMake can write a profile of the configure step:
cmake --profiling-format=google-trace --profiling-output=cmake-trace.json. Slow configures (hundreds offind_packagecalls,try_compiles) are a real problem in large projects. - Gradle has build scans (
--scan, Develocity) and--profileHTML reports; Maven has themaven-profilerextension. See Maven & Gradle. - Bazel writes a JSON trace profile of every build (
--profile, thenbazel analyze-profile). See Bazel. - MSBuild has binary logs (
-bl) viewed with the MSBuild Structured Log Viewer.
All of them answer the same three questions: what ran, why it ran, and what it waited for.
Test yourself
After editing parser.cpp, which outputs got a second entry in .ninja_log?
- Only parser.cpp.o
- parser.cpp.o, libcore.a and calc
- All six outputs
- None, the log is rewritten
Which tool shows what *would* be rebuilt without running anything?
- ninja -t deps
- ninja -n
- ninja -t recompact
- ninja -t targets
What does ninja -t deps report for an object file?
- Its link libraries
- The headers the compiler said it read (from -MD)
- Its compile time
- Its size