Skip to content

Commit 5a7fbda

Browse files
authored
Enable to collect code coverage of samples/simple (#2003)
1 parent 04616d3 commit 5a7fbda

8 files changed

Lines changed: 154 additions & 28 deletions

File tree

build-scripts/config_common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,6 @@ endif ()
348348
if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)
349349
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
350350
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
351+
add_definitions (-DCOLLECT_CODE_COVERAGE)
351352
message (" Collect code coverage enabled")
352353
endif ()

core/app-mgr/app-manager/module_wasm_app.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,14 @@ wasm_app_module_uninstall(request_t *msg)
993993

994994
app_manager_printf("Uninstall WASM app successful!\n");
995995

996+
#ifdef COLLECT_CODE_COVERAGE
997+
/* Exit app manager so as to collect code coverage data */
998+
if (!strcmp(m_name, "__exit_app_manager__")) {
999+
app_manager_printf("Exit app manager\n");
1000+
bh_queue_exit_loop_run(get_app_manager_queue());
1001+
}
1002+
#endif
1003+
9961004
#if VALGRIND_CHECK != 0
9971005
bh_queue_exit_loop_run(get_app_manager_queue());
9981006
#endif

samples/simple/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ message(STATUS "WAMR_BUILD_SDK_PROFILE=${WAMR_BUILD_SDK_PROFILE}")
1212
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
1313
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1414

15+
if ("$ENV{COLLECT_CODE_COVERAGE}" STREQUAL "1" OR COLLECT_CODE_COVERAGE EQUAL 1)
16+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
17+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
18+
endif ()
19+
1520
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
1621

1722
## use library and headers in the SDK

samples/simple/build.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ done
5656

5757

5858
if [ "$CLEAN" = "TRUE" ]; then
59-
rm -rf $CURR_DIR/cmake_build
59+
rm -rf $CURR_DIR/cmake-build
6060
fi
6161

6262

@@ -107,8 +107,8 @@ cd ${WAMR_DIR}/wamr-sdk
107107

108108
echo "#####################build simple project"
109109
cd ${CURR_DIR}
110-
mkdir -p cmake_build/$PROFILE
111-
cd cmake_build/$PROFILE
110+
mkdir -p cmake-build/$PROFILE
111+
cd cmake-build/$PROFILE
112112
cmake ../.. -DWAMR_BUILD_SDK_PROFILE=$PROFILE $CM_TOOLCHAIN $CM_BUILD_TYPE
113113
make
114114
if [ $? != 0 ];then

samples/simple/sample_test_run.py

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import time
1212
import traceback
1313

14+
WAMRC_CMD = "../../wamr-compiler/build/wamrc"
1415

1516
def start_server(cwd):
1617
"""
@@ -80,8 +81,31 @@ def main():
8081
"""
8182
parser = argparse.ArgumentParser(description="run the sample and examine outputs")
8283
parser.add_argument("working_directory", type=str)
84+
parser.add_argument("--aot", action='store_true', help="Test with AOT")
8385
args = parser.parse_args()
8486

87+
test_aot = False
88+
suffix = ".wasm"
89+
if not args.aot:
90+
print("Test with interpreter mode")
91+
else:
92+
print("Test with AOT mode")
93+
test_aot = True
94+
suffix = ".aot"
95+
wasm_files = [ "timer", "sensor", "connection",
96+
"event_publisher", "event_subscriber",
97+
"request_handler", "request_sender" ]
98+
work_dir = args.working_directory
99+
wasm_apps_dir = work_dir + "/wasm-apps"
100+
print("Compile wasm app into aot files")
101+
for wasm_file in wasm_files:
102+
CMD = []
103+
CMD.append(WAMRC_CMD)
104+
CMD.append("-o")
105+
CMD.append(wasm_apps_dir + "/" + wasm_file + ".aot")
106+
CMD.append(wasm_apps_dir + "/" + wasm_file + ".wasm")
107+
subprocess.check_call(CMD)
108+
85109
ret = 1
86110
app_server = None
87111
try:
@@ -90,35 +114,47 @@ def main():
90114
# wait for a second
91115
time.sleep(1)
92116

93-
print("--> Install timer.wasm...")
117+
print("--> Install timer" + suffix + "...")
94118
install_wasm_application(
95-
"timer", "./wasm-apps/timer.wasm", args.working_directory
119+
"timer", "./wasm-apps/timer" + suffix, args.working_directory
96120
)
97121

98-
print("--> Install event_publisher.wasm...")
122+
# wait for a second
123+
time.sleep(3)
124+
125+
print("--> Query all installed applications...")
126+
query_installed_application(args.working_directory)
127+
128+
print("--> Install event_publisher" + suffix + "...")
99129
install_wasm_application(
100130
"event_publisher",
101-
"./wasm-apps/event_publisher.wasm",
131+
"./wasm-apps/event_publisher" + suffix,
102132
args.working_directory,
103133
)
104134

105-
print("--> Install event_subscriber.wasm...")
135+
print("--> Install event_subscriber" + suffix + "...")
106136
install_wasm_application(
107137
"event_subscriber",
108-
"./wasm-apps/event_subscriber.wasm",
138+
"./wasm-apps/event_subscriber" + suffix,
109139
args.working_directory,
110140
)
111141

112-
print("--> Uninstall timer.wasm...")
142+
print("--> Query all installed applications...")
143+
query_installed_application(args.working_directory)
144+
145+
print("--> Uninstall timer" + suffix + "...")
113146
uninstall_wasm_application("timer", args.working_directory)
114147

115-
print("--> Uninstall event_publisher.wasm...")
148+
print("--> Query all installed applications...")
149+
query_installed_application(args.working_directory)
150+
151+
print("--> Uninstall event_publisher" + suffix + "...")
116152
uninstall_wasm_application(
117153
"event_publisher",
118154
args.working_directory,
119155
)
120156

121-
print("--> Uninstall event_subscriber.wasm...")
157+
print("--> Uninstall event_subscriber" + suffix + "...")
122158
uninstall_wasm_application(
123159
"event_subscriber",
124160
args.working_directory,
@@ -127,26 +163,55 @@ def main():
127163
print("--> Query all installed applications...")
128164
query_installed_application(args.working_directory)
129165

130-
print("--> Install request_handler.wasm...")
166+
print("--> Install request_handler" + suffix + "...")
131167
install_wasm_application(
132168
"request_handler",
133-
"./wasm-apps/request_handler.wasm",
169+
"./wasm-apps/request_handler" + suffix,
134170
args.working_directory,
135171
)
136172

137173
print("--> Query again...")
138174
query_installed_application(args.working_directory)
139175

140-
print("--> Install request_sender.wasm...")
176+
print("--> Install request_sender" + suffix + "...")
141177
install_wasm_application(
142178
"request_sender",
143-
"./wasm-apps/request_sender.wasm",
179+
"./wasm-apps/request_sender" + suffix,
144180
args.working_directory,
145181
)
146182

147183
print("--> Send GET to the Wasm application named request_handler...")
148184
send_get_to_wasm_application("request_handler", "/url1", args.working_directory)
149185

186+
print("--> Uninstall request_handler" + suffix + "...")
187+
uninstall_wasm_application(
188+
"request_handler",
189+
args.working_directory,
190+
)
191+
192+
print("--> Uninstall request_sender" + suffix + "...")
193+
uninstall_wasm_application(
194+
"request_sender",
195+
args.working_directory,
196+
)
197+
198+
# Install a wasm app named "__exit_app_manager__" just to make app manager exit
199+
# while the wasm app is uninstalled, so as to collect the code coverage data.
200+
# Only available when collecting code coverage is enabled.
201+
print("--> Install timer" + suffix + "...")
202+
install_wasm_application(
203+
"__exit_app_manager__", "./wasm-apps/timer" + suffix, args.working_directory
204+
)
205+
206+
print("--> Uninstall timer" + suffix + "...")
207+
uninstall_wasm_application(
208+
"__exit_app_manager__",
209+
args.working_directory,
210+
)
211+
212+
# wait for a second
213+
time.sleep(1)
214+
150215
print("--> All pass")
151216
ret = 0
152217
except AssertionError:

tests/wamr-test-suites/spec-test-script/collect_coverage.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ echo "Start to collect code coverage of ${SRC_COV_DIR} .."
2828
pushd ${SRC_COV_DIR} > /dev/null 2>&1
2929

3030
# collect all code coverage data
31-
lcov -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1
31+
lcov -q -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1
3232
# extract code coverage data of WAMR source files
33-
lcov -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \
33+
lcov -q -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \
3434
-rc lcov_branch_coverage=1 \
3535
"*/usr/*" "*/_deps/*" "*/deps/*" "*/tests/unit/*" \
3636
"*/llvm/include/*" "*/include/llvm/*" "*/samples/*" \
37-
"*/app-framework/*" "*/test-tools/*"
37+
"*/app-framework/*" "*/app-mgr/*" "*/test-tools/*" \
38+
"*/tests/standalone/*" "*/tests/*"
3839

3940
if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
4041
if [[ -s ${DST_COV_FILE} ]]; then
@@ -46,9 +47,11 @@ if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
4647
cp -a ${DST_COV_FILE} "${DST_COV_FILE}.orig"
4748
# replace the lcov file
4849
cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
50+
echo "Code coverage file ${DST_COV_FILE} was appended"
4951
else
5052
cp -a ${SRC_TEMP_COV_FILE} ${SRC_COV_FILE}
5153
cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
54+
echo "Code coverage file ${DST_COV_FILE} was generated"
5255
fi
5356

5457
# get ignored prefix path
@@ -59,7 +62,7 @@ if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
5962

6063
# generate html output for merged code coverage data
6164
rm -fr ${DST_COV_DIR}/wamr-lcov
62-
genhtml -t "WAMR Code Coverage" \
65+
genhtml -q -t "WAMR Code Coverage" \
6366
--rc lcov_branch_coverage=1 --prefix=${prefix_full_path} \
6467
-o ${DST_COV_DIR}/wamr-lcov \
6568
${DST_COV_FILE}
@@ -69,7 +72,6 @@ if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
6972
zip -r -q -o wamr-lcov.zip wamr-lcov
7073
rm -fr wamr-lcov
7174

72-
echo "Code coverage file ${DST_COV_FILE} was generated or appended"
7375
echo "Code coverage html ${DST_COV_DIR}/wamr-lcov.zip was generated"
7476
else
7577
echo "generate code coverage html failed"

tests/wamr-test-suites/test_wamr.sh

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,19 +574,64 @@ function malformed_test()
574574
./malformed_test.py --run ${IWASM_CMD} | tee ${REPORT_DIR}/malfomed_$1_test_report.txt
575575
}
576576

577+
function collect_standalone()
578+
{
579+
if [[ ${COLLECT_CODE_COVERAGE} == 1 ]]; then
580+
pushd ${WORK_DIR} > /dev/null 2>&1
581+
582+
CODE_COV_FILE=""
583+
if [[ -z "${CODE_COV_FILE}" ]]; then
584+
CODE_COV_FILE="${WORK_DIR}/wamr.lcov"
585+
else
586+
CODE_COV_FILE="${CODE_COV_FILE}"
587+
fi
588+
589+
STANDALONE_DIR=${WORK_DIR}/../../standalone
590+
591+
echo "Collect code coverage of standalone dump-call-stack"
592+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/dump-call-stack/build"
593+
echo "Collect code coverage of standalone dump-mem-profiling"
594+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/dump-mem-profiling/build"
595+
echo "Collect code coverage of standalone dump-perf-profiling"
596+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/dump-perf-profiling/build"
597+
if [[ $1 == "aot" ]]; then
598+
echo "Collect code coverage of standalone pad-test"
599+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/pad-test/build"
600+
fi
601+
echo "Collect code coverage of standalone test-invoke-native"
602+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/test-invoke-native/build"
603+
echo "Collect code coverage of standalone test-running-modes"
604+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/test-running-modes/build"
605+
echo "Collect code coverage of standalone test-running-modes/c-embed"
606+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/test-running-modes/c-embed/build"
607+
echo "Collect code coverage of standalone test-ts2"
608+
./collect_coverage.sh "${CODE_COV_FILE}" "${STANDALONE_DIR}/test-ts2/build"
609+
610+
popd > /dev/null 2>&1
611+
fi
612+
}
613+
577614
function standalone_test()
578615
{
616+
if [[ ${COLLECT_CODE_COVERAGE} == 1 ]]; then
617+
export COLLECT_CODE_COVERAGE=1
618+
fi
619+
579620
cd ${WORK_DIR}/../../standalone
580621

581622
args="--$1"
582623

583624
[[ ${SGX_OPT} == "--sgx" ]] && args="$args --sgx" || args="$args --no-sgx"
584625

585-
[[ ${ENABLE_MULTI_THREAD} == 1 ]] && args="$args --thread" && args="$args --no-thread"
626+
[[ ${ENABLE_MULTI_THREAD} == 1 ]] && args="$args --thread" || args="$args --no-thread"
586627

587-
[[ ${ENABLE_SIMD} == 1 ]] && args="$args --simd" && args="$args --no-simd"
628+
[[ ${ENABLE_SIMD} == 1 ]] && args="$args --simd" || args="$args --no-simd"
629+
630+
args="$args ${TARGET}"
588631

589632
./standalone.sh $args | tee ${REPORT_DIR}/standalone_$1_test_report.txt
633+
634+
collect_standalone "$1"
590635
}
591636

592637
function build_iwasm_with_cfg()
@@ -648,10 +693,10 @@ function collect_coverage()
648693
ln -sf ${WORK_DIR}/../spec-test-script/collect_coverage.sh ${WORK_DIR}
649694

650695
CODE_COV_FILE=""
651-
if [[ -z "${COV_FILE}" ]]; then
696+
if [[ -z "${CODE_COV_FILE}" ]]; then
652697
CODE_COV_FILE="${WORK_DIR}/wamr.lcov"
653698
else
654-
CODE_COV_FILE="${COV_FILE}"
699+
CODE_COV_FILE="${CODE_COV_FILE}"
655700
fi
656701

657702
pushd ${WORK_DIR} > /dev/null 2>&1

wamr-sdk/build_sdk.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ echo -e "\n\n"
224224

225225
echo "############## Start to build runtime sdk ###############"
226226
cd ${sdk_root}/runtime
227-
rm -fr build_runtime_sdk && mkdir build_runtime_sdk
228-
cd build_runtime_sdk
227+
rm -fr build-runtime-sdk && mkdir build-runtime-sdk
228+
cd build-runtime-sdk
229229
cmake .. $CM_DEXTRA_SDK_INCLUDE_PATH \
230230
-DWAMR_BUILD_SDK_PROFILE=${PROFILE} \
231231
-DCONFIG_PATH=${wamr_config_cmake_file} \
@@ -249,6 +249,6 @@ fi
249249

250250

251251
cd ..
252-
rm -fr build_runtime_sdk
252+
rm -fr build-runtime-sdk
253253

254254
exit 0

0 commit comments

Comments
 (0)