Fix: Wrong assumption about possible CPUs
[lttng-tools.git] / tests / utils / utils.sh
index 44cc31404b42dc460b2e3c3a8638622f457f4a56..db1a307afb69ae07426b01b9bdce8358cffd7652 100644 (file)
@@ -209,6 +209,74 @@ function randstring()
        echo
 }
 
+# Helpers for get_possible_cpus.
+function get_possible_cpus_count_from_sysfs_possible_mask()
+{
+       local max_possible_cpu_id
+
+       # The Awk script extracts the highest CPU id from the possible CPU
+       # mask. Assuming a numerical order, a field separator '-' and a record
+       # separator ','. The last value parsed is the highest id.
+       if [ -f /sys/devices/system/cpu/possible ]; then
+               max_possible_cpu_id=$(awk -F '-' 'BEGIN { RS = ","} { last = $NF } END { printf("%d\n", last) }' \
+                                     /sys/devices/system/cpu/possible)
+               echo "$((max_possible_cpu_id+1))"
+       else
+               echo "0"
+       fi
+}
+
+# This is a fallback if the possible CPU mask is not available. This will not
+# take into account unplugged CPUs.
+function get_max_cpus_count_from_sysfs_cpu_directories()
+{
+       local max_possible_cpu_id=0
+       local current_cpu_id
+
+       for i in /sys/devices/system/cpu/cpu[0-9]*; do
+               current_cpu_id="${i#/sys/devices/system/cpu/cpu}"
+               if [ "$current_cpu_id" -gt "$max_possible_cpu_id" ]; then
+                       max_possible_cpu_id="$current_cpu_id"
+               fi
+       done
+
+       echo "$((max_possible_cpu_id+1))"
+}
+
+# Return the number of possible CPUs.
+function get_possible_cpus_count()
+{
+       local possible_cpus_count
+       possible_cpus_count=$(get_possible_cpus_count_from_sysfs_possible_mask)
+
+       if [ "$possible_cpus_count" -eq "0" ]; then
+               local configured_cpus_count
+               configured_cpus_count=$(getconf _NPROCESSORS_CONF)
+               possible_cpus_count=$(get_max_cpus_count_from_sysfs_cpu_directories)
+               possible_cpus_count=$((configured_cpus_count > possible_cpus_count \
+                                                            ? configured_cpus_count \
+                                                            : possible_cpus_count))
+       fi
+
+       echo "$possible_cpus_count"
+}
+
+# Return the list of exposed CPU.
+#
+# NOTE! Use it like so:
+#
+# IFS=" " read -r -a VARIABLE <<< "$(get_exposed_cpus_list)"
+function get_exposed_cpus_list()
+{
+       local list=()
+
+       for i in /sys/devices/system/cpu/cpu[0-9]*; do
+               list+=("${i#/sys/devices/system/cpu/cpu}")
+       done
+
+       echo "${list[@]}"
+}
+
 # Return the number of _configured_ CPUs.
 function conf_proc_count()
 {
@@ -238,6 +306,20 @@ function validate_lttng_modules_present ()
        LTTNG_BAIL_OUT "LTTng modules not detected."
 }
 
+# Run the lttng binary.
+#
+# The first two arguments are stdout and stderr redirect paths, respectively.
+# The rest of the arguments are forwarded to the lttng binary
+function _run_lttng_cmd
+{
+       local stdout_dest="$1"
+       local stderr_dest="$2"
+       shift 2
+
+       diag "$TESTDIR/../src/bin/lttng/$LTTNG_BIN $*"
+       $TESTDIR/../src/bin/lttng/$LTTNG_BIN "$@" 1> "$stdout_dest" 2> "$stderr_dest"
+}
+
 function enable_kernel_lttng_event
 {
        local withtap="$1"
@@ -1006,9 +1088,25 @@ function sigstop_lttng_consumerd_notap()
 
 function list_lttng_with_opts ()
 {
+       local ret
+       local withtap=$1
+       shift
        local opts=$1
        $TESTDIR/../src/bin/lttng/$LTTNG_BIN list $opts 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
-       ok $? "Lttng-tool list command with option $opts"
+       ret=$?
+       if [ $withtap -eq "1" ]; then
+               ok $ret "Lttng-tool list command with option $opts"
+       fi
+}
+
+function list_lttng_ok ()
+{
+       list_lttng_with_opts 1 "$@"
+}
+
+function list_lttng_notap ()
+{
+       list_lttng_with_opts 0 "$@"
 }
 
 function create_lttng_session_no_output ()
@@ -1243,37 +1341,80 @@ function enable_jul_lttng_event_loglevel()
 
 function enable_log4j_lttng_event()
 {
-       sess_name=$1
-       event_name="$2"
-       channel_name=$3
+       local sess_name=$1
+       local event_name=$2
+       local channel_name=$3
 
-       if [ -z $channel_name ]; then
-               # default channel if none specified
-               chan=""
-       else
-               chan="-c $channel_name"
+       local chan_opt=()
+
+       # default channel if none specified
+       if [ -n "$channel_name" ]; then
+               chan_opt=("-c" "$channel_name")
        fi
 
-       $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event "$event_name" $chan -s $sess_name -l 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
-       ok $? "Enable LOG4J event $event_name for session $sess_name"
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               enable-event "$event_name" "${chan_opt[@]}" -s "$sess_name" --log4j
+       ok $? "Enable LOG4J event '$event_name' for session '$sess_name'"
+}
+
+function enable_log4j_lttng_event_filter()
+{
+       local sess_name=$1
+       local event_name=$2
+       local filter=$3
+
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               enable-event "$event_name" -s "$sess_name" --log4j --filter "$filter"
+       ok $? "Enable LOG4J event '$event_name' with filter '$filter' for session '$sess_name'"
+}
+
+function enable_log4j_lttng_event_filter_loglevel_only()
+{
+       local sess_name=$1
+       local event_name=$2
+       local filter=$3
+       local loglevel=$4
+
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               enable-event --loglevel-only "$loglevel" "$event_name" -s "$sess_name" -l --filter "$filter"
+       ok $? "Enable LOG4J event '$event_name' with filter '$filter' and loglevel-only '$loglevel' for session '$sess_name'"
 }
 
 function enable_log4j_lttng_event_loglevel()
 {
        local sess_name=$1
-       local event_name="$2"
+       local event_name=$2
        local loglevel=$3
        local channel_name=$4
 
-       if [ -z $channel_name ]; then
-               # default channel if none specified
-               chan=""
-       else
-               chan="-c $channel_name"
+
+       # default channel if none specified
+       if [ -n "$channel_name" ]; then
+               chan_opt=("-c" "$channel_name")
        fi
 
-       $TESTDIR/../src/bin/lttng/$LTTNG_BIN enable-event --loglevel $loglevel "$event_name" $chan -s $sess_name -l 1> $OUTPUT_DEST 2> $ERROR_OUTPUT_DEST
-       ok $? "Enable LOG4J event $event_name for session $sess_name with loglevel $loglevel"
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               enable-event --loglevel "$loglevel" "$event_name" "${chan_opt[@]}" -s "$sess_name" --log4j
+       ok $? "Enable LOG4J event '$event_name' for session '$sess_name' with loglevel '$loglevel'"
+}
+
+function enable_log4j_lttng_event_loglevel_only()
+{
+       local sess_name=$1
+       local event_name=$2
+       local loglevel=$3
+       local channel_name=$4
+
+       local chan_opt=()
+
+       # default channel if none specified
+       if [ -n "$channel_name" ]; then
+               chan_opt=("-c" "$channel_name")
+       fi
+
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               enable-event --loglevel-only "$loglevel" "$event_name" "${chan_opt[@]}" -s "$sess_name" --log4j
+       ok $? "Enable LOG4J event '$event_name' for session '$sess_name' with loglevel-only '$loglevel'"
 }
 
 function enable_python_lttng_event()
@@ -1380,8 +1521,9 @@ function disable_log4j_lttng_event ()
        local sess_name="$1"
        local event_name="$2"
 
-       $TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -l >/dev/null 2>&1
-       ok $? "Disable LOG4J event $event_name for session $sess_name"
+       _run_lttng_cmd "$OUTPUT_DEST" "$ERROR_OUTPUT_DEST" \
+               disable-event "$event_name" -s "$sess_name" --log4j
+       ok $? "Disable LOG4J event '$event_name' for session '$sess_name'"
 }
 
 function disable_python_lttng_event ()
@@ -1785,6 +1927,14 @@ function wait_live_viewer_connect ()
        pass "Waiting for live viewers on url: $url"
 }
 
+function bail_out_if_no_babeltrace()
+{
+       which "$BABELTRACE_BIN" >/dev/null
+       if [ $? -ne 0 ]; then
+               LTTNG_BAIL_OUT "\"$BABELTRACE_BIN\" binary not found. Skipping tests"
+       fi
+}
+
 function validate_metadata_event ()
 {
        local event_name=$1
@@ -1865,7 +2015,7 @@ function validate_trace
                        pass "Validate trace for event $i, $traced events"
                else
                        fail "Validate trace for event $i"
-                       diag "Found $traced occurences of $i"
+                       diag "Found $traced occurrences of $i"
                fi
        done
        ret=$?
@@ -1893,7 +2043,7 @@ function validate_trace_count
                        pass "Validate trace for event $i, $traced events"
                else
                        fail "Validate trace for event $i"
-                       diag "Found $traced occurences of $i"
+                       diag "Found $traced occurrences of $i"
                fi
                cnt=$(($cnt + $traced))
        done
@@ -1923,7 +2073,7 @@ function validate_trace_count_range_incl_min_excl_max
                        pass "Validate trace for event $i, $traced events"
                else
                        fail "Validate trace for event $i"
-                       diag "Found $traced occurences of $i"
+                       diag "Found $traced occurrences of $i"
                fi
                cnt=$(($cnt + $traced))
        done
@@ -1957,7 +2107,7 @@ function validate_trace_exp()
                pass "Validate trace for expression '${event_exp}', $traced events"
        else
                fail "Validate trace for expression '${event_exp}'"
-               diag "Found $traced occurences of '${event_exp}'"
+               diag "Found $traced occurrences of '${event_exp}'"
        fi
        ret=$?
        return $ret
This page took 0.026198 seconds and 4 git commands to generate.