Add UST and kernel streaming tests
[lttng-tools.git] / tests / utils.sh
1 #!/src/bin/bash
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
18 SESSIOND_BIN="lttng-sessiond"
19 RELAYD_BIN="lttng-relayd"
20 LTTNG_BIN="lttng"
21 BABELTRACE_BIN="babeltrace"
22
23 # Minimal kernel version supported for session daemon tests
24 KERNEL_MAJOR_VERSION=2
25 KERNEL_MINOR_VERSION=6
26 KERNEL_PATCHLEVEL_VERSION=27
27
28 function validate_kernel_version ()
29 {
30 kern_version=($(uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }' | tr '.' '\n'))
31 if [ ${kern_version[0]} -gt $KERNEL_MAJOR_VERSION ]; then
32 return 0
33 fi
34 if [ ${kern_version[1]} -gt $KERNEL_MINOR_VERSION ]; then
35 return 0
36 fi
37 if [ ${kern_version[2]} -ge $KERNEL_PATCHLEVEL_VERSION ]; then
38 return 0
39 fi
40 return 1
41 }
42
43 # Generate a random string
44 # $1 = number of characters; defaults to 16
45 # $2 = include special characters; 1 = yes, 0 = no; defaults to yes
46 function randstring()
47 {
48 [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
49 cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-16}
50 echo
51 }
52
53 function spawn_sessiond ()
54 {
55 echo ""
56 echo -n "Starting session daemon... "
57 validate_kernel_version
58 if [ $? -ne 0 ]; then
59 echo -e "\n*** Kernel too old for session daemon tests ***\n"
60 return 2
61 fi
62
63 DIR=$(readlink -f $TESTDIR)
64
65 if [ -z $(pidof lt-$SESSIOND_BIN) ]; then
66 $DIR/../src/bin/lttng-sessiond/$SESSIOND_BIN --daemonize --quiet --consumerd32-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd" --consumerd64-path="$DIR/../src/bin/lttng-consumerd/lttng-consumerd"
67 if [ $? -eq 1 ]; then
68 echo -e "\e[1;31mFAILED\e[0m"
69 return 1
70 else
71 echo -e "\e[1;32mOK\e[0m"
72 fi
73 fi
74
75 return 0
76 }
77
78 function lttng_enable_kernel_event
79 {
80 sess_name=$1
81 event_name=$2
82
83 if [ -z $event_name ]; then
84 # Enable all event if no event name specified
85 $event_name="-a"
86 fi
87
88 echo -n "Enabling kernel event $event_name for session $sess_name"
89 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $event_name -s $sess_name -k >/dev/null 2>&1
90 if [ $? -eq 1 ]; then
91 echo -e '\e[1;31mFAILED\e[0m'
92 return 1
93 else
94 echo -e "\e[1;32mOK\e[0m"
95 fi
96 }
97
98 function lttng_start_relayd
99 {
100 echo -e -n "Starting lttng-relayd... "
101 DIR=$(readlink -f $TESTDIR)
102
103 if [ -z $(pidof lt-$RELAYD_BIN) ]; then
104 $DIR/../src/bin/lttng-relayd/$RELAYD_BIN >/dev/null 2>&1 &
105 if [ $? -eq 1 ]; then
106 echo -e "\e[1;31mFAILED\e[0m"
107 return 1
108 else
109 echo -e "\e[1;32mOK\e[0m"
110 fi
111 else
112 echo -e "\e[1;32mOK\e[0m"
113 fi
114 }
115
116 function lttng_stop_relayd
117 {
118 PID_RELAYD=`pidof lt-$RELAYD_BIN`
119
120 echo -e -n "Killing lttng-relayd (pid: $PID_RELAYD)... "
121 kill $PID_RELAYD >/dev/null 2>&1
122 if [ $? -eq 1 ]; then
123 echo -e "\e[1;31mFAILED\e[0m"
124 return 1
125 else
126 out=1
127 while [ -n "$out" ]; do
128 out=$(pidof lt-$RELAYD_BIN)
129 sleep 0.5
130 done
131 echo -e "\e[1;32mOK\e[0m"
132 return 0
133 fi
134 }
135
136 function start_sessiond()
137 {
138 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
139 # Env variable requested no session daemon
140 return
141 fi
142
143 spawn_sessiond
144 out=$?
145 if [ $out -eq 2 ]; then
146 # Kernel version is not compatible.
147 exit 0
148 elif [ $out -ne 0 ]; then
149 echo "NOT bad $?"
150 exit 1
151 fi
152
153 # Simply wait for the session daemon bootstrap
154 echo "Waiting for the session daemon to bootstrap (2 secs)"
155 sleep 2
156 }
157
158 function stop_sessiond ()
159 {
160 if [ -n $TEST_NO_SESSIOND ] && [ "$TEST_NO_SESSIOND" == "1" ]; then
161 # Env variable requested no session daemon
162 return
163 fi
164
165 PID_SESSIOND=`pidof lt-$SESSIOND_BIN`
166
167 echo -e -n "Killing session daemon... "
168 kill $PID_SESSIOND >/dev/null 2>&1
169 if [ $? -eq 1 ]; then
170 echo -e "\e[1;31mFAILED\e[0m"
171 return 1
172 else
173 out=1
174 while [ -n "$out" ]; do
175 out=$(pidof lt-$SESSIOND_BIN)
176 sleep 0.5
177 done
178 echo -e "\e[1;32mOK\e[0m"
179 fi
180 }
181
182 function create_lttng_session ()
183 {
184 sess_name=$1
185 trace_path=$2
186
187 echo -n "Creating lttng session $sess_name in $trace_path "
188 $TESTDIR/../src/bin/lttng/$LTTNG_BIN create $sess_name -o $trace_path >/dev/null 2>&1
189 if [ $? -eq 1 ]; then
190 echo -e "\e[1;31mFAILED\e[0m"
191 return 1
192 else
193 echo -e "\e[1;32mOK\e[0m"
194 fi
195 }
196
197 function enable_lttng_channel()
198 {
199 sess_name=$1
200 channel_name=$2
201
202 echo -n "Enabling lttng channel $channel_name for session $sess_name"
203 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-channel $channel_name -s $sess_name >/dev/null 2>&1
204 if [ $? -eq 1 ]; then
205 echo -e "\e[1;31mFAILED\e[0m"
206 return 1
207 else
208 echo -e "\e[1;32mOK\e[0m"
209 fi
210 }
211
212 function disable_lttng_channel()
213 {
214 sess_name=$1
215 channel_name=$2
216
217 echo -n "Disabling lttng channel $channel_name for session $sess_name"
218 $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-channel $channel_name -s $sess_name >/dev/null 2>&1
219 if [ $? -eq 1 ]; then
220 echo -e "\e[1;31mFAILED\e[0m"
221 return 1
222 else
223 echo -e "\e[1;32mOK\e[0m"
224 fi
225 }
226
227 function enable_ust_lttng_event ()
228 {
229 sess_name=$1
230 event_name=$2
231
232 echo -n "Enabling lttng event $event_name for session $sess_name "
233 $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event $event_name -s $sess_name -u >/dev/null 2>&1
234 if [ $? -eq 1 ]; then
235 echo -e '\e[1;31mFAILED\e[0m'
236 return 1
237 else
238 echo -e "\e[1;32mOK\e[0m"
239 fi
240 }
241
242 function start_tracing ()
243 {
244 sess_name=$1
245
246 echo -n "Start lttng tracing for session $sess_name "
247 $TESTDIR/../src/bin/lttng/$LTTNG_BIN start $sess_name >/dev/null 2>&1
248 if [ $? -eq 1 ]; then
249 echo -e '\e[1;31mFAILED\e[0m'
250 return 1
251 else
252 echo -e "\e[1;32mOK\e[0m"
253 fi
254 }
255
256 function stop_tracing ()
257 {
258 sess_name=$1
259
260 echo -n "Stop lttng tracing for session $sess_name "
261 $TESTDIR/../src/bin/lttng/$LTTNG_BIN stop $sess_name >/dev/null 2>&1
262 if [ $? -eq 1 ]; then
263 echo -e '\e[1;31mFAILED\e[0m'
264 return 1
265 else
266 echo -e "\e[1;32mOK\e[0m"
267 fi
268 }
269
270 function destroy_lttng_session ()
271 {
272 sess_name=$1
273
274 echo -n "Destroy lttng session $sess_name "
275 $TESTDIR/../src/bin/lttng/$LTTNG_BIN destroy $sess_name >/dev/null 2>&1
276 if [ $? -eq 1 ]; then
277 echo -e '\e[1;31mFAILED\e[0m'
278 return 1
279 else
280 echo -e "\e[1;32mOK\e[0m"
281 fi
282 }
283
284 function trace_matches ()
285 {
286 event_name=$1
287 nr_iter=$2
288 trace_path=$3
289
290 which $BABELTRACE_BIN >/dev/null
291 if [ $? -eq 1 ]; then
292 echo "Babeltrace binary not found. Skipping trace matches"
293 return 0
294 fi
295
296 echo -n "Looking for $nr_iter $event_name in $trace_path "
297
298 count=$($BABELTRACE_BIN $trace_path | grep $event_name | wc -l)
299 if [ "$count" -ne "$nr_iter" ]; then
300 echo -e "$count found in trace \e[1;31mFAILED\e[0m"
301 return 1
302 else
303 echo -e "Trace is coherent \e[1;32mOK\e[0m"
304 return 0
305 fi
306 }
307
308 function validate_trace
309 {
310 event_name=$1
311 trace_path=$2
312
313 which $BABELTRACE_BIN >/dev/null
314 if [ $? -eq 1 ]; then
315 echo "Babeltrace binary not found. Skipping trace matches"
316 return 0
317 fi
318
319 echo -n "Validating trace for event $event_name... "
320 traced=$($BABELTRACE_BIN $trace_path 2>/dev/null | grep $event_name | wc -l)
321 if [ $traced -eq 0 ]; then
322 echo -e "\e[1;31mFAILED\e[0m"
323 return 1
324 else
325 echo -e "\e[1;32mOK\e[0m"
326 return 0
327 fi
328 }
This page took 0.036191 seconds and 5 git commands to generate.