From nobody Sun May 19 04:35:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1673959496; cv=none; d=zohomail.com; s=zohoarc; b=fiiUyd6oP1CJk3h++DzrtbZlLE7YoPzFKT6FgN6udu2+XcSZUz3cAcgdCC9icdd6An0tcPY9P3ygk8jbAXis1yWqas7uTrVdb3G8lVm1QaBqPiFc8q9dAav4cZTGGuMCUMNzuxxa7A1reMJb5BlgHEXCUcFXEpmnTUprqolK1Ks= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1673959496; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=E5D2kJH+R3FF1nV74MDPZ5A05sP4GVTFcIXzclxp+BI=; b=M6YXtuk1H13Uqyt1RFI3IXEqkJUhdkQvWcDkzBpdPvhQXdjP+IZKHl4m6l/DB4q+YvTU7181Bc7ZpzERdcSiApr0+ipmVCarlMEESqYYFeZwaIxxaIqoeIS8J26+HRXVAExwIqQJL1V2m7z6p+kF5zCAy6gYy87/VlWFvdCdcVg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1673959496413722.2966598143644; Tue, 17 Jan 2023 04:44:56 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-8gRJuPtxPvexYHyOy_F-nw-1; Tue, 17 Jan 2023 07:44:50 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 387A680D0EC; Tue, 17 Jan 2023 12:44:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9ABB040C6EC4; Tue, 17 Jan 2023 12:44:47 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4064C19465A4; Tue, 17 Jan 2023 12:44:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id E623019465A3 for ; Tue, 17 Jan 2023 12:44:46 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id D87E12026D68; Tue, 17 Jan 2023 12:44:46 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FCDE2026D4B for ; Tue, 17 Jan 2023 12:44:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673959495; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=E5D2kJH+R3FF1nV74MDPZ5A05sP4GVTFcIXzclxp+BI=; b=cCq7xmVuUpjPIKlqO0PMaRPckX6vI7Otg+s3rbTlWpBsGIJpTHOuggOuQA8Hwe6KjGIPXH 9tbyk8aSFSI7SOShazbbjl9MH4a1Xco1PdKXtoO0fyZopDXQ4KIe520nlqZy+ys4G5IjbY C3ApeP0fR8YbM6LJ3Sqc+wKl0nKbTq4= X-MC-Unique: 8gRJuPtxPvexYHyOy_F-nw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH] qemu: Provide virDomainGetCPUStats() implementation for session connection Date: Tue, 17 Jan 2023 13:44:44 +0100 Message-Id: <802feb24a9ed9191facebdc3ca47356ad744a6df.1673959484.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1673959498710100001 Content-Type: text/plain; charset="utf-8"; x-default="true" We have virDomainGetCPUStats() API which offers querying statistics on host CPU usage by given guest. And it works in two modes: getting overall stats (@start_cpu =3D=3D -1, @ncpus =3D=3D 1) or getting per host CPU usage. For the QEMU driver it is implemented by looking into values stored in corresponding cpuacct CGroup controller. Well, this works for system instances, where libvirt has permissions to create CGroups and place QEMU process into them. But it does not fly for session connection, where no CGroups are set up. Fortunately, we can do something similar to v8.8.0-rc1~95 and use virProcessGetStatInfo() to fill the overall stats. Unfortunately, I haven't found any source of per host CPU usage, so we just continue throwing an error in that case. Signed-off-by: Michal Privoznik --- src/qemu/qemu_driver.c | 52 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d6879175fe..a570057ecf 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -16009,6 +16009,50 @@ qemuDomainGetMetadata(virDomainPtr dom, return ret; } =20 +#define QEMU_CPU_STATS_PROC_TOTAL 3 + +static int +qemuDomainGetCPUStatsProc(virDomainObj *vm, + virTypedParameterPtr params, + unsigned int nparams) +{ + unsigned long long cpuTime =3D 0; + unsigned long long userTime =3D 0; + unsigned long long sysTime =3D 0; + + if (nparams =3D=3D 0) { + /* return supported number of params */ + return QEMU_CPU_STATS_PROC_TOTAL; + } + + if (virProcessGetStatInfo(&cpuTime, &userTime, &sysTime, + NULL, NULL, vm->pid, 0) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, "%s", + _("cannot read cputime for domain")); + return -1; + } + + if (virTypedParameterAssign(¶ms[0], VIR_DOMAIN_CPU_STATS_CPUTIME, + VIR_TYPED_PARAM_ULLONG, cpuTime) < 0) + return -1; + + if (nparams > 1 && + virTypedParameterAssign(¶ms[1], VIR_DOMAIN_CPU_STATS_USERTIME, + VIR_TYPED_PARAM_ULLONG, userTime) < 0) + return -1; + + if (nparams > 2 && + virTypedParameterAssign(¶ms[2], VIR_DOMAIN_CPU_STATS_SYSTEMTIM= E, + VIR_TYPED_PARAM_ULLONG, sysTime) < 0) + return -1; + + if (nparams > 3) + nparams =3D 3; + + return nparams; +} + +#undef QEMU_CPU_STATS_PROC_TOTAL =20 static int qemuDomainGetCPUStats(virDomainPtr domain, @@ -16037,8 +16081,12 @@ qemuDomainGetCPUStats(virDomainPtr domain, goto cleanup; =20 if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUACC= T)) { - virReportError(VIR_ERR_OPERATION_INVALID, - "%s", _("cgroup CPUACCT controller is not mounted")= ); + if (start_cpu =3D=3D -1) { + ret =3D qemuDomainGetCPUStatsProc(vm, params, nparams); + } else { + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("cgroup CPUACCT controller is not mounted")); + } goto cleanup; } =20 --=20 2.38.2