#!/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

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

NR_APPS=30
NR_ITER=10
NR_USEC_WAIT=10

TEST_DESC="UST tracer - Generate $NR_APPS processes"

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=$((11 + (2 * NR_APPS)))

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

# MUST set TESTDIR before calling those functions

function test_nprocesses ()
{
	local session_name="ust-$NR_APPS-processes"
	local app_id
	local testapp_pids=()
	local registered_pids=()
	local trace_path
	local mi_output_file
	local file_testapp_output
	local file_sync_after_first
	local file_sync_before_last
	local file_sync_before_exit_touch

	trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	mi_output_file=$(mktemp -t "tmp.${FUNCNAME[0]}_mi_output_file.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")

	start_lttng_sessiond

	for app_id in $(seq 0 $((NR_APPS - 1))); do
		"$TESTAPP_BIN" -i $NR_ITER -w $NR_USEC_WAIT \
			--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

	# Wait for all apps to have sent an event
	for app_id in $(seq 0 $((NR_APPS - 1))); do
		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

	# Get the list of registered apps
	LTTNG_TEST_MI_CLIENT=1 OUTPUT_DEST="$mi_output_file" list_lttng_ok -u

	while IFS='' read -r line; do registered_pids+=("$line"); done < <("$XML_EXTRACT" "$mi_output_file" '//lttng:pids/lttng:pid/lttng:id')
	is "${#registered_pids[@]}" "$NR_APPS" "All apps registered (${#registered_pids[@]} / $NR_APPS)"

	create_lttng_session_ok $session_name "${trace_path}"

	enable_ust_lttng_event_ok $session_name $EVENT_NAME
	start_lttng_tracing_ok $session_name

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

	# Wait for all apps to exit
	for app_id in $(seq 0 $((NR_APPS - 1))); do
		app_exit_wait_for_sync "$TESTAPP_NAME $app_id" "${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

	stop_lttng_sessiond

	validate_trace_count "$EVENT_NAME" "${trace_path}" $NR_APPS

	rm -rf "$trace_path"
	for app_id in $(seq 0 $((NR_APPS - 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
	rm -f "$mi_output_file"
}

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

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

test_nprocesses
