Cleanup: tests: name all temporary files to better identify leakage
[lttng-tools.git] / tests / regression / tools / notification / test_notification_notifier_discarded_count
CommitLineData
38eb8a68
FD
1#!/bin/bash
2#
3# Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
4#
5# SPDX-License-Identifier: LGPL-2.1-only
6
7CURDIR=$(dirname "$0")/
8TESTDIR=$CURDIR/../../../
9
33e55711 10TEST_TMPDIR=$(mktemp --tmpdir -d tmp.test_notifier_discarded_count.XXXXXX)
38eb8a68
FD
11
12TESTAPP_PATH="$TESTDIR/utils/testapp"
13TESTAPP_NAME="gen-ust-events"
14TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
15
33e55711
FD
16TESTPOINT_BASE_PATH=$(readlink -f "$TEST_TMPDIR/lttng.t_p_n")
17TESTPOINT_PIPE_PATH=$(mktemp -u --tmpdir="$TEST_TMPDIR" "lttng.t_p_n.XXXXXX")
38eb8a68
FD
18TESTPOINT=$(readlink -f "${CURDIR}/.libs/libpause_sessiond.so")
19
20SH_TAP=1
21
22# shellcheck source=../../../utils/utils.sh
23source "$TESTDIR/utils/utils.sh"
24# shellcheck source=./util_event_generator.sh
25source "$CURDIR/util_event_generator.sh"
26
27FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}"
28FULL_LTTNG_SESSIOND_BIN="${TESTDIR}/../src/bin/lttng-sessiond/lttng-sessiond"
29
96bb1ae8 30UST_NUM_TESTS=15
a5a21280 31DESTRUCTIVE_TESTS_NUM=12
96bb1ae8 32KERNEL_NUM_TESTS=$((14 + $DESTRUCTIVE_TESTS_NUM))
38eb8a68
FD
33NUM_TESTS=$(($UST_NUM_TESTS + $KERNEL_NUM_TESTS))
34
35plan_tests $NUM_TESTS
36
96bb1ae8
FD
37function trigger_get_discarded_notif_number()
38{
39 local trigger_name="$1"
33e55711 40 local list_triggers_stdout=$(mktemp --tmpdir="$TEST_TMPDIR" "list_triggers_stdout.XXXXXX")
96bb1ae8
FD
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
38eb8a68
FD
59function test_kernel_notifier_discarded_count
60{
61 local sessiond_pipe=()
62 local trigger_name="my_trigger"
33e55711 63 local list_triggers_stdout=$(mktemp --tmpdir="$TEST_TMPDIR" "list_triggers_stdout.XXXXXX")
38eb8a68
FD
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" \
64c34630 81 --condition event-rule-matches --domain=kernel --name=lttng_test_filter_event \
38eb8a68
FD
82 --action notify
83
96bb1ae8
FD
84 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
85 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
86
87 # Stop consumption of notifier tracer notifications.
709fb83f 88 diag "Pause consumption of tracer messages"
38eb8a68
FD
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.
40b21f06 96 echo -n "2000" > /proc/lttng-test-filter-event
38eb8a68 97
96bb1ae8
FD
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"
38eb8a68
FD
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)
665db063 106 is "$list_triggers_line_count" "0" "No \`event-rule-matches\` kernel notifier enabled as expected"
38eb8a68
FD
107
108 # Enable another notifier and list it to confirm the counter was cleared.
109 lttng_add_trigger_ok "$trigger_name" \
64c34630 110 --condition event-rule-matches --domain=kernel --name=lttng_test_filter_event \
38eb8a68
FD
111 --action notify
112
96bb1ae8
FD
113 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
114 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
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
125function test_kernel_notifier_discarded_count_max_bucket
126{
761ffce2 127 start_lttng_sessiond "" "--event-notifier-error-buffer-size-kernel=3"
38eb8a68
FD
128
129 diag "Kernel event notifer error counter bucket limit"
130 for i in $(seq 3); do
131 lttng_add_trigger_ok "$i" \
64c34630 132 --condition event-rule-matches --domain=kernel --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
133 --action notify
134 done
135
136 for i in $(seq 4 5); do
137 lttng_add_trigger_fail "$i" \
64c34630 138 --condition event-rule-matches --domain=kernel --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
139 --action notify
140 done
141
142 stop_lttng_sessiond_notap
143}
144
145function test_ust_notifier_discarded_count
146{
147 local sessiond_pipe=()
148 local trigger_name="my_trigger"
38eb8a68
FD
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" \
64c34630 183 --condition event-rule-matches --domain=user --name=tp:tptest \
38eb8a68
FD
184 --action notify
185
96bb1ae8
FD
186 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
187 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
188
189 # Stop consumption of notifier tracer notifications.
709fb83f 190 diag "Pause consumption of tracer messages"
38eb8a68
FD
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
96bb1ae8
FD
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"
38eb8a68
FD
199
200 # Remove the notifier.
201 lttng_remove_trigger_ok "$trigger_name"
202
a5a21280 203 # Confirm that no trigger is enabled.
38eb8a68 204 list_triggers_line_count=$("$FULL_LTTNG_BIN" list-triggers | wc -l)
665db063 205 is "$list_triggers_line_count" "0" "No \`event-rule-matches\` userspace notifier enabled as expected"
38eb8a68
FD
206
207 # Enable another notifier and list it to confirm the counter was cleared.
208 lttng_add_trigger_ok "$trigger_name" \
64c34630 209 --condition event-rule-matches --domain=user --name=tp:tptest \
38eb8a68
FD
210 --action notify
211
96bb1ae8
FD
212 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
213 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
214
215 lttng_remove_trigger_ok "$trigger_name"
216
217 stop_lttng_sessiond_notap
218
219 unset LTTNG_SESSIOND_ENV_VARS
38eb8a68 220}
96bb1ae8 221
38eb8a68
FD
222function test_ust_notifier_discarded_count_max_bucket
223{
761ffce2 224 start_lttng_sessiond "" "--event-notifier-error-buffer-size-userspace=3"
38eb8a68
FD
225
226 diag "UST event notifer error counter bucket limit"
227 for i in $(seq 3); do
228 lttng_add_trigger_ok "$i" \
64c34630 229 --condition event-rule-matches --domain=user --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
230 --action notify
231 done
232
233 for i in $(seq 4 5); do
234 lttng_add_trigger_fail "$i" \
64c34630 235 --condition event-rule-matches --domain=user --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
236 --action notify
237 done
238
239 stop_lttng_sessiond_notap
240}
241
96bb1ae8
FD
242function 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"
33e55711 247 local list_triggers_stdout=$(mktemp --tmpdir="$TEST_TMPDIR" "list_triggers_stdout.XXXXXX")
96bb1ae8
FD
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" \
537e0d79 287 --condition event-rule-matches --domain=user --name tp:tptest \
96bb1ae8
FD
288 --action notify
289
537e0d79
JG
290 lttng_add_trigger_ok "$user_trigger_name" --owner-uid "$new_uid" \
291 --condition event-rule-matches --domain=user --name tp:tptest \
96bb1ae8
FD
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 \
a5a21280 307 "Root trigger discarded notifications number ($root_trigger_discarded_nb) is non-zero"
96bb1ae8 308 isnt $user_trigger_discarded_nb 0 \
a5a21280 309 "User trigger discarded notifications number ($user_trigger_discarded_nb) is non-zero"
96bb1ae8
FD
310
311 lttng_remove_trigger_ok "$root_trigger_name"
537e0d79 312 lttng_remove_trigger_ok "$user_trigger_name" --owner-uid "$new_uid"
96bb1ae8
FD
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
a5a21280
FD
322function test_ust_notifier_discarded_regardless_trigger_owner
323{
324 local sessiond_pipe=()
325 local root_trigger_name="root_trigger"
326 local user_trigger_name="user_trigger"
33e55711 327 local list_triggers_stdout=$(mktemp --tmpdir="$TEST_TMPDIR" "list_triggers_stdout.XXXXXX")
a5a21280
FD
328 local NR_USEC_WAIT=0
329 local PIPE_SIZE
330 local NR_ITER
331 local new_user="dummy_lttng_test_user"
332
333 PIPE_SIZE=$("$CURDIR"/default_pipe_size_getter)
334 if [ $? -ne 0 ]; then
335 BAIL_OUT "Failed to get system default pipe size"
336 else
337 diag "Default system pipe size: $PIPE_SIZE bytes"
338 fi
339
340 # Find the number of events needed to overflow the event notification
341 # pipe buffer. Each LTTng-UST notification is at least 42 bytes long.
342 # Double that number to ensure enough events are created to overflow
343 # the buffer.
344 NR_ITER=$(( (PIPE_SIZE / 42) * 2 ))
345 diag "Test applications will emit $NR_ITER events"
346
347 diag "UST event notifer error counter persists when a root trigger is present"
348
349 # Create a dummy user to run test apps as.
350 useradd --no-create-home "$new_user"
351 new_uid=$(id -u "$new_user")
352
353 # Used on sessiond launch.
354 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \
355 NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \
356 LD_PRELOAD=${TESTPOINT}"
357
358 start_lttng_sessiond_notap
359
360 # This is needed since the testpoint create a pipe with the sessiond
361 # type suffixed.
362 for f in "$TESTPOINT_BASE_PATH"*; do
363 sessiond_pipe+=("$f")
364 done
365
366 lttng_add_trigger_ok "$root_trigger_name" \
537e0d79 367 --condition event-rule-matches --domain=user --name tp:tptest \
a5a21280
FD
368 --action notify
369
370 # Stop consumption of notifier tracer notifications.
371 echo -n 1 > $sessiond_pipe
372
373 su "$new_user" -c "$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT"
374 ok $? "Generating $NR_ITER tracer notifications as UID: $new_uid"
375
376 root_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$root_trigger_name")
377
378 isnt $root_trigger_discarded_nb 0 \
379 "Root trigger discarded notifications number ($root_trigger_discarded_nb) is non-zero"
380
381 lttng_remove_trigger_ok "$root_trigger_name"
382
383 stop_lttng_sessiond_notap
384
385 unset LTTNG_SESSIOND_ENV_VARS
386
387 userdel "$new_user"
388 rm -f "$list_triggers_stdout"
389}
390
38eb8a68
FD
391test_ust_notifier_discarded_count
392test_ust_notifier_discarded_count_max_bucket
393
394if [ "$(id -u)" == "0" ]; then
395
396 validate_lttng_modules_present
397
398 modprobe lttng-test
399
400 test_kernel_notifier_discarded_count
38eb8a68
FD
401 test_kernel_notifier_discarded_count_max_bucket
402
96bb1ae8 403 if destructive_tests_enabled ; then
a5a21280 404 # Those tests add a new user on the system. Since it's a quite
96bb1ae8
FD
405 # intrusive change to the system, we decide to only run it when
406 # the user knows what they are doing.
407 test_ust_notifier_discarded_count_multi_uid
a5a21280 408 test_ust_notifier_discarded_regardless_trigger_owner
96bb1ae8
FD
409 else
410 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
411 fi
412
38eb8a68
FD
413 modprobe --remove lttng-test
414
415 rm -rf "${sessiond_pipe[@]}" 2> /dev/null
416else
417 # Kernel tests are skipped.
418 skip 0 "Root access is needed. Skipping all kernel notification tests." $KERNEL_NUM_TESTS
419fi
420
33e55711 421rm -f "$TEST_TMPDIR"
This page took 0.041538 seconds and 4 git commands to generate.