[PATCH 4/8] plugins: add core API functions for QPP callbacks

Andrew Fasano posted 8 patches 1 year, 10 months ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Alexandre Iooss <erdnaxe@crans.org>, Mahmoud Mandour <ma.mandourr@gmail.com>, Thomas Huth <thuth@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Peter Maydell <peter.maydell@linaro.org>
[PATCH 4/8] plugins: add core API functions for QPP callbacks
Posted by Andrew Fasano 1 year, 10 months ago
From: Elysia Witham <elysia.witham@ll.mit.edu>

Plugin callbacks and their registered functions are stored in a
separate struct which is accessible from the plugin's ctx.
In order for plugins to use another plugin's callbacks, we have
internal functions that resolve a plugin's name to its ctx and
find a target plugin.

Signed-off-by: Elysia Witham <elysia.witham@ll.mit.edu>
Signed-off-by: Andrew Fasano <fasano@mit.edu>
---
 include/qemu/qemu-plugin.h | 10 ++++++++++
 plugins/core.c             | 23 +++++++++++++++++++++++
 plugins/loader.c           | 10 ++++++++++
 plugins/plugin.h           | 15 +++++++++++++++
 4 files changed, 58 insertions(+)

diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
index 5326f33ce8..3ec82ce97f 100644
--- a/include/qemu/qemu-plugin.h
+++ b/include/qemu/qemu-plugin.h
@@ -14,6 +14,7 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include <stddef.h>
+#include <gmodule.h>
 
 /*
  * For best performance, build the plugin with -fvisibility=hidden so that
@@ -38,6 +39,15 @@
  */
 typedef uint64_t qemu_plugin_id_t;
 
+/**
+ * typedef cb_func_t - callback function pointer type
+ * @evdata: plugin callback defined event data
+ * @udata: plugin defined user data
+ *
+ * No return value.
+ */
+typedef void (*cb_func_t) (gpointer evdata, gpointer udata);
+
 /*
  * Versioning plugins:
  *
diff --git a/plugins/core.c b/plugins/core.c
index 6a50b4a6e6..0415a55ec5 100644
--- a/plugins/core.c
+++ b/plugins/core.c
@@ -236,6 +236,17 @@ void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
     qemu_rec_mutex_unlock(&plugin.lock);
 }
 
+struct qemu_plugin_ctx *plugin_name_to_ctx_locked(const char* name)
+{
+    struct qemu_plugin_ctx *ctx;
+    QTAILQ_FOREACH(ctx, &plugin.ctxs, entry) {
+        if (strcmp(ctx->name, name) == 0) {
+            return plugin_id_to_ctx_locked(ctx->id);
+        }
+    }
+    return NULL;
+}
+
 int name_to_plugin_version(const char *name)
 {
     struct qemu_plugin_ctx *ctx;
@@ -260,6 +271,18 @@ const char *id_to_plugin_name(qemu_plugin_id_t id)
     }
 }
 
+struct qemu_plugin_qpp_cb *plugin_find_qpp_cb(struct qemu_plugin_ctx *ctx,
+                                              const char *name)
+{
+    struct qemu_plugin_qpp_cb *cb;
+    QTAILQ_FOREACH(cb, &ctx->qpp_cbs, entry) {
+        if (strcmp(cb->name, name) == 0) {
+            return cb;
+        }
+    }
+    return NULL;
+}
+
 struct plugin_for_each_args {
     struct qemu_plugin_ctx *ctx;
     qemu_plugin_vcpu_simple_cb_t cb;
diff --git a/plugins/loader.c b/plugins/loader.c
index 12c0680e03..ab01d0753c 100644
--- a/plugins/loader.c
+++ b/plugins/loader.c
@@ -277,6 +277,7 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, E
             break;
         }
     }
+    QTAILQ_INIT(&ctx->qpp_cbs);
     QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry);
     ctx->installing = true;
     rc = install(ctx->id, info, desc->argc, desc->argv);
@@ -303,6 +304,15 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, E
     return 1;
 }
 
+void plugin_add_qpp_cb(struct qemu_plugin_ctx *ctx, const char *name)
+{
+    struct qemu_plugin_qpp_cb *new_cb;
+    new_cb = qemu_memalign(qemu_dcache_linesize, sizeof(*new_cb));
+    memset(new_cb, 0, sizeof(*new_cb));
+    new_cb->name = name;
+    QTAILQ_INSERT_TAIL(&ctx->qpp_cbs, new_cb, entry);
+}
+
 /* call after having removed @desc from the list */
 static void plugin_desc_free(struct qemu_plugin_desc *desc)
 {
diff --git a/plugins/plugin.h b/plugins/plugin.h
index 9e710c23a7..fee4741bc6 100644
--- a/plugins/plugin.h
+++ b/plugins/plugin.h
@@ -47,6 +47,14 @@ struct qemu_plugin_state {
     struct qht dyn_cb_arr_ht;
 };
 
+typedef void (*cb_func_t) (gpointer evdata, gpointer udata);
+
+struct qemu_plugin_qpp_cb {
+    const char *name;
+    cb_func_t registered_cb_funcs[QEMU_PLUGIN_EV_MAX];
+    int counter;
+    QTAILQ_ENTRY(qemu_plugin_qpp_cb) entry;
+};
 
 struct qemu_plugin_ctx {
     GModule *handle;
@@ -54,6 +62,7 @@ struct qemu_plugin_ctx {
     const char *name;
     int version;
     struct qemu_plugin_cb *callbacks[QEMU_PLUGIN_EV_MAX];
+    QTAILQ_HEAD(, qemu_plugin_qpp_cb) qpp_cbs;
     QTAILQ_ENTRY(qemu_plugin_ctx) entry;
     /*
      * keep a reference to @desc until uninstall, so that plugins do not have
@@ -106,4 +115,10 @@ int name_to_plugin_version(const char *name);
 
 const char *id_to_plugin_name(qemu_plugin_id_t id);
 
+struct qemu_plugin_qpp_cb *plugin_find_qpp_cb(struct qemu_plugin_ctx *plugin_ctx,
+                                              const char *cb_name);
+
+/* loader.c */
+void plugin_add_qpp_cb(struct qemu_plugin_ctx *ctx, const char *name);
+
 #endif /* PLUGIN_H */
-- 
2.34.1
Re: [PATCH 4/8] plugins: add core API functions for QPP callbacks
Posted by Alex Bennée 1 year, 8 months ago
Andrew Fasano <fasano@mit.edu> writes:

> From: Elysia Witham <elysia.witham@ll.mit.edu>
>
> Plugin callbacks and their registered functions are stored in a
> separate struct which is accessible from the plugin's ctx.
> In order for plugins to use another plugin's callbacks, we have
> internal functions that resolve a plugin's name to its ctx and
> find a target plugin.
>
> Signed-off-by: Elysia Witham <elysia.witham@ll.mit.edu>
> Signed-off-by: Andrew Fasano <fasano@mit.edu>
> ---
>  include/qemu/qemu-plugin.h | 10 ++++++++++
>  plugins/core.c             | 23 +++++++++++++++++++++++
>  plugins/loader.c           | 10 ++++++++++
>  plugins/plugin.h           | 15 +++++++++++++++
>  4 files changed, 58 insertions(+)
>
> diff --git a/include/qemu/qemu-plugin.h b/include/qemu/qemu-plugin.h
> index 5326f33ce8..3ec82ce97f 100644
> --- a/include/qemu/qemu-plugin.h
> +++ b/include/qemu/qemu-plugin.h
> @@ -14,6 +14,7 @@
>  #include <inttypes.h>
>  #include <stdbool.h>
>  #include <stddef.h>
> +#include <gmodule.h>
>  
>  /*
>   * For best performance, build the plugin with -fvisibility=hidden so that
> @@ -38,6 +39,15 @@
>   */
>  typedef uint64_t qemu_plugin_id_t;
>  
> +/**
> + * typedef cb_func_t - callback function pointer type
> + * @evdata: plugin callback defined event data
> + * @udata: plugin defined user data
> + *
> + * No return value.
> + */
> +typedef void (*cb_func_t) (gpointer evdata, gpointer udata);
> +
>  /*
>   * Versioning plugins:
>   *
> diff --git a/plugins/core.c b/plugins/core.c
> index 6a50b4a6e6..0415a55ec5 100644
> --- a/plugins/core.c
> +++ b/plugins/core.c
> @@ -236,6 +236,17 @@ void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
>      qemu_rec_mutex_unlock(&plugin.lock);
>  }
>

This looks like another unused function. I was looking to see what
locking was done before calling it as the suffix implies there should be
some.

> +struct qemu_plugin_ctx *plugin_name_to_ctx_locked(const char* name)
> +{
> +    struct qemu_plugin_ctx *ctx;
> +    QTAILQ_FOREACH(ctx, &plugin.ctxs, entry) {
> +        if (strcmp(ctx->name, name) == 0) {
> +            return plugin_id_to_ctx_locked(ctx->id);
> +        }
> +    }
> +    return NULL;
> +}
> +
>  int name_to_plugin_version(const char *name)
>  {
>      struct qemu_plugin_ctx *ctx;
> @@ -260,6 +271,18 @@ const char *id_to_plugin_name(qemu_plugin_id_t id)
>      }
>  }
>

This one too.

> +struct qemu_plugin_qpp_cb *plugin_find_qpp_cb(struct qemu_plugin_ctx *ctx,
> +                                              const char *name)
> +{
> +    struct qemu_plugin_qpp_cb *cb;
> +    QTAILQ_FOREACH(cb, &ctx->qpp_cbs, entry) {
> +        if (strcmp(cb->name, name) == 0) {
> +            return cb;
> +        }
> +    }
> +    return NULL;
> +}
> +
>  struct plugin_for_each_args {
>      struct qemu_plugin_ctx *ctx;
>      qemu_plugin_vcpu_simple_cb_t cb;
> diff --git a/plugins/loader.c b/plugins/loader.c
> index 12c0680e03..ab01d0753c 100644
> --- a/plugins/loader.c
> +++ b/plugins/loader.c
> @@ -277,6 +277,7 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, E
>              break;
>          }
>      }
> +    QTAILQ_INIT(&ctx->qpp_cbs);
>      QTAILQ_INSERT_TAIL(&plugin.ctxs, ctx, entry);
>      ctx->installing = true;
>      rc = install(ctx->id, info, desc->argc, desc->argv);
> @@ -303,6 +304,15 @@ static int plugin_load(struct qemu_plugin_desc *desc, const qemu_info_t *info, E
>      return 1;
>  }
>  
> +void plugin_add_qpp_cb(struct qemu_plugin_ctx *ctx, const char *name)
> +{
> +    struct qemu_plugin_qpp_cb *new_cb;
> +    new_cb = qemu_memalign(qemu_dcache_linesize, sizeof(*new_cb));
> +    memset(new_cb, 0, sizeof(*new_cb));

Is there a reason to do aligned allocation here. Have you measured a
difference between this and a normal g_new0() in performance?

> +    new_cb->name = name;
> +    QTAILQ_INSERT_TAIL(&ctx->qpp_cbs, new_cb, entry);
> +}
> +

I think we need some rules for this that can be enforced. Can a plugin
create a cb at any time? Or only during initialisation?

If it's at any time we need some sort of serialisation to prevent races.

>  /* call after having removed @desc from the list */
>  static void plugin_desc_free(struct qemu_plugin_desc *desc)
>  {
> diff --git a/plugins/plugin.h b/plugins/plugin.h
> index 9e710c23a7..fee4741bc6 100644
> --- a/plugins/plugin.h
> +++ b/plugins/plugin.h
> @@ -47,6 +47,14 @@ struct qemu_plugin_state {
>      struct qht dyn_cb_arr_ht;
>  };
>  
> +typedef void (*cb_func_t) (gpointer evdata, gpointer udata);
> +
> +struct qemu_plugin_qpp_cb {
> +    const char *name;
> +    cb_func_t registered_cb_funcs[QEMU_PLUGIN_EV_MAX];
> +    int counter;
> +    QTAILQ_ENTRY(qemu_plugin_qpp_cb) entry;
> +};
>  
>  struct qemu_plugin_ctx {
>      GModule *handle;
> @@ -54,6 +62,7 @@ struct qemu_plugin_ctx {
>      const char *name;
>      int version;
>      struct qemu_plugin_cb *callbacks[QEMU_PLUGIN_EV_MAX];
> +    QTAILQ_HEAD(, qemu_plugin_qpp_cb) qpp_cbs;
>      QTAILQ_ENTRY(qemu_plugin_ctx) entry;
>      /*
>       * keep a reference to @desc until uninstall, so that plugins do not have
> @@ -106,4 +115,10 @@ int name_to_plugin_version(const char *name);
>  
>  const char *id_to_plugin_name(qemu_plugin_id_t id);
>  
> +struct qemu_plugin_qpp_cb *plugin_find_qpp_cb(struct qemu_plugin_ctx *plugin_ctx,
> +                                              const char *cb_name);
> +
> +/* loader.c */
> +void plugin_add_qpp_cb(struct qemu_plugin_ctx *ctx, const char *name);
> +
>  #endif /* PLUGIN_H */


-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro