Tests: Add get_possible_cpus_count utility
[lttng-tools.git] / tests / utils / utils.sh
index 1e7d88b76c6a74aa373793003a1b56aca42642ab..30770e58905d4c977af2aa07394f11328e39495a 100644 (file)
@@ -209,6 +209,38 @@ function randstring()
        echo
 }
 
+# Helpers for get_possible_cpus.
+function get_possible_cpus_count_from_sysfs_possible_mask()
+{
+       local max_possible_cpu_id=$(cut -d '-' -f 2 < /sys/devices/system/cpu/possible)
+       echo $((max_possible_cpu_id+1))
+}
+
+function get_max_cpus_count_from_sysfs_cpu_directories()
+{
+       local max_possible_cpu_id= \
+               $(find /sys/devices/system/cpu/ -mindepth 1 -maxdepth 1 -regex ".+cpu[0-9]+" | \
+                 sed -e 's/cpu//g' | \
+                 awk -F '/' '{ if ($NF > N) N = $NF } END { print N }')
+       echo $((max_possible_cpu_id+1))
+}
+
+# Return the number of possible CPUs.
+function get_possible_cpus_count()
+{
+       local possible_cpus_count=$(get_possible_cpus_count_from_sysfs_possible_mask)
+
+       if [ $? -ne 0 ]; then
+               possible_cpus_count=$(get_max_cpus_count_from_sysfs_cpu_directories)
+               local configured_cpus_count=$(getconf _NPROCESSORS_CONF)
+               possible_cpus_count=$(($configured_cpus_count > $possible_cpus_count \
+                                                             ? $configured_cpus_count \
+                                                             : $possible_cpus_count))
+       fi
+
+       echo $possible_cpus_count
+}
+
 # Return the number of _configured_ CPUs.
 function conf_proc_count()
 {
@@ -238,6 +270,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"
@@ -1259,37 +1305,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()
@@ -1396,8 +1485,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 ()
@@ -1801,6 +1891,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
This page took 0.025891 seconds and 4 git commands to generate.