forked from Cambricon/mlu-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.sh
executable file
·162 lines (147 loc) · 5.25 KB
/
coverage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
set -e
readonly RED='\033[1;31m'
readonly NC='\033[0m'
readonly CARD_NUM_DEFAULT=8
DEVICE_ID=0
test_cmd_=''
temp_dir_='.'
extra_test_dir_=''
lib_path_="../lib/libmluops.so"
function usage () {
echo
echo "Coverage is a code coverage testing tool of bangC for mlu-ops."
echo "Usage: ./coverage.sh COMMAND [options]"
echo "COMMAND: gtest command."
echo "[options]: "
echo " -t [optional]: temporary dir to store raw files. optional, default is current dir."
echo " -e [optional]: extra gtest dir, optional, meaning that you can run other gtest."
echo " -l [optional]: path of libmluops.so, optional, default is ../lib/libmluops.so."
echo "**********************example*********************"
echo "./coverage.sh \"./mluops_gtest --gtest_filter=*add*\""
echo "**************************************************"
}
# Handle options
function parse_args () {
if [ $# != 0 ]; then
test_cmd_=$1
shift
# options
while [ $# != 0 ]; do
case "$1" in
-e | --extra)
extra_test_dir_=$2
echo "specify extra_test_dir_ = ${extra_test_dir_}."
shift 2
;;
-h | --help)
usage
exit 0
;;
-l | --library)
lib_path_=$2
echo "specify lib_path_ = ${lib_path_}."
shift 2
;;
-t | --temporary)
temp_dir_=$2
echo "specify temp_dir = ${temp_dir_}."
shift 2
;;
*)
printf "${RED} ERROR: Unknown options ${1}, use -h or --help\n${NC}"
usage
exit -1
;;
esac
done
fi
}
function run_extra_test () {
printf "============= Coverage: run extra test ==============\n"
export LLVM_PROFILE_FILE=${temp_dir_}/output/host_extra.profraw
readonly new_case="--cases_dir=${extra_test_dir_}"
for arg in ${test_cmd_}; do
if [[ ${arg} == "--cases_dir="* || ${arg} == "--case_path="* || ${arg} == "--cases_list="* ]]; then
readonly old_case=${arg}
fi
done
if [[ -v old_case ]]; then
test_cmd_=${test_cmd_/${old_case}/${new_case}}
else
test_cmd_="${test_cmd_} ${new_case}"
fi
${test_cmd_}
}
function process () {
# run gtest
readonly mluops_dir=$(dirname ${lib_path_})
export LD_LIBRARY_PATH="${mluops_dir}":$LD_LIBRARY_PATH
export CNRT_DUMP_PGO=1 # Will be removed
export CN_DUMP_PGO=1
mkdir -p ${temp_dir_}
export CNRT_PGO_OUTPUT_DIR=${temp_dir_}/output # Will be removed
export CN_PGO_OUTPUT_DIR=${temp_dir_}/output
export LLVM_PROFILE_FILE=${temp_dir_}/output/host.profraw
${test_cmd_}
if [[ ! -z ${extra_test_dir_} ]]; then
run_extra_test
fi
# merge to a single indexed profile data file
mkdir -p ${temp_dir_}/profdata
llvm-profdata merge ${temp_dir_}/output/host*.profraw -o ${temp_dir_}/profdata/host.profdata
# export coverage data of the binaries
mkdir -p ${temp_dir_}/info
llvm-cov export ${lib_path_} -instr-profile=${temp_dir_}/profdata/host.profdata -host-only -format=lcov \
> ${temp_dir_}/info/host_info
# device info
find ${temp_dir_}/output/ -maxdepth 1 -type f -name "CNPGO*" > ${temp_dir_}/profdata/profile_list.txt
llvm-profdata merge -f ${temp_dir_}/profdata/profile_list.txt -o ${temp_dir_}/profdata/device.profdata
local arch=$(awk -F '_' '{print $(NF-3)}' ${temp_dir_}/profdata/profile_list.txt | sort | uniq)
local kernels=$(awk -F '_' '{print $(NF-2)}' ${temp_dir_}/profdata/profile_list.txt | sort | uniq)
for k in ${kernels}
do
llvm-cov export ${lib_path_} -instr-profile=${temp_dir_}/profdata/device.profdata -kernel-hash-id=${k} \
-bang-mlu-arch=${arch} -format=lcov > "${temp_dir_}/info/device_info_${k}"
done
# generate report
genhtml ${temp_dir_}/info/* -o result
for arg in ${test_cmd_}; do
if [[ ${arg} == "--gtest_filter="* ]]; then
op_dir_name=${arg##*=}
fi
done
# show the coverage test report
if [[ ! -z ${op_dir_name} ]]; then
html2text ${temp_dir_}/result/kernels/${op_dir_name}/index.html
else
html2text ${temp_dir_}/result/index.html
fi
}
function main () {
if [[ -v NEUWARE_HOME ]]; then
echo " NEUWARE_HOME: "${NEUWARE_HOME}
export PATH="${NEUWARE_HOME}/bin":$PATH
export LD_LIBRARY_PATH="${NEUWARE_HOME}/lib64":$LD_LIBRARY_PATH
export MLUOP_GTEST_FILL_RAM=OFF
else
printf "${RED} ERROR: please export NEUWARE_HOME variable first!\n${NC}"
exit 1
fi
if [[ "$#" -lt 1 ]]; then
printf "${RED} ERROR: arguments too few !\n${NC}"
usage && exit 1
fi
if [[ "$(which genhtml)" == "" ]]; then
echo "--Coverage test failed, please install genhtml"
exit 0
fi
if [[ "$(which html2text)" == "" ]]; then
echo "--Coverage test failed, please install html2text."
exit 0
fi
parse_args "$@"
export MLU_VISIBLE_DEVICES=${DEVICE_ID}
process
}
main "$@"