Fix: tests: start relayd in background mode (-b)
[lttng-tools.git] / tests / utils / utils.sh
CommitLineData
10a8a223 1#!/src/bin/bash
d3e8f6bb
DG
2#
3# Copyright (C) - 2012 David Goulet <dgoulet@efficios.com>
4#
5# This library is free software; you can redistribute it and/or modify it under
6# the terms of the GNU Lesser General Public License as published by the Free
7# Software Foundation; version 2.1 of the License.
8#
9# This library is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12# details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this library; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18SESSIOND_BIN="lttng-sessiond"
f7613992 19CONSUMERD_BIN="lttng-consumerd"
f4e40ab6 20RELAYD_BIN="lttng-relayd"
d3e8f6bb
DG
21LTTNG_BIN="lttng"
22BABELTRACE_BIN="babeltrace"
23
fd4dfcec
DG
24# Minimal kernel version supported for session daemon tests
25KERNEL_MAJOR_VERSION=2
26KERNEL_MINOR_VERSION=6
27KERNEL_PATCHLEVEL_VERSION=27
28
29655db7
CB
29source $TESTDIR/utils/tap/tap.sh
30
fec81a7e
CB
31function print_ok ()
32{
33 # Check if we are a terminal
34 if [ -t 1 ]; then
35 echo -e "\e[1;32mOK\e[0m"
36 else
37 echo -e "OK"
38 fi
39}
40
41function print_fail ()
42{
43 # Check if we are a terminal
44 if [ -t 1 ]; then
45 echo -e "\e[1;31mFAIL\e[0m"
46 else
47 echo -e "FAIL"
48 fi
49}
50
51function print_test_banner ()
52{
53 desc="$1"
29655db7 54 diag "$desc"
fec81a7e
CB
55}
56
fd4dfcec
DG
57function validate_kernel_version ()
58{
59 kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
60 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
61 return 0
62 fi
63 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
64 return 0
65 fi
66 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
67 return 0
68 fi
69 return 1
70}
71
9ac429ef 72# Generate a random string
f4e40ab6
DG
73# $1 = number of characters; defaults to 16
74# $2 = include special characters; 1 = yes, 0 = no; defaults to yes
9ac429ef 75function randstring()
f4e40ab6
DG
76{
77 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
78 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
79 echo
80}
81
f4e40ab6
DG
82function lttng_enable_kernel_event
83{
84 sess_name=$1
85 event_name=$2
07b86b52 86 channel_name=$3
f4e40ab6
DG
87
88 if [ -z $event_name ]; then
89 # Enable all event if no event name specified
29655db7 90 event_name="-a"
f4e40ab6
DG
91 fi
92
07b86b52
JD
93 if [ -z $channel_name ]; then
94 # default channel if none specified
95 chan=""
96 else
97 chan="-c $channel_name"
98 fi
99
100 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -k >/dev/null 2>&1
29655db7 101 ok $? "Enable kernel event $event_name for session $sess_name"
f4e40ab6
DG
102}
103
fb3268e3 104function start_lttng_relayd
f4e40ab6 105{
198e7caf 106 local opt=$1
173af62f 107
f4e40ab6
DG
108 DIR=$(readlink -f $TESTDIR)
109
110 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
6f137cef 111 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN -b $opt >/dev/null 2>&1
25a818af 112 #$DIR/../src/bin/lttng-relayd/$RELAYD_BIN $opt -vvv >>/tmp/relayd.log 2>&1 &
f4e40ab6 113 if [ $? -eq 1 ]; then
29655db7 114 fail "Start lttng-relayd (opt: $opt)"
f4e40ab6
DG
115 return 1
116 else
29655db7 117 pass "Start lttng-relayd (opt: $opt)"
f4e40ab6
DG
118 fi
119 else
29655db7 120 pass "Start lttng-relayd (opt: $opt)"
f4e40ab6
DG
121 fi
122}
123
fb3268e3 124function stop_lttng_relayd
f4e40ab6
DG
125{
126 PID_RELAYD=`pidof lt-$RELAYD_BIN`
127
f4e40ab6 128 kill $PID_RELAYD >/dev/null 2>&1
29655db7 129
f4e40ab6 130 if [ $? -eq 1 ]; then
29655db7 131 fail "Kill lttng-relayd (pid: $PID_RELAYD)"
f4e40ab6
DG
132 return 1
133 else
134 out=1
135 while [ -n "$out" ]; do
136 out=$(pidof lt-$RELAYD_BIN)
137 sleep 0.5
138 done
29655db7 139 pass "Kill lttng-relayd (pid: $PID_RELAYD)"
f4e40ab6
DG
140 return 0
141 fi
142}
143
fb3268e3 144function start_lttng_sessiond()
355f483d
DG
145{
146 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
147 # Env variable requested no session daemon
148 return
149 fi
150
29655db7
CB
151 validate_kernel_version
152 if [ $? -ne 0 ]; then
153 fail "Start session daemon"
154 BAIL_OUT "*** Kernel too old for session daemon tests ***"
355f483d
DG
155 fi
156
29655db7
CB
157 DIR=$(readlink -f $TESTDIR)
158
159 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
d7b63d8c 160 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --background --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
29655db7
CB
161 #$DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --verbose-consumer >>/tmp/sessiond.log 2>&1 &
162 status=$?
29655db7
CB
163 ok $status "Start session daemon"
164 fi
355f483d
DG
165}
166
fb3268e3 167function stop_lttng_sessiond ()
d3e8f6bb 168{
355f483d
DG
169 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
170 # Env variable requested no session daemon
171 return
172 fi
173
d3e8f6bb
DG
174 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
175
d3e8f6bb 176 kill $PID_SESSIOND >/dev/null 2>&1
29655db7 177
d3e8f6bb 178 if [ $? -eq 1 ]; then
29655db7 179 fail "Kill sessions daemon"
d3e8f6bb
DG
180 return 1
181 else
5fa32580
DG
182 out=1
183 while [ -n "$out" ]; do
184 out=$(pidof lt-$SESSIOND_BIN)
185 sleep 0.5
186 done
f7613992
MD
187 out=1
188 while [ -n "$out" ]; do
189 out=$(pidof $CONSUMERD_BIN)
190 sleep 0.5
191 done
29655db7 192 pass "Kill session daemon"
d3e8f6bb
DG
193 fi
194}
195
07b86b52
JD
196function create_lttng_session_no_output ()
197{
198 sess_name=$1
199
200 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name --no-output >/dev/null 2>&1
201 ok $? "Create session $sess_name in no-output mode"
202}
203
d3e8f6bb
DG
204function create_lttng_session ()
205{
206 sess_name=$1
207 trace_path=$2
208
10a8a223 209 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path >/dev/null 2>&1
29655db7 210 ok $? "Create session $sess_name in $trace_path"
d4018451
DG
211}
212
29655db7 213function enable_ust_lttng_channel()
d4018451
DG
214{
215 sess_name=$1
216 channel_name=$2
217
29655db7
CB
218 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
219 ok $? "Enable channel $channel_name for session $sess_name"
d4018451
DG
220}
221
29655db7 222function disable_ust_lttng_channel()
d4018451
DG
223{
224 sess_name=$1
225 channel_name=$2
226
29655db7
CB
227 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel -u $channel_name -s $sess_name >/dev/null 2>&1
228 ok $? "Disable channel $channel_name for session $sess_name"
d3e8f6bb
DG
229}
230
07b86b52
JD
231function enable_lttng_mmap_overwrite_kernel_channel()
232{
233 sess_name=$1
234 channel_name=$2
235
236 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -k --output mmap --overwrite >/dev/null 2>&1
237 ok $? "Enable channel $channel_name for session $sess_name"
238}
239
ebaaaf5e
JD
240function enable_lttng_mmap_overwrite_ust_channel()
241{
242 sess_name=$1
243 channel_name=$2
244
245 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel -s $sess_name $channel_name -u --output mmap --overwrite >/dev/null 2>&1
246 ok $? "Enable channel $channel_name for session $sess_name"
247}
248
d3e8f6bb
DG
249function enable_ust_lttng_event ()
250{
251 sess_name=$1
26b53d3b 252 event_name="$2"
ebaaaf5e
JD
253 channel_name=$3
254
255 if [ -z $channel_name ]; then
256 # default channel if none specified
257 chan=""
258 else
259 chan="-c $channel_name"
260 fi
d3e8f6bb 261
ebaaaf5e 262 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -u >/dev/null 2>&1
29655db7 263 ok $? "Enable event $event_name for session $sess_name"
26b53d3b
DG
264}
265
37175ce4
DG
266function enable_jul_lttng_event()
267{
268 sess_name=$1
269 event_name="$2"
270 channel_name=$3
271
272 if [ -z $channel_name ]; then
273 # default channel if none specified
274 chan=""
275 else
276 chan="-c $channel_name"
277 fi
278
279 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -j >/dev/null 2>&1
280 ok $? "Enable JUL event $event_name for session $sess_name"
281}
282
26b53d3b
DG
283function enable_ust_lttng_event_filter()
284{
285 sess_name="$1"
286 event_name="$2"
287 filter="$3"
26b53d3b 288
29655db7
CB
289 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --filter "$filter" >/dev/null 2>&1
290 ok $? "Enable event $event_name with filtering for session $sess_name"
26b53d3b
DG
291}
292
293function enable_ust_lttng_event_loglevel()
294{
295 sess_name="$1"
296 event_name="$2"
297 loglevel="$3"
26b53d3b 298
29655db7
CB
299 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel $loglevel >/dev/null 2>&1
300 ok $? "Enable event $event_name with loglevel $loglevel"
26b53d3b
DG
301}
302
303function enable_ust_lttng_event_loglevel_only()
304{
305 sess_name="$1"
306 event_name="$2"
307 loglevel="$3"
26b53d3b 308
29655db7
CB
309 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" -s $sess_name -u --loglevel-only $loglevel >/dev/null 2>&1
310 ok $? "Enable event $event_name with loglevel-only $loglevel"
26b53d3b
DG
311}
312
313function disable_ust_lttng_event ()
314{
907b42de
DG
315 sess_name="$1"
316 event_name="$2"
26b53d3b 317
907b42de 318 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -u >/dev/null 2>&1
29655db7 319 ok $? "Disable event $event_name for session $sess_name"
d3e8f6bb
DG
320}
321
fb3268e3 322function start_lttng_tracing ()
d3e8f6bb
DG
323{
324 sess_name=$1
325
10a8a223 326 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >/dev/null 2>&1
29655db7 327 ok $? "Start tracing for session $sess_name"
d3e8f6bb
DG
328}
329
fb3268e3 330function stop_lttng_tracing ()
d3e8f6bb
DG
331{
332 sess_name=$1
333
10a8a223 334 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >/dev/null 2>&1
29655db7 335 ok $? "Stop lttng tracing for session $sess_name"
d3e8f6bb
DG
336}
337
338function destroy_lttng_session ()
339{
340 sess_name=$1
341
29655db7 342
10a8a223 343 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >/dev/null 2>&1
29655db7 344 ok $? "Destroy lttng session $sess_name"
d3e8f6bb
DG
345}
346
07b86b52
JD
347function lttng_snapshot_add_output ()
348{
349 sess_name=$1
350 trace_path=$2
351
352 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot add-output -s $sess_name file://$trace_path >/dev/null 2>&1
353 ok $? "Added snapshot output file://$trace_path"
354}
355
26402e0c
DG
356function lttng_snapshot_del_output ()
357{
358 local sess_name=$1
359 local id=$2
360
361 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot del-output -s $sess_name $id >/dev/null 2>&1
362 ok $? "Deleted snapshot output id $id"
363}
364
07b86b52
JD
365function lttng_snapshot_record ()
366{
367 sess_name=$1
368 trace_path=$2
369
370 $TESTDIR/../src/bin/lttng/$LTTNG_BIN snapshot record -s $sess_name >/dev/null 2>&1
371 ok $? "Snapshot recorded"
372}
373
d3e8f6bb
DG
374function trace_matches ()
375{
376 event_name=$1
377 nr_iter=$2
378 trace_path=$3
379
317eef93 380 which $BABELTRACE_BIN >/dev/null
29655db7 381 skip $? -ne 0 "Babeltrace binary not found. Skipping trace matches"
d3e8f6bb
DG
382
383 count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
29655db7 384
d3e8f6bb 385 if [ "$count" -ne "$nr_iter" ]; then
29655db7
CB
386 fail "Trace match"
387 diag "$count events found in trace"
d3e8f6bb 388 else
29655db7 389 pass "Trace match"
d3e8f6bb
DG
390 fi
391}
f4e40ab6
DG
392
393function validate_trace
394{
395 event_name=$1
396 trace_path=$2
397
398 which $BABELTRACE_BIN >/dev/null
29655db7
CB
399 if [ $? -ne 0 ]; then
400 skip 0 "Babeltrace binary not found. Skipping trace validation"
f4e40ab6
DG
401 fi
402
07b86b52
JD
403 OLDIFS=$IFS
404 IFS=","
405 for i in $event_name; do
406 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $i | wc -l)
407 if [ "$traced" -ne 0 ]; then
ebaaaf5e 408 pass "Validate trace for event $i, $traced events"
07b86b52
JD
409 else
410 fail "Validate trace for event $i"
411 diag "Found $traced occurences of $i"
412 fi
413 done
414 ret=$?
415 IFS=$OLDIFS
416 return $ret
f4e40ab6 417}
This page took 0.05722 seconds and 5 git commands to generate.