ANDROID: Specify --dist-dir to create-tracefile.py

create-tracefile.py finds gcno mapping files in the specified
--dist-dir. By default, it looks for out/**/dist/gcno_mapping.*.json.

kunit.sh and kselftest.sh users may specify --dist-dir that does not
match the default pattern. The shell scripts should pass --dist-dir to
create-tracefile.py.

Bug: 358439710
Test: common/tools/testing/android/bin/kselftest.sh \
      --gcov --dist-dir=out/testcov
Change-Id: I81a430078676d983d1b7d44b9129246e06dce135
Signed-off-by: Hsin-Yi Chen <hsinyichen@google.com>
This commit is contained in:
Hsin-Yi Chen
2024-08-29 17:59:16 +08:00
parent 49bf3b3e80
commit 4c56ebdd90
3 changed files with 67 additions and 47 deletions
+41 -37
View File
@@ -233,7 +233,7 @@ def append_slash(path: str) -> str:
def update_multimap_from_json(
json_file: str, base_dir: str, result_multimap: {}
json_file: str, base_dir: str, result_multimap: collections.defaultdict
) -> None:
"""Reads 'to' and 'from' fields from a JSON file and updates a multimap.
@@ -264,7 +264,6 @@ def update_multimap_from_json(
Returns:
The updated dictionary.
"""
with open(json_file, "r") as file:
data = json.load(file)
@@ -274,43 +273,38 @@ def update_multimap_from_json(
if to_value and from_value:
to_value = make_absolute(to_value, base_dir)
from_value = make_absolute(from_value, base_dir)
if from_value not in result_multimap:
result_multimap[from_value] = []
result_multimap[from_value].append(to_value)
def read_gcno_mapping_files(search_dir: str, base_dir: str) -> {}:
pattern = os.path.join(search_dir, "**", "dist", "gcno_mapping.*")
result_multimap = {}
def read_gcno_mapping_files(
search_dir_pattern: str,
base_dir: str,
result_multimap: collections.defaultdict
) -> None:
"""Search a directory for gcno_mapping."""
found = False
pattern = os.path.join(search_dir_pattern, "gcno_mapping.*.json")
for filepath in glob.iglob(pattern, recursive=False):
found = True
logging.info("Reading %s", filepath)
update_multimap_from_json(filepath, base_dir, result_multimap)
return result_multimap
if not found:
logging.error("No gcno_mapping in %s", search_dir_pattern)
def read_gcno_dir(gcno_dir: str,
result_multimap: collections.defaultdict) -> None:
def read_gcno_dir(
gcno_dir: str, result_multimap: collections.defaultdict
) -> None:
"""Read a directory containing gcno_mapping and gcno files."""
pattern = os.path.join(gcno_dir, "gcno_mapping.*.json")
multimap = {}
for filepath in glob.iglob(pattern):
logging.info("Reading %s", filepath)
update_multimap_from_json(filepath, gcno_dir, multimap)
if not multimap:
logging.error("No gcno_mapping in %s", gcno_dir)
multimap = collections.defaultdict(list)
read_gcno_mapping_files(gcno_dir, gcno_dir, multimap)
to_value = append_slash(os.path.abspath(gcno_dir))
for from_value in multimap:
result_multimap[from_value].append(to_value)
def read_gcno_dirs(gcno_dirs: list) -> collections.defaultdict:
result_multimap = collections.defaultdict(list)
for gcno_dir in gcno_dirs:
read_gcno_dir(gcno_dir, result_multimap)
return result_multimap
def get_testname_from_filename(file_path: str) -> str:
filename = os.path.basename(file_path)
if "_kernel_coverage" in filename:
@@ -463,6 +457,14 @@ def main() -> None:
required=False,
help="Root directory of kernel source"
)
arg_parser.add_argument(
"--dist-dir",
dest="dist_dirs",
action="append",
default=[],
required=False,
help="Dist directory containing gcno mapping files"
)
arg_parser.add_argument(
"--gcno-dir",
dest="gcno_dirs",
@@ -517,25 +519,27 @@ def main() -> None:
logging.error("%s is not a file.", args.llvm_cov)
sys.exit(-1)
for gcno_dir in args.gcno_dirs:
for gcno_dir in args.gcno_dirs + args.dist_dirs:
if not os.path.isdir(gcno_dir):
logging.error("%s is not a directory.", gcno_dir)
sys.exit(-1)
config = Config(args.repo_dir, args.llvm_cov, args.tmp_dir)
if args.gcno_dirs:
gcno_mappings = read_gcno_dirs(args.gcno_dirs)
if not gcno_mappings:
logging.error("No gcno mapping files in %s", " ".join(args.gcno_dirs))
sys.exit(-1)
else:
gcno_mappings = read_gcno_mapping_files(
config.repo_out_dir, config.repo_dir
)
if not gcno_mappings:
logging.error("No gcno mapping files in %s", config.repo_out_dir)
sys.exit(-1)
gcno_mappings = collections.defaultdict(list)
if not args.gcno_dirs and not args.dist_dirs:
dist_dir_pattern = os.path.join(config.repo_out_dir, "**", "dist")
read_gcno_mapping_files(dist_dir_pattern, config.repo_dir, gcno_mappings)
for dist_dir in args.dist_dirs:
read_gcno_mapping_files(dist_dir, config.repo_dir, gcno_mappings)
for gcno_dir in args.gcno_dirs:
read_gcno_dir(gcno_dir, gcno_mappings)
if not gcno_mappings:
# read_gcno_mapping_files prints the error messages
sys.exit(-1)
tar_file = find_most_recent_tarfile(
args.tar_location, pattern="*kernel_coverage_*.tar.gz"
+13 -5
View File
@@ -5,6 +5,7 @@ BAZEL=tools/bazel
BIN_DIR=common/tools/testing/android/bin
ACLOUD=$BIN_DIR/acloudb.sh
TRADEFED=prebuilts/tradefed/filegroups/tradefed/tradefed.sh
DEFAULT_DIST_DIR=out/virtual_device_x86_64/dist
TESTSDIR=/tmp/kselftests
LOG_DIR=$PWD/out/test_logs/$(date +%Y%m%d_%H%M%S)
JDK_PATH=prebuilts/jdk/jdk11/linux-x86
@@ -21,7 +22,7 @@ print_help() {
echo " --skip-kernel-build Skip the kernel building step"
echo " --skip-cvd-launch Skip the CVD launch step"
echo " --skip-cvd-kill Do not kill CVD launched by running this script"
echo " -d, --dist-dir=DIR The kernel dist dir (default is /tmp/kernel_dist)"
echo " -d, --dist-dir=DIR The kernel dist dir (default is $DEFAULT_DIST_DIR)"
echo " -s, --serial=SERIAL The device serial number."
echo " If serial is specified, virtual device launch will be skipped"
echo " -t, --test=TEST_NAME The test target name. Can be repeated"
@@ -40,12 +41,13 @@ print_help() {
BUILD_KERNEL=true
LAUNCH_CVD=true
KILL_CVD=true
DIST_DIR=/tmp/kernel_dist
DIST_DIR=$DEFAULT_DIST_DIR
SERIAL_NUMBER=
MODULE_NAME="selftests"
TEST_FILTERS=
SELECTED_TESTS=
GCOV=false
GCOV_DIST_DIR=
while test $# -gt 0; do
case "$1" in
@@ -68,6 +70,7 @@ while test $# -gt 0; do
shift
if test $# -gt 0; then
DIST_DIR=$1
GCOV_DIST_DIR=$DIST_DIR
else
echo "kernel distribution directory is not specified"
exit 1
@@ -76,6 +79,7 @@ while test $# -gt 0; do
;;
--dist-dir*)
DIST_DIR=$(echo $1 | sed -e "s/^[^=]*=//g")
GCOV_DIST_DIR=$DIST_DIR
shift
;;
-s)
@@ -135,7 +139,7 @@ if $BUILD_KERNEL; then
echo "Building kernel..."
# TODO: add support to build kernel for physical device
$BAZEL run $BUILD_FLAGS //common-modules/virtual-device:virtual_device_x86_64_dist -- \
--dist_dir=$DIST_DIR
--dist_dir=$DIST_DIR
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Build kernel succeeded"
@@ -213,7 +217,11 @@ if $LAUNCH_CVD && $KILL_CVD; then
fi
if $GCOV; then
CREATE_TRACEFILE_CLI="common/tools/testing/android/bin/create-tracefile.py \
-t $LOG_DIR -o $LOG_DIR/cov.info"
if [ -n "$GCOV_DIST_DIR" ]; then
CREATE_TRACEFILE_CLI+=" --dist-dir $GCOV_DIST_DIR"
fi
echo "Creating tracefile ..."
common/tools/testing/android/bin/create-tracefile.py -t $LOG_DIR -o $LOG_DIR/cov.info && \
echo "Created tracefile at $LOG_DIR/cov.info"
$CREATE_TRACEFILE_CLI && echo "Created tracefile at $LOG_DIR/cov.info"
fi
+13 -5
View File
@@ -5,7 +5,8 @@ BAZEL=tools/bazel
BIN_DIR=common/tools/testing/android/bin
ACLOUD=$BIN_DIR/acloudb.sh
TRADEFED=prebuilts/tradefed/filegroups/tradefed/tradefed.sh
TESTSDIR=$PWD/out/tests
DEFAULT_DIST_DIR=out/virtual_device_x86_64/dist
TESTSDIR=/tmp/kunit
LOG_DIR=$PWD/out/test_logs/$(date +%Y%m%d_%H%M%S)
JDK_PATH=prebuilts/jdk/jdk11/linux-x86
@@ -21,7 +22,7 @@ print_help() {
echo " --skip-kernel-build Skip the kernel building step"
echo " --skip-cvd-launch Skip the CVD launch step"
echo " --skip-cvd-kill Do not kill CVD launched by running this script"
echo " -d, --dist-dir=DIR The kernel dist dir (default is /tmp/kernel_dist)"
echo " -d, --dist-dir=DIR The kernel dist dir (default is $DEFAULT_DIST_DIR)"
echo " -s, --serial=SERIAL The device serial number."
echo " If serial is specified, virtual device launch will be skipped"
echo " -t, --test=TEST_NAME The test target name. Can be repeated"
@@ -40,12 +41,13 @@ print_help() {
BUILD_KERNEL=true
LAUNCH_CVD=true
KILL_CVD=true
DIST_DIR=out/
DIST_DIR=$DEFAULT_DIST_DIR
SERIAL_NUMBER=
MODULE_NAME="kunit"
TEST_FILTERS=
SELECTED_TESTS=
GCOV=false
GCOV_DIST_DIR=
while test $# -gt 0; do
case "$1" in
@@ -68,6 +70,7 @@ while test $# -gt 0; do
shift
if test $# -gt 0; then
DIST_DIR=$1
GCOV_DIST_DIR=$DIST_DIR
else
echo "kernel distribution directory is not specified"
exit 1
@@ -76,6 +79,7 @@ while test $# -gt 0; do
;;
--dist-dir*)
DIST_DIR=$(echo $1 | sed -e "s/^[^=]*=//g")
GCOV_DIST_DIR=$DIST_DIR
shift
;;
-s)
@@ -222,7 +226,11 @@ if $LAUNCH_CVD && $KILL_CVD; then
fi
if $GCOV; then
CREATE_TRACEFILE_CLI="common/tools/testing/android/bin/create-tracefile.py \
-t $LOG_DIR -o $LOG_DIR/cov.info"
if [ -n "$GCOV_DIST_DIR" ]; then
CREATE_TRACEFILE_CLI+=" --dist-dir $GCOV_DIST_DIR"
fi
echo "Creating tracefile ..."
common/tools/testing/android/bin/create-tracefile.py -t $LOG_DIR -o $LOG_DIR/cov.info && \
echo "Created tracefile at $LOG_DIR/cov.info"
$CREATE_TRACEFILE_CLI && echo "Created tracefile at $LOG_DIR/cov.info"
fi