From patchwork Tue Feb 15 15:04:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Kanda X-Patchwork-Id: 12747221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8FB59C433EF for ; Tue, 15 Feb 2022 15:08:53 +0000 (UTC) Received: from localhost ([::1]:51052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nJzRk-0002Ef-KT for qemu-devel@archiver.kernel.org; Tue, 15 Feb 2022 10:08:52 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58490) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOT-0006Qy-Ts for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:29 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:63156) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOQ-0005IW-H7 for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:29 -0500 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21FEi0nX014259; Tue, 15 Feb 2022 15:04:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=PeJeZryTyh+YMT5JKhiOsCF37KPMosRotEs9y3XKHy4=; b=gl3wsc881IncDonOFzQfXZkdbbramOZHWRzkMn2Cg5EfvAoyG7DLFfQ06Uya5A/xqZcP u7Iu+cw9ySPchPoVg0itq8V38aOJjSU7Qe72YzDoOq6vEc2JggH0LDKNkGqIWvKKWYed HXaVNWOm2jivBAt+zw6AFWyD9QzBxpb1+3o9jdWkYtfSrtUJgheiMbN2Frm8otnIPJZ/ UX7Eo79b+sVVKo0ZZV9NbhSQ+7vFj29XYz2vWlGIxNJo0m9A6Kxc6g79Nm/rji0dNYWf zpKXCiH/H8NUJpmt7hIlWJTuUGPkB251Mmk0AnE6mvm7rwwT7JJeMzIerXM6roNx5330 Gw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3e8570skqc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:44 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 21FF0UMw008531; Tue, 15 Feb 2022 15:04:43 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2170.outbound.protection.outlook.com [104.47.55.170]) by aserp3030.oracle.com with ESMTP id 3e62xerfe0-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VEyKQ+34YqVJPEKjZfBhDH0pO/bdA92i/EJcOIU2+GHra9x3drfrcsMZUWtz86KI8k4mWVFZXhTaBLBkH7FQVW1Wlwv7hhbkdwiLOjAzDbA/nwJwk9fawt4J9PviaNjFf3Y22OeuyD3l2sllJ/R0vArVC0uEdgLs1yWXQT6Bd74kPRxaKyWP6cKKdUamN/1k3nzlccmaR6OjA4yiB8dY3R2CJoyvUHq7kgtSjwk9TtWlyTlOROFeo5zzvbcW9jj6Ikkfj5Ib7/m5u8lxMOmZU1VsbpsjYCmN8yoVdvVr1rmerVtRxrLTf3i/Yhhh2PFXuM2HKgjuV1O608cjA+BwpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PeJeZryTyh+YMT5JKhiOsCF37KPMosRotEs9y3XKHy4=; b=nIAtMIqF0aAa/G/wiOD1HyTojqcHFmyzfemIiokNWYABYzJgxSzojEkhd+MnC4saVaROXPj8U9ZTcetZnCen4NqHL6yzl9VlDkeU6LYRX0r3HOXsGRwMp67FLGorplO05p4dB6msnPFnxW2nvDWAhBnfW/O8Y1SUYbFklCpsiwauyFB4XhAFxVnsWtvpn38tfx8cm5NKliAnUnV9OWRZFGO0MmJavHGdCEu4KTB+OQ8nyORWf2Miv6fXtLG74HRexL+6ACrM+XPAaK6SwNtuEpdZbKED7RjulFHANSZSXp7zEzriMWgYWCRPjAc09vMIq/rH2r1jNGfcIEWz2XzpRw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PeJeZryTyh+YMT5JKhiOsCF37KPMosRotEs9y3XKHy4=; b=Dvb7/9oyBZLTB79nf9dTv28mDHGmiErlZXuWBjLwi60q78xmbcD1MoU913bLA9WFwV62rf/aiWyx0TW9WoqqX5GJR88Mb7HybgDG1TsKrUz85wewCLfucCVw4iaNLofgHvcx3JrlaUHkRn0HfNtRXzuKKylfmk8cLEweCBzMNOw= Received: from SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) by DM6PR10MB3865.namprd10.prod.outlook.com (2603:10b6:5:1d3::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.12; Tue, 15 Feb 2022 15:04:41 +0000 Received: from SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716]) by SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716%5]) with mapi id 15.20.4951.016; Tue, 15 Feb 2022 15:04:41 +0000 From: Mark Kanda To: qemu-devel@nongnu.org Subject: [PATCH v4 1/3] qmp: Support for querying stats Date: Tue, 15 Feb 2022 09:04:31 -0600 Message-Id: <20220215150433.2310711-2-mark.kanda@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220215150433.2310711-1-mark.kanda@oracle.com> References: <20220215150433.2310711-1-mark.kanda@oracle.com> X-ClientProxiedBy: SN7PR04CA0038.namprd04.prod.outlook.com (2603:10b6:806:120::13) To SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 47949303-f584-43a9-36b5-08d9f0947e3d X-MS-TrafficTypeDiagnostic: DM6PR10MB3865:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gtYrs60yPub2GDIYHJL8UYNgBH1L9p2vjuB9vAfmPHab0BQy5DJ3LI2nLupItDvscSGW58WpZ6Tsx/Df0MNIrbO8lvyXHR+HMZIgQQLJrNnXgW0IJyQhsfrelT0xl9M48zzrWRDysXdu7mOqkr/ZbSvaru2un2qG4Fe6ZCCpMUhboBoLn+BSLG4O7f5eltzCAygaifx19etySmFl21syM8eyTAeQwoXP/eTLMcc1c57+U6YzWDXBFrQcN1etW/Ennm6dx3LI8fN/zndRk0jGdk0QxKRe0EzfVu/aDH3vp1aWJM0KuCSQHadrJMAhUuxLu5+wsL5kiXCywall1IlSQAJKaHmlrPR5x0vFxCTbdnztrt4Pb55joLvtbupHV6yEajiNy0Psnd32IBmZqxAh3yYM182oYX9rUNKQqbtQA2w5arMKf8XqcTlj0Sk5A60aOyykCHvTdiHvZtA5XZV3wVivZgdHGbD7n3ACo9OXpxpkMSDePIqHnn6D0jR4WRMMM6OcfdOQC5Yih9cN3F9lxSfD5QmOn00rPVC3bYqGmQzSXxiTWimnv1K/IhcyEpyi44KiwCk/gBVQC3TSrciRVt2xZ4m/25biV1jKiSPu233AQArLXB6Env3HQR9q7/tIvpHLwYQHwEZdMU+W8jV7NRYWWjYNwOqmpSOUKyUekLowk5XJueJLDA7t0+qtv6ub6Kt1YZFGzBKtJyVYJv85WA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA1PR10MB5841.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(6506007)(107886003)(2616005)(8676002)(1076003)(66476007)(26005)(6512007)(66556008)(86362001)(6916009)(52116002)(66946007)(6486002)(316002)(6666004)(508600001)(36756003)(83380400001)(44832011)(38350700002)(38100700002)(4326008)(5660300002)(186003)(8936002)(2906002)(30864003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: z5PyvCIGY3xMmqzAceDvczsVuQQhCLv2W0YOxsImUm2SP61M3GGYWjAbKD9F2qWI6aVcTrEiDG0LTH9aihgzPn8Hi58TnasZYNaIsVUbsY3DgWE8aPHOiPBbRZt9ZLBkOo6IZXP9BSKUXMz215m0XXO8u9Da1OrMderw0Y9Tus4Wae7iX5jm14H/mpM31LkVOOIItHVnI/ZhalVgb+bQDB2rDq783B2RSJXs0Wotip5aBn5gMnygoS87TRimPtm0VXL6mB6B3GdPL9Kr5ioa10Q6VLYDnMqzjahH3JidtdrBrI38H5p8ZxUaHhQVkAuVwMGCkfu3O4lmP7SerjxHm5hMx0rhPeM/eXcMfM8NfzLjxor216x2DwEVcKlVx/DnRlLuiFl3uIbH56eNQV15cTXnQdPOXvK0YCXNcdeE9uFKQr6frCrNDvISnMUrVSZiHP7UnYdQYXEoR5aWTYE2NjTOnyx+p1t4oDAgo15BudS5gtSTa1WMTJG0UAH8dkbEaLK+QF4Nh7I6IXP4VgnMrdrXDgBDcgqeP/ZHe+2EqNTwnKOExvvBNrnOMMm9as0R/oxNtBF0ScqMMYTtsWcfXbKGOeqwKu+533QQANwod1ioYKHaxjeIXVxFZ8au7zFSrbTT9yRyebjjaIi2Sdn5VCq1FXfPg08IghSBzLrRSq81cGoDLktoOWjU5Pvg88NwTHmhsAbYkB2QMU8OgvOKFlbpuIyU/11fOP+dx49s4i8c070A8hSI1WQOOmc6pZIsdnEYgJ0YONzipM1mo/X9UBCZK6aDgnjhyr4xOMsDZbiWtYxZ3Qz5iVgtRy8qtrYleXgGKiWwKPh7tdq88aVBXcCL+pzkHenNQapmjZvZVlAl+pGRS5CA+1d53qeDkvypXb1ldwbne4hAXbXBcr7xpC+WBH+xvJ1nXN4fo5qVWHwhHIj8PPrLdlibLOaiuHFWmb/vvyLNXkQkaYdUG2SfqbMZmSTfF9ADY9SAbwj6KwpWKmaCAj6SC05uAlxG7YKhfe6G4sx7NLOZtSnSFaXeUhMT1LPEkbQF8KDdOZWihxq1gu1j4DV2lnnevwYX6qYu/pSG9yW5Z0MOhuSrCJ9xghQyP6i+IYssU5iI20bVrcbnKizQdpyRNrgbxu8sdS98yltVYJ+Xlhe/AJucnTXzLeB5TrrZs6eNqw2R+5QaiyZSFepnkjjkBf/jQuo2e2pTAis734mPKws5R1gkuu7DlTgTvRPUaiuj1OGdB9ZFwRVNA4NVbPHckf+jU3i5e74ez8OsvhX4J294NNEXDURJFjwoMww2vSD1nOrII4v+quwxYCNrwKa2BFkI5ZFOtmxGkk9CWClaSM7JY/zM2bTooB+seowWC6smUHKPf/s5c4LkH+Etm8v3mQ48oJycqbEIK17rqf3VaYuETsHxHlJUOYKil1jlqZRC/7G3BEk42/NKKomi1g5YedSABAQ5kvV11ITIxniY00Q0uuL8tSceZxh+Vz7uEip3+Yb7fCXeVTtkE0rkrs8TDWHHgy2BOIptH4Bv9ITDuw+P0ybhszHQHSMSpIXtucpMGHaQiYrLlmE5mPpHgkB1Vf93fC9OcsggU5uPc4Q1FJ+PmSjy+AdsMB0yXflBvgLtqbTFU23qHag= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 47949303-f584-43a9-36b5-08d9f0947e3d X-MS-Exchange-CrossTenant-AuthSource: SA1PR10MB5841.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Feb 2022 15:04:41.6578 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kyjxKxBp7PHAqT017lO9yfP3czHUZGBnFmlMPUgOClymI5KvA1wdbuvQKMC9QYcqVxv6v/TJp4VHL1Y4kQaTEg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3865 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10258 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 bulkscore=0 malwarescore=0 adultscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202150088 X-Proofpoint-ORIG-GUID: OVSvtA7nANtr0UoV1BvrzFbUmKmMcj3H X-Proofpoint-GUID: OVSvtA7nANtr0UoV1BvrzFbUmKmMcj3H Received-SPF: pass client-ip=205.220.165.32; envelope-from=mark.kanda@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: , Cc: pbonzini@redhat.com, berrange@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Introduce QMP support for querying stats. Provide a framework for adding new stats and support for the following commands: - query-stats Returns a list of all stats per target type (only VM and vCPU to start), with additional options for specifying stat names, vCPU qom paths, and providers. - query-stats-schemas Returns a list of stats included in each target type, with an option for specifying the provider. The framework provides a method to register callbacks for these QMP commands. The first use-case will be for fd-based KVM stats (in an upcoming patch). Examples (with fd-based KVM stats): - Query all VM stats: { "execute": "query-stats", "arguments" : { "target": "vm" } } { "return": { "vm": [ { "provider": "kvm", "stats": [ { "name": "max_mmu_page_hash_collisions", "value": 0 }, { "name": "max_mmu_rmap_size", "value": 0 }, { "name": "nx_lpage_splits", "value": 148 }, ... { "provider": "xyz", "stats": [ ... ... ] } } - Query all vCPU stats: { "execute": "query-stats", "arguments" : { "target": "vcpu" } } { "return": { "vcpus": [ { "path": "/machine/unattached/device[0]" "providers": [ { "provider": "kvm", "stats": [ { "name": "guest_mode", "value": 0 }, { "name": "directed_yield_successful", "value": 0 }, { "name": "directed_yield_attempted", "value": 106 }, ... { "provider": "xyz", "stats": [ ... ... { "path": "/machine/unattached/device[1]" "providers": [ { "provider": "kvm", "stats": [... ... } ] } } - Query 'exits' and 'l1d_flush' KVM stats, and 'somestat' from provider 'xyz' for vCPUs '/machine/unattached/device[2]' and '/machine/unattached/device[4]': { "execute": "query-stats", "arguments": { "target": "vcpu", "vcpus": [ "/machine/unattached/device[2]", "/machine/unattached/device[4]" ], "filters": [ { "provider": "kvm", "fields": [ "l1d_flush", "exits" ] }, { "provider": "xyz", "fields": [ "somestat" ] } ] } } { "return": { "vcpus": [ { "path": "/machine/unattached/device[2]" "providers": [ { "provider": "kvm", "stats": [ { "name": "l1d_flush", "value": 41213 }, { "name": "exits", "value": 74291 } ] }, { "provider": "xyz", "stats": [ ... ] } ] }, { "path": "/machine/unattached/device[4]" "providers": [ { "provider": "kvm", "stats": [ { "name": "l1d_flush", "value": 16132 }, { "name": "exits", "value": 57922 } ] }, { "provider": "xyz", "stats": [ ... ] } ] } ] } } - Query stats schemas: { "execute": "query-stats-schemas" } { "return": { "vcpu": [ { "provider": "kvm", "stats": [ { "name": "guest_mode", "unit": "none", "base": 10, "exponent": 0, "type": "instant" }, { "name": "directed_yield_successful", "unit": "none", "base": 10, "exponent": 0, "type": "cumulative" }, ... { "provider": "xyz", ... "vm": [ { "provider": "kvm", "stats": [ { "name": "max_mmu_page_hash_collisions", "unit": "none", "base": 10, "exponent": 0, "type": "peak" }, { "provider": "xyz", ... Signed-off-by: Mark Kanda Signed-off-by: Mark Kanda --- include/monitor/stats.h | 51 ++++++++ monitor/qmp-cmds.c | 219 +++++++++++++++++++++++++++++++++ qapi/meson.build | 1 + qapi/qapi-schema.json | 1 + qapi/stats.json | 259 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 531 insertions(+) create mode 100644 include/monitor/stats.h create mode 100644 qapi/stats.json diff --git a/include/monitor/stats.h b/include/monitor/stats.h new file mode 100644 index 0000000000..172dc01a4d --- /dev/null +++ b/include/monitor/stats.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2. + * See the COPYING file in the top-level directory. + */ + +#ifndef STATS_H +#define STATS_H + +#include "qapi/qapi-types-stats.h" + +/* + * Add QMP stats callbacks to the stats_callbacks list. + * + * @provider: stats provider + * + * @stats_fn: routine to query stats: + * void (*stats_fn)(StatsResults *results, StatsFilter *filter, Error **errp) + * + * @schema_fn: routine to query stat schemas: + * void (*schemas_fn)(StatsSchemaResult *results, Error **errp) + */ +void add_stats_callbacks(StatsProvider provider, + void (*stats_fn)(StatsResults *, StatsFilter *, + Error **), + void (*schemas_fn)(StatsSchemaResults *, Error **)); + +/* + * Helper routines for adding stats entries to the results lists. + */ +void add_vm_stats_entry(StatsList *, StatsResults *, StatsProvider); +void add_vcpu_stats_entry(StatsList *, StatsResults *, StatsProvider, char *); +void add_vm_stats_schema(StatsSchemaValueList *, StatsSchemaResults *, + StatsProvider); +void add_vcpu_stats_schema(StatsSchemaValueList *, StatsSchemaResults *, + StatsProvider); + +/* + * True if a stat name and provider match a filter or if no corresponding + * filters are defined. False otherwise. + */ +bool stats_requested_name(const char *, StatsProvider, StatsFilter *); + +/* + * True if a vcpu qom path and provider match a filter or if no corresponding + * filters are defined. False otherwise. + */ +bool stats_requested_vcpu(const char *, StatsProvider, StatsFilter *); + +#endif /* STATS_H */ diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index db4d186448..07f1e683be 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -36,6 +36,7 @@ #include "qapi/qapi-commands-control.h" #include "qapi/qapi-commands-machine.h" #include "qapi/qapi-commands-misc.h" +#include "qapi/qapi-commands-stats.h" #include "qapi/qapi-commands-ui.h" #include "qapi/type-helpers.h" #include "qapi/qmp/qerror.h" @@ -44,6 +45,7 @@ #include "hw/acpi/acpi_dev_interface.h" #include "hw/intc/intc.h" #include "hw/rdma/rdma.h" +#include "monitor/stats.h" NameInfo *qmp_query_name(Error **errp) { @@ -448,3 +450,220 @@ HumanReadableText *qmp_x_query_irq(Error **errp) return human_readable_text_from_str(buf); } + +typedef struct StatsCallbacks { + StatsProvider provider; + void (*stats_cb)(StatsResults *, StatsFilter *, Error **); + void (*schemas_cb)(StatsSchemaResults *, Error **); + QTAILQ_ENTRY(StatsCallbacks) next; +} StatsCallbacks; + +static QTAILQ_HEAD(, StatsCallbacks) stats_callbacks = + QTAILQ_HEAD_INITIALIZER(stats_callbacks); + +void add_stats_callbacks(StatsProvider provider, + void (*stats_fn)(StatsResults *, StatsFilter*, + Error **), + void (*schemas_fn)(StatsSchemaResults *, Error **)) +{ + StatsCallbacks *entry = g_malloc0(sizeof(*entry)); + entry->provider = provider; + entry->stats_cb = stats_fn; + entry->schemas_cb = schemas_fn; + + QTAILQ_INSERT_TAIL(&stats_callbacks, entry, next); +} + +static StatsRequestList *stats_target_filter(StatsFilter *filter) +{ + switch (filter->target) { + case STATS_TARGET_VM: + if (!filter->u.vm.has_filters) { + return NULL; + } + return filter->u.vm.filters; + case STATS_TARGET_VCPU: + if (!filter->u.vcpu.has_filters) { + return NULL; + } + return filter->u.vcpu.filters; + break; + default: + return NULL; + } +} + +static bool stats_provider_match(StatsProvider provider, + StatsRequestList *request) +{ + return (!request->value->has_provider || + (request->value->provider == provider)); +} + +static bool stats_requested_provider(StatsProvider provider, + StatsFilter *filter) +{ + StatsRequestList *request = stats_target_filter(filter); + + if (!request) { + return true; + } + for (; request; request = request->next) { + if (stats_provider_match(provider, request)) { + return true; + } + } + return false; +} + +StatsResults *qmp_query_stats(StatsFilter *filter, Error **errp) +{ + StatsResults *stats_results = g_malloc0(sizeof(*stats_results)); + StatsCallbacks *entry; + + QTAILQ_FOREACH(entry, &stats_callbacks, next) { + if (stats_requested_provider(entry->provider, filter)) { + entry->stats_cb(stats_results, filter, errp); + } + } + + return stats_results; +} + +StatsSchemaResults *qmp_query_stats_schemas(bool has_provider, + StatsProvider provider, + Error **errp) +{ + StatsSchemaResults *stats_results = g_malloc0(sizeof(*stats_results)); + StatsCallbacks *entry; + + QTAILQ_FOREACH(entry, &stats_callbacks, next) { + if (has_provider && (provider != entry->provider)) { + continue; + } + entry->schemas_cb(stats_results, errp); + } + + return stats_results; +} + +void add_vm_stats_entry(StatsList *stats_list, StatsResults *stats_results, + StatsProvider provider) +{ + StatsResultsEntry *entry = g_malloc0(sizeof(*entry)); + entry->provider = provider; + entry->stats = stats_list; + + QAPI_LIST_PREPEND(stats_results->vm, entry); + stats_results->has_vm = true; +} + +void add_vcpu_stats_entry(StatsList *stats_list, StatsResults *stats_results, + StatsProvider provider, char *path) +{ + StatsResultsVCPUEntry *value; + StatsResultsEntry *entry; + StatsResultsVCPUEntryList **tailp, *tail; + + entry = g_malloc0(sizeof(*entry)); + entry->provider = provider; + entry->stats = stats_list; + + /* Find the vCPU entry and add to its list; else create it */ + tailp = &stats_results->vcpus; + + for (tail = *tailp; tail; tail = tail->next) { + if (g_str_equal(tail->value->path, path)) { + /* Add to the existing vCPU list */ + QAPI_LIST_PREPEND(tail->value->providers, entry); + return; + } + tailp = &tail->next; + } + + /* Create and populate a new vCPU entry */ + value = g_malloc0(sizeof(*value)); + value->path = g_strdup(path); + value->providers = g_malloc0(sizeof(*value->providers)); + value->providers->value = entry; + QAPI_LIST_APPEND(tailp, value); + stats_results->has_vcpus = true; +} + +void add_vm_stats_schema(StatsSchemaValueList *stats_list, + StatsSchemaResults *schema_results, + StatsProvider provider) +{ + StatsSchemaProvider *entry = g_malloc0(sizeof(*entry)); + + entry->provider = provider; + entry->stats = stats_list; + QAPI_LIST_PREPEND(schema_results->vm, entry); + schema_results->has_vm = true; +} + +void add_vcpu_stats_schema(StatsSchemaValueList *stats_list, + StatsSchemaResults *schema_results, + StatsProvider provider) +{ + StatsSchemaProvider *entry = g_malloc0(sizeof(*entry)); + + entry->provider = provider; + entry->stats = stats_list; + QAPI_LIST_PREPEND(schema_results->vcpu, entry); + schema_results->has_vcpu = true; +} + +static bool str_in_list(const char *name, strList *list) +{ + strList *str_list = NULL; + + for (str_list = list; str_list; str_list = str_list->next) { + if (g_str_equal(name, str_list->value)) { + return true; + } + } + return false; +} + +bool stats_requested_name(const char *name, StatsProvider provider, + StatsFilter *filter) +{ + StatsRequestList *request = stats_target_filter(filter); + + if (!request) { + return true; + } + for (; request; request = request->next) { + if (!stats_provider_match(provider, request)) { + continue; + } + if (!request->value->has_fields || + str_in_list(name, request->value->fields)) { + return true; + } + } + return false; +} + +bool stats_requested_vcpu(const char *path, StatsProvider provider, + StatsFilter *filter) +{ + StatsRequestList *request = stats_target_filter(filter); + + if (!request) { + return true; + } + if (!filter->u.vcpu.has_filters) { + return true; + } + if (filter->u.vcpu.has_vcpus && !str_in_list(path, filter->u.vcpu.vcpus)) { + return false; + } + for (; request; request = request->next) { + if (stats_provider_match(provider, request)) { + return true; + } + } + return false; +} diff --git a/qapi/meson.build b/qapi/meson.build index 656ef0e039..fd5c93d643 100644 --- a/qapi/meson.build +++ b/qapi/meson.build @@ -46,6 +46,7 @@ qapi_all_modules = [ 'replay', 'run-state', 'sockets', + 'stats', 'trace', 'transaction', 'yank', diff --git a/qapi/qapi-schema.json b/qapi/qapi-schema.json index 4912b9744e..92d7ecc52c 100644 --- a/qapi/qapi-schema.json +++ b/qapi/qapi-schema.json @@ -93,3 +93,4 @@ { 'include': 'audio.json' } { 'include': 'acpi.json' } { 'include': 'pci.json' } +{ 'include': 'stats.json' } diff --git a/qapi/stats.json b/qapi/stats.json new file mode 100644 index 0000000000..ae5dc3ee2c --- /dev/null +++ b/qapi/stats.json @@ -0,0 +1,259 @@ +# -*- Mode: Python -*- +# vim: filetype=python +# +# Copyright (c) 2022 Oracle and/or its affiliates. +# +# This work is licensed under the terms of the GNU GPL, version 2 or later. +# See the COPYING file in the top-level directory. +# +# SPDX-License-Identifier: GPL-2.0-or-later + +## +# = Stats +## + +## +# @StatsType: +# +# Enumeration of stats types +# @cumulative: stat is cumulative; value can only increase. +# @instant: stat is instantaneous; value can increase or decrease. +# @peak: stat is the peak value; value can only increase. +# @linear-hist: stat is a linear histogram. +# @log-hist: stat is a logarithmic histogram. +# +# Since: 7.0 +## +{ 'enum' : 'StatsType', + 'data' : [ 'cumulative', 'instant', 'peak', 'linear-hist', 'log-hist' ] } + +## +# @StatsUnit: +# +# Enumeration of stats units +# @bytes: stat reported in bytes. +# @seconds: stat reported in seconds. +# @cycles: stat reported in clock cycles. +# @none: no unit for this stat. +# +# Since: 7.0 +## +{ 'enum' : 'StatsUnit', + 'data' : [ 'bytes', 'seconds', 'cycles', 'none' ] } + +## +# @StatsBase: +# +# Enumeration of stats base +# @pow10: scale is based on power of 10. +# @pow2: scale is based on power of 2. +# +# Since: 7.0 +## +{ 'enum' : 'StatsBase', + 'data' : [ 'pow10', 'pow2' ] } + +## +# @StatsProvider: +# +# Enumeration of stats providers. +# +# Since: 7.0 +## +{ 'enum': 'StatsProvider', + 'data': [ ] } + +## +# @StatsTarget: +# +# Enumeration of stats targets. +# @vm: stat is per vm. +# @vcpu: stat is per vcpu. +# +# Since: 7.0 +## +{ 'enum': 'StatsTarget', + 'data': [ 'vm', 'vcpu' ] } + +## +# @StatsRequest: +# +# Stats filter element. +# @provider: stat provider. +# @fields: list of stat names. +# +# Since: 7.0 +## +{ 'struct': 'StatsRequest', + 'data': { '*provider': 'StatsProvider', + '*fields': [ 'str' ] } } + +## +# @StatsRequestArray: +# +# @filters: filters for this request. +## +{ 'struct': 'StatsRequestArray', + 'data': { '*filters': [ 'StatsRequest' ] } } + +## +# @StatsVCPURequestArray: +# +# @vcpus: list of qom paths. +## +{ 'struct': 'StatsVCPURequestArray', + 'base': 'StatsRequestArray', + 'data': { '*vcpus': [ 'str' ] } } + +## +# @StatsFilter: +# +# Target specific filter. +# +# Since: 7.0 +## +{ 'union': 'StatsFilter', + 'base': { 'target': 'StatsTarget' }, + 'discriminator': 'target', + 'data': { 'vcpu': 'StatsVCPURequestArray', + 'vm': 'StatsRequestArray' } } + +## +# @StatsValueArray: +# +# @values: uint64 list. +# +# Since: 7.0 +## +{ 'struct': 'StatsValueArray', + 'data': { 'values' : [ 'uint64' ] } } + +## +# @StatsValue: +# +# @scalar: single uint64. +# @list: list of uint64. +# +# Since: 7.0 +## +{ 'alternate': 'StatsValue', + 'data': { 'scalar': 'uint64', + 'list': 'StatsValueArray' } } + +## +# @Stats: +# +# @name: name of stat. +# @value: stat value. +# +# Since: 7.0 +## +{ 'struct': 'Stats', + 'data': { 'name': 'str', + 'value' : 'StatsValue' } } + +## +# @StatsResultsEntry: +# +# @provider: stat provider. +# @stats: list of stats. +# +# Since: 7.0 +## +{ 'struct': 'StatsResultsEntry', + 'data': { 'provider': 'StatsProvider', + 'stats': [ 'Stats' ] } } + +## +# @StatsResultsVCPUEntry: +# +# @path: vCPU qom path. +# @providers: per provider results. +# +# Since: 7.0 +## +{ 'struct': 'StatsResultsVCPUEntry', + 'data': { 'path': 'str', + 'providers': [ 'StatsResultsEntry' ] } } + +## +# @StatsResults: +# +# Target specific results. +# +# Since: 7.0 +## +{ 'struct': 'StatsResults', + 'data': { + '*vcpus': [ 'StatsResultsVCPUEntry' ], + '*vm': [ 'StatsResultsEntry' ] } } + +## +# @query-stats: +# +# data: @StatsFilter. +# Returns: @StatsResults. +# +# Since: 7.0 +## +{ 'command': 'query-stats', + 'data': 'StatsFilter', + 'boxed': true, + 'returns': 'StatsResults' } + +## +# @StatsSchemaValue: +# +# Individual stat schema. +# @name: stat name. +# @type: @StatType. +# @unit: @StatUnit. +# @base: @StatBase. +# @exponent: Used together with @base. +# @bucket-size: Used with linear-hist to report bucket size +# +# Since: 7.0 +## +{ 'struct': 'StatsSchemaValue', + 'data': { 'name': 'str', + 'type': 'StatsType', + 'unit': 'StatsUnit', + 'base': 'StatsBase', + 'exponent': 'int16', + '*bucket-size': 'uint32' } } + +## +# @StatsSchemaProvider: +# +# @provider: @StatsProvider. +# @stats: list of stats. +# +# Since: 7.0 +## +{ 'struct': 'StatsSchemaProvider', + 'data': { 'provider': 'StatsProvider', + 'stats': [ 'StatsSchemaValue' ] } } + +## +# @StatsSchemaResults: +# +# @vm: vm stats schemas. +# @vcpu: vcpu stats schemas. +# +# Since: 7.0 +## +{ 'struct': 'StatsSchemaResults', + 'data': { '*vm': [ 'StatsSchemaProvider' ], + '*vcpu': [ 'StatsSchemaProvider' ] } } + +## +# @query-stats-schemas: +# +# Query Stats schemas. +# Returns @StatsSchemaResult. +# +# Since: 7.0 +## +{ 'command': 'query-stats-schemas', + 'data': { '*provider': 'StatsProvider' }, + 'returns': 'StatsSchemaResults' } From patchwork Tue Feb 15 15:04:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Kanda X-Patchwork-Id: 12747220 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1682C433FE for ; Tue, 15 Feb 2022 15:06:56 +0000 (UTC) Received: from localhost ([::1]:46984 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nJzPr-0007um-H5 for qemu-devel@archiver.kernel.org; Tue, 15 Feb 2022 10:06:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58474) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOS-0006Nw-PY for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:28 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:65246) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOO-0005Ic-Na for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:27 -0500 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21FEi1et014275; Tue, 15 Feb 2022 15:04:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=YiA/DrP9SEX7oqG5ONHNxPcq9Lbjv8Yim5iVjYApaU8=; b=mJW80n7HZCmh1KuETZt3VmonHrap1eRLdrra4z1Os38KJMeWAtruzmBP/J8QJui8OLs2 9t0qsROt/Y1I9q9/YtQxLeumq8Vynni+HA80NHauyMzlauhCCPjSu8TnK5UXqkuUJ286 CAFdsNsU/hJWHWIXsAj3/Ah7e3sitNvQ2ijcp7xZdd09dUijiioegSIPhHbalN3V8TsO 5yN1r0oIj83u4ALtNkuIOb3JV9+GH8ijKMud/3W6WUth1aRgmt0X17va48ai02r/Qzfr PWKKgU7o4KjmuNJNXZTB6GIWGC2hfabzRwPhXIM50vYJE9PV5EzFOD7adFX9CX76F2eQ Fg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3e8570skqf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:45 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 21FF0UMx008531; Tue, 15 Feb 2022 15:04:44 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2170.outbound.protection.outlook.com [104.47.55.170]) by aserp3030.oracle.com with ESMTP id 3e62xerfe0-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HkdY+2+B4CJYeE2boFJd+kbSXlSpIEJvu2Ax9IoUqSW9LR0p1nKYQ3RXUt6da/TYsOGdP70qDhkh8Oak400jE1zQOUbiWe4MEDdiXLZW3gYU09QDoIADJjWXgdFTZMjvREjPHfu/fQrLqJJblnqhPTj3FNG7IUFXIZM1vdMx0shvqmFJW3FlTQj9VEj+nUECNLeY3atAFvJB3kn2JbBO67B5mUhRELB3EwQEQCg45vOniXnWnWstIUyfEipdzVuHNHHij7GNVEumMuXosXPomityAsaDYm3Odg1+de+kJQiMtfULVNuXN1pwpnYUeoOsNRFKdMMANeNwC++SYqcpkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YiA/DrP9SEX7oqG5ONHNxPcq9Lbjv8Yim5iVjYApaU8=; b=hNGl97WLEghvNiBgmPmI866ChLZzKZ24WzHegjS+iRQMwaS4OexIT3FYndLBBxUlKo6Jp0VCAPNRowq2ysMx43PXi2HpIkCV1RdgPgK890/Q7HZ6N3vEvUl1Rjzhsni42BZhzRHE2wdEdYyIUGe5jD45NKiK0aUVx7/D2JUbLFw72nCMtHpRyBCHpPttAudoxlyeonYGrKkRP1shlv9npuueN/pSF/tZH6JWNgHcwPzVZu4wH8+y2LsU4GCHX82lyPlkbVReUcRC25kPQsLdhXHCYyUsa9qcQeM/j2Do8cpb3Zx6ECIIzHneRM3gPg4o1ALq1PvVXi9uJIX4kOnAEQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YiA/DrP9SEX7oqG5ONHNxPcq9Lbjv8Yim5iVjYApaU8=; b=mfF2rOQgtzAmQgGlGk0PAD/AbQ1G2IWLeGjWG9xNfF1prFd6TkuMy+YjReL1UHM/OFq21pCQlbqbeTizfzFnhZkU+KuVPF9E/h1Tqc0A25gSO450NjCGBvJgOQdp+PRrLmsOryhp1gaZ4Fujvfg4TKlbKZri4jS6IMhsBE+NMlA= Received: from SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) by DM6PR10MB3865.namprd10.prod.outlook.com (2603:10b6:5:1d3::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.12; Tue, 15 Feb 2022 15:04:42 +0000 Received: from SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716]) by SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716%5]) with mapi id 15.20.4951.016; Tue, 15 Feb 2022 15:04:42 +0000 From: Mark Kanda To: qemu-devel@nongnu.org Subject: [PATCH v4 2/3] hmp: Support for querying stats Date: Tue, 15 Feb 2022 09:04:32 -0600 Message-Id: <20220215150433.2310711-3-mark.kanda@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220215150433.2310711-1-mark.kanda@oracle.com> References: <20220215150433.2310711-1-mark.kanda@oracle.com> X-ClientProxiedBy: SN7PR04CA0038.namprd04.prod.outlook.com (2603:10b6:806:120::13) To SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 46ef3c01-abcb-4e1b-17c5-08d9f0947eb8 X-MS-TrafficTypeDiagnostic: DM6PR10MB3865:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:165; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OXncg3KkURBlNeWFalt9i/dpaaFVCDRCW7nWvNfez7CDoGYJsu9874DC4d7CThfTUxTk8lfifJeoQpOVJUCrS+GFO7tiomVRarGfnTmnQrw9lY6jB6tYext6SCUQVUMPZpfsxf4icyrUX2EvkQwzM67vKg9k16yCsBR830tVrz+n7qN/rd2kxJevMleLOZCanmy3ujW5SfPMkRb9/imMq52eAoY8YqYy9LkzVz7qMm7VEnN6UroQz48XhFTPkf9++aHTLCBVqV846KGuGJpgdNcU6z1u9vzXwyNQnrjVTtOmB+ab1i/DntAQRIHKGNyzHAtGiUxkdoDqRSb46aJSx11D3MNDnePCcHFYm64kvP5zIhvXYmuRkb1mia1iOMfYk336b+WWdylcBPI10ChgAr3xQBSHvvuUSqgM7nYjSR0MhH7WiEojs/muju1zWzU5/isLiNopVEd1/Bng7+Jm3U9yP8iSlFaXmwrGX6+R8OGmhpqi1jc/hMOIUJEJb3S81IWduZcSZLeFxX6N3mThHejq1FY6aaUGaXuaD2vOPTnVk2AXrMrswWLvWlXJuV+ql81FxSa80ZhbeIhxBJqMUYacm4tCcR0x+3GCQAAqiKu03d4CMHCyVnezyD3TK3m0pPM93amZgSHKE5uNZFLbBLH249919JYFIbEVkPZYaVzsp7BTWwtVM+hQ+aE9s4aRYpCb+4G0qD4TTGjivZeFLA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA1PR10MB5841.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(6506007)(107886003)(2616005)(8676002)(1076003)(66476007)(26005)(6512007)(66556008)(86362001)(6916009)(52116002)(66946007)(6486002)(316002)(6666004)(508600001)(36756003)(83380400001)(44832011)(38350700002)(38100700002)(4326008)(5660300002)(186003)(8936002)(2906002)(30864003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1oWwkfqmqvO6vdxuPOG3zABdOx28Oa+atWdVgu8NZxg88NCYh2xR6FpJuqAKwg2Wn0T4S5+L7G9zGA7y0sCBQRMaz64XU/v6/8suakC6WO1gtroh6fsMvxR9SYQR/0u9NSRCuDzL7ZZh9L8taEyXeJHe72PWEHlbEshLH1r+jUTF6G8X2T0/izt6Yx7iIDHD0yPgJQA+sfqzcIErSbMrfzy53C7xv+TBkgB/URGnfAnD4F6mdVlJVhsmCESPe0sFyPbfFLlkEhbVAC2bFyMbSeT1adpDjGhLm11gij/O4ZEL0mgHjRF9zQOzmLTQoQmoNPDvsUCUVm5uWVVvBeymvxRo2mGMwkQ5c689Zqx5B6/fHNsHVpWRC0fviZ3bNZNJaQcd4qkdgwBDnRW8PNx0HEUr6eC481MYNFnyWmmZfg0mlPjZbuLvn8+K/tJazuiugzWRXM6WMhoqvCEE/nRJlqsDjIhVsMvw1Wyil7pLqJSlCPaGpJ9OeZnuwEjNpboFFSBs4rOmckpHDsumUEGqOl/+H4Kuepm2Cf5TtaGKo6bJguvZm0JIZcSEusU7u3/No9EoGV36CxsRgGs+IGRHpz8NAz8CjeYQplLcJybwZr8NuyLdKNxdMozA03duTRL2YAJoFKRBrkp6tsVn0neka5yYK2vb30Urdh61H5XL1PNfON0Wvy7YLJC0wueNsrEkM1Jy2uAj5GcU11EPmLzv/poj3dQCr+t2FN1bzvUT0F1aKszVm846Jpmn/TxqfWJkN3uO//TQP/843LQNtb4WjYQcPi+jDQZNmN33blSy3WtkeLYb/rNJRCwp79fAML3ZZWZpVu1KMJsO0398YJIw2ylqOH0qJ/5LYaVcLlR+soOFtzBeAEX9ZDp9REwzvfxwS+cmQcMaYiqvwwWg8oLxVklDskaJq1Le4EjjsVJVSjqs3zgRtz7Wi7Nly407GvKFL9iQBfD8kYYgUC90agc3y5xSK6byp9lECRTkFY/KmQCHALs0b1zcbtGSSxBtJXv7itelgdJpeyC4GffMdb38G09/3VT++taobhOy9WnY2W87HcknGz+ilOCEJMYJrhoNS8MCnznvtnoHNN9KIRn0jDtkFKxaYKAua5cmhpBWoi1VKUoFLvP3PddLleCcy2Uh/G7DXZubenqRMYqio9vYEw5nWgJa0gdw27Jr4dmU2T7EAxqN2vOgmqkZ+4BDFKhyxn9FVn9PQSX69lPt5ymokA0DusHWMnhkib/rx4ZYePZUxceAduWb+yy6XeMNoz1eRUdtegOeUXsHSFVNTROC30CkW/29JlhJ0xBZvrjTZGWvVce1HUWSLv0oVP4FgJx5PsRt0J3CCrnZ1hIeW4jx2zPmcb8nNwLIOp+1Ca9s+MdggTI7AcfpPAL3uhXZSUfyEANsZoL/71sP0suQ3iSrqXt4SRPGxl3BNm9zEHvFzIu+vpqykI3/NVTkTuAE+lXe3UMsga4H49P74ze7dm2roO004PjaDSxOsd6IiB5MvJKnNjuExqmRqUrcikq8xadigBtJ7GpOaRo5x3A2cGSgZ1D472/AmP0EYWEzBm7LoKbc69koDYmNsDq1mlyAthwk0RTSfumgYE3Mijh42LGCUWcyD+ixlDB4vglKOq92iDA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 46ef3c01-abcb-4e1b-17c5-08d9f0947eb8 X-MS-Exchange-CrossTenant-AuthSource: SA1PR10MB5841.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Feb 2022 15:04:42.4892 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FUxOiZcPwmCu4PxZRH+DMmPJQvh9ciGt20QjaYn399NkLIEX2GCFjeEXbo8pS3x6DAqhgVJ40Ea/iHVqIgdRbQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3865 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10258 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 bulkscore=0 malwarescore=0 adultscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202150088 X-Proofpoint-ORIG-GUID: b4K72x2PClYEvKKwJ0YEwXoCd2kpRtO- X-Proofpoint-GUID: b4K72x2PClYEvKKwJ0YEwXoCd2kpRtO- Received-SPF: pass client-ip=205.220.165.32; envelope-from=mark.kanda@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: , Cc: pbonzini@redhat.com, berrange@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Leverage the QMP support for querying stats. The interface supports similar arguments as the QMP interface. Wildcard char (*) is accepted for names and stats target. Examples (with fd-based KVM stats): - Display all stats (qemu) info stats vm provider: kvm max_mmu_page_hash_collisions (peak): 0 max_mmu_rmap_size (peak): 0 ... vcpu (qom path: /machine/unattached/device[0]) provider: kvm guest_mode (instant): 0 directed_yield_successful (cumulative): 0 ... (qemu) info stats-schemas vm provider: kvm max_mmu_page_hash_collisions (peak) max_mmu_rmap_size (peak) ... vcpu provider: kvm guest_mode (instant) directed_yield_successful (cumulative) - Display 'halt_wait_ns' and 'exits' for vCPUs with qom paths /machine/unattached/device[2] and /machine/unattached/device[4]: (qemu) info stats exits,halt_wait_ns /machine/unattached/device[2], /machine/unattached/device[4] vcpu (qom path: /machine/unattached/device[2]) provider: kvm exits (cumulative): 52369 halt_wait_ns (cumulative nanoseconds): 416092704390 vcpu (qom path: /machine/unattached/device[4]) provider: kvm exits (cumulative): 52550 halt_wait_ns (cumulative nanoseconds): 419637402657 - Display all VM stats for provider KVM: (qemu) info stats * vm kvm vm provider: kvm max_mmu_page_hash_collisions (peak): 0 max_mmu_rmap_size (peak): 0 nx_lpage_splits (instant): 51 ... Signed-off-by: Mark Kanda --- hmp-commands-info.hx | 28 ++++ include/monitor/hmp.h | 2 + monitor/hmp-cmds.c | 328 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+) diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx index e90f20a107..c0ad3a3e2a 100644 --- a/hmp-commands-info.hx +++ b/hmp-commands-info.hx @@ -879,3 +879,31 @@ SRST ``info sgx`` Show intel SGX information. ERST + + { + .name = "stats", + .args_type = "names:s?,vcpus:s?,provider:s?", + .params = "[names] [vcpus] [provider]", + .help = "show statistics; optional comma separated names, " + "vcpu qom paths, and provider", + .cmd = hmp_info_stats, + }, + +SRST + ``stats`` + Show stats +ERST + + { + .name = "stats-schemas", + .args_type = "provider:s?", + .params = "[provider]", + .help = "show statistics schema for each provider", + .cmd = hmp_info_stats_schemas, + }, + +SRST + ``stats-schemas`` + Show stats per schema +ERST + diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h index 96d014826a..a748511105 100644 --- a/include/monitor/hmp.h +++ b/include/monitor/hmp.h @@ -133,5 +133,7 @@ void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict); void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict); void hmp_human_readable_text_helper(Monitor *mon, HumanReadableText *(*qmp_handler)(Error **)); +void hmp_info_stats(Monitor *mon, const QDict *qdict); +void hmp_info_stats_schemas(Monitor *mon, const QDict *qdict); #endif diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 8c384dc1b2..2a8f1432fb 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -40,6 +40,7 @@ #include "qapi/qapi-commands-pci.h" #include "qapi/qapi-commands-rocker.h" #include "qapi/qapi-commands-run-state.h" +#include "qapi/qapi-commands-stats.h" #include "qapi/qapi-commands-tpm.h" #include "qapi/qapi-commands-ui.h" #include "qapi/qapi-visit-net.h" @@ -2178,3 +2179,330 @@ void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict) } hmp_handle_error(mon, err); } + +static void print_stats_schema_value(Monitor *mon, StatsSchemaValue *value) +{ + monitor_printf(mon, " %s (%s", value->name, StatsType_str(value->type)); + + if (value->unit == STATS_UNIT_SECONDS && + value->exponent >= -9 && value->exponent <= 0 && + value->exponent % 3 == 0 && value->base == STATS_BASE_POW10) { + + const char *si_prefix[] = { "", "milli", "micro", "nano" }; + monitor_printf(mon, " %s", si_prefix[value->exponent / -3]); + + } else if (value->unit == STATS_UNIT_BYTES && + value->exponent >= 0 && value->exponent <= 40 && + value->exponent % 10 == 0 && value->base == STATS_BASE_POW2) { + + const char *si_prefix[] = { + "", "kilo", "mega", "giga", "tera" }; + monitor_printf(mon, " %s", si_prefix[value->exponent / 10]); + + } else if (value->exponent) { + /* Print the base and exponent as "x ^" */ + monitor_printf(mon, " x %s^%d", StatsBase_str(value->base), + value->exponent); + } + + /* Don't print "none" unit type */ + monitor_printf(mon, "%s)", value->unit == STATS_UNIT_NONE ? + "" : StatsUnit_str(value->unit)); + + /* Print bucket size for linear histograms */ + if ((value->type == STATS_TYPE_LINEAR_HIST) && value->has_bucket_size) { + monitor_printf(mon, " bucket-size=%d", value->bucket_size); + } +} + +static StatsSchemaValueList *find_schema_value_list( + StatsSchemaProviderList *list, StatsProvider provider) +{ + StatsSchemaProviderList *schema_provider_list; + + for (schema_provider_list = list; + schema_provider_list; + schema_provider_list = schema_provider_list->next) { + if (schema_provider_list->value->provider == provider) { + return schema_provider_list->value->stats; + } + } + return NULL; +} + +static void print_stats_results(Monitor *mon, StatsTarget type, + StatsResultsEntryList *list, + StatsSchemaProviderList *schema) +{ + StatsResultsEntryList *results_entry_list; + + for (results_entry_list = list; + results_entry_list; + results_entry_list = results_entry_list->next) { + + StatsResultsEntry *results_entry = results_entry_list->value; + /* Find provider schema */ + StatsSchemaValueList *schema_value_list = + find_schema_value_list(schema, results_entry->provider); + StatsList *stats_list; + + if (!schema_value_list) { + monitor_printf(mon, "failed to find schema list for %s\n", + StatsProvider_str(results_entry->provider)); + return; + } + + monitor_printf(mon, " provider: %s\n", + StatsProvider_str(results_entry->provider)); + + for (stats_list = results_entry->stats; + stats_list; + stats_list = stats_list->next, + schema_value_list = schema_value_list->next) { + + Stats *stats = stats_list->value; + StatsValue *stats_value = stats->value; + StatsSchemaValue *schema_value = schema_value_list->value; + + /* Find schema entry */ + while (!g_str_equal(stats->name, schema_value->name)) { + if (!schema_value_list->next) { + monitor_printf(mon, "failed to find schema entry for %s\n", + stats->name); + return; + } + schema_value_list = schema_value_list->next; + schema_value = schema_value_list->value; + } + + print_stats_schema_value(mon, schema_value); + + if (stats_value->type == QTYPE_QNUM) { + monitor_printf(mon, ": %" PRId64 "\n", stats_value->u.scalar); + } else if (stats_value->type == QTYPE_QDICT) { + uint64List *list; + int i; + + monitor_printf(mon, ": "); + for (list = stats_value->u.list.values, i = 1; + list; + list = list->next, i++) { + monitor_printf(mon, "[%d]=%" PRId64 " ", i, list->value); + } + monitor_printf(mon, "\n"); + } + } + } +} + +static const char wildcard[] = "*"; +static StatsFilter *stats_filter(StatsTarget target, const char *names, + const char *vcpus, StatsProvider provider) +{ + StatsFilter *filter = g_malloc0(sizeof(*filter)); + filter->target = target; + + switch (target) { + case STATS_TARGET_VM: + { + StatsRequestList *request_list = NULL; + StatsRequest *request = g_malloc0(sizeof(*request)); + + if (names && !g_str_equal(names, wildcard)) { + request->fields = strList_from_comma_list(names); + request->has_fields = true; + } + if (provider != STATS_PROVIDER__MAX) { + request->provider = provider; + request->has_provider = true; + } + + QAPI_LIST_PREPEND(request_list, request); + filter->u.vm.filters = request_list; + filter->u.vm.has_filters = true; + break; + } + case STATS_TARGET_VCPU: + { + StatsRequestList *request_list = NULL; + StatsRequest *request = g_malloc0(sizeof(*request)); + if (names && !g_str_equal(names, wildcard)) { + request->fields = strList_from_comma_list(names); + request->has_fields = true; + } + if (provider != STATS_PROVIDER__MAX) { + request->provider = provider; + request->has_provider = true; + } + + if (vcpus && !g_str_equal(vcpus, wildcard) && + !g_str_equal(vcpus, StatsTarget_str(STATS_TARGET_VCPU))) { + filter->u.vcpu.vcpus = strList_from_comma_list(vcpus); + filter->u.vcpu.has_vcpus = true; + } + + QAPI_LIST_PREPEND(request_list, request); + filter->u.vcpu.filters = request_list; + filter->u.vcpu.has_filters = true; + break; + } + default: + break; + } + return filter; +} + +void hmp_info_stats(Monitor *mon, const QDict *qdict) +{ + const char *names = qdict_get_try_str(qdict, "names"); + const char *vcpus = qdict_get_try_str(qdict, "vcpus"); + const char *provider = qdict_get_try_str(qdict, "provider"); + + StatsProvider stats_provider = STATS_PROVIDER__MAX; + StatsTarget target; + Error *err = NULL; + + if (provider) { + for (stats_provider = 0; stats_provider < STATS_PROVIDER__MAX; + stats_provider++) { + if (g_str_equal(StatsProvider_str(stats_provider), provider)) { + break; + } + } + if (stats_provider == STATS_PROVIDER__MAX) { + monitor_printf(mon, "invalid stats filter provider %s\n", + provider); + goto exit; + } + } + + for (target = 0; target < STATS_TARGET__MAX; target++) { + StatsResults *stats_results = NULL; + StatsSchemaResults *schema_results = NULL; + StatsResultsVCPUEntryList *results_entry_list; + + StatsFilter *filter = stats_filter(target, names, vcpus, + stats_provider); + switch (target) { + case STATS_TARGET_VM: + if (vcpus && !g_str_equal(vcpus, wildcard) && + !g_str_equal(vcpus, StatsTarget_str(STATS_TARGET_VM))) { + break; + } + stats_results = qmp_query_stats(filter, &err); + if (err) { + goto exit; + } + schema_results = + qmp_query_stats_schemas(provider ? true : false, + stats_provider, &err); + if (err) { + goto exit; + } + if (!stats_results->has_vm) { + break; + } + monitor_printf(mon, "%s\n", StatsTarget_str(STATS_TARGET_VM)); + print_stats_results(mon, STATS_TARGET_VM, stats_results->vm, + schema_results->vm); + + break; + case STATS_TARGET_VCPU: + stats_results = qmp_query_stats(filter, &err); + if (err) { + goto exit; + } + schema_results = + qmp_query_stats_schemas(provider ? true : false, + stats_provider, &err); + if (err) { + goto exit; + } + for (results_entry_list = stats_results->vcpus; + results_entry_list; + results_entry_list = results_entry_list->next) { + monitor_printf(mon, "%s (qom path: %s)\n", + StatsTarget_str(STATS_TARGET_VCPU), + results_entry_list->value->path); + + print_stats_results(mon, STATS_TARGET_VCPU, + results_entry_list->value->providers, + schema_results->vcpu); + } + break; + default: + break; + } + qapi_free_StatsFilter(filter); + qapi_free_StatsSchemaResults(schema_results); + qapi_free_StatsResults(stats_results); + } + +exit: + if (err) { + monitor_printf(mon, "%s\n", error_get_pretty(err)); + error_free(err); + } +} + +static void print_stats_schema_list(Monitor *mon, StatsSchemaProviderList *list) +{ + StatsSchemaProviderList *schema_provider_list; + + for (schema_provider_list = list; + schema_provider_list; + schema_provider_list = schema_provider_list->next) { + + StatsSchemaProvider *schema_provider = + schema_provider_list->value; + monitor_printf(mon, " provider: %s\n", + StatsProvider_str(schema_provider->provider)); + + StatsSchemaValueList *schema_value_list; + for (schema_value_list = schema_provider->stats; + schema_value_list; + schema_value_list = schema_value_list->next) { + + StatsSchemaValue *schema_value = schema_value_list->value; + print_stats_schema_value(mon, schema_value); + monitor_printf(mon, "\n"); + } + } +} + +void hmp_info_stats_schemas(Monitor *mon, const QDict *qdict) +{ + const char *provider = qdict_get_try_str(qdict, "provider"); + StatsProvider stats_provider = STATS_PROVIDER__MAX; + StatsSchemaResults *schema_result; + Error *err = NULL; + + if (provider) { + for (stats_provider = 0; stats_provider < STATS_PROVIDER__MAX; + stats_provider++) { + if (g_str_equal(StatsProvider_str(stats_provider), provider)) { + break; + } + } + if (stats_provider == STATS_PROVIDER__MAX) { + monitor_printf(mon, "invalid stats filter provider %s\n", provider); + return; + } + } + + schema_result = + qmp_query_stats_schemas(provider ? true : false, stats_provider, &err); + + if (err) { + monitor_printf(mon, "%s\n", error_get_pretty(err)); + error_free(err); + return; + } + + monitor_printf(mon, "%s\n", StatsTarget_str(STATS_TARGET_VM)); + print_stats_schema_list(mon, schema_result->vm); + monitor_printf(mon, "%s\n", StatsTarget_str(STATS_TARGET_VCPU)); + print_stats_schema_list(mon, schema_result->vcpu); + + qapi_free_StatsSchemaResults(schema_result); +} From patchwork Tue Feb 15 15:04:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Kanda X-Patchwork-Id: 12747219 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1E4BC433EF for ; Tue, 15 Feb 2022 15:06:56 +0000 (UTC) Received: from localhost ([::1]:47008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nJzPr-0007vj-7A for qemu-devel@archiver.kernel.org; Tue, 15 Feb 2022 10:06:55 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58488) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOT-0006Pu-Kz for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:29 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:3642) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nJzOQ-0005Ij-L0 for qemu-devel@nongnu.org; Tue, 15 Feb 2022 10:05:29 -0500 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21FEhw9E006438; Tue, 15 Feb 2022 15:04:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=Bc31BF1ElyVGC/v4fKu2HDRZqSdf4+H+tEpStshewts=; b=iybzzUFkJn34kwoTjtr2ExdqNzSHp0gt5vBq3ARlrjx6g16lXfOeVULRkgZkSFbNfhaD F7Fc5XXecXLV2wXclOMz9LnpJZcTHysMyTKPEp/xmYCunH3TIUPHSeg5U1KsamQbAISl fd4rYREnyldpN64vrcIYJt0j2w1LZ0xqSySAn/x03s0LVjWZXXHnpk9at+Eeeuxdyxcr Y23cNMrmi2T/aBTjdh6K7cUILZC/U0/KHrUpaXQcTSAr1PYVJec9by2NbZ/tbrUyFpmC Ew76cl7MNaqrhDC0DOoPtdVUCzsr77vL+kw098ZutkcAhyk1Mtl5Tty8WkMX5W2OFjqk Fg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3e86n0hjey-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:45 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 21FF0UN0008531; Tue, 15 Feb 2022 15:04:44 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2170.outbound.protection.outlook.com [104.47.55.170]) by aserp3030.oracle.com with ESMTP id 3e62xerfe0-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Feb 2022 15:04:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=REly/6xLZSf7Ajq/DVEADsbd1wBpGAVKy0llp9YjDPc5K8gNAz1+O1GcblVsB5nPn3DbxvbPaGvzpvL+YmdxFZ/UbznW8n5SYx8llD+Tw+zNRcwI3Ye6d76mFcM1PH9ux9C6spzHIBq0xOq/zKnNVFDaMTWN7M0uQTFb4C9+7Qp9jXZfvrnrrAKpJZBUdjR7VJ6E+6B8CvAJfava0l2SQ6JuVt93yjfKNfHyLiCYHvH1946zIY91YkCSaOdctsvhclsy6tdSCBpWZjP3+bhAWAfFg+m0vqp49MCn2PuhQB3CpDtB8uFJlMt2snqhcVq8CO+YlBaAmYRM0lmT4FASZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Bc31BF1ElyVGC/v4fKu2HDRZqSdf4+H+tEpStshewts=; b=Zk8mJQyQYLJsGuQ06gQgRNSFbZuA1UBJ/XRiVwdAxo0Z42L8g1+Ayt6lZtLVRlAgTZrpaYj5OgtK0uEZMqkjPMg+RCeOaHlpQi9lxyPTyeuO4bKZ2EDsVpIz4hS1Jef7VXUBq47EWYoxzjRJhE202/I034SF59S537F5yI5ke663Xe+KObtJRMKHhOz7SDVn8xQhVDGIZvVZdy8Z2zyg6ZCTLC3PEymrB93SIOe+ffdRvSYBFgKMiDWXEMCVKNzKC3Cs7UixcjsiLzfoBuxQHr8TyTLJowWu18O1O2WGe44SWRTc/zJ1k2Cv+mhjrnG3WxY81oBm0aODoEvkJvsfiA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Bc31BF1ElyVGC/v4fKu2HDRZqSdf4+H+tEpStshewts=; b=BIP0wYax1P4bQ5wjaPCQ8IWQ8mUZEwNdT8Pir+YkCEP5z8LggB8CKtN73hJOn90381+IF+aaMoyVIkf6pBFh2EvDrOJGsDbZRnuGPM87SvI7Lk/N7qaaEDLe0M+KPOjbucrEDlevCogXuDq6UWtwDQwm8AYT2o25ujf0ZYuNIAc= Received: from SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) by DM6PR10MB3865.namprd10.prod.outlook.com (2603:10b6:5:1d3::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.12; Tue, 15 Feb 2022 15:04:43 +0000 Received: from SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716]) by SA1PR10MB5841.namprd10.prod.outlook.com ([fe80::58ef:3181:b9d4:b716%5]) with mapi id 15.20.4951.016; Tue, 15 Feb 2022 15:04:43 +0000 From: Mark Kanda To: qemu-devel@nongnu.org Subject: [PATCH v4 3/3] kvm: Support for querying fd-based stats Date: Tue, 15 Feb 2022 09:04:33 -0600 Message-Id: <20220215150433.2310711-4-mark.kanda@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220215150433.2310711-1-mark.kanda@oracle.com> References: <20220215150433.2310711-1-mark.kanda@oracle.com> X-ClientProxiedBy: SN7PR04CA0038.namprd04.prod.outlook.com (2603:10b6:806:120::13) To SA1PR10MB5841.namprd10.prod.outlook.com (2603:10b6:806:22b::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e4fb677b-ae14-4a56-8339-08d9f0947f2a X-MS-TrafficTypeDiagnostic: DM6PR10MB3865:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:17; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Sjpp27R+9YMZ9okxAUCNWHiW/yQxDlpQnZGlx3xThOn+DFYUC43Ifc+7OOhe5k6RhPggnsJtLlcHhWaO0GpFLISWVOMVpvwyX9reDEXtJdfXXEH/4emLX/C59yYvF/pY5/SxWyJkp/gJ2oabhEcJPPobaTb3fTvQhuZLC4wJY/XtcyPAutnbJk5wtusnk88zxjhhyc3ZbZeiOZvE57G8eDql297l+NjZCH3qV3eombL0x+Fb6R9K9/C2P8YVoqCtAyMKqYALq6/tu8TGtkOLKg3IoE0ogeFh2raVhTQ6lj78MyHxUejVDY6YCjtOr3pnjI0dRkAW3QDPD3KjLB74AxOdBQLKvpCshlXUK7AGnBmsSqiaxRbZI9ewaC1/rBFp3q2+uIJH/1s5zj9y3ByroIr6hMD/lM6J4VWi9R+E4mrCUKVQox6+6G+b2+fjr6icwF+pFRE7t3EvC4srvMB1+7Purys6kAXNcY3EMeMLzCvBloFks2kumoGci8AksYSe+bIwRH5MA3nR21PzRA4243jyg82eIUfiSoQtxJTHnHfaZV6yr2pac1T1fyi06KYwZ5blRvpYsPlu+yENuz3kgrQsoAtZnStYC737sIpkFqY3Ct0F793ttmiNz2eE2Vd69SvtTAsqZs/4HKzT6+4MLfuziZW7qou23fzOV5HXqlmnH3TM+lDnLSFETX3YcrMZp/5Y0MVGvopVin/N6jyd3Q== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA1PR10MB5841.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(6506007)(107886003)(2616005)(8676002)(1076003)(66476007)(26005)(6512007)(66556008)(86362001)(6916009)(52116002)(66946007)(6486002)(316002)(6666004)(508600001)(36756003)(83380400001)(44832011)(38350700002)(38100700002)(4326008)(5660300002)(186003)(8936002)(2906002)(30864003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PpuMB18lgmR4Ymwp9YDw+Mikj3AkKpwU4LEwOjv1n+8FfL16bolJoyz5TFjjnp364k4YiJf09pJ5I2eOgVLP6+VUwCLwj4NhRiQrmoTf3EsaL7rcNGwJZxHZmX56zh/L8T6PkIVIVpPC/555SnJ0o/Hvn/XQF6SOnboHw3IpjRIjtKYjgWrWgtE1CxpFubdj+XGUe2SyMAcSHuxqIhrmIZq5G8ocfQzhZqaMUGbj79st77R15wmPVnQG+0zVNuKAVZzSe+Av2Am6M3yxEOMJogNXgnYonJW55WnAq9trsC2bHOmWnE7hBJ5VFGosRxSXoqr9ZGGUMBpk/aY07zbfxvh9eUuDrbvIadc2oWhWS4p8/jtoI67kUl/haYOxYmqMTrjhZobBXpgY7FWnneAMJmJuYgKwy6HlQgGpSpTrRt3loKVdfk25ggPWd9zqfhrNv/HSK2E1oXd2SOt681pqeX1NjNI/A8Pn5KFkDrhUmT8vjRIN20BNsi3U/IfDlxNqIUSr6udFDSYBmx7ZyXlFRva1rfdkkkmlVtV9elWdTC7oLIFs8FiATPfnENGOoDsB1cfGZ0HVCcpyC9Rh9NP8qEAU5EedCJm1cg2qVKz70YuGn0Cl8uLQI8hl+pUlbMcWn0xNoHlob4T8pN8m8ypAMA54mm7cNELvbLwOdn92vSITUOCriN8J2TwOpFaM1U1aOfUIZIYqsJxiKWSTzFhz9ZmvosPfrmTIGOOCiOnR/lX5XSl+RWjMDdR3J/AASqazZKrR+1QDEtoqpKTsOBXLVti1XpyS6QoM/vWR7xwmndE2RCAz9ud0AA5hX3IvamEr14v0avz7FoE64sPt81pUmw4huUodWLluOffNqxn6P5QZtqxZV9UmxlQRFG2Ulh1AQFj2lJJFpsPZfZ8AfHYQHEnYczGkx+MusNL/xuEz0TPb6qS4cKJYBemVeNGP99APafYshog9UNLLQeuGR6ujjOAtMvj4/IO5DAeNXkqr4wj/DJYS/ptwZZDUtYn821KlVqpQ7sh2F688FyKGpcskqaZeSqlrr5yLXe39GAM8FmvbtBdGiROu98rGgafu/uZ35WEcRTA6Jge1U5kcYrl07h5OQjBJfAp9CIzIbpg6C3y3v67tzN34d33E43B75utJO2w06BoeUmY/KzhyVdeW3c4S98pp2ewavZpm2VZT4jsEFph9EnuKU4CCfKRRtSkSybIgtyLYCPLfd0hUJZfVF1JCxleqxJ1QhWDJY9yXYQ30stKyeRU7VoA8F8P6QTYbF8gqvEhK6SKLC5kCujLo1wVFesZzoA4I+v8VU3e3rRCt14xhA65Bm+hdKUJxPWKYmE2WyeoYsrOEjdAOUNwdxqH73EYBw61fRQMn4YhdWcGRJS+tdhCftKeRZSqnRt+D3PZZWG5H+VOznQ9ZB06Gv6dyVQihUqqBB6LF/vr6H0ofiF88j6yUmGgtnGI/gcRnkYhwHe8jjp8EBIsm18/LrbQVH3gdNkOadMzTFLYjESlL+mePJfjUUvvX47xul144w+vVIs56XHrV9g3qBL3k2i+9OVsyZjJycjYc3/vneefjorMYmh4tjVIz56zkPTCQBBKFZV/6jQyFAYHp1lwlg5tZchHEyky0HcP6bfYL/jw= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e4fb677b-ae14-4a56-8339-08d9f0947f2a X-MS-Exchange-CrossTenant-AuthSource: SA1PR10MB5841.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Feb 2022 15:04:43.0672 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ylqAA3NZNduL+AdbE/7z97YE0Yp3PjPBfFKG8XygG6dITuw5XaXwnOqXTxC/Y5d1YA8Wf/+oH4Z3e4IIU31caw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3865 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10258 signatures=673431 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 bulkscore=0 malwarescore=0 adultscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202150088 X-Proofpoint-ORIG-GUID: 3-OQdQu_VedDndjA5WQfi3Ffliawwo6d X-Proofpoint-GUID: 3-OQdQu_VedDndjA5WQfi3Ffliawwo6d Received-SPF: pass client-ip=205.220.165.32; envelope-from=mark.kanda@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: , Cc: pbonzini@redhat.com, berrange@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Add support for querying fd-based KVM stats - as introduced by Linux kernel commit: cb082bfab59a ("KVM: stats: Add fd-based API to read binary stats data") Signed-off-by: Mark Kanda --- accel/kvm/kvm-all.c | 393 ++++++++++++++++++++++++++++++++++++++++++++ qapi/stats.json | 2 +- 2 files changed, 394 insertions(+), 1 deletion(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 0e66ebb497..7efd9b86c8 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -47,6 +47,7 @@ #include "kvm-cpus.h" #include "hw/boards.h" +#include "monitor/stats.h" /* This check must be after config-host.h is included */ #ifdef CONFIG_EVENTFD @@ -2309,6 +2310,9 @@ bool kvm_dirty_ring_enabled(void) return kvm_state->kvm_dirty_ring_size ? true : false; } +static void query_stats_cb(StatsResults *, StatsFilter *, Error **); +static void query_stats_schemas_cb(StatsSchemaResults *, Error **); + static int kvm_init(MachineState *ms) { MachineClass *mc = MACHINE_GET_CLASS(ms); @@ -2637,6 +2641,11 @@ static int kvm_init(MachineState *ms) } } + if (kvm_check_extension(kvm_state, KVM_CAP_BINARY_STATS_FD)) { + add_stats_callbacks(STATS_PROVIDER_KVM, &query_stats_cb, + &query_stats_schemas_cb); + } + return 0; err: @@ -3696,3 +3705,387 @@ static void kvm_type_init(void) } type_init(kvm_type_init); + +typedef struct StatsArgs { + StatsFilter *filter; + bool is_schema; + union StatsResultsType { + StatsResults *stats; + StatsSchemaResults *schema; + } result; + Error **errp; +} StatsArgs; + +static StatsList *add_kvmstat_entry(struct kvm_stats_desc *pdesc, + uint64_t *stats_data, + StatsList *stats_list, + Error **errp) +{ + + StatsList *stats_entry; + Stats *stats; + uint64List *val_list = NULL; + + switch (pdesc->flags & KVM_STATS_TYPE_MASK) { + case KVM_STATS_TYPE_CUMULATIVE: + case KVM_STATS_TYPE_INSTANT: + case KVM_STATS_TYPE_PEAK: + case KVM_STATS_TYPE_LINEAR_HIST: + case KVM_STATS_TYPE_LOG_HIST: + break; + default: + return stats_list; + } + + switch (pdesc->flags & KVM_STATS_UNIT_MASK) { + case KVM_STATS_UNIT_NONE: + case KVM_STATS_UNIT_BYTES: + case KVM_STATS_UNIT_CYCLES: + case KVM_STATS_UNIT_SECONDS: + break; + default: + return stats_list; + } + + switch (pdesc->flags & KVM_STATS_BASE_MASK) { + case KVM_STATS_BASE_POW10: + case KVM_STATS_BASE_POW2: + break; + default: + return stats_list; + } + + /* Alloc and populate data list */ + stats_entry = g_malloc0(sizeof(*stats_entry)); + stats = g_malloc0(sizeof(*stats)); + stats->name = g_strdup(pdesc->name); + stats->value = g_malloc0(sizeof(*stats->value)); + + if (pdesc->size == 1) { + stats->value->u.scalar = *stats_data; + stats->value->type = QTYPE_QNUM; + } else { + int i; + for (i = 0; i < pdesc->size; i++) { + uint64List *val_entry = g_malloc0(sizeof(*val_entry)); + val_entry->value = stats_data[i]; + val_entry->next = val_list; + val_list = val_entry; + } + stats->value->u.list.values = val_list; + stats->value->type = QTYPE_QDICT; + } + + stats_entry->value = stats; + stats_entry->next = stats_list; + + return stats_entry; +} + +static StatsSchemaValueList *add_kvmschema_entry(struct kvm_stats_desc *pdesc, + StatsSchemaValueList *list, + Error **errp) +{ + StatsSchemaValueList *schema_entry = g_malloc0(sizeof(*schema_entry)); + schema_entry->value = g_malloc0(sizeof(*schema_entry->value)); + + switch (pdesc->flags & KVM_STATS_TYPE_MASK) { + case KVM_STATS_TYPE_CUMULATIVE: + schema_entry->value->type = STATS_TYPE_CUMULATIVE; + break; + case KVM_STATS_TYPE_INSTANT: + schema_entry->value->type = STATS_TYPE_INSTANT; + break; + case KVM_STATS_TYPE_PEAK: + schema_entry->value->type = STATS_TYPE_PEAK; + break; + case KVM_STATS_TYPE_LINEAR_HIST: + schema_entry->value->type = STATS_TYPE_LINEAR_HIST; + schema_entry->value->bucket_size = pdesc->bucket_size; + schema_entry->value->has_bucket_size = true; + break; + case KVM_STATS_TYPE_LOG_HIST: + schema_entry->value->type = STATS_TYPE_LOG_HIST; + break; + default: + goto exit; + } + + switch (pdesc->flags & KVM_STATS_UNIT_MASK) { + case KVM_STATS_UNIT_NONE: + schema_entry->value->unit = STATS_UNIT_NONE; + break; + case KVM_STATS_UNIT_BYTES: + schema_entry->value->unit = STATS_UNIT_BYTES; + break; + case KVM_STATS_UNIT_CYCLES: + schema_entry->value->unit = STATS_UNIT_CYCLES; + break; + case KVM_STATS_UNIT_SECONDS: + schema_entry->value->unit = STATS_UNIT_SECONDS; + break; + default: + goto exit; + } + + switch (pdesc->flags & KVM_STATS_BASE_MASK) { + case KVM_STATS_BASE_POW10: + schema_entry->value->base = STATS_BASE_POW10; + break; + case KVM_STATS_BASE_POW2: + schema_entry->value->base = STATS_BASE_POW2; + break; + default: + goto exit; + } + + schema_entry->value->name = g_strdup(pdesc->name); + schema_entry->value->exponent = pdesc->exponent; + schema_entry->next = list; + return schema_entry; +exit: + g_free(schema_entry->value); + g_free(schema_entry); + return list; +} + +/* Cached stats descriptors */ +typedef struct StatsDescriptors { + char *ident; /* 'vm' or vCPU qom path */ + struct kvm_stats_desc *kvm_stats_desc; + struct kvm_stats_header *kvm_stats_header; + QTAILQ_ENTRY(StatsDescriptors) next; +} StatsDescriptors; + +static QTAILQ_HEAD(, StatsDescriptors) stats_descriptors = + QTAILQ_HEAD_INITIALIZER(stats_descriptors); + +static StatsDescriptors *find_stats_descriptors(StatsFilter *filter) +{ + StatsDescriptors *entry; + QTAILQ_FOREACH(entry, &stats_descriptors, next) { + switch (filter->target) { + case STATS_TARGET_VM: + if (g_str_equal(entry->ident, StatsTarget_str(STATS_TARGET_VM))) { + return entry; + } + break; + case STATS_TARGET_VCPU: + if (g_str_equal(entry->ident, + current_cpu->parent_obj.canonical_path)) { + return entry; + } + break; + default: + break; + } + } + return NULL; +} + +static void query_stats(union StatsResultsType result, StatsFilter *filter, + int stats_fd, bool is_schema, Error **errp) +{ + struct kvm_stats_desc *kvm_stats_desc; + struct kvm_stats_header *kvm_stats_header; + StatsDescriptors *descriptors; + g_autofree uint64_t *stats_data = NULL; + struct kvm_stats_desc *pdesc; + void *stats_list = NULL; + size_t size_desc, size_data = 0; + ssize_t ret; + int i; + + descriptors = find_stats_descriptors(filter); + if (!descriptors) { + descriptors = g_malloc0(sizeof(*descriptors)); + + /* Read stats header */ + kvm_stats_header = g_malloc(sizeof(*kvm_stats_header)); + ret = read(stats_fd, kvm_stats_header, sizeof(*kvm_stats_header)); + if (ret != sizeof(*kvm_stats_header)) { + error_setg(errp, "KVM stats: failed to read stats header: " + "expected %zu actual %zu", + sizeof(*kvm_stats_header), ret); + return; + } + size_desc = sizeof(*kvm_stats_desc) + kvm_stats_header->name_size; + + /* Read stats descriptors */ + kvm_stats_desc = g_malloc0(kvm_stats_header->num_desc * size_desc); + ret = pread(stats_fd, kvm_stats_desc, + size_desc * kvm_stats_header->num_desc, + kvm_stats_header->desc_offset); + + if (ret != size_desc * kvm_stats_header->num_desc) { + error_setg(errp, "KVM stats: failed to read stats descriptors: " + "expected %zu actual %zu", + size_desc * kvm_stats_header->num_desc, ret); + return; + } + descriptors->kvm_stats_header = kvm_stats_header; + descriptors->kvm_stats_desc = kvm_stats_desc; + descriptors->ident = strdup(filter->target == STATS_TARGET_VM ? + StatsTarget_str(STATS_TARGET_VM) : + current_cpu->parent_obj.canonical_path); + QTAILQ_INSERT_TAIL(&stats_descriptors, descriptors, next); + } else { + kvm_stats_header = descriptors->kvm_stats_header; + kvm_stats_desc = descriptors->kvm_stats_desc; + size_desc = sizeof(*kvm_stats_desc) + kvm_stats_header->name_size; + } + + /* Tally the total data size; read schema data */ + for (i = 0; i < kvm_stats_header->num_desc; ++i) { + pdesc = (void *)kvm_stats_desc + i * size_desc; + size_data += pdesc->size * sizeof(*stats_data); + if (is_schema) { + stats_list = add_kvmschema_entry(pdesc, (StatsSchemaValueList *) + stats_list, errp); + } + } + + /* If schema request, add the entries and return */ + if (is_schema) { + switch (filter->target) { + case STATS_TARGET_VM: + add_vm_stats_schema((StatsSchemaValueList *)stats_list, + result.schema, STATS_PROVIDER_KVM); + break; + case STATS_TARGET_VCPU: + add_vcpu_stats_schema((StatsSchemaValueList *)stats_list, + result.schema, STATS_PROVIDER_KVM); + break; + default: + break; + } + return; + } + + stats_data = g_malloc0(size_data); + ret = pread(stats_fd, stats_data, size_data, kvm_stats_header->data_offset); + + if (ret != size_data) { + error_setg(errp, "KVM stats: failed to read data: " + "expected %zu actual %zu", size_data, ret); + return; + } + + for (i = 0; i < kvm_stats_header->num_desc; ++i) { + uint64_t *stats; + pdesc = (void *)kvm_stats_desc + i * size_desc; + + /* Add entry to the list */ + stats = (void *)stats_data + pdesc->offset; + if (!stats_requested_name(pdesc->name, STATS_PROVIDER_KVM, filter)) { + continue; + } + stats_list = + add_kvmstat_entry(pdesc, stats, (StatsList *) stats_list, errp); + } + + if (!stats_list) { + return; + } + + switch (filter->target) { + case STATS_TARGET_VM: + add_vm_stats_entry((StatsList *)stats_list, result.stats, + STATS_PROVIDER_KVM); + break; + case STATS_TARGET_VCPU: + add_vcpu_stats_entry((StatsList *)stats_list, result.stats, + STATS_PROVIDER_KVM, + current_cpu->parent_obj.canonical_path); + break; + default: + break; + } +} + +static void query_stats_vcpu(CPUState *cpu, run_on_cpu_data data) +{ + StatsArgs *kvm_stats_args = (StatsArgs *) data.host_ptr; + int stats_fd = kvm_vcpu_ioctl(cpu, KVM_GET_STATS_FD, NULL); + Error *local_err = NULL; + + if (stats_fd == -1) { + error_setg(&local_err, "KVM stats: ioctl failed"); + error_propagate(kvm_stats_args->errp, local_err); + return; + } + query_stats(kvm_stats_args->result, kvm_stats_args->filter, stats_fd, + kvm_stats_args->is_schema, kvm_stats_args->errp); + close(stats_fd); +} + +static void query_stats_cb(StatsResults *stats_result, StatsFilter *filter, + Error **errp) +{ + KVMState *s = kvm_state; + CPUState *cpu; + int stats_fd; + + switch (filter->target) { + case STATS_TARGET_VM: + { + union StatsResultsType result; + result.stats = stats_result; + stats_fd = kvm_vm_ioctl(s, KVM_GET_STATS_FD, NULL); + if (stats_fd == -1) { + error_setg(errp, "KVM stats: ioctl failed"); + return; + } + query_stats(result, filter, stats_fd, false, errp); + break; + } + case STATS_TARGET_VCPU: + { + g_autofree StatsArgs *stats_args = g_malloc0(sizeof(*stats_args)); + stats_args->filter = filter; + stats_args->errp = errp; + stats_args->result.stats = stats_result; + CPU_FOREACH(cpu) { + if (!stats_requested_vcpu(cpu->parent_obj.canonical_path, + STATS_PROVIDER_KVM, filter)) { + continue; + } + run_on_cpu(cpu, query_stats_vcpu, RUN_ON_CPU_HOST_PTR(stats_args)); + } + break; + } + default: + break; + } +} + +void query_stats_schemas_cb(StatsSchemaResults *stats_result, Error **errp) +{ + g_autofree StatsArgs *stats_args = g_malloc0(sizeof(*stats_args)); + g_autofree StatsFilter *filter = g_malloc0(sizeof(*filter)); + union StatsResultsType result; + KVMState *s = kvm_state; + int stats_fd; + + stats_args->filter = filter; + stats_args->errp = errp; + + result.schema = stats_result; + + stats_fd = kvm_vm_ioctl(s, KVM_GET_STATS_FD, NULL); + if (stats_fd == -1) { + error_setg(errp, "KVM stats: ioctl failed"); + return; + } + + stats_args->result.schema = stats_result; + stats_args->is_schema = true; + + /* Query VM */ + stats_args->filter->target = STATS_TARGET_VM; + query_stats(result, filter, stats_fd, true, errp); + + /* Query vCPU */ + stats_args->filter->target = STATS_TARGET_VCPU; + run_on_cpu(first_cpu, query_stats_vcpu, RUN_ON_CPU_HOST_PTR(stats_args)); +} diff --git a/qapi/stats.json b/qapi/stats.json index ae5dc3ee2c..13708e19c1 100644 --- a/qapi/stats.json +++ b/qapi/stats.json @@ -61,7 +61,7 @@ # Since: 7.0 ## { 'enum': 'StatsProvider', - 'data': [ ] } + 'data': [ 'kvm' ] } ## # @StatsTarget: