#!/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="Streaming - Kernel tracing"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
EVENT_NAME="sched_switch"
PID_RELAYD=0
SESSION_NAME=""

TRACE_PATH=$(mktemp -d)

source $TESTDIR/utils.sh

print_test_banner "$TEST_DESC"

if [ "$(id -u)" != "0" ]; then
	echo "This test must be running as root. Aborting"
	# Exit status 0 so the tests can continue
	exit 0
fi

# LTTng kernel modules check
out=`ls /lib/modules/$(uname -r)/extra | grep lttng`
if [ -z "$out" ]; then
	echo "LTTng modules not detected. Aborting kernel tests!"
	echo ""
	# Exit status 0 so the tests can continue
	exit 0
fi

function lttng_create_session
{
	echo -n "Creating session $SESSION_NAME... "
	# Create session with default path
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN create $SESSION_NAME >/dev/null 2>&1
	if [ $? -eq 1 ]; then
		print_fail
		return 1
	else
		print_ok
	fi
}

function lttng_enable_consumer_localhost
{
	echo -n "Enabling network consumer... "
	# Create session with default path
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-consumer -k net://localhost >/dev/null 2>&1
	if [ $? -eq 1 ]; then
		print_fail
		return 1
	else
		print_ok
	fi
}

function test_kernel_before_start ()
{
	echo -e "\n=== Testing kernel streaming with event enable BEFORE start\n"
	lttng_create_session
	lttng_enable_consumer_localhost
	lttng_enable_kernel_event $SESSION_NAME $EVENT_NAME
	start_lttng_tracing $SESSION_NAME
	# Give a second
	sleep 1
	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME

	# We can not predict _yet_ when the trace is available so we have to do a
	# arbitratry sleep to validate the trace.
	echo -n "Waiting 3 seconds for the trace to be written on disk "
	for i in `seq 1 3`; do
		echo -n "."
		sleep 1
	done
	echo ""
}

# Deactivated since this feature is not yet available where we can enable
# an event AFTERE tracing has started.
function test_kernel_after_start ()
{
	echo -e "\n=== Testing kernel streaming with event enable AFTER start\n"
	lttng_create_session
	lttng_enable_consumer_localhost
	start_lttng_tracing $SESSION_NAME
	lttng_enable_kernel_event $SESSION_NAME $EVENT_NAME
	# Give a second
	sleep 1
	stop_lttng_tracing $SESSION_NAME
	destroy_lttng_session $SESSION_NAME
}

start_lttng_sessiond
start_lttng_relayd "-o $TRACE_PATH"

tests=( test_kernel_before_start )

for fct_test in ${tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test}

	# Validate test
	validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$SESSION_NAME*
	if [ $? -eq 0 ]; then
		# Only delete if successful
		rm -rf $TRACE_PATH
	else
		break
	fi
done

echo ""
stop_lttng_sessiond
stop_lttng_relayd


exit $out
