[libvirt] [PATCH v2 1/2] tests: Add script to display nodeinfo test data

Andrea Bolognani abologna at redhat.com
Wed Oct 7 12:57:09 UTC 2015


The output has been modeled after that of

  ppc64_cpu --info

but it's not limited to ppc64 test data.
---
 tests/nodeinfodata/display.sh | 113 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 113 insertions(+)
 create mode 100755 tests/nodeinfodata/display.sh

diff --git a/tests/nodeinfodata/display.sh b/tests/nodeinfodata/display.sh
new file mode 100755
index 0000000..104c9d7
--- /dev/null
+++ b/tests/nodeinfodata/display.sh
@@ -0,0 +1,113 @@
+#!/bin/bash
+
+# display.sh - Display nodeinfo test data in a nice way
+# Copyright (C) 2015 Red Hat, Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library.  If not, see
+# <http://www.gnu.org/licenses/>.
+#
+# Author: Andrea Bolognani <abologna at redhat.com>
+
+die() {
+    local message=$1
+
+    echo "$message" >&2
+
+    exit 1
+}
+
+list_cpus() {
+    local datadir=$1
+
+    local index
+
+    ls -d "$datadir/cpu/cpu"* | while read index; do
+        index=${index#*cpu/cpu}
+        case "$index" in
+            [0-9]*)
+                echo "$index"
+                ;;
+        esac
+    done | sort -un
+}
+
+is_online() {
+    local datadir=$1
+    local cpu=$2
+
+    local ret=1
+
+    # In some cases this file will not be available, eg. for cpu0 on an Intel
+    # system. libvirt considers such CPU to be online, and so should we
+    if test -e "$datadir/cpu/cpu$cpu/online"; then
+        ret=$(cat "$datadir/cpu/cpu$cpu/online")
+    fi
+
+    # Reverse boolean to use it as return code
+    case "$ret" in
+        0) ret=1 ;;
+        1) ret=0 ;;
+    esac
+
+    return $ret;
+}
+
+main() {
+    local datadir=$1
+    local threads_per_core=$2
+
+    if test "$#" -lt 2; then
+        die "Usage: $SELF DATADIR THREADS_PER_CORE"
+    fi
+    if ! test -d "$datadir"; then
+        die "$datadir: No such directory"
+    fi
+    # This also takes care of non-numeric values on the command line
+    if ! test "$threads_per_core" -gt 0 >/dev/null 2>&1; then
+        die "$threads_per_core: Must be a positive number"
+    fi
+
+    echo "Threads per core: $threads_per_core"
+
+    # This information might not be available (eg. older kernels)
+    echo -n 'Present CPUs:     '
+    if test -e "$datadir/cpu/present"; then
+        cat "$datadir/cpu/present"
+    else
+        echo 'information not available'
+    fi
+
+    let core=0
+    for cpu in $(list_cpus "$datadir"); do
+
+        mod=$(expr "$cpu" % "$threads_per_core")
+        if test "$mod" -eq "0"; then
+            printf "\nCore %3d:" "$core"
+            let core++
+        fi
+
+        if is_online "$datadir" "$cpu"; then
+            printf " %4d*" "$cpu"
+        else
+            printf " %4d " "$cpu"
+        fi
+    done
+    echo
+
+    return 0
+}
+
+SELF=$0
+main "$@"
+exit $?
-- 
2.4.3




More information about the libvir-list mailing list