File: test_ust

package info (click to toggle)
ltt-control 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,860 kB
  • sloc: cpp: 192,012; sh: 28,777; ansic: 10,960; python: 7,108; makefile: 3,520; java: 109; xml: 46
file content (959 lines) | stat: -rwxr-xr-x 30,227 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
#!/bin/bash
#
# SPDX-FileCopyrightText: 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Clear - UST tracing"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
EVENT_NAME="tp:tptest"
EVENT_STATE_DUMP_START="lttng_ust_statedump:start"
EVENT_STATE_DUMP_END="lttng_ust_statedump:end"
SESSION_NAME=""
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"

NUM_TESTS=2071
DELAYUS=500000
LIVEVIEWER_TIMEOUT=10  # Timeout in seconds
PAGE_SIZE=$(getconf PAGE_SIZE)
TRACE_PATH=$(mktemp -d -t tmp.test_clear_ust_trace_path.XXXXXX)

# shellcheck source=../utils/utils.sh
source "$TESTDIR/utils/utils.sh"

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST events binary detected."
fi

function clean_path ()
{
	local trace_path=$1
	set -u
	rm -rf $trace_path
	set +u
}

function cond_start_tracing ()
{
	local session_name=$1
	local tracing_active=$2

	if [[ $tracing_active -ne 1 ]]; then
		start_lttng_tracing_ok $session_name
	fi
}

function cond_stop_tracing ()
{
	local session_name=$1
	local tracing_active=$2

	if [[ $tracing_active -ne 1 ]]; then
		stop_lttng_tracing_ok $session_name
	fi
}

function do_clear_session ()
{
	local session_name=$1
	local tracing_active=$2
	local clear_twice=$3
	local rotate_before=$4
	local rotate_after=$5

	cond_stop_tracing $session_name $tracing_active
	if [[ $rotate_before -eq 1 ]]; then
		rotate_session_ok $SESSION_NAME
	fi
	lttng_clear_session_ok $SESSION_NAME
	if [[ $clear_twice -eq 1 ]]; then
		lttng_clear_session_ok $SESSION_NAME
	fi
	if [[ $rotate_after -eq 1 ]]; then
		if [[ $tracing_active -eq 1 ]]; then
			rotate_session_ok $SESSION_NAME
		else
			# Expect failure
			rotate_session_fail $SESSION_NAME
		fi
	fi
	cond_start_tracing $session_name $tracing_active
}

function test_ust_streaming ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local channel_name="chan"

	diag "Test ust streaming clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 10
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count $EVENT_NAME $local_path 10
	else
		if [[ "$buffer_type" == "uid" ]]; then
			validate_trace_empty $local_path
		else	# pid
			validate_directory_empty $local_path
		fi
	fi

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_streaming_no_event ()
{
	local tracing_active=$1
	local clear_twice=$2
	#local rotate_before=$3 ignored
	#local rotate_after=$4
	local buffer_type=uid
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local channel_name="chan"

	diag "Test ust streaming clear no event"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME

	do_clear_session $SESSION_NAME "$tracing_active" "$clear_twice" "$rotate_before" "$rotate_after"
	stop_lttng_tracing_ok $SESSION_NAME

	validate_directory_empty "$local_path"

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_streaming_rotate_clear ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local channel_name="chan"

	diag "Test ust streaming rotate-clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 1
	rotate_session_ok $SESSION_NAME
	$TESTAPP_BIN -i 2
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	$TESTAPP_BIN -i 3
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		local expect_count=6
	else
		local expect_count=4
	fi
	validate_trace_count $EVENT_NAME $local_path $expect_count

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_streaming_clear_rotate ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local channel_name="chan"

	diag "Test ust streaming clear-rotate"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 1
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	$TESTAPP_BIN -i 2
	rotate_session_ok $SESSION_NAME
	$TESTAPP_BIN -i 3
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		local expect_count=6
	else
		local expect_count=5
	fi
	validate_trace_count $EVENT_NAME $local_path $expect_count

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_streaming_live ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local channel_name="chan"

	diag "Test ust streaming live clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost "--live=${DELAYUS}"
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 10
	do_clear_session $SESSION_NAME $tracing_active $clear_twice 0 0
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ "$buffer_type" == "uid" ]]; then
		validate_trace_empty $local_path
	else	# pid
		validate_directory_empty $local_path
	fi

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

#no clear
function test_ust_basic_streaming_live_viewer ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local remote_trace_path="${HOSTNAME}/${SESSION_NAME}"
	local channel_name="chan"
	local bt_output_path=$(mktemp -u -t tmp.test_${FUNCNAME[0]}_bt_output_path.XXXXXX)
	local file_sync_before_exit=$(mktemp -u -t tmp.test_${FUNCNAME[0]}_sync_before_exit.XXXXXX)

	diag "Test ust basic streaming live with viewer"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost "--live=${DELAYUS}"
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME

	wait_live_trace_ready net://localhost

	# Connect a live viewer
	_run_babeltrace_cmd -i lttng-live net://localhost/host/$remote_trace_path --params=session-not-found-action=end 1> $bt_output_path &
	local viewer_pid=$!
	local viewer_iter=0

	wait_live_viewer_connect net://localhost

	$TESTAPP_BIN -i 10 --sync-before-exit $file_sync_before_exit &
	local app_pid=$!

	diag "Wait until viewer sees all 10 expected events"
	local evcount=0
	while [ $evcount -ne 10 ]; do
		evcount=$(cat $bt_output_path | wc -l)
		sleep 0.1
		viewer_iter=$((viewer_iter + 1))
		if [ "${viewer_iter}" -gt $((LIVEVIEWER_TIMEOUT * 10)) ] ; then
			break;
		fi
	done
	is "${evcount}" 10 "Live viewer read $evcount events, expect 10"

	destroy_lttng_session_ok $SESSION_NAME --no-wait
	touch $file_sync_before_exit
	diag "Waiting for application to exit"
	wait $app_pid
	pass "Wait for application to exit"
	diag "Wait for viewer to exit"
	wait $viewer_pid
	ok $? "Babeltrace succeeds"
	pass "Wait for viewer to exit"

	rm -f $bt_output_path
	rm -f $file_sync_before_exit
}

function test_ust_streaming_live_viewer ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local remote_trace_path="${HOSTNAME}/${SESSION_NAME}"
	local channel_name="chan"
	local bt_output_path=$(mktemp -u -t tmp.test_${FUNCNAME[0]}_bt_output_path.XXXXXX)

	diag "Test ust streaming live clear with viewer"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost "--live=${DELAYUS}"
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME

	wait_live_trace_ready net://localhost

	# Connect a live viewer
	_run_babeltrace_cmd -i lttng-live net://localhost/host/$remote_trace_path --params=session-not-found-action=end 1> $bt_output_path &
	local viewer_pid=$!

	wait_live_viewer_connect net://localhost

	$TESTAPP_BIN -i 10
	do_clear_session $SESSION_NAME $tracing_active $clear_twice 0 0
	stop_lttng_tracing_ok $SESSION_NAME

	destroy_lttng_session_ok $SESSION_NAME --no-wait
	diag "Wait for viewer to exit"
	wait $viewer_pid
	ok $? "Babeltrace succeeds"
	pass "Wait for viewer to exit"

	clean_path $bt_output_path
}

function test_ust_streaming_live_viewer_new_metadata_after_clear ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused
	local buffer_type=$5
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"
	local remote_trace_path="${HOSTNAME}/${SESSION_NAME}"
	local channel_name="chan"

	local bt_output_path=$(mktemp -t "tmp.${FUNCNAME[0]}_bt_output.XXXXXX")
	local file_sync_before_exit=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit.XXXXXX")

	diag "Test ust streaming live clear with viewer with new metadata after clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"
	create_lttng_session_uri "$SESSION_NAME" net://localhost "--live=${DELAYUS}"
	enable_ust_lttng_channel_ok "$SESSION_NAME" $channel_name "--buffers-$buffer_type"

	# The vpid context is added to filter events based on the vpid of the
	# test application as state dump events are used. Regenerating a
	# state dump will cause other instrumented application on the system
	# to trigger a state dump which would throw off checks that rely on an
	# event count.
	add_context_ust_ok "$SESSION_NAME" $channel_name "vpid"
	enable_ust_lttng_event_ok "$SESSION_NAME" $EVENT_NAME $channel_name
	start_lttng_tracing_ok "$SESSION_NAME"

	wait_live_trace_ready net://localhost

	# Connect a live viewer
	_run_babeltrace_cmd -i lttng-live "net://localhost/host/$remote_trace_path" --params=session-not-found-action=end 1> "$bt_output_path" &
	local viewer_pid=$!
	local viewer_iter=0

	wait_live_viewer_connect net://localhost

	$TESTAPP_BIN -i 10 --sync-before-exit "$file_sync_before_exit" &
	local app_pid=$!

	diag "Wait until viewer sees all 10 expected events"
	local evcount=0
	while [ $evcount -ne 10 ]; do
		evcount=$(wc -l < "$bt_output_path")
		sleep 0.1
		viewer_iter=$((viewer_iter + 1))
		if [ "${viewer_iter}" -gt $((LIVEVIEWER_TIMEOUT * 10)) ] ; then
			break
		fi
	done
	is "${evcount}" 10 "Live viewer read $evcount events, expect 10"

	do_clear_session "$SESSION_NAME" "$tracing_active" "$clear_twice" 0 0

	# Enable new events which will add their descriptions to the metadata
	# file. This validates that, following a clear, the relay daemon rotates
	# the metadata viewer stream to	the new metadata file.
	enable_ust_lttng_event_filter "$SESSION_NAME" $EVENT_STATE_DUMP_START "\$ctx.vpid == $app_pid" $channel_name
	enable_ust_lttng_event_filter "$SESSION_NAME" $EVENT_STATE_DUMP_END "\$ctx.vpid == $app_pid" $channel_name

	# Forcing a state dump to produce the two events enabled above
	regenerate_statedump_ok "$SESSION_NAME"

	diag "Wait until viewer sees all 12 expected events"
	local evcount=0
	local viewer_iter=0
	while [ $evcount -ne 12 ]; do
		evcount=$(wc -l < "$bt_output_path")
		sleep 0.1
		viewer_iter=$((viewer_iter + 1))
		if [ "${viewer_iter}" -gt $((LIVEVIEWER_TIMEOUT * 10)) ]; then
			break
		fi
	done
	is "${evcount}" 12 "Live viewer read $evcount events, expect 12"

	stop_lttng_tracing_ok "$SESSION_NAME"

	destroy_lttng_session_ok "$SESSION_NAME" --no-wait

	touch "$file_sync_before_exit"
	diag "Waiting for application to exit"
	wait $app_pid
	pass "Wait for application to exit"

	diag "Wait for viewer to exit"
	wait $viewer_pid
	ok $? "Babeltrace succeeds"
	pass "Wait for viewer to exit"

	clean_path "$bt_output_path"
	rm -f "$file_sync_before_exit"
}

function test_ust_local ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local channel_name="chan"

	diag "Test ust local"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 10
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count $EVENT_NAME $TRACE_PATH 10
	else
		if [[ "$buffer_type" == "uid" ]]; then
			validate_trace_empty $TRACE_PATH
		else	# pid

			# The sessiond always created a `ust/ directory
			# whenever the UST domain is active
			validate_directory_empty $TRACE_PATH/ust/
		fi
	fi

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_local_no_event ()
{
	local tracing_active=$1
	local clear_twice=$2
	#local rotate_before=$3 ignored
	#local rotate_after=$4 ignored
	local buffer_type=$5
	local channel_name="chan"

	diag "Test ust local no event"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_ok $SESSION_NAME "$TRACE_PATH"
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name "--buffers-$buffer_type"
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME

	do_clear_session $SESSION_NAME "$tracing_active" "$clear_twice" "$rotate_before" "$rotate_after"
	stop_lttng_tracing_ok $SESSION_NAME

	validate_directory_empty "$TRACE_PATH"

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_local_rotate_clear ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local channel_name="chan"

	diag "Test ust local rotate-clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 1
	rotate_session_ok $SESSION_NAME
	$TESTAPP_BIN -i 2
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	$TESTAPP_BIN -i 3
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		local expect_count=6
	else
		local expect_count=4
	fi
	validate_trace_count $EVENT_NAME $TRACE_PATH $expect_count

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_local_clear_rotate ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local channel_name="chan"

	diag "Test ust local clear-rotate"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 1
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	$TESTAPP_BIN -i 2
	rotate_session_ok $SESSION_NAME
	$TESTAPP_BIN -i 3
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		local expect_count=6
	else
		local expect_count=5
	fi
	validate_trace_count $EVENT_NAME $TRACE_PATH $expect_count

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function do_ust_snapshot ()
{
	local session_name=$1
	local trace_path=$2
	local tracing_active=$3
	local clear_twice=$4
	local buffer_type=$5
	local channel_name="snapshot"

	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $session_name $EVENT_NAME $channel_name
	start_lttng_tracing_ok $session_name

	# Generate 10 events that will sit in the buffers.
	$TESTAPP_BIN -i 10

	# Take a first snapshot and validate that the events are present.
	lttng_snapshot_record $session_name
	stop_lttng_tracing_ok $session_name
	validate_trace_count $EVENT_NAME $trace_path 10

	# Clean the output path
	clean_path $trace_path
	start_lttng_tracing_ok $session_name

	do_clear_session $SESSION_NAME $tracing_active $clear_twice 0 0

	# Make sure the subsequent snapshot is empty and valid.
	lttng_snapshot_record $session_name
	stop_lttng_tracing_ok $session_name
	validate_trace_empty $trace_path

	# Clean the output path
	clean_path $trace_path
	start_lttng_tracing_ok $session_name

	# Make sure that everything still works, generate events and take a
	# snapshot.
	$TESTAPP_BIN -i 10
	lttng_snapshot_record $session_name
	stop_lttng_tracing_ok $session_name
	validate_trace_count $EVENT_NAME $trace_path 10
}

function test_ust_streaming_snapshot ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused.
	local buffer_type=$5

	diag "Test ust streaming snapshot clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"

	create_lttng_session_uri $SESSION_NAME net://localhost "--snapshot"
	do_ust_snapshot $SESSION_NAME $TRACE_PATH $tracing_active $clear_twice $buffer_type
	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_local_snapshot ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused.
	local buffer_type=$5

	diag "Test ust local snapshot clear"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH "--snapshot"
	do_ust_snapshot $SESSION_NAME $TRACE_PATH $tracing_active $clear_twice $buffer_type
	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

# snapshot for per-pid is tested independently of the "buffer type" parameter
# because an application needs to be live to appear in a snapshot.
function test_ust_local_snapshot_per_pid ()
{
	local tracing_active=$1
	local clear_twice=$2
	# 3, 4 unused.
	local buffer_type=$5
	local channel_name="channel0"
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")
	local file_sync_before_last_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last_touch.XXXXXX")
	local file_sync_before_exit=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit.XXXXXX")
	local file_sync_before_exit_touch=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_exit_touch.XXXXXX")

	diag "Test ust local snapshot clear per pid"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, buffer_type=$buffer_type"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH "--snapshot"
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME

	# Generate 10 events that will sit in the buffers.
	$TESTAPP_BIN -i 10 -w 0 \
		--sync-before-last-event ${file_sync_before_last} \
		--sync-before-last-event-touch ${file_sync_before_last_touch} \
		--sync-before-exit ${file_sync_before_exit} \
		--sync-before-exit-touch ${file_sync_before_exit_touch} >/dev/null 2>&1 &
	local app_pid="${!}"

	# Continue only when there is only the last event remaining.
	while [ ! -f "${file_sync_before_last_touch}" ]; do
		sleep 0.1
	done

	# Take a first snapshot and validate that the events are present.
	lttng_snapshot_record $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME
	validate_trace_count $EVENT_NAME $TRACE_PATH 9

	# Clean the output path
	clean_path $TRACE_PATH
	start_lttng_tracing_ok $SESSION_NAME

	do_clear_session $SESSION_NAME $tracing_active $clear_twice 0 0

	# Make sure the subsequent snapshot is empty and valid.
	lttng_snapshot_record $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME
	validate_trace_empty $TRACE_PATH

	# Validate that tracing still works and subsequent snapshots are valid.
	# Clean the output path.
	clean_path $TRACE_PATH
	start_lttng_tracing_ok $SESSION_NAME

	# Continue over the last event.
	touch ${file_sync_before_last}

	# Wait for the before exit sync point. This ensure that we went over the
	# last tracepoint.
	while [ ! -f "${file_sync_before_exit_touch}" ]; do
		sleep 0.1
	done

	# Make sure the snapshot contains the last event.
	lttng_snapshot_record $SESSION_NAME
	stop_lttng_tracing_ok $SESSION_NAME
	validate_trace_count $EVENT_NAME $TRACE_PATH 1

	# Release the application.
	touch ${file_sync_before_exit}
	wait "${app_pid}"
	destroy_lttng_session_ok $SESSION_NAME --no-wait

	rm -f ${file_sync_before_last}
	rm -f ${file_sync_before_last_touch}
	rm -f ${file_sync_before_exit}
	rm -f ${file_sync_before_exit_touch}
}

function test_ust_streaming_tracefile_rotation ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local channel_name="rotchan"
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"

	diag "Test ust streaming clear with tracefile rotation"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --subbuf-size=$PAGE_SIZE \
		--tracefile-size=$PAGE_SIZE --tracefile-count=2 --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 10
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count $EVENT_NAME $local_path 10
	else
		if [[ "$buffer_type" == "uid" ]]; then
			validate_trace_empty $local_path
		else	# pid
			validate_directory_empty $local_path
		fi
	fi

	start_lttng_tracing_ok $SESSION_NAME
	$TESTAPP_BIN -i 20
	stop_lttng_tracing_ok

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count $EVENT_NAME $local_path 30
	else
		validate_trace_count $EVENT_NAME $local_path 20
	fi

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

# With 1 byte per event (as strict minimum), generating 200000 events
# guarantees filling up 2 files of 64k in size, which is the maximum
# page size known on Linux
function test_ust_streaming_tracefile_rotation_overwrite_files ()
{
	local tracing_active=$1
	local clear_twice=$2
	local rotate_before=$3
	local rotate_after=$4
	local buffer_type=$5
	local channel_name="rotchan"
	local local_path="${TRACE_PATH}/${HOSTNAME}/${SESSION_NAME}*"

	diag "Test ust streaming clear with tracefile rotation, overwrite files"
	diag "Parameters: tracing_active=$tracing_active, clear_twice=$clear_twice, rotate_before=$rotate_before, rotate_after=$rotate_after, buffer_type=$buffer_type"
	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_channel_ok $SESSION_NAME $channel_name --subbuf-size=$PAGE_SIZE \
		--tracefile-size=$PAGE_SIZE --tracefile-count=2 --buffers-$buffer_type
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $channel_name
	start_lttng_tracing_ok $SESSION_NAME
	retry_anycpu_taskset $TESTAPP_BIN -i 200000
	do_clear_session $SESSION_NAME $tracing_active $clear_twice $rotate_before $rotate_after
	stop_lttng_tracing_ok $SESSION_NAME

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count_range_incl_min_excl_max $EVENT_NAME $local_path 1 200000
	else
		if [[ "$buffer_type" == "uid" ]]; then
			validate_trace_empty $local_path
		else	# pid
			validate_directory_empty $local_path
		fi
	fi

	start_lttng_tracing_ok $SESSION_NAME
	retry_anycpu_taskset $TESTAPP_BIN -i 400000
	stop_lttng_tracing_ok

	if [[ $rotate_before -eq 1 ]]; then
		validate_trace_count_range_incl_min_excl_max $EVENT_NAME $local_path 1 600000
	else
		validate_trace_count_range_incl_min_excl_max $EVENT_NAME $local_path 1 200000
	fi

	destroy_lttng_session_ok $SESSION_NAME --no-wait
}

function test_ust_disallow_clear ()
{
	diag "Test ust disallow clear on relay daemon"
	SESSION_NAME=$(randstring 16 0)

	LTTNG_RELAYD_DISALLOW_CLEAR=1 start_lttng_relayd "-o $TRACE_PATH"

	start_lttng_sessiond

	create_lttng_session_uri $SESSION_NAME net://localhost
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME
	start_lttng_tracing_ok $SESSION_NAME
	lttng_clear_session_fail $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME --no-wait

	stop_lttng_sessiond
	stop_lttng_relayd
	clean_path $TRACE_PATH
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

bail_out_if_no_babeltrace

streaming_tests=(test_ust_streaming
	test_ust_streaming_rotate_clear
	test_ust_streaming_clear_rotate
	test_ust_streaming_tracefile_rotation
	test_ust_streaming_tracefile_rotation_overwrite_files
	test_ust_streaming_no_event
)

live_tests=(test_ust_streaming_live
	test_ust_basic_streaming_live_viewer
	test_ust_streaming_live_viewer
	test_ust_streaming_live_viewer_new_metadata_after_clear
)

local_tests=(test_ust_local
	test_ust_local_rotate_clear
	test_ust_local_clear_rotate
	test_ust_local_no_event
)

snapshot_uid_tests=(test_ust_streaming_snapshot
	test_ust_local_snapshot
)

snapshot_pid_tests=(test_ust_local_snapshot_per_pid)

start_lttng_relayd "-o $TRACE_PATH"
start_lttng_sessiond

# Per-UID buffers

# Clear with tracing active, clear once
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_uid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 1 0 0 0 uid
	clean_path $TRACE_PATH
done

# Clear with tracing active, clear twice
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_uid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 1 1 0 0 uid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear once
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_uid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 0 0 uid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear twice
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_uid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 1 0 0 uid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, rotate-clear once
for fct_test in ${streaming_tests[@]} ${local_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 1 0 uid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear once-rotate(fail)
for fct_test in ${streaming_tests[@]} ${local_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 0 1 uid
	clean_path $TRACE_PATH
done


# Per-PID buffers.

# Clear with tracing active, clear once
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_pid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 1 0 0 0 pid
	clean_path $TRACE_PATH
done

# Clear with tracing active, clear twice
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_pid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 1 1 0 0 pid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear once
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_pid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 0 0 pid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear twice
for fct_test in ${streaming_tests[@]} ${live_tests[@]} ${local_tests[@]} ${snapshot_pid_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 1 0 0 pid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, rotate-clear once
for fct_test in ${streaming_tests[@]} ${local_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 1 0 pid
	clean_path $TRACE_PATH
done

# Clear with tracing inactive, clear once-rotate(fail)
for fct_test in ${streaming_tests[@]} ${local_tests[@]};
do
	SESSION_NAME=$(randstring 16 0)
	${fct_test} 0 0 0 1 pid
	clean_path $TRACE_PATH
done

stop_lttng_sessiond
stop_lttng_relayd

test_ust_disallow_clear

rm -rf "$TRACE_PATH"