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