#
# rocprofv3 HIP graph bubble regression tests
#
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)

project(
    rocprofiler-sdk-tests-rocprofv3-hip-graph-bubbles
    LANGUAGES CXX
    VERSION 0.0.0)

find_package(rocprofiler-sdk REQUIRED)

# Disable all variants on gfx1151 and gfx120X.
rocprofiler_sdk_get_gfx_architectures(rocprofiler-sdk-hip-graph-bubbles-gfx-info ECHO)
set(HIP_GRAPH_BUBBLES_DISABLED OFF)
foreach(_gfx IN ITEMS gfx1151 gfx1200 gfx1201)
    if(_gfx IN_LIST rocprofiler-sdk-hip-graph-bubbles-gfx-info)
        set(HIP_GRAPH_BUBBLES_DISABLED ON)
        break()
    endif()
endforeach()

set(HIP_GRAPH_BUBBLES_ENVIRONMENT)
if(DEFINED ENV{ROCM_PATH} AND EXISTS "$ENV{ROCM_PATH}/lib/rocm_sysdeps/lib")
    list(APPEND HIP_GRAPH_BUBBLES_ENVIRONMENT
         "LD_LIBRARY_PATH=$ENV{ROCM_PATH}/lib/rocm_sysdeps/lib:$ENV{LD_LIBRARY_PATH}")
endif()

# One scheduling stall on a shared CI host can open a gap of a few hundred microseconds,
# which fails the bubble check although the profiler did not cause it. The execute step
# runs the traced application again, up to this many times, until the kernel trace passes
# the same check as validate.py. A bubble that is there on every run still fails.
set(HIP_GRAPH_BUBBLES_ATTEMPTS 3)

function(add_hip_graph_bubbles_kernel_trace_test SUFFIX NUM_KERNELS NUM_ITERATIONS
         TIMEOUT_SECS)
    set(TEST_NAME "rocprofv3-test-hip-graph-bubbles-${SUFFIX}")
    set(OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/hip-graph-bubbles-${SUFFIX}")
    math(EXPR EXPECTED_DISPATCH_COUNT "${NUM_KERNELS} * ${NUM_ITERATIONS}")
    math(EXPR EXECUTE_TIMEOUT_SECS "${TIMEOUT_SECS} * ${HIP_GRAPH_BUBBLES_ATTEMPTS}")

    rocprofiler_add_integration_execute_test(
        ${TEST_NAME}
        COMMAND
            ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/run_with_bubble_retry.py
            --csv ${OUTPUT_DIR}/out_kernel_trace.csv --attempts
            ${HIP_GRAPH_BUBBLES_ATTEMPTS} -- $<TARGET_FILE:rocprofiler-sdk::rocprofv3>
            --hip-trace --kernel-trace -d ${OUTPUT_DIR} -o out --output-format csv pftrace
            -- $<TARGET_FILE:hip-graph-bubbles> ${NUM_KERNELS} ${NUM_ITERATIONS}
            ${NUM_ITERATIONS} 256 25
        DEPENDS hip-graph-bubbles
        TIMEOUT ${EXECUTE_TIMEOUT_SECS}
        LABELS "integration-tests"
        DISABLED "${HIP_GRAPH_BUBBLES_DISABLED}"
        ENVIRONMENT ${HIP_GRAPH_BUBBLES_ENVIRONMENT}
        FIXTURES_SETUP ${TEST_NAME}
        FAIL_REGULAR_EXPRESSION
            "HSA_API|HIP_API|HIP_COMPILER_API|MARKER_CORE_API|MARKER_CONTROL_API|MARKER_NAME_API|KERNEL_DISPATCH|CODE_OBJECT"
        )

    rocprofiler_add_integration_validate_test(
        ${TEST_NAME}
        TEST_PATHS validate.py
        COPY conftest.py bubble_gaps.py
        CONFIG pytest.ini
        ARGS --kernel-input
             ${CMAKE_CURRENT_BINARY_DIR}/hip-graph-bubbles-${SUFFIX}/out_kernel_trace.csv
             --expected-dispatch-count
             ${EXPECTED_DISPATCH_COUNT}
             --expected-kernels
             ${NUM_KERNELS}
             --expected-iterations
             ${NUM_ITERATIONS}
        TIMEOUT ${TIMEOUT_SECS}
        LABELS "integration-tests"
        DISABLED "${HIP_GRAPH_BUBBLES_DISABLED}"
        FIXTURES_REQUIRED ${TEST_NAME})
endfunction()

add_hip_graph_bubbles_kernel_trace_test(k256-i20 256 20 60)
add_hip_graph_bubbles_kernel_trace_test(k256-i200 256 200 120)
add_hip_graph_bubbles_kernel_trace_test(k2000-i200 2000 200 180)
