| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 4 | # |
| 5 | # SPDX-License-Identifier: LGPL-2.1-only |
| 6 | |
| 7 | CURDIR=$(dirname "$0")/ |
| 8 | TESTDIR=$CURDIR/../../../ |
| 9 | |
| 10 | TMPDIR=$(mktemp -d) |
| 11 | |
| 12 | TESTAPP_PATH="$TESTDIR/utils/testapp" |
| 13 | TESTAPP_NAME="gen-ust-events" |
| 14 | TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME" |
| 15 | |
| 16 | TESTPOINT_BASE_PATH=$(readlink -f "$TMPDIR/lttng.t_p_n") |
| 17 | TESTPOINT_PIPE_PATH=$(mktemp -u "${TESTPOINT_BASE_PATH}.XXXXXX") |
| 18 | TESTPOINT=$(readlink -f "${CURDIR}/.libs/libpause_sessiond.so") |
| 19 | |
| 20 | SH_TAP=1 |
| 21 | |
| 22 | # shellcheck source=../../../utils/utils.sh |
| 23 | source "$TESTDIR/utils/utils.sh" |
| 24 | # shellcheck source=./util_event_generator.sh |
| 25 | source "$CURDIR/util_event_generator.sh" |
| 26 | |
| 27 | FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}" |
| 28 | FULL_LTTNG_SESSIOND_BIN="${TESTDIR}/../src/bin/lttng-sessiond/lttng-sessiond" |
| 29 | |
| 30 | UST_NUM_TESTS=15 |
| 31 | DESTRUCTIVE_TESTS_NUM=8 |
| 32 | KERNEL_NUM_TESTS=$((14 + $DESTRUCTIVE_TESTS_NUM)) |
| 33 | NUM_TESTS=$(($UST_NUM_TESTS + $KERNEL_NUM_TESTS)) |
| 34 | |
| 35 | plan_tests $NUM_TESTS |
| 36 | |
| 37 | function trigger_get_discarded_notif_number() |
| 38 | { |
| 39 | local trigger_name="$1" |
| 40 | local list_triggers_stdout=$(mktemp -t list_triggers_stdout.XXXXXX) |
| 41 | |
| 42 | "$FULL_LTTNG_BIN" list-triggers > "$list_triggers_stdout" |
| 43 | |
| 44 | cat "$list_triggers_stdout" | grep -a7 "$trigger_name" | tail -1 | grep --quiet "errors: none" |
| 45 | ret=$? |
| 46 | |
| 47 | if [ "$ret" -eq "0" ]; then |
| 48 | notif_nb="0" |
| 49 | else |
| 50 | notif_nb=$(cat "$list_triggers_stdout" | grep -a8 "$trigger_name" | tail -1 | grep "discarded tracer messages" | cut -d' ' -f8) |
| 51 | fi |
| 52 | |
| 53 | rm -f "$list_triggers_stdout" |
| 54 | |
| 55 | # Printing the value to that the caller can get it back. |
| 56 | echo "$notif_nb" |
| 57 | } |
| 58 | |
| 59 | function test_kernel_notifier_discarded_count |
| 60 | { |
| 61 | local sessiond_pipe=() |
| 62 | local trigger_name="my_trigger" |
| 63 | local list_triggers_stdout=$(mktemp -t list_triggers_stdout.XXXXXX) |
| 64 | |
| 65 | # Used on sessiond launch. |
| 66 | LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \ |
| 67 | NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \ |
| 68 | LD_PRELOAD=${TESTPOINT}" |
| 69 | |
| 70 | diag "Kernel event notifer error counter" |
| 71 | |
| 72 | start_lttng_sessiond_notap |
| 73 | |
| 74 | # This is needed since the testpoint creates a pipe with the sessiond |
| 75 | # type suffixed. |
| 76 | for f in "$TESTPOINT_BASE_PATH"*; do |
| 77 | sessiond_pipe+=("$f") |
| 78 | done |
| 79 | |
| 80 | lttng_add_trigger_ok "$trigger_name" \ |
| 81 | --condition event-rule-matches --kernel lttng_test_filter_event \ |
| 82 | --action notify |
| 83 | |
| 84 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 85 | is $trigger_discarded_nb 0 "No discarded tracer notification" |
| 86 | |
| 87 | # Stop consumption of notifier tracer notifications. |
| 88 | diag "Pause consumption of tracer messages" |
| 89 | echo -n 1 > $sessiond_pipe |
| 90 | |
| 91 | # The notifier ring buffer configuration is currently made of 16 4096 |
| 92 | # bytes subbuffers. Each kernel notification is at least 42 bytes long. |
| 93 | # To fill it, we need to generate (16 * 4096)/42 = 1561 notifications. |
| 94 | # That number is a bit larger than what we need since some of the space |
| 95 | # is lost in subbuffer boundaries. |
| 96 | echo -n "2000" > /proc/lttng-test-filter-event |
| 97 | |
| 98 | # Confirm that the number of tracer notifications discarded is non-zero. |
| 99 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 100 | isnt $trigger_discarded_nb 0 "Discarded tracer notification number non-zero ($trigger_discarded_nb) as expected" |
| 101 | |
| 102 | lttng_remove_trigger_ok "$trigger_name" |
| 103 | |
| 104 | # Confirm that no notifier is enabled. |
| 105 | list_triggers_line_count=$("$FULL_LTTNG_BIN" list-triggers | wc -l) |
| 106 | is "$list_triggers_line_count" "0" "No \`event-rule-matches\` kernel notifier enabled as expected" |
| 107 | |
| 108 | # Enable another notifier and list it to confirm the counter was cleared. |
| 109 | lttng_add_trigger_ok "$trigger_name" \ |
| 110 | --condition event-rule-matches --kernel lttng_test_filter_event \ |
| 111 | --action notify |
| 112 | |
| 113 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 114 | is $trigger_discarded_nb 0 "No discarded tracer notification" |
| 115 | |
| 116 | lttng_remove_trigger_ok "$trigger_name" |
| 117 | |
| 118 | stop_lttng_sessiond_notap |
| 119 | |
| 120 | unset LTTNG_SESSIOND_ENV_VARS |
| 121 | |
| 122 | rm -f "$list_triggers_stdout" |
| 123 | } |
| 124 | |
| 125 | function test_kernel_notifier_discarded_count_max_bucket |
| 126 | { |
| 127 | start_lttng_sessiond "" "--event-notifier-error-buffer-size-kernel=3" |
| 128 | |
| 129 | diag "Kernel event notifer error counter bucket limit" |
| 130 | for i in $(seq 3); do |
| 131 | lttng_add_trigger_ok "$i" \ |
| 132 | --condition event-rule-matches --kernel my_event_that_doesnt_need_to_really_exist_$i \ |
| 133 | --action notify |
| 134 | done |
| 135 | |
| 136 | for i in $(seq 4 5); do |
| 137 | lttng_add_trigger_fail "$i" \ |
| 138 | --condition event-rule-matches --kernel my_event_that_doesnt_need_to_really_exist_$i \ |
| 139 | --action notify |
| 140 | done |
| 141 | |
| 142 | stop_lttng_sessiond_notap |
| 143 | } |
| 144 | |
| 145 | function test_ust_notifier_discarded_count |
| 146 | { |
| 147 | local sessiond_pipe=() |
| 148 | local trigger_name="my_trigger" |
| 149 | local NR_USEC_WAIT=0 |
| 150 | local PIPE_SIZE |
| 151 | local NR_ITER |
| 152 | |
| 153 | diag "UST event notifer error counter" |
| 154 | |
| 155 | PIPE_SIZE=$("$CURDIR"/default_pipe_size_getter) |
| 156 | if [ $? -ne 0 ]; then |
| 157 | BAIL_OUT "Failed to get system default pipe size" |
| 158 | else |
| 159 | diag "Default system pipe size: $PIPE_SIZE bytes" |
| 160 | fi |
| 161 | |
| 162 | # Find the number of events needed to overflow the event notification |
| 163 | # pipe buffer. Each LTTng-UST notification is at least 42 bytes long. |
| 164 | # Double that number to ensure enough events are created to overflow |
| 165 | # the buffer. |
| 166 | NR_ITER=$(( (PIPE_SIZE / 42) * 2 )) |
| 167 | diag "Test application will emit $NR_ITER events" |
| 168 | |
| 169 | # Used on sessiond launch. |
| 170 | LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \ |
| 171 | NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \ |
| 172 | LD_PRELOAD=${TESTPOINT}" |
| 173 | |
| 174 | start_lttng_sessiond_notap |
| 175 | |
| 176 | # This is needed since the testpoint create a pipe with the sessiond |
| 177 | # type suffixed. |
| 178 | for f in "$TESTPOINT_BASE_PATH"*; do |
| 179 | sessiond_pipe+=("$f") |
| 180 | done |
| 181 | |
| 182 | lttng_add_trigger_ok "$trigger_name" \ |
| 183 | --condition event-rule-matches --userspace tp:tptest \ |
| 184 | --action notify |
| 185 | |
| 186 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 187 | is $trigger_discarded_nb 0 "No discarded tracer notification" |
| 188 | |
| 189 | # Stop consumption of notifier tracer notifications. |
| 190 | diag "Pause consumption of tracer messages" |
| 191 | echo -n 1 > $sessiond_pipe |
| 192 | |
| 193 | $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT |
| 194 | ok $? "Generating $NR_ITER tracer notifications" |
| 195 | |
| 196 | # Confirm that the number of tracer notifications discarded is non-zero. |
| 197 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 198 | isnt $trigger_discarded_nb 0 "Discarded tracer notification number non-zero ($trigger_discarded_nb) as expected" |
| 199 | |
| 200 | # Remove the notifier. |
| 201 | lttng_remove_trigger_ok "$trigger_name" |
| 202 | |
| 203 | # Confirm that no notifier is enabled. |
| 204 | list_triggers_line_count=$("$FULL_LTTNG_BIN" list-triggers | wc -l) |
| 205 | is "$list_triggers_line_count" "0" "No \`event-rule-matches\` userspace notifier enabled as expected" |
| 206 | |
| 207 | # Enable another notifier and list it to confirm the counter was cleared. |
| 208 | lttng_add_trigger_ok "$trigger_name" \ |
| 209 | --condition event-rule-matches --userspace tp:tptest \ |
| 210 | --action notify |
| 211 | |
| 212 | trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name") |
| 213 | is $trigger_discarded_nb 0 "No discarded tracer notification" |
| 214 | |
| 215 | lttng_remove_trigger_ok "$trigger_name" |
| 216 | |
| 217 | stop_lttng_sessiond_notap |
| 218 | |
| 219 | unset LTTNG_SESSIOND_ENV_VARS |
| 220 | } |
| 221 | |
| 222 | function test_ust_notifier_discarded_count_max_bucket |
| 223 | { |
| 224 | start_lttng_sessiond "" "--event-notifier-error-buffer-size-userspace=3" |
| 225 | |
| 226 | diag "UST event notifer error counter bucket limit" |
| 227 | for i in $(seq 3); do |
| 228 | lttng_add_trigger_ok "$i" \ |
| 229 | --condition event-rule-matches --userspace my_event_that_doesnt_need_to_really_exist_$i \ |
| 230 | --action notify |
| 231 | done |
| 232 | |
| 233 | for i in $(seq 4 5); do |
| 234 | lttng_add_trigger_fail "$i" \ |
| 235 | --condition event-rule-matches --userspace my_event_that_doesnt_need_to_really_exist_$i \ |
| 236 | --action notify |
| 237 | done |
| 238 | |
| 239 | stop_lttng_sessiond_notap |
| 240 | } |
| 241 | |
| 242 | function test_ust_notifier_discarded_count_multi_uid |
| 243 | { |
| 244 | local sessiond_pipe=() |
| 245 | local root_trigger_name="root_trigger" |
| 246 | local user_trigger_name="user_trigger" |
| 247 | local list_triggers_stdout=$(mktemp -t list_triggers_stdout.XXXXXX) |
| 248 | local NR_USEC_WAIT=0 |
| 249 | local PIPE_SIZE |
| 250 | local NR_ITER |
| 251 | local new_user="dummy_lttng_test_user" |
| 252 | |
| 253 | diag "UST event notifer error counter multiple UIDs" |
| 254 | |
| 255 | # Create a dummy user to run test apps as. |
| 256 | useradd --no-create-home "$new_user" |
| 257 | new_uid=$(id -u "$new_user") |
| 258 | |
| 259 | PIPE_SIZE=$("$CURDIR"/default_pipe_size_getter) |
| 260 | if [ $? -ne 0 ]; then |
| 261 | BAIL_OUT "Failed to get system default pipe size" |
| 262 | else |
| 263 | diag "Default system pipe size: $PIPE_SIZE bytes" |
| 264 | fi |
| 265 | |
| 266 | # Find the number of events needed to overflow the event notification |
| 267 | # pipe buffer. Each LTTng-UST notification is at least 42 bytes long. |
| 268 | # Double that number to ensure enough events are created to overflow |
| 269 | # the buffer. |
| 270 | NR_ITER=$(( (PIPE_SIZE / 42) * 2 )) |
| 271 | diag "Test applications will emit $NR_ITER events" |
| 272 | |
| 273 | # Used on sessiond launch. |
| 274 | LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \ |
| 275 | NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \ |
| 276 | LD_PRELOAD=${TESTPOINT}" |
| 277 | |
| 278 | start_lttng_sessiond_notap |
| 279 | |
| 280 | # This is needed since the testpoint create a pipe with the sessiond |
| 281 | # type suffixed. |
| 282 | for f in "$TESTPOINT_BASE_PATH"*; do |
| 283 | sessiond_pipe+=("$f") |
| 284 | done |
| 285 | |
| 286 | lttng_add_trigger_ok "$root_trigger_name" \ |
| 287 | --condition on-event --userspace tp:tptest \ |
| 288 | --action notify |
| 289 | |
| 290 | lttng_add_trigger_ok "$user_trigger_name" --user-id "$new_uid" \ |
| 291 | --condition on-event --userspace tp:tptest \ |
| 292 | --action notify |
| 293 | |
| 294 | # Stop consumption of notifier tracer notifications. |
| 295 | echo -n 1 > $sessiond_pipe |
| 296 | |
| 297 | $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT |
| 298 | ok $? "Generating $NR_ITER tracer notifications as UID: $(id -u)" |
| 299 | |
| 300 | su "$new_user" -c "$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT" |
| 301 | ok $? "Generating $NR_ITER tracer notifications as UID: $new_uid" |
| 302 | |
| 303 | root_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$root_trigger_name") |
| 304 | user_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$user_trigger_name") |
| 305 | |
| 306 | isnt $root_trigger_discarded_nb 0 \ |
| 307 | "Root trigger discard notifications number ($root_trigger_discarded_nb) is non-zero" |
| 308 | isnt $user_trigger_discarded_nb 0 \ |
| 309 | "User trigger discard notifications number ($user_trigger_discarded_nb) is non-zero" |
| 310 | |
| 311 | lttng_remove_trigger_ok "$root_trigger_name" |
| 312 | lttng_remove_trigger_ok "$user_trigger_name" --user-id "$new_uid" |
| 313 | |
| 314 | stop_lttng_sessiond_notap |
| 315 | |
| 316 | unset LTTNG_SESSIOND_ENV_VARS |
| 317 | |
| 318 | userdel "$new_user" |
| 319 | rm -f "$list_triggers_stdout" |
| 320 | } |
| 321 | |
| 322 | test_ust_notifier_discarded_count |
| 323 | test_ust_notifier_discarded_count_max_bucket |
| 324 | |
| 325 | if [ "$(id -u)" == "0" ]; then |
| 326 | |
| 327 | validate_lttng_modules_present |
| 328 | |
| 329 | modprobe lttng-test |
| 330 | |
| 331 | test_kernel_notifier_discarded_count |
| 332 | test_kernel_notifier_discarded_count_max_bucket |
| 333 | |
| 334 | if destructive_tests_enabled ; then |
| 335 | # This test adds a new user on the system. Since it's a quite |
| 336 | # intrusive change to the system, we decide to only run it when |
| 337 | # the user knows what they are doing. |
| 338 | test_ust_notifier_discarded_count_multi_uid |
| 339 | else |
| 340 | skip 0 "You need to set the LTTNG_ENABLE_DESTRUCTIVE_TESTS environment variable to \"will-break-my-system\" to run this test" $DESTRUCTIVE_TESTS_NUM |
| 341 | fi |
| 342 | |
| 343 | modprobe --remove lttng-test |
| 344 | |
| 345 | rm -rf "${sessiond_pipe[@]}" 2> /dev/null |
| 346 | else |
| 347 | # Kernel tests are skipped. |
| 348 | skip 0 "Root access is needed. Skipping all kernel notification tests." $KERNEL_NUM_TESTS |
| 349 | fi |
| 350 | |
| 351 | rm -rf "$TMPDIR" |