Skip to content

runtime/pprof: missing profile info for CGo code when compiled with CC="zig cc" #59466

@motiejus

Description

@motiejus

TLDR: when profiling CGo programs compiled with zig cc, we are missing the profiles of pure C functions as observed with go tool pprof.

I work with the C++ toolchain at Uber and we recently switched to using zig cc to compile our CGo programs. I have not yet heard complains internally of
missing CGo profiles, but I suspect it's a matter of time. :)

Also, this seems to be the last suite of tests that fail when running CC="zig cc" ./all.bash on Linux amd64.

Go version: master (8d68b38)
Zig version: 0.10.1 and master (ziglang/zig@8853005)

Steps to reproduce

Compile testprogcgo with zig cc and clang-15:

$ cd src/runtime/testdata/testprogcgo
$ CC="/code/zig-linux-x86_64-0.10.1/zig cc" ../../../../bin/go build -o testprogcgo-zig-0.10.1
$ CC=clang-15 ../../../../bin/go build -o testprogcgo-clang-15

Run both resulting binaries:

$ ./testprogcgo-zig-0.10.1 CgoPprof
/tmp/prof1336230719
$ ./testprogcgo-clang-15 CgoPprof
/tmp/prof2489818717

Compare the profiles:

$ go tool pprof -traces ./testprogcgo-clang-15 /tmp/prof2489818717
File: testprogcgo-clang-15
Build ID: 57e147c5c3713dc48115b4856c5c9a568154a3cc
Type: cpu
Time: Apr 6, 2023 at 2:58pm (EEST)
Duration: 1.10s, Total samples = 1s (90.71%)
-----------+-------------------------------------------------------
	 990ms   cpuHog
			 cpuHog2
			 runtime.cgocall
			 main._Cfunc_cpuHog
			 main.CgoPprof
			 main.main
			 runtime.main
-----------+-------------------------------------------------------
	  10ms   runtime.futex
			 runtime.futexwakeup
			 runtime.notewakeup
			 runtime.exitsyscallfast_pidle
			 runtime.exitsyscallfast.func1
			 runtime.systemstack
			 runtime.exitsyscallfast
			 runtime.exitsyscall
			 runtime.cgocall
			 main._Cfunc_cpuHog
			 main.CgoPprof
			 main.main
			 runtime.main
-----------+-------------------------------------------------------
$ go tool pprof -traces ./testprogcgo-zig-0.10.1 /tmp/prof1336230719
File: testprogcgo-zig-0.10.1
Type: cpu
Time: Apr 6, 2023 at 2:56pm (EEST)
Duration: 1.10s, Total samples = 1s (90.70%)
-----------+-------------------------------------------------------
	 990ms   pprofCgoTraceback
			 pprofCgoTraceback
			 runtime.cgocallbackg
			 main._Cfunc_foo2
			 main.CgoPprofCallback
			 main._Cfunc_CheckM
			 runtime.forcegchelper
-----------+-------------------------------------------------------
	  10ms   runtime.malg
			 runtime.exitsyscall0
			 runtime.unspillArgs
			 runtime.exitsyscallfast_pidle
			 syscall.runtime_BeforeFork
			 runtime.cgocallbackg1
			 main._Cfunc_foo2
			 main.CgoPprofCallback
			 main._Cfunc_CheckM
			 runtime.forcegchelper
-----------+-------------------------------------------------------

Note that cpuHog and cpuHog2 are not present in the profile of the program that was compiled with zig cc. This is what the unit test TestCgoPprof asserts.

Background / steps taken

I have observed and compared the resulting compiler and linker flags from both builds did not observe anything standing out.

I would appreciate some help debugging this. I am somewhat comfortable looking at disassembly and the low-level compiler flags, but, in this case, I don't know where to look.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions