| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2012 Christian Babeux <christian.babeux@efficios.com> |
| 4 | # Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 5 | # |
| 6 | # SPDX-License-Identifier: LGPL-2.1-only |
| 7 | |
| 8 | TEST_DESC="Streaming - High throughput with bandwidth limits" |
| 9 | |
| 10 | CURDIR=$(dirname $0)/ |
| 11 | TESTDIR=$CURDIR/../../.. |
| 12 | NR_APP_ITER=10 |
| 13 | NR_ITER=1000000 |
| 14 | TESTAPP_PATH="$TESTDIR/utils/testapp" |
| 15 | TESTAPP_NAME="gen-ust-events" |
| 16 | TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME" |
| 17 | SESSION_NAME="high-throughput" |
| 18 | EVENT_NAME="tp:tptest" |
| 19 | SESSIOND_CTRL_PORT=5342 |
| 20 | SESSIOND_DATA_PORT=5343 |
| 21 | DEFAULT_IF="lo" |
| 22 | |
| 23 | TRACE_PATH=$(mktemp -d -t tmp.test_streaming_high_throughput_limits_trace_path.XXXXXX) |
| 24 | |
| 25 | NUM_TESTS=104 |
| 26 | |
| 27 | source $TESTDIR/utils/utils.sh |
| 28 | |
| 29 | if [ ! -x "$TESTAPP_BIN" ]; then |
| 30 | BAIL_OUT "No UST events binary detected." |
| 31 | fi |
| 32 | |
| 33 | function reset_bw_limit |
| 34 | { |
| 35 | tc qdisc del dev $DEFAULT_IF root >/dev/null 2>&1 |
| 36 | return $? |
| 37 | } |
| 38 | |
| 39 | function set_bw_limit |
| 40 | { |
| 41 | limit=$1 |
| 42 | ctrlportlimit=$(($limit/10)) |
| 43 | # failsafe to have at least 1kbit/s for control (in the case where $1 < 10) |
| 44 | [ $ctrlportlimit = 0 ] && ctrlportlimit=1 |
| 45 | # if $1 < 10, we might bust the limit set here, but the |
| 46 | # parent qdisc (1:) will always limit us to the right max value |
| 47 | dataportlimit=$((9*${ctrlportlimit})) |
| 48 | |
| 49 | diag "Set bandwidth limits to ${limit}kbits, ${ctrlportlimit} for control and ${dataportlimit} for data" |
| 50 | |
| 51 | tc qdisc add dev $DEFAULT_IF root handle 1: htb default 15 >/dev/null 2>&1 |
| 52 | if [ $? -ne 0 ]; then |
| 53 | reset_bw_limit |
| 54 | return 1 |
| 55 | fi |
| 56 | |
| 57 | # the total bandwidth is the limit set by the user |
| 58 | tc class add dev $DEFAULT_IF parent 1: classid 1:1 htb rate ${limit}kbit ceil ${limit}kbit >/dev/null 2>&1 |
| 59 | if [ $? -ne 0 ]; then |
| 60 | reset_bw_limit |
| 61 | return 1 |
| 62 | fi |
| 63 | # 1/10 of the bandwidth guaranteed and traffic prioritized for the control port |
| 64 | tc class add dev $DEFAULT_IF parent 1:1 classid 1:10 htb rate ${ctrlportlimit}kbit ceil ${limit}kbit prio 1 >/dev/null 2>&1 |
| 65 | if [ $? -ne 0 ]; then |
| 66 | reset_bw_limit |
| 67 | return 1 |
| 68 | fi |
| 69 | # 9/10 of the bandwidth guaranteed and can borrow up to the total bandwidth (if unused) |
| 70 | tc class add dev $DEFAULT_IF parent 1:1 classid 1:11 htb rate ${dataportlimit}kbit ceil ${limit}kbit prio 2 >/dev/null 2>&1 |
| 71 | if [ $? -ne 0 ]; then |
| 72 | reset_bw_limit |
| 73 | return 1 |
| 74 | fi |
| 75 | |
| 76 | # filter to assign control traffic to the 1:10 class |
| 77 | tc filter add dev $DEFAULT_IF parent 1: protocol ip u32 match ip dport $SESSIOND_CTRL_PORT 0xffff flowid 1:10 >/dev/null 2>&1 |
| 78 | if [ $? -ne 0 ]; then |
| 79 | reset_bw_limit |
| 80 | return 1 |
| 81 | fi |
| 82 | # filter to assign data traffic to the 1:11 class |
| 83 | tc filter add dev $DEFAULT_IF parent 1: protocol ip u32 match ip dport $SESSIOND_DATA_PORT 0xffff flowid 1:11 >/dev/null 2>&1 |
| 84 | if [ $? -ne 0 ]; then |
| 85 | reset_bw_limit |
| 86 | return 1 |
| 87 | fi |
| 88 | |
| 89 | return 0 |
| 90 | } |
| 91 | |
| 92 | function create_lttng_session_with_uri |
| 93 | { |
| 94 | sess_name=$1 |
| 95 | uri=$2 |
| 96 | # Create session with custom URI |
| 97 | $TESTDIR/../src/bin/lttng/$LTTNG_BIN create -U $uri $sess_name >/dev/null 2>&1 |
| 98 | ok $? "Create session with uri $uri" |
| 99 | } |
| 100 | |
| 101 | function run_apps |
| 102 | { |
| 103 | for i in `seq 1 $NR_APP_ITER`; do |
| 104 | # With bandwidth limitation, unfortunately, application easily timeout |
| 105 | # due to very slow communication between the consumer and relayd making |
| 106 | # the status reply from the consumer quite slow thus delaying the |
| 107 | # registration done message. |
| 108 | LTTNG_UST_REGISTER_TIMEOUT=-1 $TESTAPP_BIN -i $NR_ITER & >/dev/null 2>&1 |
| 109 | done |
| 110 | } |
| 111 | |
| 112 | function test_high_throughput |
| 113 | { |
| 114 | NETWORK_URI="net://localhost" |
| 115 | create_lttng_session_with_uri $SESSION_NAME $NETWORK_URI |
| 116 | enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME |
| 117 | start_lttng_tracing_ok $SESSION_NAME |
| 118 | run_apps |
| 119 | diag "Waiting for applications to end" |
| 120 | wait |
| 121 | pass "waiting done" |
| 122 | stop_lttng_tracing_ok $SESSION_NAME |
| 123 | destroy_lttng_session_ok $SESSION_NAME |
| 124 | validate_event_count |
| 125 | } |
| 126 | |
| 127 | function validate_event_count |
| 128 | { |
| 129 | TEMP_FILE=$(mktemp -t tmp.streaming_high_throughput_limit_file1.XXXXXX) |
| 130 | TEMP_FILE_2=$(mktemp -t tmp.streaming_high_throughput_limit_file2.XXXXXX) |
| 131 | |
| 132 | traced=$("$BABELTRACE_BIN" $TRACE_PATH 2>/dev/null | wc -l) |
| 133 | "$BABELTRACE_BIN" $TRACE_PATH >/dev/null 2>$TEMP_FILE_2 |
| 134 | |
| 135 | cat $TEMP_FILE_2 | cut -f4 -d " " >$TEMP_FILE |
| 136 | |
| 137 | dropped=0 |
| 138 | while read line; |
| 139 | do |
| 140 | let dropped=$dropped+$line |
| 141 | done < $TEMP_FILE |
| 142 | |
| 143 | let total=$dropped+$traced |
| 144 | let wanted=$NR_APP_ITER*$NR_ITER |
| 145 | |
| 146 | if [ $wanted -ne $total ]; then |
| 147 | fail "Validate trace event count" |
| 148 | diag "Expected $wanted. Dropped $dropped. Recorded $traced. Total $total... " |
| 149 | return 1 |
| 150 | else |
| 151 | pass "Validate trace event count" |
| 152 | diag "Expected $wanted. Dropped $dropped. Recorded $traced. Total $total... " |
| 153 | |
| 154 | rm -rf $TRACE_PATH |
| 155 | rm $TEMP_FILE $TEMP_FILE_2 |
| 156 | |
| 157 | return 0 |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | function interrupt_cleanup() |
| 162 | { |
| 163 | diag "*** Exiting ***" |
| 164 | reset_bw_limit |
| 165 | # invoke utils cleanup |
| 166 | full_cleanup |
| 167 | } |
| 168 | |
| 169 | plan_tests $NUM_TESTS |
| 170 | |
| 171 | print_test_banner "$TEST_DESC" |
| 172 | |
| 173 | bail_out_if_no_babeltrace |
| 174 | |
| 175 | if [ "$(id -u)" == "0" ]; then |
| 176 | isroot=1 |
| 177 | else |
| 178 | isroot=0 |
| 179 | fi |
| 180 | |
| 181 | skip $isroot "Root access is needed to set bandwith limits. Skipping all tests." $NUM_TESTS || |
| 182 | { |
| 183 | |
| 184 | # Catch sigint and try to cleanup limits |
| 185 | trap interrupt_cleanup SIGTERM SIGINT |
| 186 | |
| 187 | BW_LIMITS=(3200 1600 800 400 200 100 50 25) |
| 188 | for BW in ${BW_LIMITS[@]}; |
| 189 | do |
| 190 | diag "Test high-throughput with bandwidth limit set to ${BW}kbits" |
| 191 | |
| 192 | set_bw_limit $BW |
| 193 | ok $? "Setting bandwidth limit" |
| 194 | |
| 195 | start_lttng_sessiond |
| 196 | start_lttng_relayd "-o $TRACE_PATH" |
| 197 | test_high_throughput |
| 198 | result=$? |
| 199 | stop_lttng_relayd |
| 200 | stop_lttng_sessiond |
| 201 | reset_bw_limit |
| 202 | ok $? "Reset bandwith limits" |
| 203 | done |
| 204 | } |