I wonder some tools to measure time cost of each function called in cgo.
my go version is 1.16.6 linux/amd64
As I know, the process of C calling Go func will use the function in _obj/_cgo_export.c
. First, _cgo_wait_runtime_init_done
will be called. Then, crosscall2
will be called.
The process above is in C side.
After crosscall2
, we will enter asm side, using cgocallback
in src/runtime/asm_amd64.s
After cgocallback
is cgocallbackg
in Go side. Then, cgocallbackg
, cgocallbackg1
. Finally, by cb(frame)
(src/runtime/cgocall.go), we enter the real go func.
So
My question is how can I measure the time cost in each function of the whole process, since the process is split into two different language.
I have heard of pprof
, but it can’t give situation of time consuming in C side.
I have used clock()
(time.h
) to calculate the time spent in the whole process of C using Go func, but still I don’t know the situation in Go side.
I have tried
//caller.c
entern int sum(int a, int b);// go func
void caller(void)
{
start = clock();
{
result = sum(1, 2); // C calls go func
}
end = clock();
time = end - start;
}
//callee.go
import "C"
//export sum
func sum(a, b C.int) C.int {
return a + b
}
I use
go build -o sum.so -buildmode=c-archive callee.go
and thengcc -g -o caller.c sum.so -lpthread
to get the executable file.go tool cgo callee.go
to get_obj
files for the convenience of gdb debug
but certainly, the measurement is not fine enough.
I have one idea:
modify the _cgo_export.c
in _obj
directory(i.e. add start,end
in each important function) and recompile.
But actually, I don’t know how to do it.
So, could someone recommend some tools to measure the timing of cgo calling or provide some help on how to realise my daydreaming(maybe )? Any guidance is appreciated