3 # Copyright (C) - 2019 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
5 # This library is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 # This is a common test for kernel and ust. DOMAIN must be set.
21 TEST_DESC
="Clear - ${DOMAIN} tracing"
23 CURDIR
=$
(dirname "$0")/
24 TESTDIR
=$CURDIR/..
/..
/..
26 TESTAPP_PATH
="$TESTDIR/utils/testapp"
27 TESTAPP_NAME
="gen-ust-events"
28 TESTAPP_BIN
="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
29 TESTPOINT
=$
(readlink
-f "${CURDIR}/.libs/librelayd_state.so")
32 PAGE_SIZE
=$
(getconf PAGE_SIZE
)
35 source "$TESTDIR/utils/utils.sh"
37 if [ "x$DOMAIN" == "x" ]; then
38 BAIL_OUT
"This script must be called with DOMAIN set."
41 function clean_path
()
44 rm -rf "${trace_path:?}"/*
47 function generate_events
()
51 if [ "$DOMAIN" == "kernel" ]; then
52 taskset
-c "$cpu" echo -n "$nb" > /proc
/lttng-test-filter-event
54 taskset
-c "$cpu" "$TESTAPP_BIN" -i "$nb"
58 function is_viewer_alive
()
61 jobs -p |
grep "$viewer_pid" > /dev
/null
65 function sync_attach_viewer
()
69 local sync_msg
="Viewer attached"
71 while ! grep -q "$sync_msg" "$state_path"; do
72 if ! is_viewer_alive
"$viewer_pid"; then
75 diag
"Waiting for viewer to attach"
80 function saturate_tracefile_rotation
()
82 local session_name
="$1"
83 local channel_name
="$2"
84 local tracefile_count
="$3"
86 local path
="$TRACE_PATH"
87 local num_cpus
="$NUM_CPUS"
88 local events_per_call
=1000
89 local saturation_count
=$
((( tracefile_count
* num_cpus
)))
90 local file_pattern
="${channel_name}_*"
92 # The number of event generation necessary to fill a single tracefile.
93 # We will use this to ensure that the tracefile rotation wrapped around.
94 local minimum_iteration_rotate
=-1
96 local current_tracefile_count
=0
98 while [ "$current_tracefile_count" -lt "$saturation_count" ]; do
99 for i
in $
(seq 0 $
(((num_cpus
- 1)))); do
100 generate_events
"$i" "$events_per_call"
102 # We need to stop & start to ensure that all data touch disk.
103 # This is useful in streaming mode where we do not have
104 # confirmation that data is present on disk otherwise.
105 stop_lttng_tracing_notap
"$session_name"
106 start_lttng_tracing_notap
"$session_name"
107 current_tracefile_count
=$
(find "$path" -name "$file_pattern" -type f \
( ! -iname "*.idx" \
) |
wc -l)
108 if [ "$minimum_iteration_rotate" -eq "-1" ] && \
109 [ "$current_tracefile_count" -ge $
(((2 * num_cpus
))) ]; then
110 minimum_iteration_rotate
="$counter"
112 (( counter
= counter
+ 1 ))
113 diag
"Saturating tracefile rotation: $counter"
116 # Ensure that we wrap around.
117 for j
in $
(seq 0 "$minimum_iteration_rotate"); do
118 for i
in $
(seq 0 $
(((num_cpus
- 1)))); do
119 generate_events
"$i" "$events_per_call"
121 diag
"Saturating tracefile rotation wrap around: $j"
123 stop_lttng_tracing_notap
"$session_name"
124 start_lttng_tracing_notap
"$session_name"
128 # The session must be configured and started before calling this session.
129 # The clear command will be issued before the stop command.
132 local session_name
=$1
136 local nb_events_per_iter
=$5
138 # Generate nb_events_per_iter events that will be flushed to disk on
139 # stop, we want to validate that tracing works. They will also validate
140 # that cleaning of file on disks works.
141 generate_events
0 "$nb_events_per_iter"
142 stop_lttng_tracing_ok
"$session_name"
143 validate_trace_count
"$event_name" "$trace_path" "$nb_events_per_iter"
146 while [[ "$i" -lt "$nb_iters" ]]; do
147 start_lttng_tracing_ok
"$session_name"
148 # Generate $nb_events_per_iter events that will sit in the
149 # buffer. In live mode we cannot control the flushing still we
150 # expect the trace to be empty on the relayd. In case of live we
151 # expect all event to be present on the viewer side.
152 generate_events
0 "$nb_events_per_iter"
154 # Clear the session while active, this take care of both the
155 # buffers and data on disks. Expect an empty trace.
156 lttng_clear_session_ok
"$session_name"
157 stop_lttng_tracing_ok
"$session_name"
158 validate_trace_empty
"$trace_path"
162 # Validate that tracing still works.
163 start_lttng_tracing_ok
"$session_name"
164 generate_events
0 "$nb_events_per_iter"
165 stop_lttng_tracing_ok
"$session_name"
166 validate_trace_count
"$event_name" "$trace_path" "$nb_events_per_iter"
168 # Validate that clear while stopped works.
169 lttng_clear_session_ok
"$session_name"
170 validate_trace_empty
"$trace_path"
173 function test_streaming
()
175 diag
"Test ${DOMAIN} streaming clear"
176 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
177 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME"
178 start_lttng_tracing_ok
"$SESSION_NAME"
179 do_uid
"$SESSION_NAME" "$TRACE_PATH" "$EVENT_NAME" 100 10
180 destroy_lttng_session_ok
"$SESSION_NAME"
183 # We cannot user babeltrace to validate live reception or buffer clearing when
184 # the session is active due to the timing of the live timer that could influence
185 # the number of event the viewer sees or not.
186 function test_streaming_live_with_viewer
()
188 local babeltrace_stdout
189 local babeltrace_stderr
191 local nb_event_per_iter
=1000
192 local events_expected
=$
(((2 + nb_iter
) * nb_event_per_iter
))
193 local url
="net://127.0.0.1/host/${HOSTNAME}/${SESSION_NAME}"
197 babeltrace_stdout
=$
(mktemp
)
198 babeltrace_stderr
=$
(mktemp
)
200 diag
"Test ${DOMAIN} streaming live clear hook viewer before clear"
201 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
"--live"
202 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME"
203 start_lttng_tracing_ok
"$SESSION_NAME"
204 # We use a testpoint inside lttng-relayd as a sync point to validate
205 # that the viewer is attached. We are looking for a string inside
206 # RELAYD_STATE_PATH. We need to bailout if the string is not found and
207 # babeltrace exited. Trash the stdout of babeltrace since we cannot
208 # infer anything from it.
209 $BABELTRACE_BIN -i lttng-live
"$url" 1> /dev
/null
2> "$babeltrace_stderr" &
212 sync_attach_viewer
"$babeltrace_pid" "$RELAYD_STATE_PATH"
214 do_uid
"$SESSION_NAME" "$TRACE_PATH" \
219 # Validate that the viewer is still alive
220 is_viewer_alive
"$babeltrace_pid"
221 ok $?
"Viewer is still alive"
223 # The viewer should detach itself on session destroy
224 destroy_lttng_session_ok
"$SESSION_NAME"
226 wait "$babeltrace_pid"
228 ok
$ret "Babeltrace exited with no error"
229 if [ "$ret" -ne "0" ]; then
230 diag
"$(cat "$babeltrace_stderr")"
233 rm -f "$babeltrace_stderr"
236 function test_streaming_live_attach_viewer_after_clear
()
238 local babeltrace_stdout
239 local babeltrace_stderr
243 local nb_event_per_iter
=100
244 local events_expected
=$nb_event_per_iter
245 local url
="net://127.0.0.1/host/${HOSTNAME}/${SESSION_NAME}"
247 babeltrace_stdout
=$
(mktemp
)
248 babeltrace_stderr
=$
(mktemp
)
250 diag
"Test ${DOMAIN} streaming live hook viewer after clear"
251 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
"--live"
252 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME"
253 start_lttng_tracing_ok
"$SESSION_NAME"
254 do_uid
"$SESSION_NAME" "$TRACE_PATH" \
259 "$BABELTRACE_BIN" -i lttng-live
"$url" 1> "$babeltrace_stdout" 2> "$babeltrace_stderr" &
261 sync_attach_viewer
"$babeltrace_pid" "$RELAYD_STATE_PATH"
263 start_lttng_tracing_ok
"$SESSION_NAME"
265 # We only expect these last events.
266 generate_events
0 "$nb_event_per_iter"
268 stop_lttng_tracing_ok
"$session_name"
269 destroy_lttng_session_ok
"$SESSION_NAME"
271 wait "$babeltrace_pid"
273 ok
$ret "Babeltrace exited with no error"
274 if [ "$ret" -ne "0" ]; then
275 diag
"$(cat "$babeltrace_stderr")"
278 # TODO: sometime the viewer does not see 100 events... not sure why yet.
279 # Does not seems related to clear.
280 #events_seen=$(cat "$babeltrace_stdout" | wc -l)
281 #test $events_seen -eq $events_expected
282 #ok $? "Viewer saw the expected number of event: ${events_seen}/${events_expected}"
284 rm -f "$babeltrace_stdout"
285 rm -f "$babeltrace_stderr"
288 function test_local
()
290 diag
"Test ${DOMAIN} local"
291 create_lttng_session_ok
"$SESSION_NAME" "$TRACE_PATH"
292 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME"
293 start_lttng_tracing_ok
"$SESSION_NAME"
294 do_uid
"$SESSION_NAME" "$TRACE_PATH" "$EVENT_NAME" 100 10
295 destroy_lttng_session_ok
"$SESSION_NAME"
298 function do_snapshot
()
300 local session_name
=$1
303 enable_
"$DOMAIN"_lttng_event_ok
"$session_name" "$EVENT_NAME"
304 start_lttng_tracing_ok
"$session_name"
306 # Generate 10 events that will sit in the buffers.
309 # Take a first snapshot and validate that the events are present.
310 lttng_snapshot_record
"$session_name"
311 stop_lttng_tracing_ok
"$session_name"
312 validate_trace_count
"$EVENT_NAME" "$trace_path" 10
314 # Clean the output path
315 clean_path
"$trace_path"
316 start_lttng_tracing_ok
"$session_name"
318 lttng_clear_session_ok
"$session_name"
320 # Make sure the subsequent snapshot is empty and valid.
321 lttng_snapshot_record
"$session_name"
322 stop_lttng_tracing_ok
"$session_name"
323 validate_trace_empty
"$trace_path"
325 # Clean the output path
326 clean_path
"$trace_path"
327 start_lttng_tracing_ok
"$session_name"
329 # Make sure that everything still works, generate events and take a
332 lttng_snapshot_record
"$session_name"
333 stop_lttng_tracing_ok
"$session_name"
334 validate_trace_count
"$EVENT_NAME" "$trace_path" 10
337 function test_streaming_snapshot
()
339 diag
"Test $DOMAIN streaming snapshot clear"
341 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
"--snapshot"
342 do_snapshot
"$SESSION_NAME" "$TRACE_PATH"
343 destroy_lttng_session_ok
"$SESSION_NAME"
346 function test_local_snapshot
()
348 diag
"Test $DOMAIN local snapshot clear"
350 create_lttng_session_ok
"$SESSION_NAME" "$TRACE_PATH" "--snapshot"
351 do_snapshot
"$SESSION_NAME" "$TRACE_PATH"
352 destroy_lttng_session_ok
"$SESSION_NAME"
355 function test_ust_local_snapshot_per_pid
()
357 local file_sync_before_last
358 local file_sync_before_last_touch
359 local file_sync_before_exit
360 local file_sync_before_exit_touch
361 local channel_name
="channel0"
363 file_sync_before_last
=$
(mktemp
-u)
364 file_sync_before_last_touch
=$
(mktemp
-u)
365 file_sync_before_exit
=$
(mktemp
-u)
366 file_sync_before_exit_touch
=$
(mktemp
-u)
368 diag
"Test ust local snapshot clear per pid "
370 create_lttng_session_ok
"$SESSION_NAME" "$TRACE_PATH" "--snapshot"
371 enable_ust_lttng_channel_ok
"$SESSION_NAME" "$channel_name" "--buffers-pid"
372 enable_ust_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME" "$channel_name"
373 start_lttng_tracing_ok
"$SESSION_NAME"
375 # Generate 10 events that will sit in the buffers.
376 "$TESTAPP_BIN" -i 10 -w 0 \
377 --sync-before-last-event "$file_sync_before_last" \
378 --sync-before-last-event-touch "$file_sync_before_last_touch" \
379 --sync-before-exit "$file_sync_before_exit" \
380 --sync-before-exit-touch "$file_sync_before_exit_touch" >/dev
/null
2>&1 &
382 # Continue only when there is only the last event remaining.
383 while [ ! -f "$file_sync_before_last_touch" ]; do
387 # Take a first snapshot and validate that the events are present.
388 lttng_snapshot_record
"$SESSION_NAME"
389 stop_lttng_tracing_ok
"$SESSION_NAME"
390 validate_trace_count
"$EVENT_NAME" "$TRACE_PATH" 9
392 # Clean the output path
393 clean_path
"$TRACE_PATH"
394 start_lttng_tracing_ok
"$SESSION_NAME"
396 lttng_clear_session_ok
"$SESSION_NAME"
398 # Make sure the subsequent snapshot is empty and valid.
399 lttng_snapshot_record
"$SESSION_NAME"
400 stop_lttng_tracing_ok
"$SESSION_NAME"
401 validate_trace_empty
"$TRACE_PATH"
403 # Validate that tracing still works and subsequent snapshots are valid.
404 # Clean the output path.
405 clean_path
"$TRACE_PATH"
406 start_lttng_tracing_ok
"$SESSION_NAME"
408 # Continue over the last event.
409 touch "$file_sync_before_last"
411 # Wait for the before exit sync point. This ensure that we went over the
413 while [ ! -f "$file_sync_before_exit_touch" ]; do
417 # Make sure the snapshot contains the last event.
418 lttng_snapshot_record
"$SESSION_NAME"
419 stop_lttng_tracing_ok
"$SESSION_NAME"
420 validate_trace_count
"$EVENT_NAME" "$TRACE_PATH" 1
422 # Release the application.
423 touch "$file_sync_before_exit"
425 destroy_lttng_session_ok
"$SESSION_NAME"
427 rm -f "$file_sync_before_last"
428 rm -f "$file_sync_before_last_touch"
429 rm -f "$file_sync_before_exit"
430 rm -f "$file_sync_before_exit_touch"
433 function test_relayd_dissalow_clear
()
435 diag
"Test lttng-relayd disallow clear"
436 start_lttng_relayd
"-o $TRACE_PATH --disallow-clear"
439 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
"--live"
440 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME"
441 start_lttng_tracing_ok
"$SESSION_NAME"
445 lttng_clear_session_fail
"$SESSION_NAME"
447 destroy_lttng_session_ok
"$SESSION_NAME"
453 function test_per_pid
()
455 local channel_name
=chan
456 diag
"Test clear on per-pid session"
457 create_lttng_session_ok
"$SESSION_NAME" "$TRACE_PATH"
458 enable_
"$DOMAIN"_lttng_channel_ok
"$SESSION_NAME" $channel_name --buffers-pid
459 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME" $channel_name
460 start_lttng_tracing_ok
"$SESSION_NAME"
462 # Per pid is not supported in this version. Clear must fail.
463 lttng_clear_session_fail
"$SESSION_NAME"
465 destroy_lttng_session_ok
"$SESSION_NAME"
468 function test_local_tracefile_rotation
()
470 local channel_name
="over9000"
471 local tracefile_count
=6
472 local tracefile_size
=$
(((3 * PAGE_SIZE
)))
475 diag
"Test ${DOMAIN} local with tracefile rotation"
476 create_lttng_session_ok
"$SESSION_NAME" "$TRACE_PATH"
477 enable_
"$DOMAIN"_lttng_channel_ok
"$SESSION_NAME" $channel_name \
478 "--tracefile-count $tracefile_count --tracefile-size $tracefile_size --overwrite"
479 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME" $channel_name
480 start_lttng_tracing_ok
"$SESSION_NAME"
482 # Saturate the tracefile.
483 for i
in $
(seq 0 $iteration); do
484 saturate_tracefile_rotation
"$SESSION_NAME" "$channel_name" "$tracefile_count"
485 lttng_clear_session_ok
"$session_name"
486 stop_lttng_tracing_ok
"$session_name"
487 validate_trace_empty
"$TRACE_PATH"
488 lttng_clear_session_ok
"$session_name"
489 start_lttng_tracing_ok
"$session_name"
492 destroy_lttng_session_ok
"$SESSION_NAME"
495 function test_streaming_tracefile_rotation
()
497 local channel_name
="over9000"
498 local tracefile_count
=6
499 local tracefile_size
=$
(((3 * PAGE_SIZE
)))
502 diag
"Test ${DOMAIN} streamin with tracefile rotation"
503 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
504 enable_
"$DOMAIN"_lttng_channel_ok
"$SESSION_NAME" "$channel_name" \
505 "--tracefile-count $tracefile_count --tracefile-size $tracefile_size --overwrite"
506 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME" "$channel_name"
507 start_lttng_tracing_ok
"$SESSION_NAME"
509 # Saturate the tracefile.
510 for i
in $
(seq 0 $iteration); do
511 saturate_tracefile_rotation
"$SESSION_NAME" "$channel_name" "$tracefile_count"
512 lttng_clear_session_ok
"$session_name"
513 stop_lttng_tracing_ok
"$session_name"
514 validate_trace_empty
"$TRACE_PATH"
515 lttng_clear_session_ok
"$session_name"
516 start_lttng_tracing_ok
"$session_name"
519 destroy_lttng_session_ok
"$SESSION_NAME"
522 function test_streaming_live_tracefile_rotation_with_viewer
()
524 local babeltrace_stderr
525 local channel_name
="over9000"
526 local tracefile_count
=6
527 local tracefile_size
=$
(((3 * PAGE_SIZE
)))
530 local url
="net://127.0.0.1/host/${HOSTNAME}/${SESSION_NAME}"
532 babeltrace_stderr
=$
(mktemp
)
535 diag
"Test ${DOMAIN} streaming live with tracefile rotation viewer attached"
536 create_lttng_session_uri
"$SESSION_NAME" net
://localhost
"--live"
537 enable_
"$DOMAIN"_lttng_channel_ok
"$SESSION_NAME" "$channel_name" \
538 "--tracefile-count $tracefile_count --tracefile-size $tracefile_size --overwrite"
539 enable_
"$DOMAIN"_lttng_event_ok
"$SESSION_NAME" "$EVENT_NAME" "$channel_name"
540 start_lttng_tracing_ok
"$SESSION_NAME"
542 $BABELTRACE_BIN -i lttng-live
"$url" 1> /dev
/null
2> "$babeltrace_stderr" &
545 sync_attach_viewer
"$babeltrace_pid" "$RELAYD_STATE_PATH"
547 # Saturate the tracefile.
548 for i
in $
(seq 0 $iteration); do
549 saturate_tracefile_rotation
"$SESSION_NAME" "$channel_name" "$tracefile_count"
550 lttng_clear_session_ok
"$session_name"
551 stop_lttng_tracing_ok
"$session_name"
552 validate_trace_empty
"$TRACE_PATH"
553 lttng_clear_session_ok
"$session_name"
554 start_lttng_tracing_ok
"$session_name"
557 # Validate that the viewer is still alive.
558 is_viewer_alive
"$babeltrace_pid"
559 ok $?
"Viewer is still alive"
561 # The viewer should detach on destroy.
562 destroy_lttng_session_ok
"$SESSION_NAME"
564 wait "$babeltrace_pid"
566 ok
"$ret" "Babeltrace exited with no error"
567 if [ "$ret" -ne "0" ]; then
568 diag
"$(cat "$babeltrace_stderr")"
571 rm -f "$babeltrace_stderr"
574 if [ "$DOMAIN" == "ust" ]; then
576 elif [ "$DOMAIN" == "kernel" ]; then
579 BAIL_OUT
"Invalid domain: $DOMAIN"
582 plan_tests
$NUM_TESTS
584 print_test_banner
"$TEST_DESC"
589 test_local_tracefile_rotation
591 test_streaming_snapshot
592 test_streaming_tracefile_rotation
593 test_streaming_live_with_viewer
594 test_streaming_live_attach_viewer_after_clear
595 test_streaming_live_tracefile_rotation_with_viewer
598 if [ "$DOMAIN" == "ust" ]; then
599 if [ ! -x "$TESTAPP_BIN" ]; then
600 BAIL_OUT
"No UST events binary detected."
603 test_ust_local_snapshot_per_pid
606 EVENT_NAME
="tp:tptest"
609 if [ "$(id -u)" -ne "0" ]; then
610 skip
0 "Root access is needed. Skipping all kernel streaming tests." $NUM_TESTS && exit 0
612 validate_lttng_modules_present
613 EVENT_NAME
="lttng_test_filter_event"
617 TRACE_PATH
=$
(mktemp
-d)
618 RELAYD_STATE_PATH
=$
(mktemp
)
619 TESTPOINT_ARGS
="RELAYD_STATE_PATH=${RELAYD_STATE_PATH} LTTNG_TESTPOINT_ENABLE=1"
620 LTTNG_RELAYD_ENV_VARS
="$TESTPOINT_ARGS LD_PRELOAD=$TESTPOINT"
622 start_lttng_relayd
"-o $TRACE_PATH"
625 if [ "$DOMAIN" == "kernel" ]; then
630 for fct_test
in "${tests[@]}";
632 SESSION_NAME
=$
(randstring
16 0)
634 clean_path
"$TRACE_PATH"
635 # Truncate the RELAYD_STATE_PATH file
636 : > "$RELAYD_STATE_PATH"
639 if [ "$DOMAIN" == "kernel" ]; then
646 if [ "$DOMAIN" == "ust" ]; then
647 # This test control how lttng-relayd is started. Do it after everything else.
648 test_relayd_dissalow_clear
651 rm -f "$RELAYD_STATE_PATH"