From nobody Thu May 2 21:25:55 2024 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=pass(p=reject dis=none) header.from=svin.in ARC-Seal: i=1; a=rsa-sha256; t=1643023131; cv=none; d=zohomail.com; s=zohoarc; b=fG6aFqYvcUVhichOrl6qvoa4cJ9HcvzNywSlELdbLPDytvLXszmVktxCH5S7eY8yY5Ye4GhOsWLtL6AGm8OUYrahNR55xBA9mDnYuvob/7gIrYEEuOStKjH+OYnGodT5wmL5h0VsAZHWwgDpuodRDaEYdn/hrcFslbXutKyuHY4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1643023131; h=Content-Type: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=Veh8eaHfGDxpGBSXxmvhs25UyWCr8QCmzh5tQePbVhc=; b=eQPybSOW2ieaL5p0iaB/LAr8S05P/0eZW+0yw7+iz+8ukp1cEtstcOsJH5aPQEcQ7/iNLHlaoQNt5X2bKjZ4Rl9VXT0xYSCUSRaW72qCdCx1hjg11zRHbh97Dz5zyztEGJ+MGfaRG+8ijHJAXAywNAvwptVulxQ8CuSgkT87myU= 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=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1643023131272197.93888048263807; Mon, 24 Jan 2022 03:18:51 -0800 (PST) Received: from localhost ([::1]:41426 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nBxN3-0002Es-Vc for importer@patchew.org; Mon, 24 Jan 2022 06:18:49 -0500 Received: from eggs.gnu.org ([209.51.188.92]:58312) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nBxLt-00019X-O8 for qemu-devel@nongnu.org; Mon, 24 Jan 2022 06:17:37 -0500 Received: from wrqvvpzp.outbound-mail.sendgrid.net ([149.72.131.227]:36606) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nBxLn-00089O-Ht for qemu-devel@nongnu.org; Mon, 24 Jan 2022 06:17:37 -0500 Received: by filterdrecv-55446c4d49-vth2g with SMTP id filterdrecv-55446c4d49-vth2g-1-61EE8AAD-1C 2022-01-24 11:17:01.462767719 +0000 UTC m=+12488213.302091489 Received: from ne2.svin.in (unknown) by geopod-ismtpd-canary-0 (SG) with ESMTP id -wn7Wt9oRZiQC6ybIHTX4w Mon, 24 Jan 2022 11:17:01.166 +0000 (UTC) Received: from authenticated-user (ne2.svin.in [152.70.50.15]) by ne2.svin.in (Postfix) with ESMTPA id CA1C517B827; Mon, 24 Jan 2022 11:16:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=svin.in; h=from:subject:mime-version:content-transfer-encoding:to:cc: content-type; s=s1; bh=Veh8eaHfGDxpGBSXxmvhs25UyWCr8QCmzh5tQePbVhc=; b=lM/DwVzKLu9prwL/sNJyFskLNB6VOQLsWZ5nqB7l5fKa1yCiElmxEYS/5zv9ljZ+CqtH ggdl0If0DIHahdrykStJBgVkX5LM/kkQG6AgmURx2iAf0BY2okMmXCBCPQe4T2/MdUHmMA CCVH0PYcbV+mGsWzeiZKkTmo7szJG5VEFKU6w9npmShvxTZ2i6E/HG4qt4df7YUDRbRKc9 UUZE/lAOVNG7HW9G2MQZ2+K0ZzjV5QVVeaJ4bGybj2ocvQ2KRFX90foZkhBXXo0QLDxRS8 1VMZMz+wP/Z4n7VxEo8OCIGKUBXMREaR3EVGyHG6PIGSxN6rsbI1WQgOqAVXtWNQ== From: Oleg Vasilev Subject: [PATCH] contrib/plugins: add mask plugin Date: Mon, 24 Jan 2022 11:17:01 +0000 (UTC) Message-Id: <20220124111602.671654-1-me@svin.in> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Authentication-Results: ne2.svin.in; auth=pass smtp.auth=admin@svin.in smtp.mailfrom=me@svin.in X-Spamd-Bar: ++++++ X-Spam: Yes X-SG-EID: =?us-ascii?Q?vuv4bBSMv+s+sLXfbA0TX5eOZYkSPfNDFag09+IwHa3cTVRX1snJTadgqJKv5P?= =?us-ascii?Q?2zYnM9NUImmOg1ncEiM7psi45XpqdYh9G2X3KLg?= =?us-ascii?Q?WSzkAjxueNJRjm9Y9rzr9cmNF64AuEKnoCRLTI9?= =?us-ascii?Q?1t1CWngWicFJ177IBvYsR09wgLMzZUDHirS=2FtrR?= =?us-ascii?Q?JMFDA5MTuevVIie+ptB3=2FwB1zs4jcBjhbEmWx7C?= =?us-ascii?Q?O2hhqqS8cLT1rSFIcmhC=2FlIs+X3GPm4EkrjZz1?= To: qemu-devel@nongnu.org Cc: Oleg Vasilev , Oleg Vasilev , Alex =?iso-8859-1?q?Benn=E9e?= , Alexandre Iooss , Mahmoud Mandour X-Entity-ID: R60PFX0ZT2z0LR3J/oF4GQ== 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=149.72.131.227; envelope-from=bounces+23648303-31c4-qemu-devel=nongnu.org@em7421.svin.in; helo=wrqvvpzp.outbound-mail.sendgrid.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 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 @svin.in) X-ZM-MESSAGEID: 1643023135097100001 Content-Type: text/plain; charset="utf-8" From: Oleg Vasilev Plugin can be used to track statistics based on virtual address mask matchi= ng. Useful for tracking kernel vs user translation blocks. Signed-off-by: Oleg Vasilev Signed-off-by: Oleg Vasilev --- contrib/plugins/Makefile | 1 + contrib/plugins/mask.c | 144 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 contrib/plugins/mask.c diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index 54ac5ccd9f..7e9cb51c9d 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -20,6 +20,7 @@ NAMES +=3D howvec NAMES +=3D lockstep NAMES +=3D hwprofile NAMES +=3D cache +NAMES +=3D mask =20 SONAMES :=3D $(addsuffix .so,$(addprefix lib,$(NAMES))) =20 diff --git a/contrib/plugins/mask.c b/contrib/plugins/mask.c new file mode 100644 index 0000000000..c6d2bd2386 --- /dev/null +++ b/contrib/plugins/mask.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2022, Oleg Vasilev + * + * Track statistics based on virtual address mask matching. + * Useful for tracking kernel vs user translation blocks. + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +typedef struct { + GMutex lock; + const char *hint; + uint64_t mask; + uint64_t bits; + uint64_t tb_exec; + uint64_t tb_trans; + uint64_t isins; +} MaskCounter; + +static GPtrArray *counters; + +static uint64_t report_every =3D 1 << 28; +static uint64_t tb_exec_every =3D 1 << 10; +static uint64_t total_tb_exec; + +static void gen_one_report(MaskCounter *counter, GString *report) +{ + g_mutex_lock(&counter->lock); + uint64_t tb_exec =3D counter->tb_exec * tb_exec_every; + + double hit_rate =3D (double)counter->tb_trans / tb_exec; + hit_rate =3D 1 - hit_rate; + + double mask_freq =3D (double) counter->tb_exec * tb_exec_every / repor= t_every; + + g_string_append_printf(report, + "hint: %s, mask: 0x%016lx, bits: 0x%016lx, hit_= rate: %f, " + "mask_freq: %f, tb_exec: %ld, tb_trans: %ld\n", + counter->hint, counter->mask, counter->bits, hi= t_rate, + mask_freq, tb_exec, counter->tb_trans); + + counter->tb_exec =3D 0; + counter->tb_trans =3D 0; + counter->isins =3D 0; + + g_mutex_unlock(&counter->lock); +} + +static void report_all(void) +{ + g_autoptr(GString) report =3D g_string_new(""); + g_ptr_array_foreach(counters, (GFunc)gen_one_report, report); + qemu_plugin_outs(report->str); +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + report_all(); +} + +static bool match(MaskCounter *counter, struct qemu_plugin_tb *tb) +{ + return (counter->mask & qemu_plugin_tb_vaddr(tb)) =3D=3D counter->bits; +} + +static void tb_exec(MaskCounter *counter, struct qemu_plugin_tb *tb) +{ + if (!match(counter, tb)) { + return; + } + g_mutex_lock(&counter->lock); + counter->tb_exec++; + g_mutex_unlock(&counter->lock); +} + +static void vcpu_tb_exec(unsigned int cpu_index, void *tb) +{ + uint64_t cur_tb_exec =3D qatomic_fetch_inc(&total_tb_exec); + if ((cur_tb_exec & (tb_exec_every - 1)) =3D=3D 0) { + g_ptr_array_foreach(counters, (GFunc)tb_exec, tb); + } + + if ((cur_tb_exec & (report_every - 1)) =3D=3D 0) { + report_all(); + } +} + +static void tb_trans(MaskCounter *counter, struct qemu_plugin_tb *tb) +{ + if (!match(counter, tb)) { + return; + } + g_mutex_lock(&counter->lock); + counter->tb_trans++; + g_mutex_unlock(&counter->lock); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec, + QEMU_PLUGIN_CB_NO_REGS, tb); + g_ptr_array_foreach(counters, (GFunc)tb_trans, tb); +} + +static void add_counter(const char *hint, uint64_t mask, uint64_t bits) +{ + MaskCounter *counter =3D g_new0(MaskCounter, 1); + counter->hint =3D hint; + counter->mask =3D mask; + counter->bits =3D bits; + g_mutex_init(&counter->lock); + g_ptr_array_add(counters, counter); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, int ar= gc, + char **argv) +{ + counters =3D g_ptr_array_new(); + + // Update for a different mask + add_counter("all", 0, 0); + add_counter("kernel", 0x1ll << 63, 0x1ll << 63); + add_counter("user", 0x1ll << 63, 0); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +} --=20 2.33.1