From nobody Mon Feb 9 10:38:27 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1656645087; cv=none; d=zohomail.com; s=zohoarc; b=f7jHSqfjPsDQbGCi5vst2INnG21yyFl6AF12KRpusF4cGuuuFb/TvoH9FEXAXSdh0fykc9LzuihFSjZ87KN6XZzDj3Z4lcr+7QTw12DU3ufxjpy5P1cKS3MwwfDWz5Rvd2iUMfeXtoPT17F4ccOC1AyEb2L0pRN/H2pFStWoCcw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1656645087; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=L55KpgNyltmEymN2pCyo6kZ6wYw1udyYPDXqBCih7sY=; b=ZgVQ7mLdtgD2So6zCgmj3V2g1R9WbL7i7mnG4aMzhWWhhzY79xViJQcdLni8jMfL05F/u1XYT9f6dkXEJ4Rae7EJ8Rx0cx4rusZ25VZL96Grf9j0FDEur5V6+puOG5ycvi67IByPp3kJZwm6nEMTF3Wff7yZoG1DM6UcbCLc+Ac= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1656645087511977.9758949076075; Thu, 30 Jun 2022 20:11:27 -0700 (PDT) Received: from localhost ([::1]:47382 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o7742-0005Et-3t for importer@patchew.org; Thu, 30 Jun 2022 23:11:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39170) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o773C-0004Ud-Gy for qemu-devel@nongnu.org; Thu, 30 Jun 2022 23:10:34 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]:34677) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o7737-00088V-Kz for qemu-devel@nongnu.org; Thu, 30 Jun 2022 23:10:31 -0400 Received: by mail-pf1-x434.google.com with SMTP id t21so1229497pfq.1 for ; Thu, 30 Jun 2022 20:10:28 -0700 (PDT) Received: from always-x1.bytedance.net ([61.120.150.70]) by smtp.gmail.com with ESMTPSA id j8-20020a17090a318800b001ed2fae2271sm2736659pjb.31.2022.06.30.20.10.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jun 2022 20:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L55KpgNyltmEymN2pCyo6kZ6wYw1udyYPDXqBCih7sY=; b=RtNuLUmAgfjVZAgp2b95Lqdf30/obgkzxnmqXt+H+x00tzu0Fo2e3MFbpbOlbGHvcN nAAoS2kFt/PygUpQ0J3Xqh9xqCm4aZna7+XkATY63q7eDzm2qI6eEkkkoODweSukbtQk a8z31pmeRG+xj2RBgNJ4miPo610JrRBcxu1MvnRzQaHRMCNdb2tcTKFIf250fTMd/Gt9 yFSgM9wad/z2cVroQ9QbT3du0yIiG5GcpvrlmrTgQygUm24sd5gBBojqflUjhtvwl0PW BwMfI4vw4ohsiv1inAq+9zw6bjGdw4jaXERbN+VbGGEbxTg2Lvl0PAEaFQCMTuzU/hzd PR4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L55KpgNyltmEymN2pCyo6kZ6wYw1udyYPDXqBCih7sY=; b=zvdnAF92sDmfTgH5/ioy0Msjg8zfUhFke0wg5b2UhtN9zoSh3PNN6626V2wZPlm98C SJIAeZTEx7BC5Le2Q3xVWZ2Kb75fwQgRVRZKiPcIW3WcivnEKav+ojzEmhslK2Ssxcut YfHo/QQmlaM6v7xvruKH9foEx39WuZSzdh+I8hGwU3icX4aaVpyukEvqkTMJMnRr0OS9 iMdJWg+XMPojl7kIZ9REtU9Vx3UIuYTxwnFTh/fkjin7CoRImhrgIew8fu7qKnjFadH6 +o8up/NsnWPNhHVQpUzUg4KFyCeqqis8RH1v3dFRQ/31yvLr9lnrZvz6p4/q46bJKGIX LZtg== X-Gm-Message-State: AJIora+q7LYaOK3te+tQI6dPDbaQjFFfjCi+eIMfVzNfEP5++BpDlzWa hrzPjGrqU4e0TD3q7OAFG2BV7A== X-Google-Smtp-Source: AGRyM1uG0n/v8Q6hrVnA+e1zdO41UEktz1riVKXfzPblu9A9xMTZIYGMa3lzkiCnzLQcNItWHypk3w== X-Received: by 2002:a63:cd4e:0:b0:411:acda:d3d2 with SMTP id a14-20020a63cd4e000000b00411acdad3d2mr4592002pgj.487.1656645027093; Thu, 30 Jun 2022 20:10:27 -0700 (PDT) From: zhenwei pi To: michael.roth@amd.com, kkostiuk@redhat.com Cc: qemu-devel@nongnu.org, marcandre.lureau@redhat.com, zhenwei pi Subject: [PATCH] qga: add command 'guest-get-cpustats' Date: Fri, 1 Jul 2022 11:05:33 +0800 Message-Id: <20220701030533.575255-1-pizhenwei@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=pizhenwei@bytedance.com; helo=mail-pf1-x434.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @bytedance-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1656645089587100001 Content-Type: text/plain; charset="utf-8" A vCPU thread always reaches 100% utilization when: - guest uses idle=3Dpoll - disable HLT vm-exit - enable MWAIT Add new guest agent command 'guest-get-cpustats' to get guest CPU statistics, we can know the guest workload and how busy the CPU is. Signed-off-by: zhenwei pi --- qga/commands-posix.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ qga/commands-win32.c | 6 ++++ qga/qapi-schema.json | 49 ++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 0469dc409d..2847023876 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2893,6 +2893,73 @@ GuestDiskStatsInfoList *qmp_guest_get_diskstats(Erro= r **errp) return guest_get_diskstats(errp); } =20 +GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) +{ + GuestCpuStatsList *head =3D NULL, **tail =3D &head; + const char *cpustats =3D "/proc/stat"; + FILE *fp; + size_t n; + char *line =3D NULL; + + fp =3D fopen(cpustats, "r"); + if (fp =3D=3D NULL) { + error_setg_errno(errp, errno, "open(\"%s\")", cpustats); + return NULL; + } + + while (getline(&line, &n, fp) !=3D -1) { + GuestCpuStats *cpustat =3D NULL; + int i; + unsigned long user, system, idle, iowait, irq, softirq, steal, gue= st; + unsigned long nice, guest_nice; + char name[64]; + + i =3D sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", + name, &user, &nice, &system, &idle, &iowait, &irq, &sof= tirq, + &steal, &guest, &guest_nice); + + /* drop "cpu 1 2 3 ...", get "cpuX 1 2 3 ..." only */ + if (strncmp(name, "cpu", 3) || (name[3] =3D=3D '\0')) { + continue; + } + + cpustat =3D g_new0(GuestCpuStats, 1); + cpustat->cpu =3D atoi(&name[3]); + cpustat->has_user =3D true; + cpustat->user =3D user * 10; + cpustat->has_system =3D true; + cpustat->system =3D system * 10; + cpustat->has_idle =3D true; + cpustat->idle =3D idle * 10; + + /* Linux version >=3D 2.6 */ + if (i > 5) { + cpustat->has_iowait =3D true; + cpustat->iowait =3D iowait * 10; + cpustat->has_irq =3D true; + cpustat->irq =3D irq * 10; + cpustat->has_softirq =3D true; + cpustat->softirq =3D softirq * 10; + } + + if (i > 8) { + cpustat->has_steal =3D true; + cpustat->steal =3D steal * 10; + } + + if (i > 9) { + cpustat->has_guest =3D true; + cpustat->guest =3D guest * 10; + } + + QAPI_LIST_APPEND(tail, cpustat); + } + + free(line); + fclose(fp); + return head; +} + #else /* defined(__linux__) */ =20 void qmp_guest_suspend_disk(Error **errp) @@ -3247,6 +3314,11 @@ GuestDiskStatsInfoList *qmp_guest_get_diskstats(Erro= r **errp) return NULL; } =20 +GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} =20 #endif /* CONFIG_FSFREEZE */ =20 diff --git a/qga/commands-win32.c b/qga/commands-win32.c index 36f94c0f9c..7ed7664715 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -2543,3 +2543,9 @@ GuestDiskStatsInfoList *qmp_guest_get_diskstats(Error= **errp) error_setg(errp, QERR_UNSUPPORTED); return NULL; } + +GuestCpuStatsList *qmp_guest_get_cpustats(Error **errp) +{ + error_setg(errp, QERR_UNSUPPORTED); + return NULL; +} diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json index 9fa20e791b..4859c887b2 100644 --- a/qga/qapi-schema.json +++ b/qga/qapi-schema.json @@ -1576,3 +1576,52 @@ { 'command': 'guest-get-diskstats', 'returns': ['GuestDiskStatsInfo'] } + +## +# @GuestCpuStats: +# +# Get statistics of each CPU in millisecond. +# +# @cpu: CPU index in guest OS +# +# @user: CPU time of user mode +# +# @system: CPU time of system mode +# +# @idle: CPU time of idle state +# +# @iowait: CPU time waiting IO +# +# @irq: CPU time of hardware interrupt +# +# @softirq: CPU time of soft interrupt +# +# @steal: CPU time stolen by host +# +# @guest: CPU time of running guest mode +# +# Since: 7.1 +## +{ 'struct': 'GuestCpuStats', + 'data': {'cpu': 'int', + '*user': 'uint64', + '*system': 'uint64', + '*idle': 'uint64', + '*iowait': 'uint64', + '*irq': 'uint64', + '*softirq': 'uint64', + '*steal': 'uint64', + '*guest': 'uint64' + } } + +## +# @guest-get-cpustats: +# +# Retrieve information about CPU stats. +# Returns: List of CPU stats of guest. +# +# Since: 7.1 +## +{ 'command': 'guest-get-cpustats', + 'returns': ['GuestCpuStats'] +} --=20 2.20.1