Clean-up: consumer.hpp: coding style indentation fix
[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
8d5a3312
MJ
10TEST_TMPDIR=$(mktemp -d -t tmp.test_notifier_discarded_count.XXXXXX)
11
12# Set TMPDIR for further call to mktemp
13export TMPDIR="$TEST_TMPDIR"
38eb8a68
FD
14
15TESTAPP_PATH="$TESTDIR/utils/testapp"
16TESTAPP_NAME="gen-ust-events"
17TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
18
33e55711 19TESTPOINT_BASE_PATH=$(readlink -f "$TEST_TMPDIR/lttng.t_p_n")
8d5a3312 20TESTPOINT_PIPE_PATH=$(mktemp -u -t "lttng.t_p_n.XXXXXX")
38eb8a68
FD
21TESTPOINT=$(readlink -f "${CURDIR}/.libs/libpause_sessiond.so")
22
23SH_TAP=1
24
25# shellcheck source=../../../utils/utils.sh
26source "$TESTDIR/utils/utils.sh"
27# shellcheck source=./util_event_generator.sh
28source "$CURDIR/util_event_generator.sh"
29
30FULL_LTTNG_BIN="${TESTDIR}/../src/bin/lttng/${LTTNG_BIN}"
31FULL_LTTNG_SESSIOND_BIN="${TESTDIR}/../src/bin/lttng-sessiond/lttng-sessiond"
32
96bb1ae8 33UST_NUM_TESTS=15
a5a21280 34DESTRUCTIVE_TESTS_NUM=12
96bb1ae8 35KERNEL_NUM_TESTS=$((14 + $DESTRUCTIVE_TESTS_NUM))
38eb8a68
FD
36NUM_TESTS=$(($UST_NUM_TESTS + $KERNEL_NUM_TESTS))
37
38plan_tests $NUM_TESTS
39
96bb1ae8
FD
40function trigger_get_discarded_notif_number()
41{
42 local trigger_name="$1"
8d5a3312 43 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
96bb1ae8
FD
44
45 "$FULL_LTTNG_BIN" list-triggers > "$list_triggers_stdout"
46
63dd3d7b 47 cat "$list_triggers_stdout" | grep -A7 "$trigger_name" | grep -A2 "event rule matches" | tail -1 | grep --quiet "errors: none"
96bb1ae8
FD
48 ret=$?
49
50 if [ "$ret" -eq "0" ]; then
51 notif_nb="0"
52 else
63dd3d7b 53 notif_nb=$(cat "$list_triggers_stdout" | grep -A7 "$trigger_name" | grep "discarded tracer messages" | cut -d' ' -f10)
96bb1ae8
FD
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
38eb8a68
FD
62function test_kernel_notifier_discarded_count
63{
64 local sessiond_pipe=()
65 local trigger_name="my_trigger"
8d5a3312 66 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
38eb8a68
FD
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" \
695f7044 84 --condition event-rule-matches --type=kernel --name=lttng_test_filter_event \
38eb8a68
FD
85 --action notify
86
96bb1ae8
FD
87 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
88 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
89
90 # Stop consumption of notifier tracer notifications.
709fb83f 91 diag "Pause consumption of tracer messages"
38eb8a68
FD
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.
40b21f06 99 echo -n "2000" > /proc/lttng-test-filter-event
38eb8a68 100
96bb1ae8
FD
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"
38eb8a68
FD
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)
665db063 109 is "$list_triggers_line_count" "0" "No \`event-rule-matches\` kernel notifier enabled as expected"
38eb8a68
FD
110
111 # Enable another notifier and list it to confirm the counter was cleared.
112 lttng_add_trigger_ok "$trigger_name" \
695f7044 113 --condition event-rule-matches --type=kernel --name=lttng_test_filter_event \
38eb8a68
FD
114 --action notify
115
96bb1ae8
FD
116 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
117 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
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
128function test_kernel_notifier_discarded_count_max_bucket
129{
761ffce2 130 start_lttng_sessiond "" "--event-notifier-error-buffer-size-kernel=3"
38eb8a68
FD
131
132 diag "Kernel event notifer error counter bucket limit"
133 for i in $(seq 3); do
134 lttng_add_trigger_ok "$i" \
695f7044 135 --condition event-rule-matches --type=kernel --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
136 --action notify
137 done
138
139 for i in $(seq 4 5); do
140 lttng_add_trigger_fail "$i" \
695f7044 141 --condition event-rule-matches --type=kernel --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
142 --action notify
143 done
144
145 stop_lttng_sessiond_notap
146}
147
148function test_ust_notifier_discarded_count
149{
150 local sessiond_pipe=()
151 local trigger_name="my_trigger"
38eb8a68
FD
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" \
695f7044 186 --condition event-rule-matches --type=user --name=tp:tptest \
38eb8a68
FD
187 --action notify
188
96bb1ae8
FD
189 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
190 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
191
192 # Stop consumption of notifier tracer notifications.
709fb83f 193 diag "Pause consumption of tracer messages"
38eb8a68
FD
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
96bb1ae8
FD
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"
38eb8a68
FD
202
203 # Remove the notifier.
204 lttng_remove_trigger_ok "$trigger_name"
205
a5a21280 206 # Confirm that no trigger is enabled.
38eb8a68 207 list_triggers_line_count=$("$FULL_LTTNG_BIN" list-triggers | wc -l)
665db063 208 is "$list_triggers_line_count" "0" "No \`event-rule-matches\` userspace notifier enabled as expected"
38eb8a68
FD
209
210 # Enable another notifier and list it to confirm the counter was cleared.
211 lttng_add_trigger_ok "$trigger_name" \
695f7044 212 --condition event-rule-matches --type=user --name=tp:tptest \
38eb8a68
FD
213 --action notify
214
96bb1ae8
FD
215 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
216 is $trigger_discarded_nb 0 "No discarded tracer notification"
38eb8a68
FD
217
218 lttng_remove_trigger_ok "$trigger_name"
219
220 stop_lttng_sessiond_notap
221
222 unset LTTNG_SESSIOND_ENV_VARS
38eb8a68 223}
96bb1ae8 224
38eb8a68
FD
225function test_ust_notifier_discarded_count_max_bucket
226{
761ffce2 227 start_lttng_sessiond "" "--event-notifier-error-buffer-size-userspace=3"
38eb8a68
FD
228
229 diag "UST event notifer error counter bucket limit"
230 for i in $(seq 3); do
231 lttng_add_trigger_ok "$i" \
695f7044 232 --condition event-rule-matches --type=user --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
233 --action notify
234 done
235
236 for i in $(seq 4 5); do
237 lttng_add_trigger_fail "$i" \
695f7044 238 --condition event-rule-matches --type=user --name=my_event_that_doesnt_need_to_really_exist_$i \
38eb8a68
FD
239 --action notify
240 done
241
242 stop_lttng_sessiond_notap
243}
244
96bb1ae8
FD
245function 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"
8d5a3312 250 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
96bb1ae8
FD
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" \
695f7044 290 --condition event-rule-matches --type=user --name tp:tptest \
96bb1ae8
FD
291 --action notify
292
537e0d79 293 lttng_add_trigger_ok "$user_trigger_name" --owner-uid "$new_uid" \
695f7044 294 --condition event-rule-matches --type=user --name tp:tptest \
96bb1ae8
FD
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 \
a5a21280 310 "Root trigger discarded notifications number ($root_trigger_discarded_nb) is non-zero"
96bb1ae8 311 isnt $user_trigger_discarded_nb 0 \
a5a21280 312 "User trigger discarded notifications number ($user_trigger_discarded_nb) is non-zero"
96bb1ae8
FD
313
314 lttng_remove_trigger_ok "$root_trigger_name"
537e0d79 315 lttng_remove_trigger_ok "$user_trigger_name" --owner-uid "$new_uid"
96bb1ae8
FD
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
a5a21280
FD
325function 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"
8d5a3312 330 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
a5a21280
FD
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" \
695f7044 370 --condition event-rule-matches --type=user --name tp:tptest \
a5a21280
FD
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
38eb8a68
FD
394test_ust_notifier_discarded_count
395test_ust_notifier_discarded_count_max_bucket
396
3a174400
XC
397check_skip_kernel_test "$KERNEL_NUM_TESTS" "Skipping kernel notification tests." ||
398{
38eb8a68
FD
399
400 validate_lttng_modules_present
401
402 modprobe lttng-test
403
404 test_kernel_notifier_discarded_count
38eb8a68
FD
405 test_kernel_notifier_discarded_count_max_bucket
406
96bb1ae8 407 if destructive_tests_enabled ; then
a5a21280 408 # Those tests add a new user on the system. Since it's a quite
96bb1ae8
FD
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
a5a21280 412 test_ust_notifier_discarded_regardless_trigger_owner
96bb1ae8
FD
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
38eb8a68
FD
417 modprobe --remove lttng-test
418
419 rm -rf "${sessiond_pipe[@]}" 2> /dev/null
3a174400
XC
420
421}
38eb8a68 422
0d0386e0 423rm -rf "$TEST_TMPDIR"
This page took 0.062065 seconds and 4 git commands to generate.