#!/bin/bash
#
# Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
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=58

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"
}

function wait_apps
{
	diag "Waiting for applications to end..."
	while [ -n "$(pidof $TESTAPP_NAME)" ]; do
		sleep 1
	done
}

test_after_multiple_apps() {
	local out
	local i

	diag "Start multiple applications AFTER tracing is started"

	# BEFORE application is spawned
	create_lttng_session $SESSION_NAME $TRACE_PATH
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing $SESSION_NAME

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

	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME

	trace_matches $EVENT_NAME $[NR_ITER * 5] $TRACE_PATH

	return $?
}

test_before_multiple_apps() {
	local out
	local i

	diag "Start multiple applications BEFORE tracing is started"

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

	# BEFORE application is spawned
	create_lttng_session $SESSION_NAME $TRACE_PATH
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing $SESSION_NAME

	# At least hit one event
	sleep 2

	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME

	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

	wait_apps

	return $out
}

test_after_app() {
	local out

	diag "Start application AFTER tracing is started"

	# BEFORE application is spawned
	create_lttng_session $SESSION_NAME $TRACE_PATH
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing $SESSION_NAME

	$TESTAPP_BIN $NR_ITER $NR_USEC_WAIT
	ok $? "Start application to trace"

	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME

	trace_matches $EVENT_NAME $NR_ITER $TRACE_PATH

	return $?
}

test_before_app() {
	local out

	diag "Start application BEFORE tracing is started"

	$TESTAPP_BIN $NR_ITER $NR_USEC_WAIT &
	ok $? "Start application to trace"

	# BEFORE application is spawned
	create_lttng_session $SESSION_NAME $TRACE_PATH
	enable_channel_per_pid $SESSION_NAME "channel0"
	enable_ust_lttng_event $SESSION_NAME $EVENT_NAME "channel0"
	start_lttng_tracing $SESSION_NAME

	# At least hit one event
	sleep 2

	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME

	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

	wait_apps

	return $out
}

test_multiple_channels() {
	local out

	diag "Start with multiple channels"

	# BEFORE application is spawned
	create_lttng_session $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 $SESSION_NAME

	$TESTAPP_BIN $NR_ITER $NR_USEC_WAIT
	ok $? "Start application to trace"

	stop_lttng_tracing $SESSION_NAME
	trace_matches $EVENT_NAME $[NR_ITER * 5] $TRACE_PATH
	out=$?

	destroy_lttng_session $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 -d)
	${TESTS[$i]}
	rm -rf $TRACE_PATH
	let "i++"
done

stop_lttng_sessiond
