Hi, I tried compiling dgraph from the source and I have the jemalloc dependency being built in /usr/local/lib. When I exec into the dgraph docker container (the official 24.0.0 image), the jemalloc library is missing, however, when I run the dgraph command it says “jemalloc: true”
I ran ldd on the binary and it doesn’t have jemalloc listed as a dependency. I’m not sure what I’m missing.
My use case is to swap the docker binary out with a debug symbol-enabled binary for debugging some issues. Right now I manually copied in the libraries for jemalloc but I am a bit nervous about it so I was wondering if anyone could help me shed light on this topic.
How are you building the image? Did you see the image-local target in the Makefile? That target builds a dgraph/dgraph:local image on your machine, which you can then use however you like.
graph
Dgraph SHA-256 : fde573b63113a7c7289da5e2bfd61423cb3f97bd7100416a9333402d01fe7f7e
Commit SHA-1 : 3486cf4
Commit timestamp : 2024-06-06 14:07:31 -0700
Branch : HEAD
Go version : go1.22.4
jemalloc enabled : true
To get around this, I was doing the manual build but I ran into the issue of missing libraries. I copied those in and it seems to work but I am puzzled about the solution and it’s brittle because I am not building the libraries with the same version of glibc
I guess I still don’t understand how the jemalloc linking is working, and why the docker hub image doesn’t have the jemalloc library files but still shows that it’s using it? I also looked through this:
to see how a release image was created but these logs appear to be missing so I can’t find a difference between the two…
Have a look at the jemalloc target in the dgraph/Makefile. You might try removing the @ sign and running make jemalloc to see what’s going on there. Also check out the variable HAS_JEMALLOC which is defined a couple dozen lines above.
Hey, Matthew, so I did see that and when I run make install I am able to get the jemalloc library to download and the output is put in /usr/local/lib the question is how dgraph is baking it into its own binary. I think this is happening by packaging the libjemalloc.a static library into the go binary dgraph but I don’t see where that is happening. It’s a bit of a side project for me so I will look at it more this weekend. Thanks for your pointers, maybe there’s some info somewhere about go and C static library linking that I will find.
And this is where we bake (or statically link) the jemalloc lib into the Dgraph binary by passing its path to the C linker via a CGO LDFLAGS directive:
$ go tool cgo | grep ldflags
..
-ldflags flags
Flags to pass to the C linker. The cmd/go tool uses this to pass in the flags in the CGO_LDFLAGS variable.
..
thanks, Rahul, this sounds like what I’ve been looking for! I still am not getting it to build the same way… Will post once I’ve figured that part out. THanks again for your help!
I tried hardcoding the LDFLAGS from ristretto after Rahul’s link(/usr/local/lib/libjemalloc.a -L/usr/local/lib -Wl,-rpath,/usr/local/lib -ljemalloc -lm -lstdc++ -pthread -ldl) to the Makefile here: dgraph/dgraph/Makefile at main · dgraph-io/dgraph · GitHub
also to no avail yet. Since I have been packaging my docker container with the libjemalloc libraries, I’ve been working around it for now.