Huge binary size of core binaries (mongod, mongo)

Hi,

I am trying to build the core binaries from the mongo 4.4.12 source code using the below command on the x86_64 arch.

python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors

I am able to build the binaries successfully but the binary size is very large. (in gigs)

du -sh mongod
3.3G    mongod

du -sh mongo
1.3G    mongo

However, the prebuilt binaries on https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-4.4.12.tgz has size of around 50-80 MB

Can somebody help me to solve this problem?
Thanks!

By default, our scons script will include all debug symbols when building MongoDB. Our packaging script strips them out for the prebuilt binaries by using “objcopy --strip-debug”.

3 Likes

Hi @Eric_Milkie Thanks for your response.
I tried to build mongo version 4.4.12 using some debug options, but it is failing with errors.

Command 1:
python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --strip-debug

ERROR

scons: done reading SConscript files.
usage: scons [OPTION] [TARGET] ...

SCons Error: no such option: --strip-debug
The command '/bin/sh -c python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --strip-debug' returned a non-zero code: 2

Command2:
python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on --spider-monkey-dbg=on --install-mode=hygienic

Error

src/third_party/mozjs-60/extract/js/src/jit/VMFunctions.h:310:42: error: 'const struct js::jit::VMFunction' has no member named 'name_'
  310 |         MOZ_ASSERT(strcmp(f1->name_, f2->name_) == 0);
      |                                          ^~~~~
----

src/mongo/db/matcher/expression_type.h:74:30: note: remove 'std::move' call
scons: *** [build/opt/third_party/mozjs-60/platform/s390x/linux/build/Unified_cpp_js_src40.o] Error 1
scons: building terminated because of errors.
build/opt/third_party/mozjs-60/platform/s390x/linux/build/Unified_cpp_js_src40.o failed: Error 1
The command '/bin/sh -c python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on --spider-monkey-dbg=on --install-mode=hygienic' returned a non-zero code: 2

scons does not have a --strip-debug parameter. I suggest using the objcopy command to strip the binaries after building them, by passing the --strip-debug flag to it:
objcopy --strip-debug <the mongod binary>

1 Like

Hi @Rahul_Arora1 -

For MongoDB v4.4 and later, you can use the --separate-debug flag to automatically invoke objcopy to separate the debug symbols at build time. When you use this flag, new targets are enabled: along with the install-core target there will be an install-core-debug target which will install the separated debug files.

So I think your Command2 should more or less work. I think the compilation error you received with that command is due to the --spider-monkey-dbg=on flag, which you should not use. You also don’t need the --install-mode=hygienic flag since it is the default in v4.4

So, my recommended command line for you would be something like:

python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on

Note that that command won’t install the debug files. It is often a good idea to keep them around though. So, you might want to run:

python3 buildscripts/scons.py PREFIX=/opt/IBM/mongo-distro archive-core archive-core-debug --disable-warnings-as-errors --separate-debug=on

That will create archive files containing the runtime and debug symbols, respectively, which are packaged with the path /opt/IBM/mongo-distro (note the change from DESTDIR to PREFIX).

Thanks,
Andrew

2 Likes

Hi @Andrew_Morrow Thanks for the detailed explanation.
I was able to use --separate-debug alone for the version 4.4.12 and it reduced the size of binaries as well.
command for 4.4.12

python3 buildscripts/scons.py DESTDIR=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on

I have one more requirement to build the 4.2.18 version as well. So I tried to use the same command for that, but it did not work…

error:

Cannot use --separate-debug without --install-mode=hygienic

So I tried to use the command as below:

python3 buildscripts/scons.py --prefix=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on --install-mode=hygienic

But this procedure did not reduce the size of the binary. I tried to debug and found that for 4.2.18 version, I must enable the SpiderMonkey flag as well.

python3 buildscripts/scons.py --help | grep debug

--separate-debug=[on|off]   Produce separate debug files (only effective in
                              Enable SpiderMonkey debug mode

Final Command for 4.2.18:

python3 buildscripts/scons.py --prefix=/opt/IBM/mongo-distro install-core --disable-warnings-as-errors --separate-debug=on --install-mode=hygienic --spider-monkey-dbg=on

Error:

src/third_party/mozjs-60/extract/js/src/jit/VMFunctions.h:310:42: error: 'const struct js::jit::VMFunction' has no member named 'name_'
  310 |         MOZ_ASSERT(strcmp(f1->name_, f2->name_) == 0);
      |                                          ^~~~~


scons: building terminated because of errors.
build/opt/third_party/mozjs-60/extract/js/src/frontend/Parser.o failed: Error 1

Any idea on this?
Thanks!

@Rahul_Arora1 -

The --separate-debug and --install-mode=hygienic features should not be used with v4.2. They are only fully supported on v4.4 and later. For your v4.2 build, you will need to manually use objcopy to separate (or strip to strip) the debug information.

Thanks,
Andrew

1 Like

Hi @Andrew_Morrow @Eric_Milkie Thanks for sharing all the pointers.

I was able to successfully reduce the size of the binaries.
Thanks!

1 Like

@Rahul_Arora1 - That’s great, I’m happy to hear we were able to get it working.

One subtle thing that I did want to point out. You are using DESTDIR but not PREFIX. By using DESTDIR like that, you are basically requiring that your /opt/IBM/mongo-distro path be writeable by the build. That may not always be what you want. Instead, you should be able to do:

scons ... PREFIX=/opt/IBM/mongo-distro 

That will create a tree within the mongodb source clone like build/install/opt/IBM/mongo-distro, without needing write permissions to the real /opt/IBM/mongo-distro, and ensure that targets like archive-core build archives that contain the prefix path opt/IBM/mongo-distro so that if you unpack them in / (presumably with sudo) they populate the directory you want.

So, that may be something to play with if you are interested.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.