Tests: use CPU ids from online ranges
authorKienan Stewart <kstewart@efficios.com>
Tue, 15 Aug 2023 15:50:16 +0000 (11:50 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 21 Aug 2023 18:16:47 +0000 (14:16 -0400)
test_tracefile_count could fail randomly on systems where there are CPUs
present but not online. For example:

  $ cat /sys/devices/system/cpu/online
  0-7
  $ cat /sys/devices/system/cpu/present
  0-39

When a CPU is present, it will have an entry in
/sys/devices/system/cpu/cpuX for it's ID, and thus the test may pick
that CPU's ID. However, a present CPU which is not online is not a valid
target for taskset.

In cases where `get_any_available_cpu` is used with task set, the tests
could fail for a similar reason. This case can be somewhat less common,
because it would return the numerically lowest CPU first; however, with
online as follows cpu 0 isn't available and taskset fails.

  $ cat /sys/devices/system/cpu/online
  18-19,135,142
  $ cat /sys/devices/system/cpu/present
  0-167

Change-Id: I06ac2e67495552f54765794b154acc9e8e9990ec
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/regression/tools/snapshots/ust_test
tests/regression/tools/tracefile-limits/test_tracefile_count
tests/utils/utils.sh

index ae734fc90851da0d6b53992fac3392f8d6acf7b4..697567c805d40c2556512fe07ad1a6dd7bde5bb5 100755 (executable)
@@ -212,7 +212,7 @@ function test_ust_local_snapshot_small_discard_buffers ()
        OLDCPUSET=$(taskset -p $$)
 
        diag "Test local UST snapshots with small discard buffers"
-       taskset -p 0x1 $$ 1>/dev/null 2>&1      # CPU 0 only
+       taskset -c "$(get_any_available_cpu)" -p $$ 1>/dev/null 2>&1
        create_lttng_session_no_output $SESSION_NAME
        enable_mmap_small_discard_ust_channel $SESSION_NAME $CHANNEL_NAME
        enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
@@ -257,7 +257,7 @@ function test_ust_local_snapshot_small_overwrite_buffers ()
        OLDCPUSET=$(taskset -p $$)
 
        diag "Test local UST snapshots with small overwrite buffers"
-       taskset -p 0x1 $$ 1>/dev/null 2>&1      # CPU 0 only
+       taskset -p "$(get_any_available_cpu)" $$ 1>/dev/null 2>&1
        create_lttng_session_no_output $SESSION_NAME
        enable_mmap_small_overwrite_ust_channel $SESSION_NAME $CHANNEL_NAME
        enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
@@ -304,7 +304,7 @@ function test_ust_local_snapshot_max_size ()
        local snapshot_max_size
        local channel_max_size_per_cpu
 
-       IFS=" " read -r -a cpus_list <<< "$(get_exposed_cpus_list)"
+       IFS=" " read -r -a cpus_list <<< "$(get_online_cpus)"
 
        possible_cpus=$(get_possible_cpus_count)
        subbuf_size=$(getconf PAGE_SIZE)
index a844c1057d70a9681c75329e9b17b2ea77337769..fad5429cb470494d731cd1f0dd7d5e192fa9b742 100755 (executable)
@@ -22,30 +22,15 @@ TRACEFILE_SIZE=$PAGE_SIZE
 
 source "$TESTDIR"/utils/utils.sh
 
-NUM_CPUS=$(conf_proc_count)
-
 if [ ! -x "$TESTAPP_BIN" ]; then
        BAIL_OUT "No UST events binary detected."
 fi
 
 function pick_random_cpuid ()
 {
-        local cpuid=0
-
-        # On Linux pick a random available cpuid from sysfs
-        if [ -d "/sys/devices/system/cpu" ]; then
-                local cpuids=()
-
-                for i in /sys/devices/system/cpu/cpu[0-9]*; do
-                        cpuids+=("${i#/sys/devices/system/cpu/cpu}")
-                done
-
-                cpuid=${cpuids[ $RANDOM % ${#cpuids[@]} ]}
-        else
-                cpuid=$((RANDOM % NUM_CPUS))
-        fi
-
-        echo $cpuid
+       local cpuids
+       read -r -a cpuids <<< "$(get_online_cpus)"
+       echo "${cpuids[ $RANDOM % ${#cpuids[@]} ]}"
 }
 
 function enable_lttng_channel_count_limit ()
index 3c6455e2c4d7ae1e0d7026c1d1798ae1c08ca6b4..faa87e783436111793c4e4cea4d771c0491139ae 100644 (file)
@@ -209,6 +209,27 @@ function randstring()
        echo
 }
 
+# Return a space-separated string of online CPU IDs, based on
+# /sys/devices/system/cpu/online, or from 0 to nproc - 1 otherwise.
+function get_online_cpus()
+{
+       local cpus=()
+       local range_re
+       if [ -f /sys/devices/system/cpu/online ]; then
+               range_re='([0-9]+)-([0-9]+)'
+               while read -r range ; do
+                       if [[ "${range}" =~ ${range_re} ]] ; then
+                               mapfile -t -O "${#cpus[*]}" cpus <<< $(seq "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}")
+                       else
+                               cpus+=("${range}")
+                       fi
+               done < <(tr ',' $'\n' < /sys/devices/system/cpu/online)
+       else
+               read -r -a cpus <<< $(seq 0 $(( $(conf_proc_count) - 1 )) )
+       fi
+       echo "${cpus[*]}"
+}
+
 # Helpers for get_possible_cpus.
 function get_possible_cpus_count_from_sysfs_possible_mask()
 {
@@ -281,8 +302,8 @@ function get_exposed_cpus_list()
 # value, e.g. that it could be 0.
 function get_any_available_cpu()
 {
-       for cpu in /sys/devices/system/cpu/cpu[0-9]*; do
-               echo "${cpu#/sys/devices/system/cpu/cpu}"
+       for cpu in $(get_online_cpus); do
+               echo "${cpu}"
                break;
        done
 }
This page took 0.028306 seconds and 4 git commands to generate.