#!/bin/bash
#
# SPDX-FileCopyrightText: 2015 Julien Desfossez <jdesfossez@efficios.com>
# SPDX-FileCopyrightText: 2025 Michael Jeanson <mjeanson@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Streaming - User space tracing"

CURDIR=$(dirname "$0")/
TESTDIR=$CURDIR/../../..

NR_ITER=1
NR_USEC_WAIT=0

TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME"
TESTAPP_TIMEOUT_SEC=10

EVENT_NAME="tp:tptest"

NUM_TESTS=69

# shellcheck source-path=SCRIPTDIR/../../..
source "$TESTDIR/utils/utils.sh"

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST events binary detected."
fi

# MUST set TESTDIR before calling those functions

function test_ust_streaming_regenerate_metadata ()
{
	local session_name="streaming"

	local ret
	local metadata_path
	local testapp_pid
	local trace_path
	local file_testapp_output
	local file_sync_application_in_main_touch
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Test UST streaming with metadata regeneration"

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_testapp_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_testapp_output.XXXXXX")
	file_sync_application_in_main_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_application_in_main_touch.XXXXXX")
	file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	file_sync_before_exit_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit_touch.XXXXXX")

	start_lttng_relayd -o "$trace_path"

	create_lttng_session_uri "$session_name" net://localhost
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME

	"$TESTAPP_BIN" -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-application-in-main-touch "${file_sync_application_in_main_touch}" \
		--sync-before-last-event "${file_sync_before_last}" \
		--sync-before-exit-touch "${file_sync_before_exit_touch}" >"${file_testapp_output}" 2>&1 &
	testapp_pid="${!}"
	diag "Launched testapp '$TESTAPP_NAME' (pid: $testapp_pid) to generate $NR_ITER events"

	start_lttng_tracing_ok "$session_name"

	# Wait for the testapp to start
	app_alive_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_application_in_main_touch"

	# Tell the testapp to continue
	touch "${file_sync_before_last}"

	# Wait for the applications started in background
	app_exit_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_before_exit_touch"

	# Expect a valid trace
	stop_lttng_tracing_ok "$session_name"
	validate_trace $EVENT_NAME "$trace_path/$HOSTNAME"
	validate_metadata_event $EVENT_NAME 1 "$trace_path/$HOSTNAME"

	# Find the metadata file
	metadata_path=$(find "$trace_path/$HOSTNAME" -name "metadata")
	ret=$?
	ok $ret "Found metadata file"

	if [[ $ret = 0 ]]; then
		# Empty the metadata file
		echo -n > "$metadata_path"
	fi

	test ! -s "$metadata_path"
	ok $? "Metadata cleared"

	start_lttng_tracing_ok "$session_name"
	regenerate_metadata_ok "$session_name"
	stop_lttng_tracing_ok "$session_name"

	# Metadata file must have been regenerated
	validate_trace $EVENT_NAME "$trace_path/$HOSTNAME"
	validate_metadata_event $EVENT_NAME 1 "$trace_path/$HOSTNAME"
	destroy_lttng_session_ok "$session_name"

	stop_lttng_relayd

	rm -rf "$trace_path"
	rm -f "$file_testapp_output"
	rm -f "$file_sync_application_in_main_touch"
	rm -f "$file_sync_before_last"
	rm -f "$file_sync_before_exit_touch"
}

function test_ust_local_regenerate_metadata ()
{
	local session_name="local"

	local ret
	local metadata_path
	local testapp_pid
	local trace_path
	local file_testapp_output
	local file_sync_application_in_main_touch
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Test UST local with metadata regeneration"

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_testapp_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_testapp_output.XXXXXX")
	file_sync_application_in_main_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_application_in_main_touch.XXXXXX")
	file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	file_sync_before_exit_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit_touch.XXXXXX")

	create_lttng_session_ok "$session_name" "$trace_path"
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME

	"$TESTAPP_BIN" -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-application-in-main-touch "${file_sync_application_in_main_touch}" \
		--sync-before-last-event "${file_sync_before_last}" \
		--sync-before-exit-touch "${file_sync_before_exit_touch}" >"${file_testapp_output}" 2>&1 &
	testapp_pid="${!}"
	diag "Launched testapp '$TESTAPP_NAME' (pid: $testapp_pid) to generate $NR_ITER events"

	start_lttng_tracing_ok "$session_name"

	# Wait for the testapp to start
	app_alive_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_application_in_main_touch"

	# Tell the testapp to continue
	touch "${file_sync_before_last}"

	# Wait for the applications started in background
	app_exit_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_before_exit_touch"

	# Expect a valid trace
	stop_lttng_tracing_ok "$session_name"
	validate_trace $EVENT_NAME "$trace_path"
	validate_metadata_event $EVENT_NAME 1 "$trace_path"

	# Find the metadata file
	metadata_path=$(find "$trace_path" -name "metadata")
	ret=$?
	ok $ret "Found metadata file"

	if [[ $ret = 0 ]]; then
		# Empty the metadata file
		echo -n > "$metadata_path"
	fi

	test ! -s "$metadata_path"
	ok $? "Metadata cleared"

	start_lttng_tracing_ok "$session_name"
	regenerate_metadata_ok "$session_name"
	stop_lttng_tracing_ok "$session_name"

	# Metadata file must have been regenerated
	validate_trace $EVENT_NAME "$trace_path"
	validate_metadata_event $EVENT_NAME 1 "$trace_path"
	destroy_lttng_session_ok "$session_name"

	rm -rf "$trace_path"
	rm -f "$file_testapp_output"
	rm -f "$file_sync_application_in_main_touch"
	rm -f "$file_sync_before_last"
	rm -f "$file_sync_before_exit_touch"
}

function test_ust_pid_regenerate_metadata ()
{
	local session_name="pid"

	local testapp_pid
	local trace_path
	local file_testapp_output
	local file_sync_application_in_main_touch
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Test UST per-pid with metadata regeneration (expect failure)"

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_testapp_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_testapp_output.XXXXXX")
	file_sync_application_in_main_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_application_in_main_touch.XXXXXX")
	file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	file_sync_before_exit_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit_touch.XXXXXX")

	create_lttng_session_ok "$session_name" "$trace_path"
	enable_ust_lttng_channel_ok "$session_name" "channel0" --buffer-ownership=process
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME "channel0"

	"$TESTAPP_BIN" -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-application-in-main-touch "${file_sync_application_in_main_touch}" \
		--sync-before-last-event "${file_sync_before_last}" \
		--sync-before-exit-touch "${file_sync_before_exit_touch}" >"${file_testapp_output}" 2>&1 &
	testapp_pid="${!}"
	diag "Launched testapp '$TESTAPP_NAME' (pid: $testapp_pid) to generate $NR_ITER events"

	start_lttng_tracing_ok "$session_name"

	# Wait for the testapp to start
	app_alive_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_application_in_main_touch"

	# Tell the testapp to continue
	touch "${file_sync_before_last}"

	# Wait for the applications started in background
	app_exit_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_before_exit_touch"

	regenerate_metadata_fail "$session_name"

	stop_lttng_tracing_ok "$session_name"
	validate_trace $EVENT_NAME "$trace_path"
	validate_metadata_event $EVENT_NAME 1 "$trace_path"
	destroy_lttng_session_ok "$session_name"

	rm -rf "$trace_path"
	rm -f "$file_testapp_output"
	rm -f "$file_sync_application_in_main_touch"
	rm -f "$file_sync_before_last"
	rm -f "$file_sync_before_exit_touch"
}

function test_ust_live_regenerate_metadata ()
{
	local session_name="live"

	local testapp_pid
	local trace_path
	local file_testapp_output
	local file_sync_application_in_main_touch
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Test UST live with metadata regeneration (expect failure)"

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_testapp_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_testapp_output.XXXXXX")
	file_sync_application_in_main_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_application_in_main_touch.XXXXXX")
	file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	file_sync_before_exit_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit_touch.XXXXXX")

	start_lttng_relayd -o "$trace_path"

	create_lttng_session_uri "$session_name" net://localhost --live
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME

	"$TESTAPP_BIN" -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-application-in-main-touch "${file_sync_application_in_main_touch}" \
		--sync-before-last-event "${file_sync_before_last}" \
		--sync-before-exit-touch "${file_sync_before_exit_touch}" >"${file_testapp_output}" 2>&1 &
	testapp_pid="${!}"
	diag "Launched testapp '$TESTAPP_NAME' (pid: $testapp_pid) to generate $NR_ITER events"

	start_lttng_tracing_ok "$session_name"

	# Wait for the testapp to start
	app_alive_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_application_in_main_touch"

	# Tell the testapp to continue
	touch "${file_sync_before_last}"

	# Wait for the applications started in background
	app_exit_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_before_exit_touch"

	regenerate_metadata_fail "$session_name"

	stop_lttng_tracing_ok "$session_name"
	validate_trace $EVENT_NAME "$trace_path"
	validate_metadata_event $EVENT_NAME 1 "$trace_path"
	destroy_lttng_session_ok "$session_name"

	stop_lttng_relayd

	rm -rf "$trace_path"
	rm -f "$file_testapp_output"
	rm -f "$file_sync_application_in_main_touch"
	rm -f "$file_sync_before_last"
	rm -f "$file_sync_before_exit_touch"
}

function test_ust_local_snapshot_after_regenerate_metadata ()
{
	local session_name="snapshot-regen"

	local trace_path
	local metadata_path
	local file_testapp_output

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	file_testapp_output=$(mktemp -u -t "tmp.${FUNCNAME[0]}_testapp_output.XXXXXX")

	diag "Test UST local snapshot after regenerate metadata"

	create_lttng_session_ok "$session_name" "$trace_path" --snapshot
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME
	start_lttng_tracing_ok "$session_name"

	run_testapp_ok "$file_testapp_output" "$TESTAPP_BIN" -i "$NR_ITER" -w "$NR_USEC_WAIT"

	regenerate_metadata_ok "$session_name"

	lttng_snapshot_record "$session_name" "$trace_path"

	stop_lttng_tracing_ok "$session_name"
	validate_trace $EVENT_NAME "$trace_path"
	validate_metadata_event $EVENT_NAME 1 "$trace_path"
	destroy_lttng_session_ok "$session_name"

	rm -rf "$trace_path"
	rm -f "$file_testapp_output"
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"
bail_out_if_no_babeltrace

start_lttng_sessiond

test_ust_streaming_regenerate_metadata
test_ust_local_regenerate_metadata
test_ust_pid_regenerate_metadata
test_ust_live_regenerate_metadata
test_ust_local_snapshot_after_regenerate_metadata

stop_lttng_sessiond
