Skip to contentSkip to conversation

plugins/syscall: Added a table-like summary output

PATCH Superseded Mahmoud Mandour 2 participants 3 messages Apr 20, 2021 – May 5, 2021

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 plugins/syscall: Added a table-like summary output

Added a table-like output which contains the total number of calls for each used syscall along with the number of errors that occurred.

Per-call tracing is still available through supplying the argument ``print`` to the plugin.

---
v2 -> v3: * Locked shared hash-table to protect access across multiple
callback invocations.
* Used better variable names for the hash table and entries.
* Factored the creation and looking-up for hash table entries.
* Eliminated the usage of a global boolean and checked against the existence of the ``statistics`` hash-table. (it's allocated only if the plugin in the summary(default) mode.
* Removed the function ``free_entry`` and passed ``g_free`` directly to ``g_hash_table_new_full()``.

 tests/plugin/syscall.c | 98 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 93 insertions(+), 5 deletions(-)
tests/plugin/syscall.c +93 -5
index 53ee2ab6c4..7b856f2fe2 100644
@@ -16,32 +16,120 @@
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
 
+typedef struct {
+    int64_t num;
+    int64_t calls;
+    int64_t errors;
+} SyscallStats;
+
+static GMutex lock;
+static GHashTable *statistics;
+
+static SyscallStats *get_or_create_entry(int64_t num)
+{
+    SyscallStats *entry =
+        (SyscallStats *) g_hash_table_lookup(statistics, GINT_TO_POINTER(num));
+
+    if (!entry) {
+        entry = g_new0(SyscallStats, 1);
+        entry->num = num;
+        g_hash_table_insert(statistics, GINT_TO_POINTER(num), (gpointer) entry);
+    }
+
+    return entry;
+}
+
 static void vcpu_syscall(qemu_plugin_id_t id, unsigned int vcpu_index,
                          int64_t num, uint64_t a1, uint64_t a2,
                          uint64_t a3, uint64_t a4, uint64_t a5,
                          uint64_t a6, uint64_t a7, uint64_t a8)
 {
-    g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
-    qemu_plugin_outs(out);
+    if (statistics) {
+        SyscallStats *entry;
+        g_mutex_lock(&lock);
+        entry = get_or_create_entry(num);
+        entry->calls++;
+        g_mutex_unlock(&lock);
+    } else {
+        g_autofree gchar *out = g_strdup_printf("syscall #%" PRIi64 "\n", num);
+        qemu_plugin_outs(out);
+    }
 }
 
 static void vcpu_syscall_ret(qemu_plugin_id_t id, unsigned int vcpu_idx,
                              int64_t num, int64_t ret)
+{
+    if (statistics) {
+        SyscallStats *entry;
+
+        g_mutex_lock(&lock);
+        /* Should always return an existent entry. */
+        entry = get_or_create_entry(num);
+        if (ret < 0) {
+            entry->errors++;
+        }
+        g_mutex_unlock(&lock);
+    } else {
+        g_autofree gchar *out;
+        out = g_strdup_printf("syscall #%" PRIi64 " returned -> %" PRIi64 "\n",
+                num, ret);
+        qemu_plugin_outs(out);
+    }
+}
+
+void print_entry(gpointer val, gpointer user_data)
 {
     g_autofree gchar *out;
-    out = g_strdup_printf("syscall #%" PRIi64 " returned -> %" PRIi64 "\n",
-            num, ret);
+    SyscallStats *entry = (SyscallStats *) val;
+    int64_t syscall_num = entry->num;
+    out = g_strdup_printf(
+        "%-13" PRIi64 "%-6" PRIi64 " %" PRIi64 "\n",
+        syscall_num, entry->calls, entry->errors);
     qemu_plugin_outs(out);
 }
 
+static gint comp_func(gconstpointer ea, gconstpointer eb)
+{
+    SyscallStats *ent_a = (SyscallStats *) ea;
+    SyscallStats *ent_b = (SyscallStats *) eb;
+
+    return ent_a->calls > ent_b->calls ? -1 : 1;
+}
+
 /* ************************************************************************* */
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+    if (!statistics) {
+        return;
+    }
+
+    g_mutex_lock(&lock);
+    GList *entries = g_hash_table_get_values(statistics);
+    entries = g_list_sort(entries, comp_func);
+    qemu_plugin_outs("syscall no.  calls  errors\n");
 
-static void plugin_exit(qemu_plugin_id_t id, void *p) {}
+    g_list_foreach(entries, print_entry, NULL);
+
+    g_list_free(entries);
+    g_hash_table_destroy(statistics);
+    g_mutex_unlock(&lock);
+}
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info,
                                            int argc, char **argv)
 {
+    if (argc == 0) {
+        statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
+    } else {
+        for (int i = 0; i < argc; i++) {
+            if (g_strcmp0(argv[i], "print") != 0) {
+                fprintf(stderr, "unsupported argument: %s\n", argv[i]);
+                return -1;
+            }
+        }
+    }
+
     qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
     qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · Mahmoud Mandour

Added a table-like output which contains the total number of calls for each used syscall along with the number of errors that occurred.

Per-call tracing is still available through supplying the argument ``print`` to the plugin.

---

<snip>

tests/plugin/syscall.c line 89

Quoted 13 lines
tests/plugin/syscall.c +7 -2
+
+void print_entry(gpointer val, gpointer user_data)
 {
     g_autofree gchar *out;
-    out = g_strdup_printf("syscall #%" PRIi64 " returned -> %" PRIi64 "\n",
-            num, ret);
+    SyscallStats *entry = (SyscallStats *) val;
+    int64_t syscall_num = entry->num;
+    out = g_strdup_printf(
+        "%-13" PRIi64 "%-6" PRIi64 " %" PRIi64 "\n",
+        syscall_num, entry->calls, entry->errors);
     qemu_plugin_outs(out);
 }

This still fails to compile due to a missing static:

[2/10] Compiling C object tests/plugin/libsyscall.so.p/syscall.c.o
FAILED: tests/plugin/libsyscall.so.p/syscall.c.o
cc -Itests/plugin/libsyscall.so.p -Itests/plugin -I../../tests/plugin -I../../include/qemu -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -fdiagnostics-color=auto -pipe -Wall -Winvalid-pch -Werror -std=gnu99 -O2 -g -isystem /home/alex/lsrc/qemu.git/linux-headers -isystem linux-headers -iquote . -iquote /home/alex/lsrc/qemu.git -iquote /home/alex/lsrc/qemu.git/include -iquote /home/alex/lsrc/qemu.git/disas/libvixl -iquote /home/alex/lsrc/qemu.git/tcg/i386 -pthread -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -m64 -mcx16 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -fwrapv -Wold-style-declaration -Wold-style-definition -Wtype-limits -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined -Wimplicit-fallthrough=2 -Wno-missing-include-dirs -Wno-shift-negative-value -Wno-psabi -fstack-protector-strong -DLEGACY_RDMA_REG_MR -fPIC -MD -MQ tests/plugin/libsyscall.so.p/syscall.c.o -MF tests/plugin/libsyscall.so.p/syscall.c.o.d -o tests/plugin/libsyscall.so.p/syscall.c.o -c ../../tests/plugin/syscall.c ../../tests/plugin/syscall.c:80:6: error: no previous prototype for ‘print_entry’ [-Werror=missing-prototypes]
void print_entry(gpointer val, gpointer user_data)

      ^~~~~~~~~~~

cc1: all warnings being treated as errors
ninja: build stopped: subcommand failed.
make: *** [Makefile:152: run-ninja] Error 1

Quoted 18 lines
tests/plugin/syscall.c +16 -0
+static gint comp_func(gconstpointer ea, gconstpointer eb)
+{
+    SyscallStats *ent_a = (SyscallStats *) ea;
+    SyscallStats *ent_b = (SyscallStats *) eb;
+
+    return ent_a->calls > ent_b->calls ? -1 : 1;
+}
+
 /* ************************************************************************* */
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+    if (!statistics) {
+        return;
+    }
+
+    g_mutex_lock(&lock);
+    GList *entries = g_hash_table_get_values(statistics);

tests/plugin/syscall.c line 116

Quoted 10 lines
tests/plugin/syscall.c +8 -1
+    entries = g_list_sort(entries, comp_func);
+    qemu_plugin_outs("syscall no.  calls  errors\n");
 
-static void plugin_exit(qemu_plugin_id_t id, void *p) {}
+    g_list_foreach(entries, print_entry, NULL);
+
+    g_list_free(entries);
+    g_hash_table_destroy(statistics);
+    g_mutex_unlock(&lock);
+}

Hmm it looks like we see multiple plugin_exit's when running multiple threads:

./qemu-aarch64 -d plugin -D output -plugin ./tests/plugin/libsyscall.so ./tests/tcg/aarch64-linux-user/linux-test

I don't see it with testthread or the new signals test though which is confusing. Something linux-test is doing must be different.

Quoted 9 lines

QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
const qemu_info_t *info, int argc, char **argv)
{

tests/plugin/syscall.c +4 -0
+    if (argc == 0) {
+        statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL, g_free);
+    } else {
+        for (int i = 0; i < argc; i++) {

tests/plugin/syscall.c line 135

Quoted 10 lines
tests/plugin/syscall.c +7 -0
+            if (g_strcmp0(argv[i], "print") != 0) {
+                fprintf(stderr, "unsupported argument: %s\n", argv[i]);
+                return -1;
+            }
+        }
+    }
+
     qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
     qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

Otherwise looking pretty good - certainly a more useful default ;-)

Signature · 1 line
Alex Bennée
#2 lore raw
Quoted 53 lines · Alex Bennée
Quoted 8 lines · Mahmoud Mandour

Added a table-like output which contains the total number of calls for each used syscall along with the number of errors that occurred.

Per-call tracing is still available through supplying the argument ``print`` to the plugin.

---

<snip>

tests/plugin/syscall.c

Quoted 5 lines
tests/plugin/syscall.c +2 -1
+
+void print_entry(gpointer val, gpointer user_data)
 {
     g_autofree gchar *out;
-    out = g_strdup_printf("syscall #%" PRIi64 " returned -> %" PRIi64

"\n",

tests/plugin/syscall.c line 89

Quoted 8 lines
tests/plugin/syscall.c +5 -1
-            num, ret);
+    SyscallStats *entry = (SyscallStats *) val;
+    int64_t syscall_num = entry->num;
+    out = g_strdup_printf(
+        "%-13" PRIi64 "%-6" PRIi64 " %" PRIi64 "\n",
+        syscall_num, entry->calls, entry->errors);
     qemu_plugin_outs(out);
 }

This still fails to compile due to a missing static:

[2/10] Compiling C object tests/plugin/libsyscall.so.p/syscall.c.o
FAILED: tests/plugin/libsyscall.so.p/syscall.c.o
cc -Itests/plugin/libsyscall.so.p -Itests/plugin -I../../tests/plugin

(quoted diff) +0 -3
-I../../include/qemu -I/usr/include/glib-2.0
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include -fdiagnostics-color=auto -pipe
-Wall -Winvalid-pch -Werror -std=gnu99 -O2 -g -isystem

/home/alex/lsrc/qemu.git/linux-headers -isystem linux-headers -iquote .

(quoted diff) +0 -2
-iquote /home/alex/lsrc/qemu.git -iquote /home/alex/lsrc/qemu.git/include
-iquote /home/alex/lsrc/qemu.git/disas/libvixl -iquote

/home/alex/lsrc/qemu.git/tcg/i386 -pthread -U_FORTIFY_SOURCE

(quoted diff) +0 -10
-D_FORTIFY_SOURCE=2 -m64 -mcx16 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wundef
-Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common
-fwrapv -Wold-style-declaration -Wold-style-definition -Wtype-limits
-Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers
-Wempty-body -Wnested-externs -Wendif-labels -Wexpansion-to-defined
-Wimplicit-fallthrough=2 -Wno-missing-include-dirs
-Wno-shift-negative-value -Wno-psabi -fstack-protector-strong
-DLEGACY_RDMA_REG_MR -fPIC -MD -MQ tests/plugin/libsyscall.so.p/syscall.c.o
-MF tests/plugin/libsyscall.so.p/syscall.c.o.d -o

tests/plugin/libsyscall.so.p/syscall.c.o -c ../../tests/plugin/syscall.c ../../tests/plugin/syscall.c:80:6: error: no previous prototype for ‘print_entry’ [-Werror=missing-prototypes]
void print_entry(gpointer val, gpointer user_data)

Quoted 6 lines
      ^~~~~~~~~~~

cc1: all warnings being treated as errors
ninja: build stopped: subcommand failed.
make: *** [Makefile:152: run-ninja] Error 1

Thanks for the notice. I got it working because I was building it using

the makefile in the
contrib/plugins makefile and it does not report warnings. I fixed it.

Quoted 33 lines

tests/plugin/syscall.c

Quoted 10 lines
tests/plugin/syscall.c +8 -0
+static gint comp_func(gconstpointer ea, gconstpointer eb)
+{
+    SyscallStats *ent_a = (SyscallStats *) ea;
+    SyscallStats *ent_b = (SyscallStats *) eb;
+
+    return ent_a->calls > ent_b->calls ? -1 : 1;
+}
+
 /*

************************************************************************* */

Quoted 8 lines
tests/plugin/syscall.c +8 -0
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+    if (!statistics) {
+        return;
+    }
+
+    g_mutex_lock(&lock);
+    GList *entries = g_hash_table_get_values(statistics);

tests/plugin/syscall.c line 116

Quoted 10 lines
tests/plugin/syscall.c +8 -1
+    entries = g_list_sort(entries, comp_func);
+    qemu_plugin_outs("syscall no.  calls  errors\n");
 
-static void plugin_exit(qemu_plugin_id_t id, void *p) {}
+    g_list_foreach(entries, print_entry, NULL);
+
+    g_list_free(entries);
+    g_hash_table_destroy(statistics);
+    g_mutex_unlock(&lock);
+}

Hmm it looks like we see multiple plugin_exit's when running multiple threads:

Quoted 6 lines

./qemu-aarch64 -d plugin -D output -plugin ./tests/plugin/libsyscall.so
./tests/tcg/aarch64-linux-user/linux-test

I don't see it with testthread or the new signals test though which is confusing. Something linux-test is doing must be different.

The output of other plugins is similar so apparently there's something different with linux-test, yes.

Since this is not a bug related to this particular patch, should I fix the compilation error and resend the patch?

Quoted 22 lines

Review

Quoted 8 lines

QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
const qemu_info_t *info, int argc, char **argv)
{

(quoted diff) +2 -0
+    if (argc == 0) {
+        statistics = g_hash_table_new_full(NULL, g_direct_equal, NULL,

g_free);

tests/plugin/syscall.c line 135

Quoted 12 lines
tests/plugin/syscall.c +9 -0
+    } else {
+        for (int i = 0; i < argc; i++) {
+            if (g_strcmp0(argv[i], "print") != 0) {
+                fprintf(stderr, "unsupported argument: %s\n", argv[i]);
+                return -1;
+            }
+        }
+    }
+
     qemu_plugin_register_vcpu_syscall_cb(id, vcpu_syscall);
     qemu_plugin_register_vcpu_syscall_ret_cb(id, vcpu_syscall_ret);
     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
Quoted 8 lines

Otherwise looking pretty good - certainly a more useful default ;-)

--
Alex Bennée

Thanks,
Mahmoud