#!/bin/bash
#
# Copyright (C) 2012 David Goulet <dgoulet@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
NR_USEC_WAIT=100000
SESSION_NAME="buffers-pid"

TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
EVENT_NAME="tp:tptest"
NUM_TESTS=59

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

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel --buffers-pid -u $channel_name -s $sess_name >/dev/null 2>&1
	ok $? "Enable channel $channel_name per PID for session $sess_name"
}

test_after_multiple_apps() {
	local out
	local i

	diag "Start multiple applications AFTER tracing is started"

	# 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

	for i in `seq 1 5`; do
		$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT >/dev/null 2>&1 &
		ok $? "Start application $i for tracing"
	done
	wait

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	trace_match_only $EVENT_NAME $[NR_ITER * 5] $TRACE_PATH

	return $?
}

test_before_multiple_apps() {
	local out
	local i
	local file_sync_after_first=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")

	diag "Start multiple applications BEFORE tracing is started"

	for i in `seq 1 5`; do
		$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
			--sync-after-first-event ${file_sync_after_first}_${i} \
			--sync-before-last-event ${file_sync_before_last}_${i} >/dev/null 2>&1 &
		ok $? "Start application $i for tracing"
	done

	# 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

	for i in `seq 1 5`; do
		touch ${file_sync_before_last}_${i}
	done

	diag "Waiting for applications to end"
	wait
	pass "Waiting done"

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	for i in `seq 1 5`; do
		rm -f ${file_sync_after_first}_${i}
		rm -f ${file_sync_before_last}_${i}
	done

	out=$(babeltrace $TRACE_PATH | grep $EVENT_NAME | wc -l)
	if [ $out -eq 0 ]; then
		fail "Trace validation"
		diag "No event(s) found. We are supposed to have at least one."
		out=1
	else
		pass "Trace validation"
		diag "Found $out event(s). Coherent."
		out=0
	fi

	return $out
}

test_after_app() {
	local out

	diag "Start application AFTER tracing is started"

	# 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

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT &
	ok $? "Start application to trace"
	wait

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	trace_match_only $EVENT_NAME $NR_ITER $TRACE_PATH

	return $?
}

test_before_app() {
	local out
	local file_sync_after_first=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")

	diag "Start application BEFORE tracing is started"


	# 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 -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} &
	ok $? "Start application to trace"

	start_lttng_tracing_ok $SESSION_NAME

	touch ${file_sync_before_last}

	wait

	stop_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}

	out=$(babeltrace $TRACE_PATH | grep $EVENT_NAME | wc -l)
	if [ $out -eq 0 ]; then
		fail "Trace validation"
		diag "No event(s) found. We are supposed to have at least one."
		out=1
	else
		pass "Trace validation"
		diag "Found $out event(s). Coherent."
		out=0
	fi

	return $out
}

test_multiple_channels() {
	local out

	diag "Start with multiple channels"

	# 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.
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $EVENT_NAME -c channel0 -s $SESSION_NAME -u >/dev/null 2>&1
	ok $? "Enable event $EVENT_NAME for session $SESSION_NAME in channel0"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $EVENT_NAME -c channel1 -s $SESSION_NAME -u >/dev/null 2>&1
	ok $? "Enable event $EVENT_NAME for session $SESSION_NAME in channel1"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $EVENT_NAME -c channel2 -s $SESSION_NAME -u >/dev/null 2>&1
	ok $? "Enable event $EVENT_NAME for session $SESSION_NAME in channel2"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $EVENT_NAME -c channel3 -s $SESSION_NAME -u >/dev/null 2>&1
	ok $? "Enable event $EVENT_NAME for session $SESSION_NAME in channel3"
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $EVENT_NAME -c channel4 -s $SESSION_NAME -u >/dev/null 2>&1
	ok $? "Enable event $EVENT_NAME for session $SESSION_NAME in channel4"
	start_lttng_tracing_ok $SESSION_NAME

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT &
	ok $? "Start application to trace"
	wait

	stop_lttng_tracing_ok $SESSION_NAME
	trace_match_only $EVENT_NAME $[NR_ITER * 5] $TRACE_PATH
	out=$?

	destroy_lttng_session_ok $SESSION_NAME

	return $out
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

TESTS=(
	"test_before_app"
	"test_after_app"
	"test_after_multiple_apps"
	"test_before_multiple_apps"
	"test_multiple_channels"
)

TEST_COUNT=${#TESTS[@]}
i=0

start_lttng_sessiond

while [ $i -lt $TEST_COUNT ]; do
	TRACE_PATH=$(mktemp --tmpdir -d tmp.test_buffers_pid_trace_path.XXXXXX)
	${TESTS[$i]}
	rm -rf $TRACE_PATH
	let "i++"
done

stop_lttng_sessiond
