Skip to content

Commit

Permalink
Add --ftime-trace to dmd
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Apr 7, 2024
1 parent e9c6884 commit 7d5c46c
Show file tree
Hide file tree
Showing 17 changed files with 657 additions and 7 deletions.
16 changes: 16 additions & 0 deletions changelog/dmd.ftime-trace.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Build time profiling has been added to dmd

The `-ftime-trace` switch that the LDC compiler already has, is now also available in dmd.
It can be used to figure out which parts of your code take the longest to compile, so you can optimize your build times.

$(CONSOLE
dmd --ftime-trace app.d
)

This will output `app.o.time-trace`.

A different output file can be selected with `--ftime-trace-file=trace.json`.

The output is in Google Chrome's profiler format, which can be converted to readable text with the included `timetrace2txt` tool, or be viewed in an interactive viewer like [ui.perfetto.dev](https://ui.perfetto.dev).

See also this YouTube tutorial: [Easily Reduce Build Times by Profiling the D Compiler](https://www.youtube.com/watch?v=b8wZqU5t9vs)
2 changes: 1 addition & 1 deletion compiler/src/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ auto sourceFiles()
stringtable.d utf.d
"),
common: fileArray(env["COMMON"], "
bitfields.d file.d int128.d blake3.d outbuffer.d smallbuffer.d charactertables.d identifiertables.d
bitfields.d file.d int128.d blake3.d outbuffer.d smallbuffer.d timetrace.d charactertables.d identifiertables.d
"),
commonHeaders: fileArray(env["COMMON"], "
outbuffer.h
Expand Down
21 changes: 21 additions & 0 deletions compiler/src/dmd/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ dmd -cov -unittest myprog.d
"generate position independent executables",
cast(TargetOS) (TargetOS.all & ~(TargetOS.Windows | TargetOS.OSX))
),
Option("-ftime-trace",
"turn on compile time profiler, generate JSON file with results",
"Per function, the time to analyze it, call it from CTFE, generate code for it etc. will be measured,
and events with a time longer than 500 microseconds (adjustable with `-ftime-trace-granularity`)
will be recorded.
The profiling result is output in the Chrome Trace Event Format,
$(LINK2 https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview, described here).
This can be turned into a more readable text file with the included tool `timetrace2txt`, or inspected
with an interactive viewer such as $(LINK2 https://ui.perfetto.dev/, Perfetto)."
),
Option("-ftime-trace-granularity=",
"Minimum time granularity (in microseconds) traced by time profiler (default: 500)",
"Measured events shorter than the specified time will be discarded from the output.
Set it too high, and interesting events may not show up in the output.
Set too low, and the profiler overhead will be larger, and the output will be cluttered with tiny events."
),
Option("-ftime-trace-file=<filename>",
"specify output file for --ftime-trace",
"By default, the output name is the same as the first object file name, but with the `.time-trace` extension appended.
A different filename can be chosen with this option, including a path relative to the current directory or an absolute path."
),
Option("g",
"add symbolic debug info",
`$(WINDOWS
Expand Down
Loading

0 comments on commit 7d5c46c

Please sign in to comment.