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