From 0239f7969501262db6394b9e517c511c19051f7f Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 30 Nov 2020 09:20:18 +0800 Subject: [PATCH] Generate code coverage reports only for incremental files (#28508) * Generate code coverage reports only for incremental files, test=develop * Generate code coverage reports only for incremental files, test=develop * Generate code coverage reports only for incremental files, test=develop * test for diff python file, test=develop * fix no python diff report, test=develop * add cc test file, test=develop * fix bug in generic.cmake, test=develop * for debug no cc report, test=develp * modify compire branch form test_pr to test, test=develop * fix bug, test=develop * test for h file changed, test=develop * debug for redefinition of argument optimize error, test=develop * close -o3 for test, test=develop * remove -o3 for test, test=develop * remove coverage option for nvcc, test=develop * use CMAKE_CXX_FLAGS open coverage option when header file changed, test=develop * reopen -o3, test=develop * remove debug code, test=develop * remove unused code, test=develop --- cmake/coveralls.cmake | 6 +- cmake/generic.cmake | 71 +++++++++++++++++-- paddle/scripts/paddle_build.sh | 11 +++ .../fluid/tests/unittests/CMakeLists.txt | 25 ++++--- tools/coverage/paddle_coverage.sh | 14 ++++ 5 files changed, 109 insertions(+), 18 deletions(-) diff --git a/cmake/coveralls.cmake b/cmake/coveralls.cmake index c0e96e2877..aad02d24be 100644 --- a/cmake/coveralls.cmake +++ b/cmake/coveralls.cmake @@ -62,8 +62,10 @@ function(code_coverage _COVERAGE_SRCS _COVERALLS_UPLOAD _CMAKE_SCRIPT_PATH) endfunction() if(WITH_COVERAGE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") + if (NOT ("$ENV{PADDLE_GIT_DIFF_H_FILE}" STREQUAL "")) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") + endif() set(EXCLUDE_DIRS "demo/" diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 5475386224..835ea5f61c 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -266,6 +266,32 @@ function(merge_static_libs TARGET_NAME) endif(WIN32) endfunction(merge_static_libs) +function(check_coverage_opt TARGET_NAME SRCS) + if(WITH_COVERAGE) + if ("$ENV{PADDLE_GIT_DIFF_H_FILE}" STREQUAL "") + if (NOT ("$ENV{PADDLE_GIT_DIFF_CC_FILE}" STREQUAL "")) + string(REPLACE "," ";" CC_FILE_LIST $ENV{PADDLE_GIT_DIFF_CC_FILE}) + set(use_coverage_opt FALSE) + FOREACH(cc_file ${CC_FILE_LIST}) + if("${SRCS};" MATCHES "${cc_file}") + set(use_coverage_opt TRUE) + break() + endif() + ENDFOREACH(cc_file) + + if (use_coverage_opt) + message(STATUS "cc changed, add coverage opt for ${TARGET_NAME}") + target_compile_options(${TARGET_NAME} PRIVATE -g -O0 -fprofile-arcs -ftest-coverage) + target_link_libraries(${TARGET_NAME} -fprofile-arcs) + get_target_property(WH_TARGET_COMPILE_OPTIONS ${TARGET_NAME} COMPILE_OPTIONS) + message(STATUS "property for ${TARGET_NAME} is ${WH_TARGET_COMPILE_OPTIONS}") + endif() + endif() + endif() + endif() +endfunction(check_coverage_opt) + + function(cc_library TARGET_NAME) set(options STATIC static SHARED shared INTERFACE interface) set(oneValueArgs "") @@ -325,6 +351,9 @@ function(cc_library TARGET_NAME) list(APPEND cc_library_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/${source}.h) endif() endforeach() + + check_coverage_opt(${TARGET_NAME} ${cc_library_SRCS}) + else(cc_library_SRCS) if(cc_library_DEPS) list(REMOVE_DUPLICATES cc_library_DEPS) @@ -352,6 +381,9 @@ function(cc_binary TARGET_NAME) endif() get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) target_link_libraries(${TARGET_NAME} ${os_dependency_modules}) + + check_coverage_opt(${TARGET_NAME} ${cc_binary_SRCS}) + endfunction(cc_binary) function(cc_test_build TARGET_NAME) @@ -371,6 +403,9 @@ function(cc_test_build TARGET_NAME) add_dependencies(${TARGET_NAME} ${cc_test_DEPS} paddle_gtest_main lod_tensor memory gtest gflags glog) common_link(${TARGET_NAME}) endif() + + check_coverage_opt(${TARGET_NAME} ${cc_test_SRCS}) + endfunction() function(cc_test_run TARGET_NAME) @@ -532,6 +567,9 @@ function(hip_library TARGET_NAME) list(APPEND hip_library_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/${source}.h) endif() endforeach() + + check_coverage_opt(${TARGET_NAME} ${hip_library_SRCS}) + else(hip_library_SRCS) if (hip_library_DEPS) merge_static_libs(${TARGET_NAME} ${hip_library_DEPS}) @@ -555,6 +593,9 @@ function(hip_binary TARGET_NAME) common_link(${TARGET_NAME}) endif() endif() + + check_coverage_opt(${TARGET_NAME} ${hip_binary_SRCS}) + endfunction(hip_binary) function(hip_test TARGET_NAME) @@ -576,6 +617,9 @@ function(hip_test TARGET_NAME) common_link(${TARGET_NAME}) add_test(${TARGET_NAME} ${TARGET_NAME}) endif() + + check_coverage_opt(${TARGET_NAME} ${hip_test_SRCS}) + endfunction(hip_test) function(go_library TARGET_NAME) @@ -655,6 +699,9 @@ function(go_binary TARGET_NAME) WORKING_DIRECTORY "${PADDLE_IN_GOPATH}/go") add_custom_target(${TARGET_NAME} ALL DEPENDS go_vendor ${TARGET_NAME}_timestamp ${go_binary_DEPS}) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} DESTINATION bin) + + check_coverage_opt(${TARGET_NAME} ${go_binary_SRCS}) + endfunction(go_binary) function(go_test TARGET_NAME) @@ -743,13 +790,23 @@ function(py_test TARGET_NAME) cmake_parse_arguments(py_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) if(WITH_COVERAGE) - add_test(NAME ${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env FLAGS_init_allocated_mem=true FLAGS_cudnn_deterministic=true - FLAGS_cpu_deterministic=true - PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_ENVS} - COVERAGE_FILE=${PADDLE_BINARY_DIR}/python-coverage.data - ${PYTHON_EXECUTABLE} -m coverage run --branch -p ${py_test_SRCS} ${py_test_ARGS} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + if ("$ENV{PADDLE_GIT_DIFF_PY_FILE}" STREQUAL "") + add_test(NAME ${TARGET_NAME} + COMMAND ${CMAKE_COMMAND} -E env FLAGS_init_allocated_mem=true FLAGS_cudnn_deterministic=true + FLAGS_cpu_deterministic=true + PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_ENVS} + ${PYTHON_EXECUTABLE} -u ${py_test_SRCS} ${py_test_ARGS} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + + else() + add_test(NAME ${TARGET_NAME} + COMMAND ${CMAKE_COMMAND} -E env FLAGS_init_allocated_mem=true FLAGS_cudnn_deterministic=true + FLAGS_cpu_deterministic=true + PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_ENVS} + COVERAGE_FILE=${PADDLE_BINARY_DIR}/python-coverage.data + ${PYTHON_EXECUTABLE} -m coverage run --branch -p --include=$ENV{PADDLE_GIT_DIFF_PY_FILE} ${py_test_SRCS} ${py_test_ARGS} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + endif() else() add_test(NAME ${TARGET_NAME} COMMAND ${CMAKE_COMMAND} -E env FLAGS_init_allocated_mem=true FLAGS_cudnn_deterministic=true diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index 1c6e6e4f3b..12077781da 100755 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -766,6 +766,16 @@ function check_approvals_of_unittest() { set -x } +function check_diff_file_for_coverage() { + diff_h_file=$(git diff --name-status test develop | awk '$1 != "D" {print $2}' | grep '\.h$' | awk -F "/" '{printf "%s,",$NF}') + diff_cc_file=$(git diff --name-status test develop | awk '$1 != "D" {print $2}' | grep -E '\.(cc|c)$' | awk -F "/" '{printf "%s,",$NF}') + diff_py_file=$(git diff --name-status test develop | grep '\.py$' | awk '$1 != "D" {printf "%s,",$2}') + + export PADDLE_GIT_DIFF_H_FILE=${diff_h_file%*,} + export PADDLE_GIT_DIFF_CC_FILE=${diff_cc_file%*,} + export PADDLE_GIT_DIFF_PY_FILE=${diff_py_file%*,} +} + function check_change_of_unittest() { generate_unittest_spec "PR" fetch_upstream_develop_if_not_exist @@ -1720,6 +1730,7 @@ function main() { ;; cicheck_coverage) check_approvals_of_unittest 1 + check_diff_file_for_coverage cmake_gen_and_build ${PYTHON_ABI:-""} ${parallel_number} enable_unused_var_check parallel_test diff --git a/python/paddle/fluid/tests/unittests/CMakeLists.txt b/python/paddle/fluid/tests/unittests/CMakeLists.txt index 1ddafa97a5..b6a99498c7 100644 --- a/python/paddle/fluid/tests/unittests/CMakeLists.txt +++ b/python/paddle/fluid/tests/unittests/CMakeLists.txt @@ -207,16 +207,23 @@ function(py_test_modules TARGET_NAME) cmake_parse_arguments(py_test_modules "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) if(WITH_COVERAGE) - add_test(NAME ${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_modules_ENVS} - COVERAGE_FILE=${PADDLE_BINARY_DIR}/python-coverage.data - ${PYTHON_EXECUTABLE} -m coverage run --branch -p ${PADDLE_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + if ("$ENV{PADDLE_GIT_DIFF_PY_FILE}" STREQUAL "") + add_test(NAME ${TARGET_NAME} + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_modules_ENVS} + ${PYTHON_EXECUTABLE} ${PADDLE_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + else() + add_test(NAME ${TARGET_NAME} + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_modules_ENVS} + COVERAGE_FILE=${PADDLE_BINARY_DIR}/python-coverage.data + ${PYTHON_EXECUTABLE} -m coverage run --branch -p --include=$ENV{PADDLE_GIT_DIFF_PY_FILE} ${PADDLE_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + endif() else() - add_test(NAME ${TARGET_NAME} - COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_modules_ENVS} - ${PYTHON_EXECUTABLE} ${PADDLE_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + add_test(NAME ${TARGET_NAME} + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${PADDLE_BINARY_DIR}/python ${py_test_modules_ENVS} + ${PYTHON_EXECUTABLE} ${PADDLE_SOURCE_DIR}/tools/test_runner.py ${py_test_modules_MODULES} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) endif() if (py_test_modules_SERIAL) diff --git a/tools/coverage/paddle_coverage.sh b/tools/coverage/paddle_coverage.sh index 008b35d01c..148a27358e 100644 --- a/tools/coverage/paddle_coverage.sh +++ b/tools/coverage/paddle_coverage.sh @@ -1,5 +1,19 @@ #!/usr/bin/env bash +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + set -xe PADDLE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}")/../../" && pwd )" -- GitLab