#!/bin/bash
#
# SPDX-FileCopyrightText: 2012 David Goulet <dgoulet@efficios.com>
# SPDX-FileCopyrightText: 2025 Michael Jeanson <mjeanson@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="UST tracer - Tracing with per PID buffers"

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

NR_ITER=100

SESSION_NAME="buffers-pid"

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 enable_channel_per_pid()
{
	sess_name=$1
	channel_name=$2

	enable_ust_lttng_channel_ok "$sess_name" "$channel_name" --buffer-ownership=process
}

test_after_multiple_apps() {
	local app_cnt=5
	local app_id
	local testapp_pids=()
	local trace_path
	local file_testapp_output
	local file_sync_after_first
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Start multiple applications AFTER tracing is started"

	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_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.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")

	# BEFORE applications are spawned
	create_lttng_session_ok $SESSION_NAME "$trace_path"
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing_ok $SESSION_NAME
	for app_id in $(seq 0 $((app_cnt - 1))); do
		"$TESTAPP_BIN" -i $NR_ITER \
			--sync-after-first-event "${file_sync_after_first}_${app_id}" \
			--sync-before-last-event "${file_sync_before_last}_${app_id}" \
			--sync-before-exit-touch "${file_sync_before_exit_touch}_${app_id}" >"${file_testapp_output}_${app_id}" 2>&1 &
		testapp_pids+=($!)
		diag "Launched testapp '$TESTAPP_NAME $app_id' (pid: ${testapp_pids[$app_id]}) to generate $NR_ITER events"
	done

	for app_id in $(seq 0 $((app_cnt - 1))); do
		# Wait for the testapp first event
		app_alive_wait_for_sync "$TESTAPP_NAME $app_id" "${testapp_pids[$app_id]}" "$TESTAPP_TIMEOUT_SEC" "${file_testapp_output}_${app_id}" "${file_sync_after_first}_${app_id}"
	done

	for app_id in $(seq 0 $((app_cnt - 1))); do
		# Tell the testapp to continue
		touch "${file_sync_before_last}_${app_id}"
	done

	for app_id in $(seq 0 $((app_cnt - 1))); do
		app_exit_wait_for_sync "$TESTAPP_NAME" "${testapp_pids[$app_id]}" "$TESTAPP_TIMEOUT_SEC" "${file_testapp_output}_${app_id}" "${file_sync_before_exit_touch}_${app_id}"
	done

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	trace_match_only $EVENT_NAME $((NR_ITER * app_cnt)) "$trace_path"

	rm -rf "$trace_path"
	for app_id in $(seq 0 $((app_cnt - 1))); do
		rm -f "${file_testapp_output}_${app_id}"
		rm -f "${file_sync_after_first}_${app_id}"
		rm -f "${file_sync_before_last}_${app_id}"
		rm -f "${file_sync_before_exit_touch}_${app_id}"
	done
}

test_before_multiple_apps() {
	local app_cnt=5
	local app_id
	local testapp_pids=()
	local trace_path
	local file_testapp_output
	local file_sync_after_first
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Start multiple applications BEFORE tracing is started"

	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_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.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")

	for app_id in $(seq 0 $((app_cnt - 1))); do
		"$TESTAPP_BIN" -i $NR_ITER \
			--sync-after-first-event "${file_sync_after_first}_${app_id}" \
			--sync-before-last-event "${file_sync_before_last}_${app_id}" \
			--sync-before-exit-touch "${file_sync_before_exit_touch}_${app_id}" >"${file_testapp_output}_${app_id}" 2>&1 &
		testapp_pids+=($!)
		diag "Launched testapp '$TESTAPP_NAME $app_id' (pid: ${testapp_pids[$app_id]}) to generate $NR_ITER events"
	done

	for app_id in $(seq 0 $((app_cnt - 1))); do
		# Wait for the testapp first event
		app_alive_wait_for_sync "$TESTAPP_NAME $app_id" "${testapp_pids[$app_id]}" "$TESTAPP_TIMEOUT_SEC" "${file_testapp_output}_${app_id}" "${file_sync_after_first}_${app_id}"
	done

	# AFTER applications are spawned
	create_lttng_session_ok $SESSION_NAME "$trace_path"
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing_ok $SESSION_NAME

	for app_id in $(seq 0 $((app_cnt - 1))); do
		# Tell the testapp to continue
		touch "${file_sync_before_last}_${app_id}"
	done

	for app_id in $(seq 0 $((app_cnt - 1))); do
		app_exit_wait_for_sync "$TESTAPP_NAME" "${testapp_pids[$app_id]}" "$TESTAPP_TIMEOUT_SEC" "${file_testapp_output}_${app_id}" "${file_sync_before_exit_touch}_${app_id}"
	done

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	validate_trace $EVENT_NAME "$trace_path"

	rm -rf "$trace_path"
	for app_id in $(seq 0 $((app_cnt - 1))); do
		rm -f "${file_testapp_output}_${app_id}"
		rm -f "${file_sync_after_first}_${app_id}"
		rm -f "${file_sync_before_last}_${app_id}"
		rm -f "${file_sync_before_exit_touch}_${app_id}"
	done
}

test_after_app() {
	local trace_path
	local file_testapp_output

	diag "Start application AFTER tracing is started"

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

	# BEFORE application is spawned
	create_lttng_session_ok $SESSION_NAME "$trace_path"
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing_ok $SESSION_NAME

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

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	trace_match_only $EVENT_NAME $NR_ITER "$trace_path"

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

test_before_app() {
	local testapp_pid
	local trace_path
	local file_testapp_output
	local file_sync_after_first
	local file_sync_before_last
	local file_sync_before_exit_touch

	diag "Start application BEFORE tracing is started"

	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_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.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")

	# BEFORE application is spawned
	create_lttng_session_ok $SESSION_NAME "$trace_path"
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME "channel0"

	"$TESTAPP_BIN" -i $NR_ITER \
		--sync-after-first-event "${file_sync_after_first}" \
		--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"

	# Wait for the testapp first event
	app_alive_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_after_first"

	start_lttng_tracing_ok $SESSION_NAME

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

	app_exit_wait_for_sync "$TESTAPP_NAME" "$testapp_pid" "$TESTAPP_TIMEOUT_SEC" "$file_testapp_output" "$file_sync_before_exit_touch"

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	validate_trace $EVENT_NAME "$trace_path"

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

test_multiple_channels() {
	local trace_path
	local file_testapp_output

	diag "Start with multiple channels"

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

	# BEFORE application is spawned
	create_lttng_session_ok $SESSION_NAME "$trace_path"

	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_channel_per_pid $SESSION_NAME "channel1"
	enable_channel_per_pid $SESSION_NAME "channel2"
	enable_channel_per_pid $SESSION_NAME "channel3"
	enable_channel_per_pid $SESSION_NAME "channel4"

	# Enable event in all channels.
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME channel0
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME channel1
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME channel2
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME channel3
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME channel4

	start_lttng_tracing_ok $SESSION_NAME

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

	stop_lttng_tracing_ok $SESSION_NAME
	trace_match_only $EVENT_NAME $((NR_ITER * 5)) "$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_before_app
test_after_app
test_after_multiple_apps
test_before_multiple_apps
test_multiple_channels

stop_lttng_sessiond
