+#!/bin/bash
+#
+# Copyright (C) - 2013 Julien Desfossez <jdesfossez@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="Snapshots - UST tracing"
+
+CURDIR=$(dirname $0)/
+TESTDIR=$CURDIR/../../..
+EVENT_NAME="tp:tptest"
+PID_RELAYD=0
+SESSION_NAME=""
+CHANNEL_NAME="snapchan"
+TESTAPP_PATH="$TESTDIR/utils/testapp"
+TESTAPP_NAME="gen-ust-events"
+TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
+NR_ITER=2000000
+NR_USEC_WAIT=100
+
+NUM_TESTS=76
+
+TRACE_PATH=$(mktemp -d)
+
+source $TESTDIR/utils/utils.sh
+
+if [ ! -x "$TESTAPP_BIN" ]; then
+ BAIL_OUT "No UST events binary detected."
+fi
+
+# Need the number of snapshot to do.
+if [ -z $1 ]; then
+ BAIL_OUT "A number of snapshot is needed."
+fi
+NR_SNAPSHOT=$1
+
+NUM_TESTS=$(($NUM_TESTS + ($NR_SNAPSHOT * 2)))
+
+function start_test_app()
+{
+ local tmp_file="/tmp/lttng_test_ust.42.file"
+
+ # Start application with a temporary file.
+ $TESTAPP_BIN $NR_ITER $NR_USEC_WAIT $tmp_file &
+ ok $? "Start application to trace"
+
+ # Wait for the application file to appear indicating that at least one
+ # tracepoint has been fired.
+ while [ ! -f "$tmp_file" ]; do
+ sleep 0.5
+ done
+ diag "Removing test app temporary file $tmp_file"
+ rm -f $tmp_file
+}
+
+function kill_test_app()
+{
+ diag "Killing $TESTAPP_NAME"
+ PID_APP=`pidof $TESTAPP_NAME`
+ kill $PID_APP >/dev/null 2>&1
+}
+
+function snapshot_add_output ()
+{
+ local sess_name=$1
+ local trace_path=$2
+ local name=$3
+ local max_size=$4
+ local extra_opt=""
+
+ if [ ! -z $name ]; then
+ extra_opt+=" -n $name "
+ fi
+
+ if [ ! -z $max_size ]; then
+ extra_opt+=" -m $max_size "
+ fi
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output \
+ -s $sess_name $extra_opt $trace_path > /dev/null 2>&1
+
+ ok $? "Added snapshot output $trace_path ($extra_opt)"
+}
+
+function snapshot_del_output ()
+{
+ local sess_name=$1
+ local name=$2
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output \
+ -s $sess_name $name > /dev/null 2>&1
+
+ ok $? "Deleted snapshot output named $name"
+}
+
+function enable_mmap_overwrite_subbuf_ust_channel ()
+{
+ local sess_name=$1
+ local chan_name=$2
+ local subbuf_size=$3
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name \
+ $chan_name -u --output mmap --overwrite \
+ --subbuf-size $subbuf_size > /dev/null 2>&1
+
+ ok $? "Enable channel $channel_name for session $sess_name with subbuf size $subbuf_size"
+}
+
+
+function test_ust_list_output ()
+{
+ output_names=("randomname" "somesnapshot")
+
+ diag "Test UST snapshot output listing"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_lttng_mmap_overwrite_ust_channel $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+
+ start_lttng_tracing $SESSION_NAME
+
+ snapshot_add_output $SESSION_NAME "file://$TRACE_PATH" ${output_names[0]}
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot list-output \
+ -s $SESSION_NAME 2>&1 | grep ${output_names[0]} > /dev/null
+ ok $? "Snapshot named ${output_names[0]} present in list-output listing"
+
+ snapshot_del_output $SESSION_NAME ${output_names[0]}
+
+ snapshot_add_output $SESSION_NAME "file://$TRACE_PATH" ${output_names[1]}
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot list-output \
+ -s $SESSION_NAME 2>&1 | grep ${output_names[1]} > /dev/null
+
+ ok $? "Snapshot named ${output_names[1]} present in list-output listing"
+
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+}
+
+function test_ust_local_snapshot ()
+{
+ diag "Test local UST snapshots"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_lttng_mmap_overwrite_ust_channel $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+ lttng_snapshot_add_output $SESSION_NAME $TRACE_PATH
+
+ # Returns once the application has at least fired ONE tracepoint.
+ start_test_app
+
+ lttng_snapshot_record $SESSION_NAME
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ # Validate test
+ validate_trace $EVENT_NAME $TRACE_PATH/
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ else
+ break
+ fi
+
+ kill_test_app
+}
+
+function test_ust_local_snapshot_max_size ()
+{
+ subbuf_size=8192
+ num_cpus=`nproc`
+
+ # The minimum size limit is min(subbuf_size) * nb_streams
+ max_size=$(($subbuf_size*$num_cpus))
+
+ diag "Test local UST snapshots with max size $max_size"
+ create_lttng_session_no_output $SESSION_NAME
+
+ enable_mmap_overwrite_subbuf_ust_channel $SESSION_NAME $CHANNEL_NAME $subbuf_size
+
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+
+ snapshot_add_output $SESSION_NAME "file://$TRACE_PATH" "" $max_size
+
+ # Returns once the application has at least fired ONE tracepoint.
+ start_test_app
+
+ lttng_snapshot_record $SESSION_NAME
+
+ # Check file size
+ sum_size_tracefiles=$(find $TRACE_PATH -name "${CHANNEL_NAME}_*" \
+ -exec stat -c '%s' {} \; | awk '{s = s + $1}END{print s}')
+
+ if [ "$sum_size_tracefiles" -gt "$max_size" ]; then
+ fail "Tracefiles size sum validation"
+ diag "Tracefiles size sum: $sum_size_tracefiles Expected max: $max_size"
+ fi
+
+ pass "Tracefiles size sum validation"
+
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ # Validate test
+ validate_trace $EVENT_NAME $TRACE_PATH/
+
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ fi
+
+ kill_test_app
+}
+
+function test_ust_local_snapshot_large_metadata ()
+{
+ LM_EVENT="tp:tptest1,tp:tptest2,tp:tptest3,tp:tptest4,tp:tptest5"
+ LM_PATH="$TESTDIR/utils/testapp"
+ LM_NAME="gen-ust-nevents"
+ LM_BIN="$LM_PATH/$LM_NAME/$LM_NAME"
+
+ diag "Test local UST snapshots with > 4kB metadata"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_lttng_mmap_overwrite_ust_channel $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $LM_EVENT $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+ lttng_snapshot_add_output $SESSION_NAME $TRACE_PATH
+ $LM_BIN 1 1
+ ok $? "Start application to trace"
+ lttng_snapshot_record $SESSION_NAME
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ # Validate test
+ validate_trace $LM_EVENT $TRACE_PATH/
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ else
+ break
+ fi
+}
+
+function enable_channel_per_uid_mmap_overwrite()
+{
+ sess_name=$1
+ channel_name=$2
+
+ $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel --buffers-uid -u $channel_name -s $sess_name --output mmap --overwrite >/dev/null 2>&1
+ ok $? "Enable channel $channel_name per UID for session $sess_name"
+}
+
+function test_ust_per_uid_local_snapshot ()
+{
+ diag "Test local UST snapshots"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_channel_per_uid_mmap_overwrite $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+ lttng_snapshot_add_output $SESSION_NAME $TRACE_PATH
+
+ # Returns once the application has at least fired ONE tracepoint.
+ start_test_app
+
+ lttng_snapshot_record $SESSION_NAME
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ # Validate test
+ validate_trace $EVENT_NAME $TRACE_PATH/
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ else
+ break
+ fi
+
+ kill_test_app
+}
+
+function test_ust_per_uid_local_snapshot_post_mortem ()
+{
+ diag "Test local UST snapshots post-mortem"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_channel_per_uid_mmap_overwrite $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+ lttng_snapshot_add_output $SESSION_NAME $TRACE_PATH
+
+ # Returns once the application has at least fired ONE tracepoint.
+ start_test_app
+ kill_test_app
+
+ lttng_snapshot_record $SESSION_NAME
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ # Validate test
+ validate_trace $EVENT_NAME $TRACE_PATH/
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ else
+ break
+ fi
+}
+
+function test_ust_local_snapshots ()
+{
+ diag "Test $NR_SNAPSHOT local UST snapshots"
+ create_lttng_session_no_output $SESSION_NAME
+ enable_lttng_mmap_overwrite_ust_channel $SESSION_NAME $CHANNEL_NAME
+ enable_ust_lttng_event $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
+ start_lttng_tracing $SESSION_NAME
+ lttng_snapshot_add_output $SESSION_NAME $TRACE_PATH
+
+ # Returns once the application has at least fired ONE tracepoint.
+ start_test_app
+
+ for i in $(seq 1 $NR_SNAPSHOT); do
+ diag "Snapshot $i/$NR_SNAPSHOT"
+ rm -rf $TRACE_PATH/snapshot/* 2>/dev/null
+ lttng_snapshot_record $SESSION_NAME
+ # Validate test
+ validate_trace $EVENT_NAME $TRACE_PATH/
+ if [ $? -eq 0 ]; then
+ # Only delete if successful
+ rm -rf $TRACE_PATH
+ else
+ break
+ fi
+ done
+ stop_lttng_tracing $SESSION_NAME
+ destroy_lttng_session $SESSION_NAME
+
+ kill_test_app
+}
+
+plan_tests $NUM_TESTS
+
+print_test_banner "$TEST_DESC"
+
+start_lttng_sessiond
+
+tests=( test_ust_list_output
+ test_ust_local_snapshot
+ test_ust_local_snapshot_max_size
+ test_ust_per_uid_local_snapshot
+ test_ust_per_uid_local_snapshot_post_mortem
+ test_ust_local_snapshot_large_metadata
+ test_ust_local_snapshots)
+
+for fct_test in ${tests[@]};
+do
+ SESSION_NAME=$(randstring 16 0)
+ ${fct_test}
+done
+
+stop_lttng_sessiond