Skip to contentSkip to conversation

qga: implement 'guest-get-nvidia-smi' command

PATCH In review João Vilaça 4 participants 8 messages Mar 31 – Apr 1

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qga: implement 'guest-get-nvidia-smi' command

---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +64 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown error");
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}
qga/commands-win32.c +64 -0

67 lines not on this page · raw message

qga/qapi-schema.json +59 -0

65 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw

Hi João,

Thanks for your patch. Can you explain the use case of this command?

Best Regards,
Kostiantyn Kostiuk.

Quoted 227 lines · João Vilaça
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +30 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

qga/commands-posix.c +34 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}
qga/commands-win32.c +30 -0
index c0bf3467bd..a78d5b71f5 100644
@@ -2764,3 +2764,67 @@ GuestNetworkRouteList
 *qmp_guest_network_get_route(Error **errp)
     g_hash_table_destroy(interface_metric_cache);
     return head;
 }
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

qga/commands-win32.c +34 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}
qga/qapi-schema.json +59 -1
index c57bc9a02f..8abbf71131 100644
@@ -1876,6 +1876,65 @@
   'if': { 'any': ['CONFIG_WIN32', 'CONFIG_GETLOADAVG'] }
 }
 
+##
+# @GuestNvidiaGpu:
+#
+# Information about a single NVIDIA GPU as reported by nvidia-smi.
+#
+# @index: GPU index (0-based), stable across reboots for a given
+#         hardware slot
+#
+# @name: GPU product name (e.g. "NVIDIA A100-SXM4-80GB")
+#
+# @driver-version: version string of the installed NVIDIA driver
+#
+# @temperature: GPU die temperature in degrees Celsius
+#
+# @gpu-utilization: GPU compute engine utilization in percent (0-100)
+#
+# @memory-utilization: GPU memory controller utilization in percent
+#                      (0-100)
+#
+# @memory-total: total framebuffer memory in MiB
+#
+# @memory-free: free framebuffer memory in MiB
+#
+# @memory-used: used framebuffer memory in MiB
+#
+# Since: 10.1
+##
+{ 'struct': 'GuestNvidiaGpu',
+  'data': {
+      'index':              'int',
+      'name':               'str',
+      'driver-version':     'str',
+      'temperature':        'int',
+      'gpu-utilization':    'int',
+      'memory-utilization': 'int',
+      'memory-total':       'int',
+      'memory-free':        'int',
+      'memory-used':        'int'
+  }
+}
+
+##
+# @guest-get-nvidia-smi:
+#
+# Query NVIDIA GPU information via nvidia-smi inside the guest.
+#
+# Returns one @GuestNvidiaGpu entry per physical GPU (or MIG instance)
+# detected by the NVIDIA driver.
+#
+# Errors:
+#   - If nvidia-smi is not installed or not found in $PATH
+#   - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
+#     device)
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #
--

2.53.0

#2 lore raw

Monitoring, on the hypervisor side, of GPU workloads running in guests with NVIDIA GPU passthrough (VFIO) or vGPU (NVIDIA GRID).
Operators who want GPU metrics from a fleet of VMs must either SSH into each guest or deploy a separate monitoring agent inside the guest itself (e.g. DCGM exporter).

Quoted 238 lines · Kostiantyn Kostiuk

Hi João,

Thanks for your patch. Can you explain the use case of this command?

Best Regards,
Kostiantyn Kostiuk.

Quoted 227 lines · João Vilaça
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +30 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

qga/commands-posix.c +34 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}
qga/commands-win32.c +30 -0
index c0bf3467bd..a78d5b71f5 100644
@@ -2764,3 +2764,67 @@ GuestNetworkRouteList
 *qmp_guest_network_get_route(Error **errp)
     g_hash_table_destroy(interface_metric_cache);
     return head;
 }
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

qga/commands-win32.c +34 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}
qga/qapi-schema.json +59 -1
index c57bc9a02f..8abbf71131 100644
@@ -1876,6 +1876,65 @@
   'if': { 'any': ['CONFIG_WIN32', 'CONFIG_GETLOADAVG'] }
 }
 
+##
+# @GuestNvidiaGpu:
+#
+# Information about a single NVIDIA GPU as reported by nvidia-smi.
+#
+# @index: GPU index (0-based), stable across reboots for a given
+#         hardware slot
+#
+# @name: GPU product name (e.g. "NVIDIA A100-SXM4-80GB")
+#
+# @driver-version: version string of the installed NVIDIA driver
+#
+# @temperature: GPU die temperature in degrees Celsius
+#
+# @gpu-utilization: GPU compute engine utilization in percent (0-100)
+#
+# @memory-utilization: GPU memory controller utilization in percent
+#                      (0-100)
+#
+# @memory-total: total framebuffer memory in MiB
+#
+# @memory-free: free framebuffer memory in MiB
+#
+# @memory-used: used framebuffer memory in MiB
+#
+# Since: 10.1
+##
+{ 'struct': 'GuestNvidiaGpu',
+  'data': {
+      'index':              'int',
+      'name':               'str',
+      'driver-version':     'str',
+      'temperature':        'int',
+      'gpu-utilization':    'int',
+      'memory-utilization': 'int',
+      'memory-total':       'int',
+      'memory-free':        'int',
+      'memory-used':        'int'
+  }
+}
+
+##
+# @guest-get-nvidia-smi:
+#
+# Query NVIDIA GPU information via nvidia-smi inside the guest.
+#
+# Returns one @GuestNvidiaGpu entry per physical GPU (or MIG instance)
+# detected by the NVIDIA driver.
+#
+# Errors:
+#   - If nvidia-smi is not installed or not found in $PATH
+#   - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
+#     device)
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #
--

2.53.0

#3 lore raw
Quoted 5 lines · João Vilaça

Monitoring, on the hypervisor side, of GPU workloads running in guests with NVIDIA GPU passthrough (VFIO) or vGPU (NVIDIA GRID).
Operators who want GPU metrics from a fleet of VMs must either SSH into each guest or deploy a separate monitoring agent inside the guest itself (e.g. DCGM exporter).

IMHO, directly people to deploy an existing monmitoring agent solution in the guest is likely the right thing todo.

We don't really want the QEMU guest agent expanding arbitrarily such that it effectively re-invents what's already offered by general purpose monitoring agents.

If the problem is that those existing monitoring agents need network support, then IMHO the answer is likely to enhance the agent to use vsock, rather than add more monitoring commands to QEMU.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#4 lore raw
In reply to João Vilaça

You neglected to cc: me. We recommend to use scripts/get_maintainer.pl to find all the maintainers, then use common sense to trim.

João Vilaça <[email protected]> writes:

The commit message needs to explain why and how the patch is useful.

For a patch adding a command to qemu-ga, like this one, it needs to state the command's anticipated use cases.

Quoted 26 lines
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +12 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1439

Quoted 10 lines
qga/commands-posix.c +10 -0
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {

Why not ga_run_command()? Hmm, it throws away the command's output on success.

Kostiantyn, should ga_run_command() be rewritten on top of g_spawn_sync()?

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1447

Quoted 8 lines
qga/commands-posix.c +8 -0
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown error");

This is wrong if @stderr_buf can contain newlines. qapi/error.h:

* The resulting message should be a single phrase, with no newline or
* trailing punctuation.

However, there's similar misuse elsewhere in this file. Oh well, carry on.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1444

Quoted 3 lines
qga/commands-posix.c +3 -0
+        return NULL;
+    }
+

I figure the command's output is some form of CSV. Can you point to its documentation?

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1455

Quoted 5 lines
qga/commands-posix.c +5 -0
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;

Silently ignore empty lines. Okay.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1458

Quoted 3 lines
qga/commands-posix.c +3 -0
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);

If the line has more than 9 values, they are squashed into the last one.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1461

Quoted 3 lines
qga/commands-posix.c +3 -0
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;

Silently ignore lines with less than 9 values.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1465

Quoted 4 lines
qga/commands-posix.c +4 -0
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);

If the value doesn't parse as decimal signed integer, we silently assume zero.

If it parses, we silently ignore any text following it.

If it parses a value outside 64 bit signed range, we silently assume its largest or smallest value.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1478

Quoted 13 lines
qga/commands-posix.c +13 -0
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);

Are you *sure* this is robust enough?

Please consider a bog-standard LL(1) parser.

Quoted 65 lines
qga/commands-posix.c +3 -0
+
+    return head;
+}
qga/commands-win32.c +54 -0
index c0bf3467bd..a78d5b71f5 100644
@@ -2764,3 +2764,67 @@ GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)
     g_hash_table_destroy(interface_metric_cache);
     return head;
 }
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown error");
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);

qga/commands-win32.c GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp) · line 2830

Quoted 10 lines
qga/commands-win32.c +10 -0
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}

Duplicates the output parser. Can you avoid that?

Quoted 5 lines
qga/qapi-schema.json +0 -0
index c57bc9a02f..8abbf71131 100644

qga/qapi-schema.json line 1885

Quoted 10 lines
qga/qapi-schema.json +7 -0
@@ -1876,6 +1876,65 @@
   'if': { 'any': ['CONFIG_WIN32', 'CONFIG_GETLOADAVG'] }
 }
 
+##
+# @GuestNvidiaGpu:
+#
+# Information about a single NVIDIA GPU as reported by nvidia-smi.
+#
+# @index: GPU index (0-based), stable across reboots for a given
+#         hardware slot

Please format like

# @index: GPU index (0-based), stable across reboots for a given
# hardware slot

qga/qapi-schema.json line 1893

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @name: GPU product name (e.g. "NVIDIA A100-SXM4-80GB")
+#
+# @driver-version: version string of the installed NVIDIA driver
+#
+# @temperature: GPU die temperature in degrees Celsius
+#
+# @gpu-utilization: GPU compute engine utilization in percent (0-100)

(0-100) feels redundant.

qga/qapi-schema.json line 1896

Quoted 3 lines
qga/qapi-schema.json +3 -0
+#
+# @memory-utilization: GPU memory controller utilization in percent
+#                      (0-100)

Likewise.

qga/qapi-schema.json line 1904

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @memory-total: total framebuffer memory in MiB
+#
+# @memory-free: free framebuffer memory in MiB
+#
+# @memory-used: used framebuffer memory in MiB
+#
+# Since: 10.1

11.1 most likely.

Quoted 17 lines
qga/qapi-schema.json +17 -0
+##
+{ 'struct': 'GuestNvidiaGpu',
+  'data': {
+      'index':              'int',
+      'name':               'str',
+      'driver-version':     'str',
+      'temperature':        'int',
+      'gpu-utilization':    'int',
+      'memory-utilization': 'int',
+      'memory-total':       'int',
+      'memory-free':        'int',
+      'memory-used':        'int'
+  }
+}
+
+##
+# @guest-get-nvidia-smi:

qga/qapi-schema.json line 1931

Quoted 10 lines
qga/qapi-schema.json +10 -0
+#
+# Query NVIDIA GPU information via nvidia-smi inside the guest.
+#
+# Returns one @GuestNvidiaGpu entry per physical GPU (or MIG instance)
+# detected by the NVIDIA driver.
+#
+# Errors:
+#   - If nvidia-smi is not installed or not found in $PATH
+#   - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
+#     device)

We commonly mention the error kind like this:

# - If nvidia-smi is not installed or not found in $PATH,
# GenericError
# - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
# device), GenericError

qga/qapi-schema.json line 1940

Quoted 9 lines
qga/qapi-schema.json +6 -0
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #

Why not use existing guest-exec, and leave the parsing to the client?

#5 lore raw
Quoted 50 lines · Markus Armbruster

You neglected to cc: me. We recommend to use scripts/get_maintainer.pl to find all the maintainers, then use common sense to trim.

João Vilaça <[email protected]> writes:

The commit message needs to explain why and how the patch is useful.

For a patch adding a command to qemu-ga, like this one, it needs to state the command's anticipated use cases.

Quoted 26 lines
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +12 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1439

Quoted 10 lines
qga/commands-posix.c +10 -0
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {

Why not ga_run_command()? Hmm, it throws away the command's output on success.

Quoted 3 lines

Kostiantyn, should ga_run_command() be rewritten on top of g_spawn_sync()?

The idea of ga_run_command was different.
Now, we can improve it if we see new use cases.

Quoted 148 lines

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp)

Quoted 9 lines
qga/commands-posix.c +8 -0
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

This is wrong if @stderr_buf can contain newlines. qapi/error.h:

* The resulting message should be a single phrase, with no newline or
* trailing punctuation.

However, there's similar misuse elsewhere in this file. Oh well, carry on.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1444

Quoted 3 lines
qga/commands-posix.c +3 -0
+        return NULL;
+    }
+

I figure the command's output is some form of CSV. Can you point to its documentation?

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1455

Quoted 5 lines
qga/commands-posix.c +5 -0
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;

Silently ignore empty lines. Okay.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1458

Quoted 3 lines
qga/commands-posix.c +3 -0
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);

If the line has more than 9 values, they are squashed into the last one.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1461

Quoted 3 lines
qga/commands-posix.c +3 -0
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;

Silently ignore lines with less than 9 values.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1465

Quoted 4 lines
qga/commands-posix.c +4 -0
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);

If the value doesn't parse as decimal signed integer, we silently assume zero.

If it parses, we silently ignore any text following it.

If it parses a value outside 64 bit signed range, we silently assume its largest or smallest value.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1478

Quoted 13 lines
qga/commands-posix.c +13 -0
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);

Are you *sure* this is robust enough?

Please consider a bog-standard LL(1) parser.

qga/commands-win32.c GuestNetworkRouteList

Quoted 8 lines
qga/commands-posix.c +3 -0
+
+    return head;
+}
qga/commands-win32.c +0 -0
index c0bf3467bd..a78d5b71f5 100644
@@ -2764,3 +2764,67 @@ GuestNetworkRouteList

*qmp_guest_network_get_route(Error **errp)

Quoted 23 lines
qga/commands-win32.c +20 -0
     g_hash_table_destroy(interface_metric_cache);
     return head;
 }
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,

qga/commands-win32.c GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)

Quoted 10 lines
qga/commands-win32.c +10 -0
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s", gerr->message);
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

Quoted 28 lines
qga/commands-win32.c +28 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
Quoted 9 lines

qga/commands-win32.c GuestNetworkRouteList · line 2830

Quoted 6 lines
qga/commands-win32.c +6 -0
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}

Duplicates the output parser. Can you avoid that?

There is an open question of who should be looking for the nvidia-smi command?
In the case of Windows, we definitely know that nvidia-smi will be in Program Files or
in the DriverStore, so it will be missing in PATH by default. If QGA should use this knowledge, then the Windows part will be different (except for the parsing logic)
because we will be looking for nvidia-smi path. If no, and the user must update PATH manually,
then this can be moved to commands.c

Quoted 85 lines
Quoted 6 lines
qga/qapi-schema.json +0 -0
index c57bc9a02f..8abbf71131 100644

qga/qapi-schema.json line 1885

Quoted 10 lines
qga/qapi-schema.json +7 -0
@@ -1876,6 +1876,65 @@
   'if': { 'any': ['CONFIG_WIN32', 'CONFIG_GETLOADAVG'] }
 }
 
+##
+# @GuestNvidiaGpu:
+#
+# Information about a single NVIDIA GPU as reported by nvidia-smi.
+#
+# @index: GPU index (0-based), stable across reboots for a given
+#         hardware slot

Please format like

# @index: GPU index (0-based), stable across reboots for a given
# hardware slot

qga/qapi-schema.json line 1893

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @name: GPU product name (e.g. "NVIDIA A100-SXM4-80GB")
+#
+# @driver-version: version string of the installed NVIDIA driver
+#
+# @temperature: GPU die temperature in degrees Celsius
+#
+# @gpu-utilization: GPU compute engine utilization in percent (0-100)

(0-100) feels redundant.

qga/qapi-schema.json line 1896

Quoted 3 lines
qga/qapi-schema.json +3 -0
+#
+# @memory-utilization: GPU memory controller utilization in percent
+#                      (0-100)

Likewise.

qga/qapi-schema.json line 1904

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @memory-total: total framebuffer memory in MiB
+#
+# @memory-free: free framebuffer memory in MiB
+#
+# @memory-used: used framebuffer memory in MiB
+#
+# Since: 10.1

11.1 most likely.

Quoted 17 lines
qga/qapi-schema.json +17 -0
+##
+{ 'struct': 'GuestNvidiaGpu',
+  'data': {
+      'index':              'int',
+      'name':               'str',
+      'driver-version':     'str',
+      'temperature':        'int',
+      'gpu-utilization':    'int',
+      'memory-utilization': 'int',
+      'memory-total':       'int',
+      'memory-free':        'int',
+      'memory-used':        'int'
+  }
+}
+
+##
+# @guest-get-nvidia-smi:

qga/qapi-schema.json line 1931

Quoted 10 lines
qga/qapi-schema.json +10 -0
+#
+# Query NVIDIA GPU information via nvidia-smi inside the guest.
+#
+# Returns one @GuestNvidiaGpu entry per physical GPU (or MIG instance)
+# detected by the NVIDIA driver.
+#
+# Errors:
+#   - If nvidia-smi is not installed or not found in $PATH
+#   - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
+#     device)

We commonly mention the error kind like this:

# - If nvidia-smi is not installed or not found in $PATH,
# GenericError
# - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
# device), GenericError

Quoted 12 lines

qga/qapi-schema.json line 1940

Quoted 9 lines
qga/qapi-schema.json +6 -0
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #

Why not use existing guest-exec, and leave the parsing to the client?

I agree with Daniel that guest-exec is a command that should not exist. For example, in RHEL/CentOS, this command is disabled by default, and we recommend using SSH over VSock to run any command.
We have seen the problem several times with guest-exec. Also, we have an upstream patch
for guest-exec + SELinux support to try to workaround a security issue. Maybe we should discuss marking guest-exec as deprecated.

So, I agree with the author to add a separate command instead.

#6 lore raw

I tested initially with guest-exec but had issues with SELinux. I was able to create specific rules to allow virt_qemu_ga_t to access NVIDIA devices.
But while investigating, I saw a bug discussing guest-exec being disabled RHEL/CentOS
and whether or not Fedora should do the same. So, I decided to pursue this alternative.

Quoted 350 lines · Kostiantyn Kostiuk
Quoted 51 lines · Markus Armbruster

You neglected to cc: me. We recommend to use scripts/get_maintainer.pl to find all the maintainers, then use common sense to trim.

João Vilaça <[email protected]> writes:

The commit message needs to explain why and how the patch is useful.

For a patch adding a command to qemu-ga, like this one, it needs to state the command's anticipated use cases.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error

Quoted 11 lines
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +0 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error

**errp)

Quoted 15 lines
qga/commands-posix.c +12 -0
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1439

Quoted 10 lines
qga/commands-posix.c +10 -0
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {

Why not ga_run_command()? Hmm, it throws away the command's output on success.

Quoted 3 lines

Kostiantyn, should ga_run_command() be rewritten on top of g_spawn_sync()?

The idea of ga_run_command was different.
Now, we can improve it if we see new use cases.

Quoted 150 lines

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error · line 1440

Quoted 2 lines
qga/commands-posix.c +1 -0
+        error_setg(errp, "failed to run nvidia-smi: %s",

gerr->message);

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp)

Quoted 7 lines
qga/commands-posix.c +7 -0
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

This is wrong if @stderr_buf can contain newlines. qapi/error.h:

* The resulting message should be a single phrase, with no newline or
* trailing punctuation.

However, there's similar misuse elsewhere in this file. Oh well, carry on.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1444

Quoted 3 lines
qga/commands-posix.c +3 -0
+        return NULL;
+    }
+

I figure the command's output is some form of CSV. Can you point to its documentation?

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1455

Quoted 5 lines
qga/commands-posix.c +5 -0
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;

Silently ignore empty lines. Okay.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1458

Quoted 3 lines
qga/commands-posix.c +3 -0
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);

If the line has more than 9 values, they are squashed into the last one.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1461

Quoted 3 lines
qga/commands-posix.c +3 -0
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;

Silently ignore lines with less than 9 values.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1465

Quoted 4 lines
qga/commands-posix.c +4 -0
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);

If the value doesn't parse as decimal signed integer, we silently assume zero.

If it parses, we silently ignore any text following it.

If it parses a value outside 64 bit signed range, we silently assume its largest or smallest value.

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1478

Quoted 13 lines
qga/commands-posix.c +13 -0
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
+        g_strfreev(f);
+    }
+    g_strfreev(lines);

Are you *sure* this is robust enough?

Please consider a bog-standard LL(1) parser.

qga/commands-win32.c GuestNetworkRouteList

Quoted 8 lines
qga/commands-posix.c +3 -0
+
+    return head;
+}
qga/commands-win32.c +0 -0
index c0bf3467bd..a78d5b71f5 100644
@@ -2764,3 +2764,67 @@ GuestNetworkRouteList

*qmp_guest_network_get_route(Error **errp)

Quoted 16 lines
qga/commands-win32.c +13 -0
     g_hash_table_destroy(interface_metric_cache);
     return head;
 }
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;
+    g_autofree gchar *stderr_buf = NULL;

qga/commands-win32.c GuestNetworkRouteList *qmp_guest_network_get_route(Error **errp)

Quoted 10 lines
qga/commands-win32.c +10 -0
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {
+        error_setg(errp, "failed to run nvidia-smi: %s",

gerr->message);

qga/commands-win32.c GuestNetworkRouteList · line 2796

Quoted 7 lines
qga/commands-win32.c +7 -0
+        g_error_free(gerr);
+        return NULL;
+    }
+
+    if (exit_status != 0) {
+        error_setg(errp, "nvidia-smi failed (exit %d): %s",
+                   exit_status, stderr_buf ? stderr_buf : "unknown

error");

Quoted 28 lines
qga/commands-win32.c +28 -0
+        return NULL;
+    }
+
+    gchar **lines = g_strsplit(stdout_buf, "\n", -1);
+    for (int i = 0; lines[i] != NULL; i++) {
+        gchar *line = g_strstrip(lines[i]);
+        if (*line == '\0') {
+            continue;
+        }
+
+        gchar **f = g_strsplit(line, ", ", 9);
+        if (g_strv_length(f) < 9) {
+            g_strfreev(f);
+            continue;
+        }
+
+        GuestNvidiaGpu *gpu     = g_new0(GuestNvidiaGpu, 1);
+        gpu->index              = (int)g_ascii_strtoll(f[0], NULL, 10);
+        gpu->name               = g_strdup(g_strstrip(f[1]));
+        gpu->driver_version     = g_strdup(g_strstrip(f[2]));
+        gpu->temperature        = (int)g_ascii_strtoll(f[3], NULL, 10);
+        gpu->gpu_utilization    = (int)g_ascii_strtoll(f[4], NULL, 10);
+        gpu->memory_utilization = (int)g_ascii_strtoll(f[5], NULL, 10);
+        gpu->memory_total       = (int)g_ascii_strtoll(f[6], NULL, 10);
+        gpu->memory_free        = (int)g_ascii_strtoll(f[7], NULL, 10);
+        gpu->memory_used        = (int)g_ascii_strtoll(f[8], NULL, 10);
+
+        QAPI_LIST_APPEND(tail, gpu);
Quoted 9 lines

qga/commands-win32.c GuestNetworkRouteList · line 2830

Quoted 6 lines
qga/commands-win32.c +6 -0
+        g_strfreev(f);
+    }
+    g_strfreev(lines);
+
+    return head;
+}

Duplicates the output parser. Can you avoid that?

There is an open question of who should be looking for the nvidia-smi command?
In the case of Windows, we definitely know that nvidia-smi will be in Program Files or
in the DriverStore, so it will be missing in PATH by default. If QGA should use this knowledge, then the Windows part will be different (except for the parsing logic)
because we will be looking for nvidia-smi path. If no, and the user must update PATH manually,
then this can be moved to commands.c

Quoted 85 lines
Quoted 6 lines
qga/qapi-schema.json +0 -0
index c57bc9a02f..8abbf71131 100644

qga/qapi-schema.json line 1885

Quoted 10 lines
qga/qapi-schema.json +7 -0
@@ -1876,6 +1876,65 @@
   'if': { 'any': ['CONFIG_WIN32', 'CONFIG_GETLOADAVG'] }
 }
 
+##
+# @GuestNvidiaGpu:
+#
+# Information about a single NVIDIA GPU as reported by nvidia-smi.
+#
+# @index: GPU index (0-based), stable across reboots for a given
+#         hardware slot

Please format like

# @index: GPU index (0-based), stable across reboots for a given
# hardware slot

qga/qapi-schema.json line 1893

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @name: GPU product name (e.g. "NVIDIA A100-SXM4-80GB")
+#
+# @driver-version: version string of the installed NVIDIA driver
+#
+# @temperature: GPU die temperature in degrees Celsius
+#
+# @gpu-utilization: GPU compute engine utilization in percent (0-100)

(0-100) feels redundant.

qga/qapi-schema.json line 1896

Quoted 3 lines
qga/qapi-schema.json +3 -0
+#
+# @memory-utilization: GPU memory controller utilization in percent
+#                      (0-100)

Likewise.

qga/qapi-schema.json line 1904

Quoted 8 lines
qga/qapi-schema.json +8 -0
+#
+# @memory-total: total framebuffer memory in MiB
+#
+# @memory-free: free framebuffer memory in MiB
+#
+# @memory-used: used framebuffer memory in MiB
+#
+# Since: 10.1

11.1 most likely.

Quoted 17 lines
qga/qapi-schema.json +17 -0
+##
+{ 'struct': 'GuestNvidiaGpu',
+  'data': {
+      'index':              'int',
+      'name':               'str',
+      'driver-version':     'str',
+      'temperature':        'int',
+      'gpu-utilization':    'int',
+      'memory-utilization': 'int',
+      'memory-total':       'int',
+      'memory-free':        'int',
+      'memory-used':        'int'
+  }
+}
+
+##
+# @guest-get-nvidia-smi:

qga/qapi-schema.json line 1931

Quoted 10 lines
qga/qapi-schema.json +10 -0
+#
+# Query NVIDIA GPU information via nvidia-smi inside the guest.
+#
+# Returns one @GuestNvidiaGpu entry per physical GPU (or MIG instance)
+# detected by the NVIDIA driver.
+#
+# Errors:
+#   - If nvidia-smi is not installed or not found in $PATH
+#   - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
+#     device)

We commonly mention the error kind like this:

# - If nvidia-smi is not installed or not found in $PATH,
# GenericError
# - If nvidia-smi exits with a non-zero status (e.g. no NVIDIA
# device), GenericError

Quoted 12 lines

qga/qapi-schema.json line 1940

Quoted 9 lines
qga/qapi-schema.json +6 -0
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #

Why not use existing guest-exec, and leave the parsing to the client?

I agree with Daniel that guest-exec is a command that should not exist. For example, in RHEL/CentOS, this command is disabled by default, and we recommend using SSH over VSock to run any command.
We have seen the problem several times with guest-exec. Also, we have an upstream patch
for guest-exec + SELinux support to try to workaround a security issue. Maybe we should discuss marking guest-exec as deprecated.

So, I agree with the author to add a separate command instead.

#7 lore raw
In reply to Kostiantyn Kostiuk
Quoted 52 lines · Kostiantyn Kostiuk
Quoted 50 lines · Markus Armbruster

You neglected to cc: me. We recommend to use scripts/get_maintainer.pl to find all the maintainers, then use common sense to trim.

João Vilaça <[email protected]> writes:

The commit message needs to explain why and how the patch is useful.

For a patch adding a command to qemu-ga, like this one, it needs to state the command's anticipated use cases.

Quoted 26 lines
---
 qga/commands-posix.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/commands-win32.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
 qga/qapi-schema.json | 59 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
qga/commands-posix.c +12 -0
index 837be51c40..631a8a9ee6 100644
@@ -1415,3 +1415,67 @@ GuestLoadAverage *qmp_guest_get_load(Error **errp)
     return ret;
 }
 #endif
+
+GuestNvidiaGpuList *qmp_guest_get_nvidia_smi(Error **errp)
+{
+    const gchar *argv[] = {
+        "nvidia-smi",
+        "--query-gpu=index,name,driver_version,"
+            "temperature.gpu,utilization.gpu,utilization.memory,"
+            "memory.total,memory.free,memory.used",
+        "--format=csv,noheader,nounits",
+        NULL
+    };
+    g_autofree gchar *stdout_buf = NULL;

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp) · line 1439

Quoted 10 lines
qga/commands-posix.c +10 -0
+    g_autofree gchar *stderr_buf = NULL;
+    gint exit_status;
+    GError *gerr = NULL;
+    GuestNvidiaGpuList *head = NULL, **tail = &head;
+
+    if (!g_spawn_sync(NULL, (gchar **)argv, NULL,
+                      G_SPAWN_SEARCH_PATH,
+                      NULL, NULL,
+                      &stdout_buf, &stderr_buf,
+                      &exit_status, &gerr)) {

Why not ga_run_command()? Hmm, it throws away the command's output on success.

Quoted 6 lines
Quoted 3 lines

Kostiantyn, should ga_run_command() be rewritten on top of g_spawn_sync()?

The idea of ga_run_command was different.
Now, we can improve it if we see new use cases.

It looks pretty simple to modify the existing ga_run_command to return stdout &stderr as buffers, rather than discarding on success.

Re-writing to use g_spawn_sync would not be required for that, though it might be a nice thing todo at some point for platform portability.

Quoted 21 lines
Quoted 12 lines

qga/commands-posix.c GuestLoadAverage *qmp_guest_get_load(Error **errp)

Quoted 9 lines
qga/commands-posix.c +6 -0
+#
+# Since: 10.1
+##
+{ 'command': 'guest-get-nvidia-smi',
+  'returns': ['GuestNvidiaGpu'] }
+
 ##
 # @GuestNetworkRoute:
 #

Why not use existing guest-exec, and leave the parsing to the client?

I agree with Daniel that guest-exec is a command that should not exist. For example, in RHEL/CentOS, this command is disabled by default, and we recommend using SSH over VSock to run any command.
We have seen the problem several times with guest-exec. Also, we have an upstream patch
for guest-exec + SELinux support to try to workaround a security issue. Maybe we should discuss marking guest-exec as deprecated.

Quoted 1 line

So, I agree with the author to add a separate command instead.

I'm somewhat sceptical that any of this should be in scope for the QEMU agent, as opposed to being left as a job for an existing general purpose monitoring agent.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|