From patchwork Fri Jul 5 08:40:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 810360 Delivered-To: patch@linaro.org Received: by 2002:adf:a199:0:b0:367:895a:4699 with SMTP id u25csp1508002wru; Fri, 5 Jul 2024 01:43:23 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXMAWFmIp2l0loSqnIB/kEdmj91xHs0FEDgpeqHAp7vXkG7GtTw82P8ZRAeTMFzir1o2IKZu6+gh55aV0HiPHwf X-Google-Smtp-Source: AGHT+IEhl9izd2ujM78pf+fFcFQ5SFHYfqdyUj59qgLisnfKHIYbtaaH/yc7BePeW9zCX/Fb0kbV X-Received: by 2002:a05:600c:420c:b0:425:5ea6:236 with SMTP id 5b1f17b1804b1-4264a3f6196mr25370085e9.28.1720169003298; Fri, 05 Jul 2024 01:43:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720169003; cv=none; d=google.com; s=arc-20160816; b=OVUcgISObF+oJuM28cbsDqNUECsQhf2nwjr2HV4gnWiQAEzxq1BjpYfeCljytlOSnu Qppuw/keaZo7JE0g1A3ZzN+yXz8+VtuZsLfGi9up6DrVNsloxOy/V/s8OMsC29ycgQ9y HUxsSq4+BIquT42tGZd8wXvbXDzqke41bawJ/QNRyuRahgXef5I9dLsN0Gaq91HlNAvy 8wh4eUhG3MmwbimGa+hRssokD6C7r0opSRAnlv6aiMtMVnGKVfJjO9HiS4JVe90kZiIZ aoEY6iMn8jp1+QCiBgAMTrcFTEbaqbFuJ98/+uzf4vYXgjj2LWH6yA7g1E15grUJxSwj BkvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Y1MGJW50FjVhjyrhFQsVUOHRmt7nlCHL11oCXI6mA2A=; fh=fOmDb3LWEc8YYk2jJaPNBibuO+xI4aDvcJfVffKo+VU=; b=F3BE7OOm1e6Mws4dP+lKGgSIEvQC5agGpwqvqE2MxHA3RCKBcZcP+aDtUCBd/7f3lm lAiGlKIsNdYkmOjN3zzWBsXnAdmxwIkqj7IzluS5duXEsk7ecpvbYvhygta66qqbHm/r ZYc4zdUsEYteOLakWOfjCRXHo5CRRh/gL0RNxSndC/RhZ4RgP6DyQhUHeNqX5AUd73On Ts47nDcc6invMizpdRvybP9i8fjk+Asu3WEbnR8TaqC7H0mRoSGp3Gl7nPodHa+ssCTt Fx+SYRqoQTfMce9EZg2VmV3GG8tMBhXwOc/cNxZgKS45gR11uW0qv29agK0BVPNu6KOa Yarw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Oz7D96m4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 5b1f17b1804b1-4264a26fd34si18670015e9.218.2024.07.05.01.43.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Jul 2024 01:43:23 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Oz7D96m4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sPeVn-0001hU-Dh; Fri, 05 Jul 2024 04:41:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sPeVD-00008N-Rp for qemu-devel@nongnu.org; Fri, 05 Jul 2024 04:41:14 -0400 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sPeV7-0003Gl-SS for qemu-devel@nongnu.org; Fri, 05 Jul 2024 04:41:10 -0400 Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-a77b51631adso156476566b.3 for ; Fri, 05 Jul 2024 01:41:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1720168864; x=1720773664; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y1MGJW50FjVhjyrhFQsVUOHRmt7nlCHL11oCXI6mA2A=; b=Oz7D96m4ffwQ+TnEe2QTva841BsyXZNmUebwLGrQcN2iyigVc9QDn6tKflOwciaUYF TlezIijOkaM1eStDbeOt19jY8wGZmij9KJQnCllZf7nqa2P1wOJpU5YnaeFm1ftLyzuj mVmsxVo9TDL5xethBmpkjBhRuVL13kNJWQIgoJpGAjN0T0C805eX07diAGUatTi6JOPa r1IHOfHL8qURFJr00UrFY42c9SM2+zpxwUkvo1yniJyB4Dzq7mSZdCEj8acsPV2fxtzD CZR1EgG8Jp6MTH1C5LSMXkNXL7GPVCt9f5dJdw0S6qkgKDl6o6AooN0lVt1+/dkeMxjY NMXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720168864; x=1720773664; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y1MGJW50FjVhjyrhFQsVUOHRmt7nlCHL11oCXI6mA2A=; b=l1PRO1v5hS5bC8Gm1zaYWHvZY51fX+BXl07FbGFKy4JG96bkUKrUoISIDPMhkodGav xn2hPBwVDY9AW3FolW6zhrsizSsIqMjF4/ISiFLCeChK/1b63FqkoFx7yC7H7LktvRpo 0EyuebGY6YBu0zrxkVqmvQpEyS04ia6FzMHye0Y8mObp3z8yf1aqCa/k1Sgb5N6l6+sL dlv4DVeOr7a/IGGSDLvJQpv7vmvMX4ZySl3zwwIsTsjym4IjoZ9OVcTCHG2csiVLAp7+ 2kLMODAMKhRzjBhrJT8ALVxwzi/nom4P0youpeumBzBtBhee03IxuaBtd606VuppWHOA 6b+w== X-Gm-Message-State: AOJu0Yw/cOwyMSd5mXTTlyIyuzQK4HWALmY/OpqQpMuyrRGt/nFPbBWN qqc5qsgnDGDlBnBsxHTXTmbjKLHgGWBzJ95MsAZoUFyflAB+4coL750p4RN7zvM= X-Received: by 2002:a17:906:d8c8:b0:a77:daa9:402 with SMTP id a640c23a62f3a-a77daa9052bmr11169466b.35.1720168864058; Fri, 05 Jul 2024 01:41:04 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a77c69041f6sm61531166b.103.2024.07.05.01.40.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jul 2024 01:40:58 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id CFEC75F92A; Fri, 5 Jul 2024 09:40:49 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Wainer dos Santos Moschetta , Beraldo Leal , David Hildenbrand , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Pierrick Bouvier , Alexandre Iooss , Thomas Huth , Mahmoud Mandour , Peter Maydell , qemu-arm@nongnu.org, Aleksandar Rikalo , Mads Ynddal , Yanan Wang , Eduardo Habkost , Peter Xu , Richard Henderson , Marcel Apfelbaum , Paul Burton , Stefan Hajnoczi , =?utf-8?q?Alex_Benn=C3=A9e?= , Laurent Vivier Subject: [PATCH v2 22/40] test/plugins: preserve the instruction record over translations Date: Fri, 5 Jul 2024 09:40:29 +0100 Message-Id: <20240705084047.857176-23-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240705084047.857176-1-alex.bennee@linaro.org> References: <20240705084047.857176-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62c; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x62c.google.com 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_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We are interested in the particular instruction so we should use a stable record for it. We could bring this down to physical address but for now vaddr + disas seems to do the trick. Signed-off-by: Alex Bennée Reviewed-by: Manos Pitsidianakis --- tests/plugin/insn.c | 76 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/tests/plugin/insn.c b/tests/plugin/insn.c index 524f9ddde8..baf2d07205 100644 --- a/tests/plugin/insn.c +++ b/tests/plugin/insn.c @@ -43,6 +43,44 @@ typedef struct { char *disas; } Instruction; +/* A hash table to hold matched instructions */ +static GHashTable *match_insn_records; +static GMutex match_hash_lock; + + +static Instruction * get_insn_record(const char *disas, uint64_t vaddr, Match *m) +{ + g_autofree char *str_hash = g_strdup_printf("%"PRIx64" %s", vaddr, disas); + Instruction *record; + + g_mutex_lock(&match_hash_lock); + + if (!match_insn_records) { + match_insn_records = g_hash_table_new(g_str_hash, g_str_equal); + } + + record = g_hash_table_lookup(match_insn_records, str_hash); + + if (!record) { + g_autoptr(GString) ts = g_string_new(str_hash); + + record = g_new0(Instruction, 1); + record->disas = g_strdup(disas); + record->vaddr = vaddr; + record->match = m; + + g_hash_table_insert(match_insn_records, str_hash, record); + + g_string_prepend(ts, "Created record for: "); + g_string_append(ts, "\n"); + qemu_plugin_outs(ts->str); + } + + g_mutex_unlock(&match_hash_lock); + + return record; +} + /* * Initialise a new vcpu with reading the register list */ @@ -131,16 +169,19 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) * 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. + * + * We only want one record for each occurrence of the matched + * instruction. */ if (matches->len) { char *insn_disas = qemu_plugin_insn_disas(insn); for (int j = 0; j < matches->len; j++) { Match *m = &g_array_index(matches, Match, j); if (g_str_has_prefix(insn_disas, m->match_string)) { - Instruction *rec = g_new0(Instruction, 1); - rec->disas = g_strdup(insn_disas); - rec->vaddr = qemu_plugin_insn_vaddr(insn); - rec->match = m; + Instruction *rec = get_insn_record(insn_disas, + qemu_plugin_insn_vaddr(insn), + m); + qemu_plugin_register_vcpu_insn_exec_cb( insn, vcpu_insn_matched_exec_before, QEMU_PLUGIN_CB_NO_REGS, rec); @@ -173,13 +214,38 @@ static void plugin_exit(qemu_plugin_id_t id, void *p) qemu_plugin_u64_sum(insn_count)); } qemu_plugin_outs(out->str); - qemu_plugin_scoreboard_free(insn_count.score); + + g_mutex_lock(&match_hash_lock); + for (i = 0; i < matches->len; ++i) { Match *m = &g_array_index(matches, Match, i); + GHashTableIter iter; + Instruction *record; + qemu_plugin_u64 hit_e = qemu_plugin_scoreboard_u64_in_struct(m->counts, MatchCount, hits); + uint64_t hits = qemu_plugin_u64_sum(hit_e); + + g_string_printf(out, "Match: %s, hits %"PRId64"\n", m->match_string, hits); + qemu_plugin_outs(out->str); + + g_hash_table_iter_init(&iter, match_insn_records); + while (g_hash_table_iter_next(&iter, NULL, (void **)&record)) { + if (record->match == m) { + g_string_printf(out, + " %"PRIx64": %s (hits %"PRId64")\n", + record->vaddr, + record->disas, + record->hits); + qemu_plugin_outs(out->str); + } + } + g_free(m->match_string); qemu_plugin_scoreboard_free(m->counts); } + + g_mutex_unlock(&match_hash_lock); + g_array_free(matches, TRUE); g_array_free(sizes, TRUE); }