#!/bin/bash
#
# Copyright (C) 2013 Christian Babeux <christian.babeux@efficios.com>
# Copyright (C) 2014 Julien Desfossez <jdesfossez@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only

TEST_DESC="UST tracer - Perf counters"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
SESSION_NAME="perf_counters"
EVENT_NAME="tp:tptest"
NUM_TESTS=24
NR_ITER=1
NR_USEC_WAIT=1
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"

source $TESTDIR/utils/utils.sh

function enable_ust_lttng_event_per_chan()
{
	sess_name="$1"
	event_name="$2"
	chan_name="$3"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -c $chan_name -u >/dev/null 2>&1
	ok $? "Enable event $event_name for session $sess_name in channel $chan_name"
}

function add_context_ust_skip_ok()
{
	local session_name=$1
	local channel_name=$2
	local context_name=$3
	local skip_num=$4

	local ret

	"$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context -u \
		-s "$session_name" -c "$channel_name" \
		-t "$context_name"  1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
	ret=$?

	if [ "$ret" == "4" ]; then
		skip 0 "Current UST lib doesn't implement '$context_name' context" "$skip_num"
	else
		ok $ret "Add context command for type: $context_name"
	fi

	return $ret
}

# Only test parsing of the enabling by raw ID
function test_parsing_raw()
{
	TRACE_PATH=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	SESSION_NAME="ust_event_basic"
	CHAN_NAME="mychan"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	enable_ust_lttng_channel_ok $SESSION_NAME $CHAN_NAME

	enable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME $CHAN_NAME

	add_context_ust_skip_ok $SESSION_NAME $CHAN_NAME "perf:thread:raw:r0110:test" 1
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:rZZZ:test"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:b0110:test"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:r0110:"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:r0110::"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:r:test"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:r::"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw::"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw"
	add_context_ust_fail $SESSION_NAME $CHAN_NAME "perf:thread:raw:r0110:test:wrong"

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH
}

function test_event_basic()
{
	TRACE_PATH=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	SESSION_NAME="ust_event_basic"
	CHAN_NAME="mychan"

	local ret

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	enable_ust_lttng_channel_ok $SESSION_NAME $CHAN_NAME

	enable_ust_lttng_event_per_chan $SESSION_NAME $EVENT_NAME $CHAN_NAME

	add_context_ust_skip_ok $SESSION_NAME $CHAN_NAME "perf:thread:page-fault" 4
	ret=$?

	if [ "$ret" != "4" ]; then
		start_lttng_tracing_ok

		$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT >/dev/null 2>&1

		stop_lttng_tracing_ok

		validate_trace "perf_thread_page_fault" "$TRACE_PATH"
	fi

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf "$TRACE_PATH"
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

start_lttng_sessiond

test_parsing_raw

check_skip_kernel_test 8 "Skipping UST perf tests." ||
{
	test_event_basic
}

stop_lttng_sessiond
