Skip to content

Commit

Permalink
[build] Add option to link to tcmalloc, script to install it (#4564)
Browse files Browse the repository at this point in the history
Add a configure option to link to tcmalloc_minimal.so, and
a script in tools. to download and build tcmalloc.
  • Loading branch information
LvHang authored Sep 8, 2021
1 parent 0f0f362 commit 47ea7ae
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/configure
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Configuration options:
--android-incdir=DIR Android include directory
--enable-kenlm enable kenlm runtime library installed in "../tools/kenlm"
via extras/install_kenlm_query_only.sh, [default=no]
--enable-tcmalloc enable tcmalloc library installed in "../tools/gperftools"
via extras/install_tcmalloc.sh, [default=no]
Following environment variables can be used to override the default toolchain.
CXX C++ compiler [default=g++]
Expand Down Expand Up @@ -609,6 +611,7 @@ static_math=false
static_speex=false
android=false
enable_kenlm=false
enable_tcmalloc=false

FSTROOT=`rel2abs ../tools/openfst`
CUBROOT=`rel2abs ../tools/cub`
Expand Down Expand Up @@ -755,6 +758,9 @@ do
--enable-kenlm)
enable_kenlm=true
shift ;;
--enable-tcmalloc)
enable_tcmalloc=true
shift ;;
*) echo "Unknown argument: $1, exiting"; usage; exit 1 ;;
esac
done
Expand Down Expand Up @@ -1296,6 +1302,22 @@ else
appropriate configuration for this platform. Please contact the developers."
fi
if $enable_tcmalloc ; then
if [ -f ../tools/gperftools/lib/libtcmalloc_minimal.so ]; then
TCMALLOC_ROOT=$(rel2abs ../tools/gperftools)
echo "Configuring tcmalloc since --enable-tcmalloc"
if [ "$(uname)" == "Linux" ]; then
echo "LDLIBS += -Wl,-rpath=$TCMALLOC_ROOT/lib -ltcmalloc_minimal" >> kaldi.mk
else
failure "Now, --enable-tcmalloc option is only supported for Linux"
fi
echo "Successfully configured tcmalloc for Linux from $TCMALLOC_ROOT"
else
failure "Can't find tcmalloc in tools/gperftools. Run" \
"'extras/install_tcmalloc.sh' in 'tools/' to install."
fi
fi
# Append the flags set by environment variables last so they can be used
# to override the automatically generated configuration.
echo >> kaldi.mk
Expand Down
1 change: 1 addition & 0 deletions tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.tar.gz
*.tgz
*.zip
*.bak

env.sh

Expand Down
52 changes: 52 additions & 0 deletions tools/extras/install_tcmalloc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
#
# Copyright 2021 Hang Lyu
# 2021 kkm
#
# This script attempts to install tcmalloc.
# The tcmalloc provides the more efficient way to malloc so that it can speed
# up the code, especially for the decoding code which contains massive memory
# allocation operations.
#
# At the same time, the tcmalloc also provides some profilers which can help
# the user to analysis the performance of the code.
# However, there may have some deadlock problems when you use the profilers with
# default build-in glibc. So the libunwind is recommanded to be installed. When
# the deadlock problems happen, the user can try to link with the library
# libunwind. But there may still be some crash on x64 platform. (Link with
# "-lunwind" after your installation if you want to include it.)
# As very rare end users will need it and we believe the users who need it must
# be qualified to reconfigure and build as much of toolset as they want, we skip
# the installation around libunwind.
#
# Depending on different platforms which are used by different users, the users
# also can try differnet malloc libraries such as tbbmalloc, ptmalloc and so on.
# From our test, the tcmalloc is most efficient on our platform.

set -e

# Make sure we are in the tools/ directory.
if [ $(basename $PWD) == extras ]; then
cd ..
fi

! [ $(basename $PWD) == tools ] && \
echo "You must call this script from the tools/ directory" && exit 1;

# prepare tcmalloc
if [ -d gperftools ]; then
echo "$0: existing 'gperftools' subdirectory is renamed 'gperftools.bak'"
mv -f gpreftools gpreftools.bak
fi

wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.9.1/gperftools-2.9.1.tar.gz &&
tar xzf gperftools-2.9.1.tar.gz &&
mv gperftools-2.9.1 gperftools

# install tcmalloc
(
cd gperftools &&
./configure --prefix=$PWD --enable-minimal --disable-debugalloc --disable-static &&
make &&
make install
)

0 comments on commit 47ea7ae

Please sign in to comment.