Skip to content

Commit bd98ca2

Browse files
committed
topology: cmake: build topologies in parallel, add -s to force sequential
alsatplg custom commands were marked USES_TERMINAL. With the Ninja generator this places them in the single-job "console" pool, which serializes every topology build regardless of the -j value passed to ninja. Building all 579 topologies therefore never used more than one core, even on a many-core machine. Drop USES_TERMINAL by default so Ninja can run alsatplg invocations in parallel, controlled by a new TPLG_SEQUENTIAL_BUILD CMake option (default OFF). Add a -s flag to build-tools.sh that sets TPLG_SEQUENTIAL_BUILD=ON, to restore the old, easier to debug one-at-a-time behavior when needed. It updates the CMake cache in place, so it also works on an existing incremental build tree without deleting it. Results Sequential (before this patch or with -T -s with this patch): ./scripts/build-tools.sh -T -s 19.30s user 2.34s system 99% cpu 21.817 total Parallel: rm -rf tools/build_tools; time ./scripts/build-tools.sh -T ./scripts/build-tools.sh -T 33.79s user 3.52s system 986% cpu 3.783 total The build time changes from ~22s to ~4s Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
1 parent 73c674a commit bd98ca2

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎scripts/build-tools.sh‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Attention: the list of selected shortcuts below is _not_ exhaustive. To
1616
build _everything_ don't select any particular target; this will build
1717
CMake's default target "ALL".
1818
19-
usage: $0 [-c|-f|-h|-l|-p|-t|-T|-X|-Y|-A]
19+
usage: $0 [-c|-f|-h|-l|-p|-s|-t|-T|-X|-Y|-A]
2020
-h Display help
2121
2222
-c Rebuild ctl/
@@ -25,6 +25,8 @@ usage: $0 [-c|-f|-h|-l|-p|-t|-T|-X|-Y|-A]
2525
-T Rebuild topology/ (not topology/development/! Use ALL)
2626
-X Rebuild topology1 only
2727
-Y Rebuild topology2 only
28+
-s Force sequential (one at a time) topology builds, for debugging.
29+
Normally topologies build in parallel with -j "$NO_PROCESSORS".
2830
-t Rebuild test/topology/ (or tools/test/topology/tplg-build.sh directly)
2931
-A Clone and rebuild local ALSA lib and utils.
3032
@@ -41,10 +43,19 @@ reconfigure_build()
4143
mkdir -p "$BUILD_TOOLS_DIR"
4244

4345
( cd "$BUILD_TOOLS_DIR"
44-
cmake -GNinja -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" "${SOF_REPO}/tools"
46+
cmake -GNinja -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
47+
-DTPLG_SEQUENTIAL_BUILD="$TPLG_SEQUENTIAL_BUILD" "${SOF_REPO}/tools"
4548
)
4649
}
4750

51+
# Update the TPLG_SEQUENTIAL_BUILD CMake cache entry without wiping an
52+
# existing, already configured build tree.
53+
update_sequential_build_option()
54+
{
55+
cmake -S "${SOF_REPO}/tools" -B "$BUILD_TOOLS_DIR" \
56+
-DTPLG_SEQUENTIAL_BUILD="$TPLG_SEQUENTIAL_BUILD"
57+
}
58+
4859
make_tool()
4960
{
5061
# if no argument provided, all the tools will be built. Empty tool is
@@ -95,12 +106,13 @@ main()
95106
{
96107
local DO_BUILD_ctl DO_BUILD_logger DO_BUILD_probes \
97108
DO_BUILD_tests DO_BUILD_topologies1 DO_BUILD_topologies2 SCRIPT_DIR SOF_REPO \
98-
CMAKE_ONLY BUILD_ALL
109+
CMAKE_ONLY BUILD_ALL TPLG_SEQUENTIAL_BUILD
99110
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
100111
SOF_REPO=$(dirname "$SCRIPT_DIR")
101112
: "${BUILD_TOOLS_DIR:=$SOF_REPO/tools/build_tools}"
102113
: "${NO_PROCESSORS:=$(nproc)}"
103114
BUILD_ALL=false
115+
TPLG_SEQUENTIAL_BUILD=OFF
104116

105117
if [ $# -eq 0 ]; then
106118
BUILD_ALL=true
@@ -120,11 +132,12 @@ main()
120132

121133
# eval is a sometimes necessary evil
122134
# shellcheck disable=SC2034
123-
while getopts "cfhlptTCXYA" OPTION; do
135+
while getopts "cfhlpstTCXYA" OPTION; do
124136
case "$OPTION" in
125137
c) DO_BUILD_ctl=true ;;
126138
l) DO_BUILD_logger=true ;;
127139
p) DO_BUILD_probes=true ;;
140+
s) TPLG_SEQUENTIAL_BUILD=ON ;;
128141
t) DO_BUILD_tests=true ;;
129142
T) DO_BUILD_topologies1=true ; DO_BUILD_topologies2=true ;;
130143
X) DO_BUILD_topologies1=true ;;
@@ -153,6 +166,11 @@ main()
153166
reconfigure_build
154167
}
155168

169+
# -s must apply even to an already configured, incremental build tree.
170+
if [ "$TPLG_SEQUENTIAL_BUILD" = ON ]; then
171+
update_sequential_build_option
172+
fi
173+
156174
if "$BUILD_ALL"; then
157175
# default CMake targets
158176
make_tool # trust set -e

‎tools/topology/CMakeLists.txt‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ set(SOF_TOPOLOGY_BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
22
set(SOF_ALSA_TOOLS_DIR "${SOF_ROOT_SOURCE_DIRECTORY}/../tools/bin")
33
set(ALSATPLG_CMD "${SOF_ALSA_TOOLS_DIR}/alsatplg")
44

5+
# With the Ninja generator, USES_TERMINAL forces the single-job "console"
6+
# pool, serializing every topology build regardless of -j. Enable this
7+
# option to force that serialized, easier to debug behavior back on.
8+
option(TPLG_SEQUENTIAL_BUILD "Build topologies one at a time (for debugging)" OFF)
9+
if(TPLG_SEQUENTIAL_BUILD)
10+
set(TPLG_USES_TERMINAL USES_TERMINAL)
11+
else()
12+
set(TPLG_USES_TERMINAL "")
13+
endif()
14+
515
function(alsatplg_version OUT_STATUS OUT_VERSION)
616
execute_process(COMMAND ${ALSATPLG_CMD} --version
717
RESULT_VARIABLE status
@@ -69,7 +79,7 @@ macro(add_alsatplg_command)
6979
# the -o(utput) file.
7080
# See bug https://github.com/alsa-project/alsa-utils/issues/126
7181
COMMAND ${ALSATPLG_CMD} \$\${VERBOSE:+-v 1} -c ${ARGV0} -o ${ARGV1}
72-
USES_TERMINAL
82+
${TPLG_USES_TERMINAL}
7383
)
7484
endmacro()
7585

@@ -97,7 +107,7 @@ macro(add_alsatplg2_command conf_header conf_target input_name output_name inclu
97107
# -p to pre-process Topology2.0 conf file
98108
COMMAND ALSA_CONFIG_DIR=${CMAKE_SOURCE_DIR}/topology/topology2 ${ALSATPLG_CMD} \$\${VERBOSE:+-v 1}
99109
-I ${include_path} -D "'${defines}'" -p -c ${output_name}.conf -o ${output_name}.tplg
100-
USES_TERMINAL
110+
${TPLG_USES_TERMINAL}
101111
)
102112
endmacro()
103113

0 commit comments

Comments
 (0)