GCC Debug Information Overhead

I was curious how various debug information generation flags affect compile times and disk usage, in particular, how GCC and Binutils respond to different debug level and DWARF compression options.

To test this I decided to build the Linux kernel with all the different flag combinations while measuring the time taken and disk space used by build directory and output vmlinux image.

The GCC source code says the following about debug levels:

  • -g0: Write no debugging info
  • -g1: Write minimal info to support tracebacks only.
  • -g2: Write info for all declarations (and line table).
  • -g3: Write normal info plus #define/#undef info.

And compression is controlled with:

  • -gz=zlib: (Equivalent to -gz) Use zlib compression in ELF gABI format
  • -gz=none: Don’t compress debug sections

Build specifications:

  • Toolchain: GCC 10.2.0 and GNU Binutils 2.35.1
  • Machine: i7-3770 CPU, 16GB RAM, SSD
  • Source code to build: Linux 5.9.11
  • Build config: default (x86_64_defconfig)
  • Build line: make --jobs=10 vmlinux O=../linux-build 'KCFLAGS=-g$x -gz=$y' LDFLAGS_vmlinux=--compress-debug-sections=$x
  • real is the amount of real time the build took
  • user is the amount of CPU time (in user space) the build took
  • builddir is the size of ../linux-build
  • vmlinux is the size of ../linux-build/vmlinux

Results:

flagsrealuserbuilddirvmlinux
-g04m46s34m52s346M30M
-g14m56s
(3% vs g0)
35m57s
( 3% vs g0)
444M
( 28% vs g0)
59M
( 97% vs g0)
-g25m52s
(23% vs g0)
40m19s
(16% vs g0)
1422M
( 311% vs g0)
275M
(817% vs g0)
-g38m24s
(76% vs g0)
45m54s
(32% vs g0)
4912M
(1320% vs g0)
314M
(947% vs g0)
-g0 -gz4m47s34m55s346M30M
-g1 -gz4m58s
( 4% vs g0)
( 1% vs no-gz)
35m59s
( 3% vs g0)
( 0% vs no-gz)
423M
( 22% vs g0)
( -5% vs no-gz)
40M
( 33% vs g0)
(-32% vs no-gz)
-g2 -gz6m11s
(29% vs g0)
( 5% vs no-gz)
41m13s
(18% vs g0)
( 2% vs no-gz)
1218M
(252% vs g0)
(-14% vs no-gz)
152M
(407% vs g0)
(-45% vs no-gz)
-g3 -gz8m54s
(86% vs g0)
( 6% vs no-gz)
48m51s
(39% vs g0)
( 6% vs no-gz)
3715M
(974% vs g0)
(-24% vs no-gz)
163M
(443% vs g0)
(-48% vs no-gz)
Chart of builddir sizes
Chart of builddir sizes

So compressing DWARF data doesn't increase build times too much (if at all), but can drastically reduce the amount of disk space used.

The difference between -g2 and -g3 is a lot larger than I expected, both in terms of time and size. This may be because the Linux kernel makes heavy use of macros.

So I think -g2 -gz will be my goto debug flags from now on :)