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