liblttng-ctl: use lttng_payload for serialize/create_from_buffer
[lttng-tools.git] / tests / regression / tools / notification / test_notification_multi_app
CommitLineData
434f8068
JR
1#!/bin/bash
2#
9d16b343 3# Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficiso.com>>
434f8068 4#
9d16b343 5# SPDX-License-Identifier: LGPL-2.1-only
434f8068
JR
6
7TEST_DESC="Notification"
8
9CURDIR=$(dirname $0)/
10TESTDIR=$CURDIR/../../../
11
434f8068
JR
12TESTPOINT=$(readlink -f ${CURDIR}/.libs/libpause_consumer.so)
13
14TESTAPP_PATH="$TESTDIR/utils/testapp"
15TESTAPP_NAME="gen-ust-events"
16TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
854382b8 17TESTAPP_STATE_FILE="$(mktemp -u)"
434f8068 18
854382b8 19NR_ITER=1000
434f8068
JR
20NR_USEC_WAIT=5
21
22SESSION_NAME="my_session"
854382b8 23CHANNEL_NAME="my_channel"
434f8068 24
434f8068 25
434f8068
JR
26DIR=$(readlink -f $TESTDIR)
27
1bd26228
JG
28PAGE_SIZE=$(getconf PAGE_SIZE)
29
854382b8
JR
30NUM_TEST_UST=50
31NUM_TEST_KERNEL=50
32NUM_TESTS=$(($NUM_TEST_UST + $NUM_TEST_KERNEL))
434f8068
JR
33
34source $TESTDIR/utils/utils.sh
35
36consumerd_pipe=()
37file_sync_after_first_event=$(mktemp -u)
38
39# MUST set TESTDIR before calling those functions
40plan_tests $NUM_TESTS
41
42print_test_banner "$TEST_DESC"
43
44app_pids=()
867313e2 45
cb723729 46function kernel_event_generator_toggle_state
854382b8
JR
47{
48 kernel_event_generator_suspended=$((kernel_event_generator_suspended==0))
49
50}
51function kernel_event_generator
52{
53 state_file=$1
54 kernel_event_generator_suspended=0
cb723729 55 trap kernel_event_generator_toggle_state SIGUSR1
3be453c9 56
854382b8
JR
57 while (true); do
58 if [[ $kernel_event_generator_suspended -eq "1" ]]; then
59 touch $state_file
60 sleep 0.5
61 else
62 if [[ -f $state_file ]]; then
63 rm $state_file 2> /dev/null
64 fi
65 echo -n "1000" > /proc/lttng-test-filter-event
66 fi
67 done
68}
69
cb723729 70function ust_event_generator_toggle_state
854382b8
JR
71{
72 ust_event_generator_suspended=$((ust_event_generator_suspended==0))
73
74}
75function ust_event_generator
76{
77 state_file=$1
78 ust_event_generator_suspended=0
cb723729 79 trap ust_event_generator_toggle_state SIGUSR1
854382b8
JR
80 trap "exit" SIGTERM SIGINT
81 while (true); do
82 if [[ $ust_event_generator_suspended -eq "1" ]]; then
83 touch $state_file
84 sleep 0.5
85 else
86 if [[ -f $state_file ]]; then
87 rm $state_file 2> /dev/null
88 fi
6c4a91d6 89 taskset -c 0 $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT > /dev/null 2>&1
854382b8
JR
90 fi
91 done
92}
93
434f8068
JR
94function start_client {
95 local pid=-1
96 local output_file=$1
97 local session_name=$2
98 local channel_name=$3
99 local domain_type=$4
100 local buffer_usage_type=$5
101 local buffer_usage_threshold_type=$6
102 local buffer_usage_threshold_value=$7
103 local nr_expected_notification=$8
104
105 ${CURDIR}/base_client ${session_name} ${channel_name} ${domain_type} ${buffer_usage_type} ${buffer_usage_threshold_type} ${buffer_usage_threshold_value} ${nr_expected_notification} > ${output_file} &
106 pid=$!
107
108 app_pids+=("$pid")
109}
110
111function wait_for_message ()
112{
345ed219
JR
113 local directory=$1
114 local file_pattern=$2
115 local message=$3
434f8068 116
345ed219 117 for file in $directory/${file_pattern}*; do
434f8068
JR
118 while(true); do
119 # Check for "error" message
120 grep -q "error:" ${file}
121 app_error=$?
122 if [ $app_error -eq "0" ] ; then
123 # An error occurred
124 fail "Waiting message: error logged see file content: ${message}, ${file}"
125 return 1
126 fi
127
128 grep -q "${message}" ${file}
129 if [[ "$?" -ne "0" ]]; then
130 # Lookup failed restart loop
131 diag "Lookup failed sleep and retry grep for: ${message}, ${file}"
132 sleep 0.25
133 continue
134 fi
135 break
136 done
137 done
138 pass "Message received: ${message}"
139 return 0
140}
141
142function print_errors ()
143{
345ed219
JR
144 local directory=$1
145 local file_pattern=$2
434f8068 146
345ed219 147 for file in $directory/${file_pattern}*; do
434f8068
JR
148 # Check for "error" message
149 error_message=$(grep "error:" ${file})
867313e2 150 if [[ "${error_message}x" != "x" ]]; then
434f8068
JR
151 diag "Errors for application ${file}:"
152 diag "${error_message}"
153 fi
154 done
155}
156
157function comm_consumerd ()
158{
159 local message=$1
160 local pipe=$2
161 echo -ne "${message}" > "${pipe}"
434f8068
JR
162}
163
164function stop_consumerd ()
165{
166 local pipe=$1
167 comm_consumerd "1" "$pipe"
434f8068
JR
168}
169
170function resume_consumerd ()
171{
172 local pipe=$1
173 comm_consumerd "\0" "$pipe"
434f8068
JR
174}
175
176function test_multi_app ()
177{
854382b8
JR
178 local domain_type=$1
179 local event_generator_pid=$2
180
434f8068
JR
181 local app_pids=()
182 local low_output_file_pattern="low_app_output_file_"
183 local high_output_file_pattern="high_app_output_file_"
345ed219 184 local output_dir=$(mktemp -d)
434f8068 185
345ed219 186 local testpoint_base_path=$(readlink -f "$output_dir/lttng.t_p_n_multi_app")
6a1eee92
JR
187 local testpoint_pipe_path=$(mktemp -u "${testpoint_base_path}.XXXXXX")
188
189 local nr_notification_expected=5
190 local nr_client_app=50
854382b8
JR
191 local domain_string=""
192 local event_name=""
193
194 case $domain_type in
195 ust)
196 domain_string=LTTNG_DOMAIN_UST
197 event_name="tp:tptest"
198 ;;
199 kernel)
200 domain_string=LTTNG_DOMAIN_KERNEL
201 event_name="lttng_test_filter_event"
202 ;;
203 *)
204 fail "Invalid domain type"
205 exit 1
206 ;;
207 esac
6a1eee92 208
434f8068 209 # Setup
6a1eee92 210 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 CONSUMER_PAUSE_PIPE_PATH=${testpoint_pipe_path} LD_PRELOAD=${TESTPOINT}"
434f8068
JR
211 start_lttng_sessiond
212
434f8068 213 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
854382b8
JR
214 enable_${domain_type}_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME --subbuf-size=$PAGE_SIZE
215 enable_${domain_type}_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
434f8068
JR
216
217 # Fetch consumerd testpoint pipe information
218 # This is needed since the testpoint create a pipe with the consumer type suffixed
345ed219
JR
219 consumerd_pipe=()
220 for f in "$testpoint_pipe_path"*; do
434f8068
JR
221 consumerd_pipe+=("$f")
222 done
223
6a1eee92 224 for (( i = 0; i < $nr_client_app; i++ )); do
345ed219
JR
225 low_app_output_file=$output_dir/${low_output_file_pattern}${i}
226 high_app_output_file=$output_dir/${high_output_file_pattern}${i}
854382b8
JR
227 start_client $low_app_output_file $SESSION_NAME $CHANNEL_NAME $domain_string LOW RATIO 0.0 $nr_notification_expected
228 start_client $high_app_output_file $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 $nr_notification_expected
434f8068
JR
229 done
230
345ed219
JR
231 wait_for_message $output_dir "${low_output_file_pattern}" "sync: ready"
232 wait_for_message $output_dir "${high_output_file_pattern}" "sync: ready"
434f8068
JR
233
234 # Test notification reception
6a1eee92 235 for (( i = 0; i < $nr_notification_expected; i++ )); do
434f8068
JR
236
237 # Stop consumerd consumption to force high notification
238 start_lttng_tracing_ok $SESSION_NAME
239 for pipe in "${consumerd_pipe[@]}"; do
240 stop_consumerd "${pipe}"
241 done
242
345ed219 243 wait_for_message $output_dir "${high_output_file_pattern}" "notification: high $i"
434f8068 244
854382b8
JR
245 # Put application in suspend mode to prevent double low
246 # notification and synchronize on state file.
247 kill -s SIGUSR1 $event_generator_pid
248 while [ ! -f "${TESTAPP_STATE_FILE}" ]; do
249 sleep 0.5
250 done
251
434f8068
JR
252 # Resume consumerd
253 for pipe in "${consumerd_pipe[@]}"; do
254 resume_consumerd "${pipe}"
255 done
256 # Stop tracing forcing full buffer consumption
7fe98a98 257 stop_lttng_tracing_ok $SESSION_NAME
434f8068
JR
258
259 # Check for notifications reception
345ed219 260 wait_for_message $output_dir "${low_output_file_pattern}" "notification: low $i"
434f8068
JR
261 ret=$?
262 ok $ret "Notifications $i received"
263 if [[ $ret -ne "0" ]]; then
264 # Error occurred bail out
265 break;
266 fi
854382b8
JR
267
268 # Put application in active mode and synchronize on state file.
269 kill -s SIGUSR1 $event_generator_pid
270 while [ -f "${TESTAPP_STATE_FILE}" ]; do
271 sleep 0.5
272 done
434f8068
JR
273 done
274
345ed219 275 wait_for_message $output_dir "${low_output_file_pattern}" "exit: 0"
434f8068
JR
276 ret=$?
277 ok $ret "Application for low notification terminated normally"
345ed219
JR
278 if [[ $ret -ne "0" ]]; then
279 print_errors $output_dir "${low_output_file_pattern}"
434f8068
JR
280 fi
281
345ed219 282 wait_for_message $output_dir "${high_output_file_pattern}" "exit: 0"
434f8068
JR
283 ret=$?
284 ok $ret "Application for high notification terminated normally"
345ed219
JR
285 if [[ $ret -ne "0" ]]; then
286 print_errors $output_dir "${high_output_file_pattern}"
434f8068
JR
287 fi
288
345ed219
JR
289 rm -rf $output_dir
290
854382b8 291 destroy_lttng_session_ok $SESSION_NAME
434f8068 292 stop_lttng_sessiond
854382b8
JR
293
294 for pipe in "${consumerd_pipe[@]}"; do
295 rm -rf "${pipe}"
296 done
297}
298
299function test_multi_app_ust ()
300{
301 diag "Multi client app UST notification"
302 ust_event_generator $TESTAPP_STATE_FILE &
303 local generator_pid=$!
304
305 test_multi_app ust $generator_pid
306
e212acd4 307 kill -s SIGTERM $generator_pid 2> /dev/null
854382b8
JR
308 wait $generator_pid 2> /dev/null
309 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
310}
311
312function test_multi_app_kernel ()
313{
314 diag "Multi client app kernel notification"
315 modprobe lttng-test
316
317 kernel_event_generator $TESTAPP_STATE_FILE &
318 local generator_pid=$!
319
320 test_multi_app kernel $generator_pid
321
322
e212acd4 323 kill -s SIGTERM $generator_pid 2> /dev/null
854382b8
JR
324 wait $generator_pid 2> /dev/null
325 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
326
327 rmmod lttng-test
328}
329
330function test_on_register_evaluation_ust ()
331{
332 diag "On register notification UST"
333
334 # Start app in infinite loop
335 ust_event_generator $TESTAPP_STATE_FILE &
336 local generator_pid=$!
337
338 test_on_register_evaluation ust $generator_pid
339
e212acd4 340 kill -s SIGTERM $generator_pid 2> /dev/null
854382b8
JR
341 wait $generator_pid 2> /dev/null
342 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
343
344}
345
346function test_on_register_evaluation_kernel()
347{
348 diag "On register notification kernel"
349
350 modprobe lttng-test
351
352 kernel_event_generator $TESTAPP_STATE_FILE &
353 local generator_pid=$!
354
355 test_on_register_evaluation kernel $generator_pid
356
357
e212acd4 358 kill -s SIGTERM $generator_pid 2> /dev/null
854382b8
JR
359 wait $generator_pid 2> /dev/null
360 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
361
362 rmmod lttng-test
434f8068
JR
363}
364
867313e2
JR
365function test_on_register_evaluation ()
366{
854382b8
JR
367 local domain_type=$1
368 local event_generator_pid=$2
369
867313e2
JR
370 local app_pids=()
371 local high_output_file_pattern="high_app_output_file_on_register_evaluation"
372
345ed219
JR
373 local output_dir=$(mktemp -d)
374 local testpoint_base_path=$(readlink -f "$output_dir/lttng.t_p_n_register_evaluation")
867313e2 375 local testpoint_pipe_path=$(mktemp -u "${testpoint_base_path}.XXXXXX")
854382b8
JR
376 local domain_string=""
377 local event_name=""
867313e2 378
854382b8
JR
379 case $domain_type in
380 ust)
381 domain_string=LTTNG_DOMAIN_UST
382 event_name="tp:tptest"
383 ;;
384 kernel)
385 domain_string=LTTNG_DOMAIN_KERNEL
386 event_name="lttng_test_filter_event"
387 ;;
388 *)
389 fail "Invalid domain type"
390 exit 1
391 ;;
392 esac
393
867313e2
JR
394 # Setup
395 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 CONSUMER_PAUSE_PIPE_PATH=${testpoint_pipe_path} LD_PRELOAD=${TESTPOINT}"
396 start_lttng_sessiond
397
867313e2 398 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
854382b8
JR
399 enable_${domain_type}_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME --subbuf-size=$PAGE_SIZE
400 enable_${domain_type}_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
867313e2
JR
401
402 # Fetch consumerd testpoint pipe information
403 # This is needed since the testpoint create a pipe with the consumer type suffixed
345ed219
JR
404 consumerd_pipe=()
405 for f in "$testpoint_pipe_path"*; do
867313e2
JR
406 consumerd_pipe+=("$f")
407 done
408
409 high_app_output_file=${high_output_file_pattern}.first_receiver
345ed219 410 high_app_output_path=$output_dir/${high_app_output_file}
854382b8 411 start_client $high_app_output_path $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 1
867313e2 412
345ed219 413 wait_for_message $output_dir "${high_app_output_file}" "sync: ready"
867313e2
JR
414
415 # Stop consumerd consumption to force high notification
416 start_lttng_tracing_ok $SESSION_NAME
417
418 for pipe in "${consumerd_pipe[@]}"; do
419 stop_consumerd "${pipe}"
420 done
421
345ed219 422 wait_for_message $output_dir "${high_app_output_file}" "notification: high 0"
867313e2
JR
423
424 # Start a second receiver, the receiver should receive a high
425 # notification on subscription
426 high_app_output_file=${high_output_file_pattern}.second_receiver
345ed219 427 high_app_output_path=$output_dir/${high_app_output_file}
854382b8 428 start_client $high_app_output_path $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 1
345ed219
JR
429 wait_for_message $output_dir "${high_app_output_file}" "sync: ready"
430 wait_for_message $output_dir "${high_app_output_file}" "notification: high 0"
867313e2
JR
431
432 # Resume consumerd
433 for pipe in "${consumerd_pipe[@]}"; do
434 resume_consumerd "${pipe}"
435 done
436
345ed219 437 wait_for_message $output_dir "${high_output_file_pattern}" "exit: 0"
867313e2
JR
438 ret=$?
439 ok $ret "Application for high notification terminated normally"
345ed219 440 if [[ $ret -ne "0" ]]; then
867313e2
JR
441 # Keep the file
442 print_errors "${high_output_file_pattern}"
443 fi
444
345ed219
JR
445 rm -rf $output_dir
446
854382b8 447 destroy_lttng_session_ok $SESSION_NAME
867313e2
JR
448 stop_lttng_sessiond
449
e212acd4 450 kill -s SIGTERM $generator_pid 2> /dev/null
854382b8
JR
451 wait $generator_pid 2> /dev/null
452
453 for pipe in "${consumerd_pipe[@]}"; do
454 rm -rf "${pipe}"
455 done
456
867313e2
JR
457}
458
434f8068
JR
459
460TESTS=(
854382b8
JR
461 test_multi_app_ust
462 test_on_register_evaluation_ust
434f8068
JR
463)
464
854382b8 465if [ "$(id -u)" == "0" ]; then
fb180e6e 466 validate_lttng_modules_present
854382b8
JR
467 TESTS+=(
468 test_multi_app_kernel
469 test_on_register_evaluation_kernel
470)
471else
472 skip 0 "Root access is needed. Skipping all kernel multi-app notification tests." $NUM_TEST_KERNEL
473fi
474
475
434f8068
JR
476for fct_test in ${TESTS[@]};
477do
478 TRACE_PATH=$(mktemp -d)
479
480 ${fct_test}
481 if [ $? -ne 0 ]; then
482 break;
483 fi
484
485 # Only delete if successful
486 rm -rf $TRACE_PATH
487done
This page took 0.052079 seconds and 4 git commands to generate.