From patchwork Wed Feb 28 00:52:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 207573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3072763dyb; Tue, 27 Feb 2024 17:28:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVAXx2QCYc+xf786l2NCuSYyLk7WC8IonO/Y25Wx6jO45jmkcTnn4h8th+2hLgmmZkp0g7Bx6oj1BaoybBwy4ANU9QI7A== X-Google-Smtp-Source: AGHT+IG1GCpc6WQu1mf9bOJ0zUW3mI8iR3A5Nfejbtdh5ZR+DNFQFQZhgazHr9jY5aAhY6FL6SDJ X-Received: by 2002:a17:902:d4c6:b0:1dc:af71:4d46 with SMTP id o6-20020a170902d4c600b001dcaf714d46mr6804853plg.13.1709083723755; Tue, 27 Feb 2024 17:28:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709083723; cv=pass; d=google.com; s=arc-20160816; b=Frfy0PybULdtWO6TtfLUZIBJh1ULeenl6lxFHx+nT3eCQrRzwBTg2TuB3xbZThk7xn J7EqZVuoReZIT+VIM3W2/DAfAVtiEjhH6aKm0NrK1Ys6rd+c5f8blovFYOt5nCZmhboT VdhJSABCbhH1kOWm1Rqkpd1wnX0zYFJqGrGNzFowrlI5PKCPTR/qfEYhAvJ01rr0Q+uN nKxSchKssu7TedSgWCYRJYvUZwTX2KFAF0wFkjNUnzWzGE+GX+1qZlSe7Sd/mqRFwQs1 E4w1C5P8NkULIThBBetd7UIRdLmk4PftTu1ac27ofVcj3/IdUXapudtSjRa3oGFTJ0li 2Q1w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=s4+A7X7slYUgx3YlOKQ8yDILaISTaJytvSMATYccNa8=; fh=s1fZEdGGHFKlkrF3njJuxobE+Xf6SbIV8h7pc+69Jsg=; b=ddhshBX48te/jtktgWPAfDIeaqgvwWnot44fbxsGsawYJrCGV3ijTlceysdfFiraoU +YwhV6pp7YBnJVX6HoTiJyMM3WTiE1rou9h/aeGTLdk/ZidKXKUshq/7fKhZ4hmmheGg kYSR4lp1RqGhus4lGaOiKdT/9yOC6vMNZ10nT3a6tU3XgCr6cHONuxNytgPaGMei2Ss0 sBvKPCoE9u1qWZPYmjkp6tGEulIooOMo7f11qMWa1Y6HSn1D/3GxGdoYsnvX2CTWY0WU X2uDThjIAbAdVbaZxgk88bH+xUEdyOir1o4J6fGUbZbz3xIHKc/mFy63NsKiNVQZEFUE oMFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ayNfeHRt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84287-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84287-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id l12-20020a170903120c00b001dc577b2537si2307491plh.297.2024.02.27.17.28.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 17:28:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84287-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ayNfeHRt; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84287-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84287-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 1EF67B27CAA for ; Wed, 28 Feb 2024 00:53:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8A5A51AAC9; Wed, 28 Feb 2024 00:52:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ayNfeHRt" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 845A5EA4; Wed, 28 Feb 2024 00:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081552; cv=none; b=NEjvJRTZvPkBnzLqJIcqfzLidn/kHK6ecM06F5d54OdBFdGpeYFkEO2rXHBjyYNf3sr02YKcJrl1M6ESA//47QhC+B+2Z2vSmt/t1XHcAoiCqcPG7ykDgtXpSs1C4fTK0Y9e96Boa/xdpSLZ7zWNsiUuesyOfuv2Q301RoisqAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081552; c=relaxed/simple; bh=+m3kd5Z7Kmf1Mj9lEd0zN/MsAmvTX0/FfPUZA4vXFoM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FR6r1wvy2dZlAlH1+xOklZWRG8hBSOQNonbZazqYJLKovMQJrCUVcJyUIpO8qQyu6BBWixl1nSejqfqB+uJ+kafZ+VIZsP5hmtshZuFYvpaoi05frQPPmO9ueLpY50MixeNJMd9ixb3SbZkfGkjtfuhN7GIUdArbF+gIMJtJmZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ayNfeHRt; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 157A0C43390; Wed, 28 Feb 2024 00:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709081552; bh=+m3kd5Z7Kmf1Mj9lEd0zN/MsAmvTX0/FfPUZA4vXFoM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ayNfeHRtAIhY6qlGvEeVBw/GKfAvT7jyq9OGoGPBWklJZYQ4F9rKawa+uOAjSEj7I n2Uo5Y0BSZQwM3r+mr9tMYWAHK9Cv7Z0RHK11N63x07yYZCzTYRmi5HSpK4nQeDikE kgap5QyS+4MI0Q5hnRwdPrfNjn5xWGc4Z+/Wz1oDE1NVfG6k2me5tQzJXr02BAhSpF yCDCfu1VE4cMl3K5utdoDyoHz4/ACWuBRJDIGGooOfs+ZGh8smLF4OtkLqQM7zrrx5 O3HOAJb21/PO3odzZxkw1wtGEXwigOnllFeQGKRPnU6SvAw3huDiQjHdWEGYakFsT7 sPY0nrRyDqmsw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Andi Kleen Subject: [PATCH 1/4] perf annotate: Add a hashmap for symbol histogram Date: Tue, 27 Feb 2024 16:52:27 -0800 Message-ID: <20240228005230.287113-2-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240228005230.287113-1-namhyung@kernel.org> References: <20240228005230.287113-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792103732415277704 X-GMAIL-MSGID: 1792104174733097578 Now symbol histogram uses an array to save per-offset sample counts. But it wastes a lot of memory if the symbol has a few samples only. Add a hashmap to save values only for actual samples. For now, it has duplicate histogram (one in the existing array and another in the new hash map). Once it can convert to use the hash in all places, we can get rid of the array later. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 40 +++++++++++++++++++++++++++++++++++++- tools/perf/util/annotate.h | 2 ++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 107b264fa41e..7a70e4d35c9b 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -38,6 +38,7 @@ #include "arch/common.h" #include "namespaces.h" #include "thread.h" +#include "hashmap.h" #include #include #include @@ -863,6 +864,17 @@ bool arch__is(struct arch *arch, const char *name) return !strcmp(arch->name, name); } +/* symbol histogram: key = offset << 16 | evsel->core.idx */ +static size_t sym_hist_hash(long key, void *ctx __maybe_unused) +{ + return (key >> 16) + (key & 0xffff); +} + +static bool sym_hist_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 == key2; +} + static struct annotated_source *annotated_source__new(void) { struct annotated_source *src = zalloc(sizeof(*src)); @@ -877,6 +889,8 @@ static __maybe_unused void annotated_source__delete(struct annotated_source *src { if (src == NULL) return; + + hashmap__free(src->samples); zfree(&src->histograms); free(src); } @@ -909,6 +923,14 @@ static int annotated_source__alloc_histograms(struct annotated_source *src, src->sizeof_sym_hist = sizeof_sym_hist; src->nr_histograms = nr_hists; src->histograms = calloc(nr_hists, sizeof_sym_hist) ; + + if (src->histograms == NULL) + return -1; + + src->samples = hashmap__new(sym_hist_hash, sym_hist_equal, NULL); + if (src->samples == NULL) + zfree(&src->histograms); + return src->histograms ? 0 : -1; } @@ -920,6 +942,7 @@ void symbol__annotate_zero_histograms(struct symbol *sym) if (notes->src != NULL) { memset(notes->src->histograms, 0, notes->src->nr_histograms * notes->src->sizeof_sym_hist); + hashmap__clear(notes->src->samples); } if (notes->branch && notes->branch->cycles_hist) { memset(notes->branch->cycles_hist, 0, @@ -983,8 +1006,10 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms, struct perf_sample *sample) { struct symbol *sym = ms->sym; + long hash_key; unsigned offset; struct sym_hist *h; + struct sym_hist_entry *entry; pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr)); @@ -1002,15 +1027,28 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms, __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC); return -ENOMEM; } + + hash_key = offset << 16 | evidx; + if (!hashmap__find(src->samples, hash_key, &entry)) { + entry = zalloc(sizeof(*entry)); + if (entry == NULL) + return -ENOMEM; + + if (hashmap__add(src->samples, hash_key, entry) < 0) + return -ENOMEM; + } + h->nr_samples++; h->addr[offset].nr_samples++; h->period += sample->period; h->addr[offset].period += sample->period; + entry->nr_samples++; + entry->period += sample->period; pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", sym->start, sym->name, addr, addr - sym->start, evidx, - h->addr[offset].nr_samples, h->addr[offset].period); + entry->nr_samples, entry->period); return 0; } diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 94435607c958..a2b0c8210740 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -12,6 +12,7 @@ #include "symbol_conf.h" #include "mutex.h" #include "spark.h" +#include "hashmap.h" struct hist_browser_timer; struct hist_entry; @@ -280,6 +281,7 @@ struct annotated_source { size_t sizeof_sym_hist; struct sym_hist *histograms; struct annotation_line **offsets; + struct hashmap *samples; int nr_histograms; int nr_entries; int nr_asm_entries; From patchwork Wed Feb 28 00:52:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 207574 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3072873dyb; Tue, 27 Feb 2024 17:29:05 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVqGdCBQoTnscH0mHp8Qg+6wRGbkNuI2M2W5+KT+2j/eodce48tD76G2F8jHirMTgM27slHf4PddH1Lmu0s4eyF6MNefA== X-Google-Smtp-Source: AGHT+IGeLpfuTUI8mKcK05ex1iYOa+r5pKrB2clRLCWF94R0eBvehewpxREAVd6Fa3xLuxwXAwed X-Received: by 2002:a05:6a21:398f:b0:19e:a448:71cf with SMTP id ad15-20020a056a21398f00b0019ea44871cfmr4514210pzc.24.1709083745445; Tue, 27 Feb 2024 17:29:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709083745; cv=pass; d=google.com; s=arc-20160816; b=YnbZ6cPA9kEk0UkizhYOr+LIKrOWi8pqOw9BP8bNPCGN8KUXIe/4xnV8o43C7e+N6O X7CkkCbhhMNxIh/tLu+tWLKY0G4Q6vb49GmuFxDc9agrUSF2mHV5Vgvsc1ic4y+gyFlZ CvFqpK+RMaltuubEPJzLwO1maV8gQOLtQb6CwZdjchKfyyM0Xgo9jrp+qPNjZQAmZZDa QBKeAFWAvYt+hx1YlQ/4oGhiro0SCf53jc4Can3drlLoEK+Y82NRgx4FpP+k57H6iT4E lksZYSEltTPpxcJF5uRPuFqxlootLVahfkl5t4MDG7pmeG+Cwz/b71IXgoCP4hvM8Fkr MFRA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=bcyT8KbB3gJtfiqS4SkYAiisL8FM3QXyqFLBg5gjyDI=; fh=s1fZEdGGHFKlkrF3njJuxobE+Xf6SbIV8h7pc+69Jsg=; b=lMC/q7ZxrmzBaIQar8NvcNb9WI0oN5VX/lXNiQDjk8p+LogTBjHKGDLHWnmOKAVjKw h+L67QNJkhXpNnshMJ9k+tZrJ/VfTkHSRDOIIdUtufLnhXdsrErkrCmB9WzM2sSwr12Y VplmkUTIlaR8CbUNmibGvEznfljhcCQ4g1RpYCxUogs5uT6xDb+9YkmJWfeD6O8aVzdI kooEyrcvOw0vOrXyDPbCP5sY+C0/gfZhVkFoBGouEaMdtBCh17N40xv9OZxbkVsdOeh6 TlNTprnroxl+E1ie7pLd8xMT3FAk3WPlM5rF25O5BVqky1+0Y6czbT74/1OvOTCEL2c9 jN0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qlH9CSSa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84288-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84288-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s190-20020a632cc7000000b005e2b1d8f234si6173033pgs.342.2024.02.27.17.29.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 17:29:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84288-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qlH9CSSa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84288-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84288-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 229BAB27FCC for ; Wed, 28 Feb 2024 00:53:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4479F1CF99; Wed, 28 Feb 2024 00:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qlH9CSSa" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1972E1865; Wed, 28 Feb 2024 00:52:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081553; cv=none; b=mpGfafpuCtKfGgwVL9/I4EPR6NIQBFRoqXZYSkqR/M4j/N81FWAon1Wv3Wm3P6H6YW61Gzad9jpP+OVPDDrYgUpdl9/nHjcSff/2tpke2xUjpJHCkZiQ8+jROqMjgUFXAnYCxvRWuQ+FdDyciIrtdGxjdVIWSVOeYDmDer4oOmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081553; c=relaxed/simple; bh=rvlDlfIuCnTKdn5MnigqcDg5DtdXoGeqGh8gOu2Zuy4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MR8ErPvhv7DsiHc2RyBtUZRF3RJOek3lYB/SZYuWCMUXpD/tcpTcg4TiCfH24O9/w+AAgm9RgyS7W5qYVk4qCpP8MZXPGZxMKbhXU3nkU4CRonfICVloOEe1rhws8spgLqdW2ctcWaflyQHD1gS4Avf9rv8sea3sxpFDFE3+vGM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qlH9CSSa; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89A0FC433B2; Wed, 28 Feb 2024 00:52:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709081552; bh=rvlDlfIuCnTKdn5MnigqcDg5DtdXoGeqGh8gOu2Zuy4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qlH9CSSaNcWpR/79oxZUYyzEnO9TumVOqoVC/Qzs73tgKsP50ZL3roXRaOozNjbyo mHH9Lx0f3BnzzDdWrqYZoApz91or1r4CHeJN+JljTpQKH6V581dqHjO+1FJYLpIM4a oY6AVbhPKS7BSPLOfYi4sVnHB2CDJFG9JttDKZPCH+hxFNzxYj9ntvGOM9zaSDNA9J 2g6oPHEowOCHPSNbyEsxQjGhy29c+WR7DFQGhINYMt6W9Y+0kY+jmSQ6fg1DgEtQc3 MaYLgsjYF2XArC9Qi27UYC3S54WNUMiE/2aumO/MscCjq3TKHF+OQP/szQt5NGftG6 EfWrp/UZjaz7Q== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Andi Kleen Subject: [PATCH 2/4] perf annotate: Calculate instruction overhead using hashmap Date: Tue, 27 Feb 2024 16:52:28 -0800 Message-ID: <20240228005230.287113-3-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240228005230.287113-1-namhyung@kernel.org> References: <20240228005230.287113-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792104197384189961 X-GMAIL-MSGID: 1792104197384189961 Use annotated_source.samples hashmap instead of addr array in the struct sym_hist. Signed-off-by: Namhyung Kim --- tools/perf/ui/gtk/annotate.c | 14 +++++++++--- tools/perf/util/annotate.c | 44 ++++++++++++++++++++++++------------ tools/perf/util/annotate.h | 11 +++++++++ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/tools/perf/ui/gtk/annotate.c b/tools/perf/ui/gtk/annotate.c index 394861245fd3..93ce3d47e47e 100644 --- a/tools/perf/ui/gtk/annotate.c +++ b/tools/perf/ui/gtk/annotate.c @@ -28,21 +28,29 @@ static const char *const col_names[] = { static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym, struct disasm_line *dl, int evidx) { + struct annotation *notes; struct sym_hist *symhist; + struct sym_hist_entry *entry; double percent = 0.0; const char *markup; int ret = 0; + u64 nr_samples = 0; strcpy(buf, ""); if (dl->al.offset == (s64) -1) return 0; - symhist = annotation__histogram(symbol__annotation(sym), evidx); - if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples) + notes = symbol__annotation(sym); + symhist = annotation__histogram(notes, evidx); + entry = annotated_source__hist_entry(notes->src, evidx, dl->al.offset); + if (entry) + nr_samples = entry->nr_samples; + + if (!symbol_conf.event_group && nr_samples == 0) return 0; - percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples; + percent = 100.0 * nr_samples / symhist->nr_samples; markup = perf_gtk__get_percent_color(percent); if (markup) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 7a70e4d35c9b..e7859f756252 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -2368,17 +2368,25 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) return err; } -static void calc_percent(struct sym_hist *sym_hist, - struct hists *hists, +static void calc_percent(struct annotation *notes, + struct evsel *evsel, struct annotation_data *data, s64 offset, s64 end) { + struct hists *hists = evsel__hists(evsel); + int evidx = evsel->core.idx; + struct sym_hist *sym_hist = annotation__histogram(notes, evidx); unsigned int hits = 0; u64 period = 0; while (offset < end) { - hits += sym_hist->addr[offset].nr_samples; - period += sym_hist->addr[offset].period; + struct sym_hist_entry *entry; + + entry = annotated_source__hist_entry(notes->src, evidx, offset); + if (entry) { + hits += entry->nr_samples; + period += entry->period; + } ++offset; } @@ -2415,16 +2423,13 @@ static void annotation__calc_percent(struct annotation *notes, end = next ? next->offset : len; for_each_group_evsel(evsel, leader) { - struct hists *hists = evsel__hists(evsel); struct annotation_data *data; - struct sym_hist *sym_hist; BUG_ON(i >= al->data_nr); - sym_hist = annotation__histogram(notes, evsel->core.idx); data = &al->data[i++]; - calc_percent(sym_hist, hists, data, al->offset, end); + calc_percent(notes, evsel, data, al->offset, end); } } } @@ -2619,14 +2624,19 @@ static void print_summary(struct rb_root *root, const char *filename) static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel) { + int evidx = evsel->core.idx; struct annotation *notes = symbol__annotation(sym); - struct sym_hist *h = annotation__histogram(notes, evsel->core.idx); + struct sym_hist *h = annotation__histogram(notes, evidx); u64 len = symbol__size(sym), offset; - for (offset = 0; offset < len; ++offset) - if (h->addr[offset].nr_samples != 0) + for (offset = 0; offset < len; ++offset) { + struct sym_hist_entry *entry; + + entry = annotated_source__hist_entry(notes->src, evidx, offset); + if (entry && entry->nr_samples != 0) printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, - sym->start + offset, h->addr[offset].nr_samples); + sym->start + offset, entry->nr_samples); + } printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); } @@ -2855,8 +2865,14 @@ void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) h->nr_samples = 0; for (offset = 0; offset < len; ++offset) { - h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; - h->nr_samples += h->addr[offset].nr_samples; + struct sym_hist_entry *entry; + + entry = annotated_source__hist_entry(notes->src, evidx, offset); + if (entry == NULL) + continue; + + entry->nr_samples = entry->nr_samples * 7 / 8; + h->nr_samples += entry->nr_samples; } } diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index a2b0c8210740..3362980a5d3d 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -356,6 +356,17 @@ static inline struct sym_hist *annotation__histogram(struct annotation *notes, i return annotated_source__histogram(notes->src, idx); } +static inline struct sym_hist_entry * +annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset) +{ + struct sym_hist_entry *entry; + long key = offset << 16 | idx; + + if (!hashmap__find(src->samples, key, &entry)) + return NULL; + return entry; +} + static inline struct annotation *symbol__annotation(struct symbol *sym) { return (void *)sym - symbol_conf.priv_size; From patchwork Wed Feb 28 00:52:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 207575 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3072911dyb; Tue, 27 Feb 2024 17:29:13 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUkcUQ7KbIOqtQSlptOpA0xEtZ2dC5zpz9D802RGvPznWiJOQZlKuTuVgcrMm82GSPUQAvjR8D6bu13vOlVErRbY48Y8Q== X-Google-Smtp-Source: AGHT+IElc4mA/pplgXUO2ojG2FX9ve/BuNkbpFocwrJJByjdCapqHxndCrpGsvszFA0bh+Vf2tkH X-Received: by 2002:a17:90a:a112:b0:299:4f5a:62d5 with SMTP id s18-20020a17090aa11200b002994f5a62d5mr9640661pjp.18.1709083752953; Tue, 27 Feb 2024 17:29:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709083752; cv=pass; d=google.com; s=arc-20160816; b=Ww1GsupzrlOlK8Fmp6x1zb8BqSXYo7yAw4U0TApju2yuV/W/FxQYLWzmcKuCsOKmaU fyfRNugpppwhHVtpOMPrz42CifrbaMiH2lXwi/3OI+DKNuT7Yv9ltZ5pWXupZ7itzGV7 DbStrZPJ4uX2FYmvs01YDlXH++EIXgvJFPpEjvrHBdzwCZUNXw3hwM4EEsvaPmixs6c2 hrbbeig56Ap812Exj27Kq+/MuNwQ1oKsMLrN6nD5fyoVZvvFSyBCDDXSaLgX/i9AGoeA OO2OUhfvcc+V0VJBkMTCKKutc3c+jRqOEH7k3nNHnGAPFQZEjZ+8l6BJPzNEFgR7I9L3 ta1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=IfBuunwtiWBXDReIqjk2VwaHc87dvfCu3Oz8mgKS4mA=; fh=s1fZEdGGHFKlkrF3njJuxobE+Xf6SbIV8h7pc+69Jsg=; b=DTVRIy6ymuSvcltpuhH4qA/A2Ks1OR1MubrJWtDF6uyF/0RlQhSrsEs75ETbAnF/rI DXq/nA0dSSA+ZbsQiyQCptw8RQBXHtBZHpyaW0IQu3LvS3GMzozgPiWhiNLyVbY7p+/i DxZAsp2oBTDt9syfC6TmTW6pk2HMuffZtkEsbL2HvQ/QahyAi5dW/Gt1/WvD/Crfdom9 5HlcdeCh9ebn+I9LvzJhRyxHdmts3Jsa3HDjaj0yLxpdbPhqcfjr8/BJI2+7UDvuWUy7 rrv0s2j8wLkzM+uj6Jq/F1Fnq6y5xbIikRama/mE5pbMqCMUDxH0vjrolgBTn4Aruzf4 30uQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ukT/kDj3"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84289-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84289-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id f8-20020a17090a4a8800b00297074eed36si328173pjh.181.2024.02.27.17.29.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 17:29:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84289-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="ukT/kDj3"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84289-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84289-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id F03E3B28173 for ; Wed, 28 Feb 2024 00:53:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9C9301D681; Wed, 28 Feb 2024 00:52:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ukT/kDj3" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB64923CB; Wed, 28 Feb 2024 00:52:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081553; cv=none; b=hooZXwwD3KE88mI5nVw3tZ7qstP5lAkPIxZjY8jQN3rjaIbiiaxmIgcLGPySRVsYEYGXrkIysJ53bg2dRwtjPGllQAM0EKozDpmByClozSKr/byGShSME+2uR2m23hCRFMwXjOzWNFW8Pd6B/oUq87CKgPflHUnIxtTllwTGBZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081553; c=relaxed/simple; bh=HFQajpgQRni2lxL7vlCRDNCH5GBRC8OcnQVmLr+IUUQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aoQnhS3BC0zK8zDY8C5FgeP7Wa66sf+oymdPkQx+tTCIqSbXXM9xkgzNNtxTVpkNuy3l4jqi1dXNd/UcOgnelB06JS4pMZTYpAgOnKgW8XVAEh7UJxGHfT3qEcjfZ84a+UKtwQL9fUxGZ4lPKLpz4NzfQ0Q2dBjQnr1VA+Uq8hA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ukT/kDj3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BF0FC433C7; Wed, 28 Feb 2024 00:52:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709081553; bh=HFQajpgQRni2lxL7vlCRDNCH5GBRC8OcnQVmLr+IUUQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ukT/kDj30vjh++aZFNI2tAwiRi6F5hkfR2MwKojMKr0mysmiX8ZRfG5XTNn8Dw5c9 ZrRVidkkBaHoMWBA0U4QeQXQcooe+n4wRhuETOdn9i/RYxNNeMLkVTGBNfCGAVo6pd c9nLjJdUGGiC3euaZCKrSzmaQMP5BP71L5OeQDeKZE73ln44hw7kv3a05C8iX4eg/3 SSRGg685teCatBS/iNObaGjFKqb0PL1k49iENaId0S4psm5ZLca21h4h5arq3gdX3R EihCgSNU3q5wt5f2ylD4a6PpONC61aZexMCQXKBIe6z8UiOURWZ9b0luhAn2LY2im+ 4bEuekd4f20vQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Andi Kleen Subject: [PATCH 3/4] perf annotate: Remove sym_hist.addr[] array Date: Tue, 27 Feb 2024 16:52:29 -0800 Message-ID: <20240228005230.287113-4-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240228005230.287113-1-namhyung@kernel.org> References: <20240228005230.287113-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792104205233993439 X-GMAIL-MSGID: 1792104205233993439 It's not used anymore and the code is coverted to use a hash map. Now sym_hist has a static size, so no need to have sizeof_sym_hist in the struct annotated_source. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 36 +++++------------------------------- tools/perf/util/annotate.h | 4 +--- 2 files changed, 6 insertions(+), 34 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index e7859f756252..43b204815020 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -896,33 +896,10 @@ static __maybe_unused void annotated_source__delete(struct annotated_source *src } static int annotated_source__alloc_histograms(struct annotated_source *src, - size_t size, int nr_hists) + int nr_hists) { - size_t sizeof_sym_hist; - - /* - * Add buffer of one element for zero length symbol. - * When sample is taken from first instruction of - * zero length symbol, perf still resolves it and - * shows symbol name in perf report and allows to - * annotate it. - */ - if (size == 0) - size = 1; - - /* Check for overflow when calculating sizeof_sym_hist */ - if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry)) - return -1; - - sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry)); - - /* Check for overflow in zalloc argument */ - if (sizeof_sym_hist > SIZE_MAX / nr_hists) - return -1; - - src->sizeof_sym_hist = sizeof_sym_hist; src->nr_histograms = nr_hists; - src->histograms = calloc(nr_hists, sizeof_sym_hist) ; + src->histograms = calloc(nr_hists, sizeof(*src->histograms)); if (src->histograms == NULL) return -1; @@ -941,7 +918,7 @@ void symbol__annotate_zero_histograms(struct symbol *sym) annotation__lock(notes); if (notes->src != NULL) { memset(notes->src->histograms, 0, - notes->src->nr_histograms * notes->src->sizeof_sym_hist); + notes->src->nr_histograms * sizeof(*notes->src->histograms)); hashmap__clear(notes->src->samples); } if (notes->branch && notes->branch->cycles_hist) { @@ -1039,9 +1016,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms, } h->nr_samples++; - h->addr[offset].nr_samples++; h->period += sample->period; - h->addr[offset].period += sample->period; entry->nr_samples++; entry->period += sample->period; @@ -1094,8 +1069,7 @@ struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) if (notes->src->histograms == NULL) { alloc_histograms: - annotated_source__alloc_histograms(notes->src, symbol__size(sym), - nr_hists); + annotated_source__alloc_histograms(notes->src, nr_hists); } return notes->src; @@ -2854,7 +2828,7 @@ void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) struct annotation *notes = symbol__annotation(sym); struct sym_hist *h = annotation__histogram(notes, evidx); - memset(h, 0, notes->src->sizeof_sym_hist); + memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms); } void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 3362980a5d3d..4bdc70a9d376 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -242,7 +242,6 @@ void symbol__calc_percent(struct symbol *sym, struct evsel *evsel); struct sym_hist { u64 nr_samples; u64 period; - struct sym_hist_entry addr[]; }; struct cyc_hist { @@ -278,7 +277,6 @@ struct cyc_hist { */ struct annotated_source { struct list_head source; - size_t sizeof_sym_hist; struct sym_hist *histograms; struct annotation_line **offsets; struct hashmap *samples; @@ -348,7 +346,7 @@ void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *m static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx) { - return ((void *)src->histograms) + (src->sizeof_sym_hist * idx); + return &src->histograms[idx]; } static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx) From patchwork Wed Feb 28 00:52:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 207576 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3072955dyb; Tue, 27 Feb 2024 17:29:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXrjkBU08mWUMk49oV2+RdwXCunlQ8h35nsdQAk3/ch/U3bWN0jbTN/O40sy3TWES0b7wCAnp660fNywSN1Vy7TL8AExQ== X-Google-Smtp-Source: AGHT+IHUdgPVXDc9LisgBqPmZmCQi+lLvOj9qkB0EuTISJcURRbj49PhFbSiDbAzQW/dyKcOW0cJ X-Received: by 2002:a17:902:ccc7:b0:1db:e24f:f092 with SMTP id z7-20020a170902ccc700b001dbe24ff092mr12883029ple.6.1709083760720; Tue, 27 Feb 2024 17:29:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709083760; cv=pass; d=google.com; s=arc-20160816; b=ZFzTxx86wfnKDHxrJ65BgJuE9hlXJDQ3ESvfjpIswM3G+84Wqs23pFpNxlFjVtOzKR ucEevQDmSdSFQ3i793aNWMDgEF8XgpT2IP4OaSM7Gn3G7t5xQyiPLcLRy2a0Lem2SvXA N8hucAjxIq+X7xF2SEWoxDbxUrRURHLDqOunLhCRkpkZDtXCmouO5y3YbeDjgdtMDQhF ndyftZOzNX8aWSLs5rV2WWICZjmhfHAk10KMtzm2V+cW6l1rDgAZpleRJWKYyjsnEK3w GpNx9vluJSHstrYld/zSYsiNTyELJ9bPA96wX/HfSVJ33ARjBNEPVi8G3GLxilfZ3AmL UVRA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=r1qzS73VIDdsqS6qRKKn/i40luj1kDA2ll9DZ6vwohc=; fh=s1fZEdGGHFKlkrF3njJuxobE+Xf6SbIV8h7pc+69Jsg=; b=urmg0+q0xL05LaQbr3qka69ZtFhiE/ye+26VVVWL6mvQSBdnefPuCI31BDHnpffo2S ZjQxtvo5yZf31rITAn0A/4OVyfaGnM5CAfq7BhOHbVXrb08sJ2A44JNOSNsXGzKG4pSH lU8Ikv/j8n1cLRSXOV7QsrQjzaCVPPOH89PSoyxTejhZeT8vP/DGQHtKmVp2HO8N8v5t nS7O8LjwkwfONal0hmuuM+YVP+dzeYul0mNqX+DVEgRF5YE0wDBO0nkkX6MJup+GQQCC jPbHPx7KpqKGgw6NY94j6tdJJV0BBnSxNugLbWICY7oFIlvL/T2RdIB0u8VfmYqqIxYQ 1lqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FGKUgvTp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84290-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84290-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d11-20020a170903230b00b001dc9e696655si2357087plh.451.2024.02.27.17.29.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 17:29:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84290-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FGKUgvTp; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-84290-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84290-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 071F2B280E5 for ; Wed, 28 Feb 2024 00:53:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05FD61DDF1; Wed, 28 Feb 2024 00:52:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FGKUgvTp" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1E513C3C; Wed, 28 Feb 2024 00:52:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081554; cv=none; b=mEbXJRHadWSxhh7etAhCOSLm9BkpcTMsNb3Qa04D71Q1LigYbyrB0bpkAsyKAk6NjK00tm2QI5ulLhvyAXwWRKtcFDDHrakZ+BWlU52LmhtVck9J8MZ+KM/bKun4vGwOV9wG5bYnNHIJaw+C1xbw5Tq4+iPw7YtasXH3CrGmp5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709081554; c=relaxed/simple; bh=+IlYujNPWkLr0hPSR2olDKMPWbM11MBEXxrwzg1RtZE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gTDHMYHCTjxp30hJLzYsPXO9wTLdokYotrdI36YP31hEFIbr7Sk7GsgPISvUGsDuA1ONYzNhvx8h8cA8ba1pCC7pXM230q6w+BV3L5Bh+STIGDbijxYMfEi2I7CQ1Ij8KUADFHql66+CPlj/kGfluBZ/ChECvVmA3RybMLJJUPI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FGKUgvTp; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F4DEC43394; Wed, 28 Feb 2024 00:52:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709081553; bh=+IlYujNPWkLr0hPSR2olDKMPWbM11MBEXxrwzg1RtZE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FGKUgvTpckyQdW4eaNXoe9a8Sn14HMeP4Ll79sX7Je6DsZ1JrgvnbAqRW3R9OVtcb jsa/V4e5Pwp1iQ8HWIvvDkG9W5fPGaEbmkWHQ0ECg3an5LUanyg5fpJ0FRIoxfjsMG a5fVbEBWTzSHsRGMynujYn53u0KjgNKjibxAa/ym/es5SrqMz0VtSSEud+ltw5naUW GcCQxfGhT1fHB35BsvsaDJNc2N1NLmLJ8ZQa03ETethrrS2QfCYnNwk0m0wSTTCH09 vr7HrqzKbQpU/Rd/zDWav4hcqZ22RhHBKej9rvx/nS1yJ81pAnQp0PVERCBkDzZruE ZUsDBA+CmYZpw== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Andi Kleen Subject: [PATCH 4/4] perf annotate: Add comments in the data structures Date: Tue, 27 Feb 2024 16:52:30 -0800 Message-ID: <20240228005230.287113-5-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240228005230.287113-1-namhyung@kernel.org> References: <20240228005230.287113-1-namhyung@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792104213663969704 X-GMAIL-MSGID: 1792104213663969704 Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.h | 69 ++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 4bdc70a9d376..13cc659e508c 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -239,11 +239,42 @@ int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool r size_t disasm__fprintf(struct list_head *head, FILE *fp); void symbol__calc_percent(struct symbol *sym, struct evsel *evsel); +/** + * struct sym_hist - symbol histogram information for an event + * + * @nr_samples: Total number of samples. + * @period: Sum of sample periods. + */ struct sym_hist { u64 nr_samples; u64 period; }; +/** + * struct cyc_hist - (CPU) cycle histogram for a basic block + * + * @start: Start address of current block (if known). + * @cycles: Sum of cycles for the longest basic block. + * @cycles_aggr: Total cycles for this address. + * @cycles_max: Max cycles for this address. + * @cycles_min: Min cycles for this address. + * @cycles_spark: History of cycles for the longest basic block. + * @num: Number of samples for the longest basic block. + * @num_aggr: Total number of samples for this address. + * @have_start: Whether the current branch info has a start address. + * @reset: Number of resets due to a different start address. + * + * If sample has branch_stack and cycles info, it can construct basic blocks + * between two adjacent branches. It'd have start and end addresses but + * sometimes the start address may not be available. So the cycles are + * accounted at the end address. If multiple basic blocks end at the same + * address, it will take the longest one. + * + * The @start, @cycles, @cycles_spark and @num fields are used for the longest + * block only. Other fields are used for all cases. + * + * See __symbol__account_cycles(). + */ struct cyc_hist { u64 start; u64 cycles; @@ -258,18 +289,24 @@ struct cyc_hist { u16 reset; }; -/** struct annotated_source - symbols with hits have this attached as in sannotation +/** + * struct annotated_source - symbols with hits have this attached as in annotation * - * @histograms: Array of addr hit histograms per event being monitored - * nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if + * @source: List head for annotated_line (embeded in disasm_line). + * @histograms: Array of symbol histograms per event to maintain the total number + * of samples and period. + * @nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if * we have more than a group in a evlist, where we will want * to see each group separately, that is why symbol__annotate2() * sets src->nr_histograms to evsel->nr_members. - * @lines: If 'print_lines' is specified, per source code line percentages - * @source: source parsed from a disassembler like objdump -dS - * @cyc_hist: Average cycles per basic block + * @offsets: Array of annotation_line to be accessed by offset. + * @samples: Hash map of sym_hist_entry. Keyed by event index and offset in symbol. + * @nr_entries: Number of annotated_line in the source list. + * @nr_asm_entries: Number of annotated_line with actual asm instruction in the + * source list. + * @max_line_len: Maximum length of objdump output in an annotated_line. * - * lines is allocated, percentages calculated and all sorted by percentage + * disasm_lines are allocated, percentages calculated and all sorted by percentage * when the annotation is about to be presented, so the percentages are for * one of the entries in the histogram array, i.e. for the event/counter being * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate @@ -286,6 +323,24 @@ struct annotated_source { u16 max_line_len; }; +/** + * struct annotated_branch - basic block and IPC information for a symbol. + * + * @hit_cycles: Total executed cycles. + * @hit_insn: Total number of instructions executed. + * @total_insn: Number of instructions in the function. + * @cover_insn: Number of distinct, actually executed instructions. + * @cycles_hist: Array of cyc_hist for each instruction. + * @max_coverage: Maximum number of covered basic block (used for block-range). + * + * This struct is used by two different codes when the sample has branch stack + * and cycles information. annotation__compute_ipc() calculates average IPC + * using @hit_insn / @hit_cycles. The actual coverage can be calculated using + * @cover_insn / @total_insn. The @cycles_hist can give IPC for each (longest) + * basic block ends at the given address. + * process_basic_block() calculates coverage of instructions (or basic blocks) + * in the function. + */ struct annotated_branch { u64 hit_cycles; u64 hit_insn;