Extend the 'IOThreadInfo' structure to include attachment metrics.
This allows users to monitor how many devices are associated with
a specific IOThread and identify them by their QOM paths.

New fields added to IOThreadInfo:
- @attached-cnt: An integer representing the number of devices
  currently using this IOThread's AioContext.
- @attached-dev: A string containing a delimited list ("|") of
  QOM paths for the attached devices.

These fields are also exposed via the Human Monitor Interface (HMP)
command 'info iothreads' to assist with manual debugging and
performance tuning.

Example QMP output:
{
    "id": "iothread0",
    "thread-id": 3134,
    "attached-cnt": 2,
    "attached-dev": "/machine/peripheral/blk1/virtio-backend | 
/machine/peripheral/blk0/virtio-backend",
    ...
}

Signed-off-by: Zhang Chen <[email protected]>
---
 iothread.c         | 24 ++++++++++++++++++++++++
 monitor/hmp-cmds.c |  3 +++
 qapi/misc.json     | 19 +++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/iothread.c b/iothread.c
index b869637497..4fceed28c5 100644
--- a/iothread.c
+++ b/iothread.c
@@ -71,6 +71,28 @@ void iothread_unref(IOThread *iothread, const char *holder)
     }
 }
 
+static char *iothread_get_attached_dev_list(IOThread *iothread)
+{
+    guint len = g_list_length(iothread->attached_dev);
+
+    if (len == 0) {
+        return g_strdup("none");
+    }
+
+    gchar **array = g_new0(gchar *, len + 1);
+    GList *l;
+    int i = 0;
+
+    for (l = iothread->attached_dev; l != NULL; l = l->next) {
+        array[i++] = l->data;
+    }
+
+    char *result = g_strjoinv(" | ", array);
+    g_free(array);
+
+    return result;
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
@@ -388,6 +410,8 @@ static int query_one_iothread(Object *object, void *opaque)
     info = g_new0(IOThreadInfo, 1);
     info->id = iothread_get_id(iothread);
     info->thread_id = iothread->thread_id;
+    info->attached_cnt = iothread->attached_cnt;
+    info->attached_dev = iothread_get_attached_dev_list(iothread);
     info->poll_max_ns = iothread->poll_max_ns;
     info->poll_grow = iothread->poll_grow;
     info->poll_shrink = iothread->poll_shrink;
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index bad034937a..026bed1545 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -203,6 +203,9 @@ void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
         value = info->value;
         monitor_printf(mon, "%s:\n", value->id);
         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
+        monitor_printf(mon, "  attached-cnt=%" PRId64 "\n",
+                       value->attached_cnt);
+        monitor_printf(mon, "  attached-dev=%s\n", value->attached_dev);
         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
diff --git a/qapi/misc.json b/qapi/misc.json
index 1f5062df2a..ca53638684 100644
--- a/qapi/misc.json
+++ b/qapi/misc.json
@@ -76,6 +76,19 @@
 #
 # @thread-id: ID of the underlying host thread
 #
+# @attached-cnt: The parameter is a counter indicating how many
+#     active devices are currently associated with this iothread
+#     (e.g. virtio-blk).  In hotplug scenarios, users can
+#     pre-allocate multiple iothread objects to serve as a persistent
+#     thread pool.  When a device is hot-unplugged, the corresponding
+#     IOThread is released but remains available, allowing subsequent
+#     hot-plugged devices to attach to and reuse the existing thread.
+#     (since 11.0)
+#
+# @attached-dev: A list of QOM paths for the devices currently
+#     associated with this IOThread, delimited by " | ".
+#     Returns "none" if no devices are attached. (since 11.0)
+#
 # @poll-max-ns: maximum polling time in ns, 0 means polling is
 #     disabled (since 2.9)
 #
@@ -93,6 +106,8 @@
 { 'struct': 'IOThreadInfo',
   'data': {'id': 'str',
            'thread-id': 'int',
+           'attached-cnt': 'int',
+           'attached-dev': 'str',
            'poll-max-ns': 'int',
            'poll-grow': 'int',
            'poll-shrink': 'int',
@@ -118,6 +133,8 @@
 #              {
 #                 "id":"iothread0",
 #                 "thread-id":3134,
+#                 "attached-cnt":2,
+#                 "attached-dev":/machine/peripheral/blk1/virtio-backend | 
/machine/peripheral/blk0/virtio-backend,
 #                 "poll-max-ns":32768,
 #                 "poll-grow":0,
 #                 "poll-shrink":0,
@@ -126,6 +143,8 @@
 #              {
 #                 "id":"iothread1",
 #                 "thread-id":3135,
+#                 "attached-cnt":1,
+#                 "attached-dev":/machine/peripheral/blk3/virtio-backend,
 #                 "poll-max-ns":32768,
 #                 "poll-grow":0,
 #                 "poll-shrink":0,
-- 
2.49.0


Reply via email to