qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: mdroth@linux.vnet.ibm.com, lcapitulino@redhat.com,
	eblake@redhat.com, pbonzini@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 2/3] qga: implement qmp_guest_get_vcpus() for Linux with sysfs
Date: Mon,  4 Mar 2013 23:19:56 +0100	[thread overview]
Message-ID: <1362435597-20018-3-git-send-email-lersek@redhat.com> (raw)
In-Reply-To: <1362435597-20018-1-git-send-email-lersek@redhat.com>


Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 qga/commands-posix.c |   87 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index 1ad231a..d4b6bdc 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -15,6 +15,9 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qapi/qmp/qerror.h"
@@ -1083,9 +1086,93 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
 }
 #endif
 
+#if defined(__linux__)
+#define SYSCONF_EXACT(name, err) sysconf_exact((name), #name, (err))
+
+static long sysconf_exact(int name, const char *name_str, Error **err)
+{
+    long ret;
+
+    errno = 0;
+    ret = sysconf(name);
+    if (ret == -1) {
+        if (errno == 0) {
+            error_setg(err, "sysconf(%s): value indefinite", name_str);
+        } else {
+            error_setg_errno(err, errno, "sysconf(%s)", name_str);
+        }
+    }
+    return ret;
+}
+
+/*
+ * Store a VCPU structure under the link, and return the link to store into
+ * at the next time.
+ */
+static GuestLogicalProcessorList **
+append_vcpu(int64_t logical_id, bool online, GuestLogicalProcessorList **link)
+{
+    GuestLogicalProcessor *vcpu;
+    GuestLogicalProcessorList *entry;
+
+    vcpu = g_malloc0(sizeof *vcpu);
+    vcpu->logical_id = logical_id;
+    vcpu->online = online;
+
+    entry = g_malloc0(sizeof *entry);
+    entry->value = vcpu;
+
+    *link = entry;
+    return &entry->next;
+}
+#endif
+
 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp)
 {
+#if defined(__linux__)
+    long current;
+    GuestLogicalProcessorList **link, *head;
+    long sc_max;
+    Error *local_err = NULL;
+
+    current = 0;
+    link = append_vcpu(current++, true, &head);
+
+    sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err);
+    while (local_err == NULL && current < sc_max) {
+        char *buf;
+        FILE *f;
+
+        buf = g_strdup_printf("/sys/devices/system/cpu/cpu%ld/online",
+                              current);
+        f = fopen(buf, "r");
+        if (f == NULL) {
+            error_setg_errno(&local_err, errno, "fopen(\"%s\", \"r\")", buf);
+        } else {
+            unsigned online;
+
+            if (fscanf(f, "%u", &online) != 1) {
+                error_setg(&local_err, "failed to read or parse \"%s\"", buf);
+            } else {
+                link = append_vcpu(current++, online != 0, link);
+            }
+
+            if (fclose(f) == EOF && local_err == NULL) {
+                error_setg_errno(&local_err, errno, "fclose(\"%s\")", buf);
+            }
+        }
+        g_free(buf);
+    }
+
+    if (local_err == NULL) {
+        return head;
+    }
+
+    qapi_free_GuestLogicalProcessorList(head);
+    error_propagate(errp, local_err);
+#else
     error_set(errp, QERR_UNSUPPORTED);
+#endif
     return NULL;
 }
 
-- 
1.7.1

  parent reply	other threads:[~2013-03-04 22:18 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-04 22:19 [Qemu-devel] [PATCH 0/3] qga/Linux: online/offline/query VCPUs via guest sysfs Laszlo Ersek
2013-03-04 22:19 ` [Qemu-devel] [PATCH 1/3] qga: introduce guest-get-vcpus / guest-set-vcpus with stubs Laszlo Ersek
2013-03-05 21:08   ` Eric Blake
2013-03-05 23:05     ` Laszlo Ersek
2013-03-05 23:12       ` Eric Blake
2013-03-05 23:32         ` Laszlo Ersek
2013-03-06  7:40       ` Andrew Jones
2013-03-06 13:49       ` Eric Blake
2013-03-06 16:37         ` Laszlo Ersek
2013-03-04 22:19 ` Laszlo Ersek [this message]
2013-03-05 20:03   ` [Qemu-devel] [PATCH 2/3] qga: implement qmp_guest_get_vcpus() for Linux with sysfs mdroth
2013-03-05 20:22     ` Eric Blake
2013-03-05 20:45       ` mdroth
2013-03-05 21:05     ` Laszlo Ersek
2013-03-05 20:25   ` Eric Blake
2013-03-05 21:34     ` Laszlo Ersek
2013-03-05 22:06       ` mdroth
2013-03-04 22:19 ` [Qemu-devel] [PATCH 3/3] qga: implement qmp_guest_set_vcpus() " Laszlo Ersek
2013-03-05 20:09   ` mdroth
2013-03-05 21:09     ` Laszlo Ersek
2013-03-05 21:19   ` Eric Blake
2013-03-05 23:23     ` Laszlo Ersek
2013-03-05 23:37       ` Eric Blake
2013-03-06  0:44         ` Laszlo Ersek
2013-03-06  9:57           ` Paolo Bonzini
2013-03-06 13:46           ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1362435597-20018-3-git-send-email-lersek@redhat.com \
    --to=lersek@redhat.com \
    --cc=eblake@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).