From patchwork Mon Mar 4 22:19:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laszlo Ersek X-Patchwork-Id: 224848 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id CA7082C0311 for ; Tue, 5 Mar 2013 09:18:42 +1100 (EST) Received: from localhost ([::1]:39690 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCdiS-0007rg-Iu for incoming@patchwork.ozlabs.org; Mon, 04 Mar 2013 17:18:40 -0500 Received: from eggs.gnu.org ([208.118.235.92]:42462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCdhz-0007mT-Tg for qemu-devel@nongnu.org; Mon, 04 Mar 2013 17:18:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UCdhx-0007Z5-2c for qemu-devel@nongnu.org; Mon, 04 Mar 2013 17:18:11 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51755) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UCdhw-0007Z1-RD for qemu-devel@nongnu.org; Mon, 04 Mar 2013 17:18:09 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r24MHv1e031398 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 4 Mar 2013 17:17:58 -0500 Received: from lacos-laptop.usersys.redhat.com (vpn1-6-15.ams2.redhat.com [10.36.6.15]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r24MHqnZ013650; Mon, 4 Mar 2013 17:17:56 -0500 From: Laszlo Ersek To: mdroth@linux.vnet.ibm.com, lcapitulino@redhat.com, eblake@redhat.com, pbonzini@redhat.com, qemu-devel@nongnu.org Date: Mon, 4 Mar 2013 23:19:56 +0100 Message-Id: <1362435597-20018-3-git-send-email-lersek@redhat.com> In-Reply-To: <1362435597-20018-1-git-send-email-lersek@redhat.com> References: <1362435597-20018-1-git-send-email-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/3] qga: implement qmp_guest_get_vcpus() for Linux with sysfs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Laszlo Ersek --- 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 #include #include +#include +#include +#include #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; }