From nobody Tue Feb 10 06:07:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1644417512482697.7679221406311; Wed, 9 Feb 2022 06:38:32 -0800 (PST) Received: from localhost ([::1]:51102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nHo74-0006JY-0O for importer@patchew.org; Wed, 09 Feb 2022 09:38:32 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44480) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHnlO-0002b2-Iy for qemu-devel@nongnu.org; Wed, 09 Feb 2022 09:16:06 -0500 Received: from [2a00:1450:4864:20::62e] (port=37625 helo=mail-ej1-x62e.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nHnlJ-0007sS-4k for qemu-devel@nongnu.org; Wed, 09 Feb 2022 09:16:06 -0500 Received: by mail-ej1-x62e.google.com with SMTP id fj5so4876275ejc.4 for ; Wed, 09 Feb 2022 06:15:52 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id da16sm3379595edb.72.2022.02.09.06.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 06:15:48 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 779CD1FFCF; Wed, 9 Feb 2022 14:15:31 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D2SU2BVtazPSSzG80PekxDXRIejR2lmUffnXojxk3AU=; b=MFCSa+KpMWCyNzEY8714Lf2PNjkOC0kvIp/tWKY9OaHRH+eFQfv5MLSUtpdWzzXWkh 1nzZbwMpv+XElDicIlFfiCK1MFMgqugRM/VlVe7pA1/Ad1H8IsERcfnS1Yg61aJTWkqy 57d7mSNgwYKL0Yoa9gmgpdt1mhAMeWpjd37Q/vTE/rQj5qAsXa/tbIgH6ahaHekD5O/Y p9pANd4yyJ6f6C/rh76tR2dVS3F/Sr0SxZkgEFjNznrWreY2LHfOlWAUEzYlfKTQlyYK RNjBuhqX2g8XE7TW921U/gE0122BEa+Ae0ZXuTRS+YbReOgAnbyG08+NfEO3loF3ouT7 Ir2Q== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=D2SU2BVtazPSSzG80PekxDXRIejR2lmUffnXojxk3AU=; b=lXpuRzIBaP8otvvE4qi4hoSOTrOID5kHNqu8iik4xX1DzNyexzXWNPj8At4ZfHgDJc PK/YWlr3z1KbJ2ZB0EMroraXNoQmZAc2AYt8wd8SndxnpPKudRg6Iilk7pN6ZbEIWnxP +qOcRxEgam2ZyduY0MeLQNtESPcLXV1MrQs3iMaFyDXyYyPuYePfu1yBGuX/XbYdk80r 6R3vN5RqK1Rg/iOhxqLGgq26Mb7CrhWcjtREHzfJA9CgYoFLsFFA2wWZBBAH5E99yS+k 1SzjGIit3WAitS/Y+uoKtH7xVjpIxiUZJL5NnPQbIPqsFlwyIIUAfXUPdTcghKUoMyuq OZ6A== X-Gm-Message-State: AOAM532s1Ix7bS+yHf3BLFcykmvpUsgokFHLjX7oeZcI4YgCZuDdgsij 6BwdUWXINg3+lIhul2buaB1qdw== X-Google-Smtp-Source: ABdhPJxvdXHaFCcdv+7JXzElBAsTL3sQSWZj8jkbXRumUpusoxW24N+M72UkCAyKHhpaY51yyM7Saw== X-Received: by 2002:a17:906:488b:: with SMTP id v11mr2144897ejq.311.1644416151555; Wed, 09 Feb 2022 06:15:51 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 24/28] tests/plugins: add instruction matching to libinsn.so Date: Wed, 9 Feb 2022 14:15:25 +0000 Message-Id: <20220209141529.3418384-25-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220209141529.3418384-1-alex.bennee@linaro.org> References: <20220209141529.3418384-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::62e (failed) 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=2a00:1450:4864:20::62e; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x62e.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 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, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: Vasilev Oleg , Richard Henderson , qemu-devel@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Emilio Cota , Alexandre Iooss , Mahmoud Mandour , =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1644417514655100001 This adds simple instruction matching to the libinsn.so plugin which is useful for examining the execution distance between instructions. For example to track how often we flush in ARM due to TLB updates: -plugin ./tests/plugin/libinsn.so,match=3Dtlbi which leads to output like this: 0xffffffc01019a918, 'tlbi vale1is, x1', 5702 hits, 31825 match hits, =CE= =94+8112 since last match, 68859 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5703 hits, 56593 match hits, =CE= =94+17712125 since last match, 33455 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5704 hits, 56594 match hits, =CE= =94+12689 since last match, 33454 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5705 hits, 56595 match hits, =CE= =94+12585 since last match, 33454 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5706 hits, 56596 match hits, =CE= =94+10491 since last match, 33454 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5707 hits, 56597 match hits, =CE= =94+4721 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5708 hits, 56598 match hits, =CE= =94+10733 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5709 hits, 56599 match hits, =CE= =94+61959 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5710 hits, 56600 match hits, =CE= =94+55235 since last match, 33454 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5711 hits, 56601 match hits, =CE= =94+54373 since last match, 33454 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5712 hits, 56602 match hits, =CE= =94+2705 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5713 hits, 56603 match hits, =CE= =94+17262 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5714 hits, 56604 match hits, =CE= =94+17206 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5715 hits, 56605 match hits, =CE= =94+28940 since last match, 33453 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5716 hits, 56606 match hits, =CE= =94+7370 since last match, 33452 avg insns/match 0xffffffc01019a918, 'tlbi vale1is, x1', 5717 hits, 56607 match hits, =CE= =94+7066 since last match, 33452 avg insns/match showing we do some sort of TLBI invalidation every 33 thousand instructions. Cc: Vasilev Oleg Cc: Richard Henderson Cc: Emilio Cota Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20220204204335.1689602-25-alex.bennee@linaro.org> diff --git a/tests/plugin/insn.c b/tests/plugin/insn.c index d5a0a08cb4..cd5ea5d4ae 100644 --- a/tests/plugin/insn.c +++ b/tests/plugin/insn.c @@ -28,9 +28,25 @@ static uint64_t inline_insn_count; =20 static bool do_inline; static bool do_size; -static bool do_frequency; static GArray *sizes; =20 +typedef struct { + char *match_string; + uint64_t hits[MAX_CPUS]; + uint64_t last_hit[MAX_CPUS]; + uint64_t total_delta[MAX_CPUS]; + GPtrArray *history[MAX_CPUS]; +} Match; + +static GArray *matches; + +typedef struct { + Match *match; + uint64_t vaddr; + uint64_t hits; + char *disas; +} Instruction; + static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata) { unsigned int i =3D cpu_index % MAX_CPUS; @@ -45,6 +61,37 @@ static void vcpu_insn_exec_before(unsigned int cpu_index= , void *udata) c->insn_count++; } =20 +static void vcpu_insn_matched_exec_before(unsigned int cpu_index, void *ud= ata) +{ + unsigned int i =3D cpu_index % MAX_CPUS; + Instruction *insn =3D (Instruction *) udata; + Match *match =3D insn->match; + g_autoptr(GString) ts =3D g_string_new(""); + + insn->hits++; + g_string_append_printf(ts, "0x%" PRIx64 ", '%s', %"PRId64 " hits", + insn->vaddr, insn->disas, insn->hits); + + uint64_t icount =3D counts[i].insn_count; + uint64_t delta =3D icount - match->last_hit[i]; + + match->hits[i]++; + match->total_delta[i] +=3D delta; + + g_string_append_printf(ts, + ", %"PRId64" match hits, " + "=CE=94+%"PRId64 " since last match," + " %"PRId64 " avg insns/match\n", + match->hits[i], delta, + match->total_delta[i] / match->hits[i]); + + match->last_hit[i] =3D icount; + + qemu_plugin_outs(ts->str); + + g_ptr_array_add(match->history[i], insn); +} + static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) { size_t n =3D qemu_plugin_tb_n_insns(tb); @@ -71,6 +118,29 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct q= emu_plugin_tb *tb) unsigned long *cnt =3D &g_array_index(sizes, unsigned long, sz= ); (*cnt)++; } + + /* + * If we are tracking certain instructions we will need more + * information about the instruction which we also need to + * save if there is a hit. + */ + if (matches) { + char *insn_disas =3D qemu_plugin_insn_disas(insn); + int j; + for (j =3D 0; j < matches->len; j++) { + Match *m =3D &g_array_index(matches, Match, j); + if (g_str_has_prefix(insn_disas, m->match_string)) { + Instruction *rec =3D g_new0(Instruction, 1); + rec->disas =3D g_strdup(insn_disas); + rec->vaddr =3D qemu_plugin_insn_vaddr(insn); + rec->match =3D m; + qemu_plugin_register_vcpu_insn_exec_cb( + insn, vcpu_insn_matched_exec_before, + QEMU_PLUGIN_CB_NO_REGS, rec); + } + } + g_free(insn_disas); + } } } =20 @@ -105,6 +175,21 @@ static void plugin_exit(qemu_plugin_id_t id, void *p) qemu_plugin_outs(out->str); } =20 + +/* Add a match to the array of matches */ +static void parse_match(char *match) +{ + Match new_match =3D { .match_string =3D match }; + int i; + for (i =3D 0; i < MAX_CPUS; i++) { + new_match.history[i] =3D g_ptr_array_new(); + } + if (!matches) { + matches =3D g_array_new(false, true, sizeof(Match)); + } + g_array_append_val(matches, new_match); +} + QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, int argc, char **argv) @@ -122,6 +207,8 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_= id_t id, fprintf(stderr, "boolean argument parsing failed: %s\n", o= pt); return -1; } + } else if (g_strcmp0(tokens[0], "match") =3D=3D 0) { + parse_match(tokens[1]); } else { fprintf(stderr, "option parsing failed: %s\n", opt); return -1; --=20 2.30.2