Use compiler-agnostic defines to silence warning
[lttng-tools.git] / tests / regression / tools / notification / test_notification_notifier_discarded_count
... / ...
CommitLineData
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
10TEST_TMPDIR=$(mktemp -d -t tmp.test_notifier_discarded_count.XXXXXX)
11
12# Set TMPDIR for further call to mktemp
13export TMPDIR="$TEST_TMPDIR"
14
15TESTAPP_PATH="$TESTDIR/utils/testapp"
16TESTAPP_NAME="gen-ust-events"
17TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
18
19TESTPOINT_BASE_PATH=$(readlink -f "$TEST_TMPDIR/lttng.t_p_n")
20TESTPOINT_PIPE_PATH=$(mktemp -u -t "lttng.t_p_n.XXXXXX")
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
33UST_NUM_TESTS=15
34DESTRUCTIVE_TESTS_NUM=12
35KERNEL_NUM_TESTS=$((14 + $DESTRUCTIVE_TESTS_NUM))
36NUM_TESTS=$(($UST_NUM_TESTS + $KERNEL_NUM_TESTS))
37
38plan_tests $NUM_TESTS
39
40function 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
62function 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
128function 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
148function 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=$(get_pipe_max_size)
159
160 # Find the number of events needed to overflow the event notification
161 # pipe buffer. Each LTTng-UST notification is at least 42 bytes long.
162 # Double that number to ensure enough events are created to overflow
163 # the buffer.
164 NR_ITER=$(( (PIPE_SIZE / 42) * 2 ))
165 diag "Test application will emit $NR_ITER events"
166
167 # Used on sessiond launch.
168 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \
169 NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \
170 LD_PRELOAD=${TESTPOINT}"
171
172 start_lttng_sessiond_notap
173
174 # This is needed since the testpoint create a pipe with the sessiond
175 # type suffixed.
176 for f in "$TESTPOINT_BASE_PATH"*; do
177 sessiond_pipe+=("$f")
178 done
179
180 lttng_add_trigger_ok "$trigger_name" \
181 --condition event-rule-matches --type=user --name=tp:tptest \
182 --action notify
183
184 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
185 is $trigger_discarded_nb 0 "No discarded tracer notification"
186
187 # Stop consumption of notifier tracer notifications.
188 diag "Pause consumption of tracer messages"
189 echo -n 1 > $sessiond_pipe
190
191 $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT
192 ok $? "Generating $NR_ITER tracer notifications"
193
194 # Confirm that the number of tracer notifications discarded is non-zero.
195 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
196 isnt $trigger_discarded_nb 0 "Discarded tracer notification number non-zero ($trigger_discarded_nb) as expected"
197
198 # Remove the notifier.
199 lttng_remove_trigger_ok "$trigger_name"
200
201 # Confirm that no trigger is enabled.
202 list_triggers_line_count=$("$FULL_LTTNG_BIN" list-triggers | wc -l)
203 is "$list_triggers_line_count" "0" "No \`event-rule-matches\` userspace notifier enabled as expected"
204
205 # Enable another notifier and list it to confirm the counter was cleared.
206 lttng_add_trigger_ok "$trigger_name" \
207 --condition event-rule-matches --type=user --name=tp:tptest \
208 --action notify
209
210 trigger_discarded_nb=$(trigger_get_discarded_notif_number "$trigger_name")
211 is $trigger_discarded_nb 0 "No discarded tracer notification"
212
213 lttng_remove_trigger_ok "$trigger_name"
214
215 stop_lttng_sessiond_notap
216
217 unset LTTNG_SESSIOND_ENV_VARS
218}
219
220function test_ust_notifier_discarded_count_max_bucket
221{
222 start_lttng_sessiond "" "--event-notifier-error-buffer-size-userspace=3"
223
224 diag "UST event notifer error counter bucket limit"
225 for i in $(seq 3); do
226 lttng_add_trigger_ok "$i" \
227 --condition event-rule-matches --type=user --name=my_event_that_doesnt_need_to_really_exist_$i \
228 --action notify
229 done
230
231 for i in $(seq 4 5); do
232 lttng_add_trigger_fail "$i" \
233 --condition event-rule-matches --type=user --name=my_event_that_doesnt_need_to_really_exist_$i \
234 --action notify
235 done
236
237 stop_lttng_sessiond_notap
238}
239
240function test_ust_notifier_discarded_count_multi_uid
241{
242 local sessiond_pipe=()
243 local root_trigger_name="root_trigger"
244 local user_trigger_name="user_trigger"
245 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
246 local NR_USEC_WAIT=0
247 local PIPE_SIZE
248 local NR_ITER
249 local new_user="dummy_lttng_test_user"
250
251 diag "UST event notifer error counter multiple UIDs"
252
253 # Create a dummy user to run test apps as.
254 useradd --no-create-home "$new_user"
255 new_uid=$(id -u "$new_user")
256
257 PIPE_SIZE=$(get_pipe_max_size)
258
259 # Find the number of events needed to overflow the event notification
260 # pipe buffer. Each LTTng-UST notification is at least 42 bytes long.
261 # Double that number to ensure enough events are created to overflow
262 # the buffer.
263 NR_ITER=$(( (PIPE_SIZE / 42) * 2 ))
264 diag "Test applications will emit $NR_ITER events"
265
266 # Used on sessiond launch.
267 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \
268 NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \
269 LD_PRELOAD=${TESTPOINT}"
270
271 start_lttng_sessiond_notap
272
273 # This is needed since the testpoint create a pipe with the sessiond
274 # type suffixed.
275 for f in "$TESTPOINT_BASE_PATH"*; do
276 sessiond_pipe+=("$f")
277 done
278
279 lttng_add_trigger_ok "$root_trigger_name" \
280 --condition event-rule-matches --type=user --name tp:tptest \
281 --action notify
282
283 lttng_add_trigger_ok "$user_trigger_name" --owner-uid "$new_uid" \
284 --condition event-rule-matches --type=user --name tp:tptest \
285 --action notify
286
287 # Stop consumption of notifier tracer notifications.
288 echo -n 1 > $sessiond_pipe
289
290 $TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT
291 ok $? "Generating $NR_ITER tracer notifications as UID: $(id -u)"
292
293 su "$new_user" -c "$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT"
294 ok $? "Generating $NR_ITER tracer notifications as UID: $new_uid"
295
296 root_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$root_trigger_name")
297 user_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$user_trigger_name")
298
299 isnt $root_trigger_discarded_nb 0 \
300 "Root trigger discarded notifications number ($root_trigger_discarded_nb) is non-zero"
301 isnt $user_trigger_discarded_nb 0 \
302 "User trigger discarded notifications number ($user_trigger_discarded_nb) is non-zero"
303
304 lttng_remove_trigger_ok "$root_trigger_name"
305 lttng_remove_trigger_ok "$user_trigger_name" --owner-uid "$new_uid"
306
307 stop_lttng_sessiond_notap
308
309 unset LTTNG_SESSIOND_ENV_VARS
310
311 userdel "$new_user"
312 rm -f "$list_triggers_stdout"
313}
314
315function test_ust_notifier_discarded_regardless_trigger_owner
316{
317 local sessiond_pipe=()
318 local root_trigger_name="root_trigger"
319 local user_trigger_name="user_trigger"
320 local list_triggers_stdout=$(mktemp -t "list_triggers_stdout.XXXXXX")
321 local NR_USEC_WAIT=0
322 local PIPE_SIZE
323 local NR_ITER
324 local new_user="dummy_lttng_test_user"
325
326 PIPE_SIZE=$(get_pipe_max_size)
327
328 # Find the number of events needed to overflow the event notification
329 # pipe buffer. Each LTTng-UST notification is at least 42 bytes long.
330 # Double that number to ensure enough events are created to overflow
331 # the buffer.
332 NR_ITER=$(( (PIPE_SIZE / 42) * 2 ))
333 diag "Test applications will emit $NR_ITER events"
334
335 diag "UST event notifer error counter persists when a root trigger is present"
336
337 # Create a dummy user to run test apps as.
338 useradd --no-create-home "$new_user"
339 new_uid=$(id -u "$new_user")
340
341 # Used on sessiond launch.
342 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 \
343 NOTIFIER_PAUSE_PIPE_PATH=${TESTPOINT_PIPE_PATH} \
344 LD_PRELOAD=${TESTPOINT}"
345
346 start_lttng_sessiond_notap
347
348 # This is needed since the testpoint create a pipe with the sessiond
349 # type suffixed.
350 for f in "$TESTPOINT_BASE_PATH"*; do
351 sessiond_pipe+=("$f")
352 done
353
354 lttng_add_trigger_ok "$root_trigger_name" \
355 --condition event-rule-matches --type=user --name tp:tptest \
356 --action notify
357
358 # Stop consumption of notifier tracer notifications.
359 echo -n 1 > $sessiond_pipe
360
361 su "$new_user" -c "$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT"
362 ok $? "Generating $NR_ITER tracer notifications as UID: $new_uid"
363
364 root_trigger_discarded_nb=$(trigger_get_discarded_notif_number "$root_trigger_name")
365
366 isnt $root_trigger_discarded_nb 0 \
367 "Root trigger discarded notifications number ($root_trigger_discarded_nb) is non-zero"
368
369 lttng_remove_trigger_ok "$root_trigger_name"
370
371 stop_lttng_sessiond_notap
372
373 unset LTTNG_SESSIOND_ENV_VARS
374
375 userdel "$new_user"
376 rm -f "$list_triggers_stdout"
377}
378
379test_ust_notifier_discarded_count
380test_ust_notifier_discarded_count_max_bucket
381
382check_skip_kernel_test "$KERNEL_NUM_TESTS" "Skipping kernel notification tests." ||
383{
384
385 validate_lttng_modules_present
386
387 modprobe lttng-test
388
389 test_kernel_notifier_discarded_count
390 test_kernel_notifier_discarded_count_max_bucket
391
392 if destructive_tests_enabled ; then
393 # Those tests add a new user on the system. Since it's a quite
394 # intrusive change to the system, we decide to only run it when
395 # the user knows what they are doing.
396 test_ust_notifier_discarded_count_multi_uid
397 test_ust_notifier_discarded_regardless_trigger_owner
398 else
399 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
400 fi
401
402 modprobe --remove lttng-test
403
404 rm -rf "${sessiond_pipe[@]}" 2> /dev/null
405
406}
407
408rm -rf "$TEST_TMPDIR"
This page took 0.02607 seconds and 5 git commands to generate.