-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How would I build a shared object file? #27
Comments
Modifying the That said, all of TCMalloc's dependencies (in Abseil) will end up being dynamically linked in that case, so the performance overhead will be higher than necessary. |
@SamSaffron I had the same idea after seeing it on HackerNews yesterday. I was able to build a shared library using code in #16. Benchmarks were run on Linux 5.6.7, AMD 2700, compiled with gcc 9.3.0
|
How to link abseil in tcmalloc.so? |
Having a documented way to build a self-contained tcmalloc.so or tcmalloc.a will be very useful. I am playing with lld with different malloc implementations today (https://gist.github.com/MaskRay/219effe23a767b85059097f863ebc085). For many allocators using them is simply ld.lld @response.txt -o lld.glibc
ld.lld ~/Dev/mimalloc/out/release/libmimalloc.a @response.txt -o lld.mi
ld.lld ~/Dev/jemalloc/out/release/lib/libjemalloc.a @response.txt -o lld.je
ld.lld ~/Dev/snmalloc/out/release/libsnmallocshim-static.a @response.txt /usr/lib/x86_64-linux-gnu/libatomic.so.1 -o lld.sn It seems that if a project doesn't use Bazel, it's very difficult to plug tcmalloc into it. (Yeah I know llvm-project has an unofficial |
AFAIK, creating a shared library with Bazel requires a |
Following this instruction solves the problem. Briefly, adding a new
A new question: How can I access the headers outside of the tcmalloc project? |
this is an old benchmark... Is there more recent benchmark that compares the latest TCMalloc version performance against other allocators? I had the assumption that the latest version was at least as good as the gperf one and certainly better with all the new bells and whistles such as rseq, THP support and C++14 sized delete... but since I am having such a hard time playing with the new version, I need to make sure that the effort is worth the trouble... I am currently doubting after having spent a full day learning bazel and getting results not working well... I do not care if the allocation time is slightly slower if the use of THP means less TLB lookups, less page faults and makes the overall process better but those things have to be measured. With all the rich documentation that the TCMalloc team is providing, I am surprised that there is no performance numbers published anywhere... gperf TCMalloc had a lot of benchmarks numbers to compare it against pt2malloc... |
just copy tcmalloc directory under tcmalloc repo to /usr/local/include, or add -I point to tcmalloc repo path |
comment #linkstatic = 1, still works |
there is something not quite right... I am not very fond of bazel for being very opaque about what it is doing. This recipe to create a shared library will create one. With one small but critical omission. With the help of @fweimer-rh he made me discover that bazel was not including the elf soname field in the created shared lib. This omission will stop LD_PRELOAD to work. Unless you happen to also have the libtcmalloc.so within the LD_LIBRARY_PATH search paths. to find if you have the problem, you can use: if the output is empty, you have a halfbacked shared lib... maybe there is a way to specify bazel to include the soname but I don't know enough the tool to know how... |
Wl,soname may works |
cc_binary( this works |
I am going to give you the nickname of bazel master! thx a lot... I am determined to make tcmalloc work with my project but it may take few days... the other issue is the ODR violation if so lib user is also using Abseil that Chris Kennelly did report in some other issues... |
interesting info... I am definitely keeping a bookmark on this important bazel reference page I am not too sure what the is the value added of the cc_shared_library rule over cc_library or cc_binary with the needed flags value. Nonetheless, I gave cc_shared_library a try. you still need: to have the soname added in the binary. I am giving the target a different name. Bazel cannot have 2 targets with the same names. |
I am improving my custom bazel setup: tcmalloc/BUILD modif:
built with this cmdline: build is successful but I get ODR warnings:
I have created a pull request to address the problem: |
How to modify it if I want to access "libtcmalloc.a" ? |
A prerequisite to have a tcmalloc shared object file is to have abseil offer the same thing through bazel: now once that is being said, this path appears to be a difficult one to go through... I have come to consider 3 alternatives:
Concerning 2, this is a new idea/concept to me but this is apparently possible: This is such an out-of-the-norm idea that I will need to have a better understanding of how the loader works. I know that with LD_PRELOAD_LIBRARY=tcmalloc, its symbols will take precedence over libc ones... but how about if they are exported from the executable? I suppose the executable symbols will also take precedence over any other ones but I need to confirm this point. I have never done this before... My main motivation for making this succeed is that I want to make third parties libs used by my program all use tcmalloc, with OpenSSL being the main one. |
option 2 looks promising... I have replaced linking with abseil shared libraries with tcmalloc private abseil obj files and my other shared libraries using the abseil classes are now using symbols exported by the exec binary: (I had to modify WORKSPACE file to make tcmalloc use the latest abseil LTS release)
|
this is my final update. This is a first time for me. Yes, a program executable can export symbols for its shared libraries and it works. This conclusion totally removes the need for me to have tcmalloc in a shared library format.
BUILD file:
|
I am not sure.... tcmalloc/BUILD tcmalloc rule is already a cc_library rule yet it does not create a libtcmalloc.a file... when you include the cc_library tc_malloc into another binary rule, underneath, bazel will collect and link a bunch of object files from the tcmalloc dependencies and link them into the final target... If I needed to get what you want, I would look into the cc_library rule arguments at you are right... with a libtcmalloc.a, you could use it outside bazel... Otherwise building manually the dependency object files list is a pain... it is not easily made because AFAIK, the philosophy of bazel is to enable its users to use the master branch of all their project dependencies. So as soon as a commit is performed, it is recompiled and added immediately at the next bazel build... Creating a lib file goes against its philosophy... update: I assume that you did just follow the tcmalloc quickstart guide. when you do: because of the tcmalloc however if your target is tcmalloc itself, a lib file is going to be created... do it will create bazel-bin/tcmalloc/libtcmalloc.lo what is a .lo file? idk for sure....
I did a quick search... It is an archive containing PIC code... here is the best reference that I have found but it does not answer my question precisely... I guess that it can be used for linking as if it was a .a file... update: just be aware that linking against the libtcmalloc.a is tricky... You must somehow instruct the linker to use libtcmalloc.a malloc/free, new/delete from libtcmalloc.a file and not the ones provided by libc... I have not found how to do it... (or I even have not tried)... vs linking all the tcmalloc objects in, you leave no room for the linker to decide that... |
I remove 'alwayslink = 1' from the file tcmalloc/BUILD and execute command 'bazel build //tcmalloc:tcmalloc'. I get it! Thanks a lot! |
Trying to get my head around
bazel
is there a way of building atcmalloc.so
shared object file from this project?I know the recommendation is just to compile applications with tcmalloc directly but for my use case: https://github.com/SamSaffron/allocator_bench I would like to do a side by side comparison to perftools and jemalloc that are LD_PRELOADed.
Also not against experimenting with a statically compiled ruby including tcmalloc, if we can prove it is faster / better maybe Ruby folks would be open to adding it.
The text was updated successfully, but these errors were encountered: