Message ID | 20240214063708.972376-3-irogers@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-64797-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1026488dyb; Tue, 13 Feb 2024 22:42:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXJxGxt2h4MOxnuMF6pMkpQQq5GmLFFDCcSbmuw0sihqCOsaxzA0XtJHouEu5hW7yygTN2AJEC7Gc5p8FymjAj0vJI6XQ== X-Google-Smtp-Source: AGHT+IHxoyQx8K6pXJE5/UD7zWJpoCRkixWz7QEmtXvQAHqTvCw+6y1fRTDJ6RNY4TW2j3zw8oH9 X-Received: by 2002:a05:6808:1202:b0:3bf:dd31:939b with SMTP id a2-20020a056808120200b003bfdd31939bmr1939438oil.0.1707892963715; Tue, 13 Feb 2024 22:42:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707892963; cv=pass; d=google.com; s=arc-20160816; b=YDT/u8M99HxBzoMe1v55UvlW24YGk3ECEYP/gb4Ke5WbL7Oa7WPmw7YaeIWJRC16Lw vEAZZlnexPJy7rbz95/kktfdugYxaG3zS6x0lzA8vQP1s3lWAZ1L1YkzZUDoufZeRYyN dAh4hf6fkyLBLpce59HfDq08RbYSRG9XF3Yj5IvJXUdk4vzSkFSicCJ8kWsyUQjcTtCr FpL5KQmRajTM0qnYLkzcqL0lGCktlsEdmQoxbZb5HV8oB6GFmuhSWgnKr4SjKC7WhS4F MO9BepYzBv5iu43RruT4oLvDpftxCO0+2MrMmR97RKq0nFpRM5KVhH3+0Gm9hPRNc3TD INig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=VZC75fzSKnLOcOdvEmiJR26JJ9Cyn2cw2nBv54DIcSM=; fh=VP3BsYqhWLx1X9euQeJsbk3MTkKjnwBby6pkMdT/ICI=; b=wsvgzKy5+6mRmAjHtVQ8w893Div2XqIbiJpv3GLt8wYVrtQgQ0aRL6FI57rZIaAID1 YSuVKMix6WkvuBVWhlQ7Y1TAigdKtbLkPo3gVg25TxYxIhULiVFLvAxjiTXx/oT29uJj Zw3Fn7oPDMDQVSiXgjBaWYgq2bEL5xOKwLeLAQ+W2bxa/tDpoO5+OXRO1xa/hiQVTTN/ veN9KOXfx7HdOUMN37Gcr4uyH8THraz/PNnNsQ+maNoMsdidGwiS6jQyxL/aW2kuU3ef VFpc2DybidKCekzInVTKeA9g6tfkYsVdNxEMv6/FpfLJ+b/Kyy3A1xPObtMnVMgfrvAh GtbA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Y1ONCkJ2; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64797-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64797-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXH7dBIFTNzwnofa+4oe4rZBXN+Nb881s6yVayDfUNhgYRR5iIq7yWC482I3fuoCIu5GgXbwj5s6p5CyQ2j9ZpYWLGRIQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id dw7-20020a056a00368700b006e100469067si1828388pfb.292.2024.02.13.22.42.43 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 22:42:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64797-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=@google.com header.s=20230601 header.b=Y1ONCkJ2; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64797-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64797-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 936C4B23CED for <ouuuleilei@gmail.com>; Wed, 14 Feb 2024 06:39:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A827C1643E; Wed, 14 Feb 2024 06:37:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y1ONCkJ2" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAE7E125D8 for <linux-kernel@vger.kernel.org>; Wed, 14 Feb 2024 06:37:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892648; cv=none; b=TJoBxJY1Kl5CALnRqUdI0qkZ4jjra+tl2lPNurAWDQRAkAynZ6npq5VmuZJiseIuag2C8E06lqSlcTZefS7Zgs9r3AC6BLszKX4NhfrEQGvZ2ICDk437D5Z+DaJWh37fH0FF019J4TBU/XT7Ea8gdgsDH1VUjOA5wQoCv0fokeE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892648; c=relaxed/simple; bh=Xqgs303n2TD+hO6ata6FdmAxJD9pFTCMplqiFePigLM=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=szM9k9MxuGwsrH/MXp3xDLZ3DuZodTBnUkkOFKLgGAPXoD2TJ3A1qqIAQIcmIAZ1j0OZtMFJXouE20UvIpbIXv11IxlwW8z6JWuIJZKizln8tXKZF801L6fYa6WXfXU+/3/DB88IML0E86IK/11161DXSy7aQG0JigTqfUXMGoE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y1ONCkJ2; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc4563611cso2980463276.3 for <linux-kernel@vger.kernel.org>; Tue, 13 Feb 2024 22:37:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892645; x=1708497445; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=VZC75fzSKnLOcOdvEmiJR26JJ9Cyn2cw2nBv54DIcSM=; b=Y1ONCkJ2OAX+WN00azK6e3AHlxqO+fHpojweUxt07+clWH7PSgGrETM9/2sm+KGohF 0jYRE6Qj+uE1CDzkgbvlGsM3QB2yHBfOO/Ah4VRhtBYolaqAhxXu0cQMs7UVWUP9rfy0 yiSxVrNs2SS+rWNSdx934q7umTgZdzeBr9/2GHsdG4avcbR/LdG00G3J2V/4gH2hcNYt vjYaQRCaBOC4X267+3I0y3JJikfpqiJ3wMUfOO7BPQ0zRLkJm45Wj63zKpkDFb3+j90a YDlxWMlzmhSDBNc//O2H5XN0ooLn1MwxjK1J6fWRSXiA8okiJdw3gnSkbayNSt0FaDJB WdVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892645; x=1708497445; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VZC75fzSKnLOcOdvEmiJR26JJ9Cyn2cw2nBv54DIcSM=; b=slYHfAGRMOvgO6RL49FLCEYkDk7ihCGDWr44y7Dcn2qhT3GUL80h8a/hfL0JTGj8Nl CQi0osrSQODzK3LdKqVVfa25UkD++yMIdWwTFeo99mP6VOI4MfWl7qxrPkKFPAPfV9NO ub44xo28q8ddegnpNtWPnSVApWKsovkkTGwBsbviyGBeLyM/ld25LW5clj1Js74szYQr rAs+Y0n/8Nsm4zzkv2/IG5K9pyZH8TRNF60meXHzTNdfc7YyyE21R/g7UhhMGIAQLLhz v6Ogn4glmeJhyC38OiO4xygj8VIkTnNfJp0pYqV7jDNgIwYc98Ej3anV/wpZVPp7l/lJ gjTQ== X-Forwarded-Encrypted: i=1; AJvYcCWsXS3piuRd/0iCC0YOcvl+G60jfFhgUQzWgsSDhBQvbLw7PCoMFHPyEAP6u3jQg2bM5xgUCBGzZj4tIItWEtxQwWS2jfL5AwFa5+rm X-Gm-Message-State: AOJu0Ywldk9I4mqVFMNx0TVJWJro8SuLuYOHcVQ80ecSqgGckW2wp8NX OHI7jnoZD/dPi0mIMsdafHLNbrTllbFH1VBpnJ1WgW7lmhXoN9870fApLL23LIKIpnzWR75MTgl NfUv/6Q== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:114d:b0:dc6:d2c8:6e50 with SMTP id p13-20020a056902114d00b00dc6d2c86e50mr330849ybu.7.1707892645603; Tue, 13 Feb 2024 22:37:25 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:04 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> Mime-Version: 1.0 References: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 2/6] perf trace: Ignore thread hashing in summary From: Ian Rogers <irogers@google.com> To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>, Arnaldo Carvalho de Melo <acme@kernel.org>, Namhyung Kim <namhyung@kernel.org>, Mark Rutland <mark.rutland@arm.com>, Alexander Shishkin <alexander.shishkin@linux.intel.com>, Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>, Adrian Hunter <adrian.hunter@intel.com>, Oliver Upton <oliver.upton@linux.dev>, Yang Jihong <yangjihong1@huawei.com>, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790855572374699526 X-GMAIL-MSGID: 1790855572374699526 |
Series |
Thread memory improvements and fixes
|
|
Commit Message
Ian Rogers
Feb. 14, 2024, 6:37 a.m. UTC
Commit 91e467bc568f ("perf machine: Use hashtable for machine
threads") made the iteration of thread tids unordered. The perf trace
--summary output sorts and prints each hash bucket, rather than all
threads globally. Change this behavior by turn all threads into a
list, sort the list by number of trace events then by tids, finally
print the list. This also allows the rbtree in threads to be not
accessed outside of machine.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-trace.c | 41 +++++++++++++++++++++----------------
tools/perf/util/rb_resort.h | 5 -----
2 files changed, 23 insertions(+), 23 deletions(-)
Comments
On Tue, Feb 13, 2024 at 10:37:04PM -0800, Ian Rogers wrote: > Commit 91e467bc568f ("perf machine: Use hashtable for machine > threads") made the iteration of thread tids unordered. The perf trace > --summary output sorts and prints each hash bucket, rather than all > threads globally. Change this behavior by turn all threads into a > list, sort the list by number of trace events then by tids, finally > print the list. This also allows the rbtree in threads to be not > accessed outside of machine. Can you please provide a refresh of the output that is changed by your patch? - Arnaldo > Signed-off-by: Ian Rogers <irogers@google.com> > --- > tools/perf/builtin-trace.c | 41 +++++++++++++++++++++---------------- > tools/perf/util/rb_resort.h | 5 ----- > 2 files changed, 23 insertions(+), 23 deletions(-) > > diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c > index 109b8e64fe69..90eaff8c0f6e 100644 > --- a/tools/perf/builtin-trace.c > +++ b/tools/perf/builtin-trace.c > @@ -74,6 +74,7 @@ > #include <linux/err.h> > #include <linux/filter.h> > #include <linux/kernel.h> > +#include <linux/list_sort.h> > #include <linux/random.h> > #include <linux/stringify.h> > #include <linux/time64.h> > @@ -4312,34 +4313,38 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace) > return ttrace ? ttrace->nr_events : 0; > } > > -DEFINE_RESORT_RB(threads, > - (thread__nr_events(thread__priv(a->thread)) < > - thread__nr_events(thread__priv(b->thread))), > - struct thread *thread; > -) > +static int trace_nr_events_cmp(void *priv __maybe_unused, > + const struct list_head *la, > + const struct list_head *lb) > { > - entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; > + struct thread_list *a = list_entry(la, struct thread_list, list); > + struct thread_list *b = list_entry(lb, struct thread_list, list); > + unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread)); > + unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread)); > + > + if (a_nr_events != b_nr_events) > + return a_nr_events < b_nr_events ? -1 : 1; > + > + /* Identical number of threads, place smaller tids first. */ > + return thread__tid(a->thread) < thread__tid(b->thread) > + ? -1 > + : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0); > } > > static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) > { > size_t printed = trace__fprintf_threads_header(fp); > - struct rb_node *nd; > - int i; > - > - for (i = 0; i < THREADS__TABLE_SIZE; i++) { > - DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); > + LIST_HEAD(threads); > > - if (threads == NULL) { > - fprintf(fp, "%s", "Error sorting output by nr_events!\n"); > - return 0; > - } > + if (machine__thread_list(trace->host, &threads) == 0) { > + struct thread_list *pos; > > - resort_rb__for_each_entry(nd, threads) > - printed += trace__fprintf_thread(fp, threads_entry->thread, trace); > + list_sort(NULL, &threads, trace_nr_events_cmp); > > - resort_rb__delete(threads); > + list_for_each_entry(pos, &threads, list) > + printed += trace__fprintf_thread(fp, pos->thread, trace); > } > + thread_list__delete(&threads); > return printed; > } > > diff --git a/tools/perf/util/rb_resort.h b/tools/perf/util/rb_resort.h > index 376e86cb4c3c..d927a0d25052 100644 > --- a/tools/perf/util/rb_resort.h > +++ b/tools/perf/util/rb_resort.h > @@ -143,9 +143,4 @@ struct __name##_sorted *__name = __name##_sorted__new > DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \ > __ilist->rblist.nr_entries) > > -/* For 'struct machine->threads' */ > -#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) \ > - DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_root, \ > - __machine->threads[hash_bucket].nr) > - > #endif /* _PERF_RESORT_RB_H_ */ > -- > 2.43.0.687.g38aa6559b0-goog
On Wed, Feb 14, 2024 at 01:36:46PM -0800, Ian Rogers wrote: > On Wed, Feb 14, 2024 at 1:15 PM Ian Rogers <irogers@google.com> wrote: > > On Wed, Feb 14, 2024 at 10:27 AM Ian Rogers <irogers@google.com> wrote: > > > On Wed, Feb 14, 2024 at 9:25 AM Arnaldo Carvalho de Melo > > > <acme@kernel.org> wrote: > > > > On Tue, Feb 13, 2024 at 10:37:04PM -0800, Ian Rogers wrote: > > > > > Commit 91e467bc568f ("perf machine: Use hashtable for machine > > > > > threads") made the iteration of thread tids unordered. The perf trace > > > > > --summary output sorts and prints each hash bucket, rather than all > > > > > threads globally. Change this behavior by turn all threads into a > > > > > list, sort the list by number of trace events then by tids, finally > > > > > print the list. This also allows the rbtree in threads to be not > > > > > accessed outside of machine. > > > > Can you please provide a refresh of the output that is changed by your patch? > > > > > > Hmm.. looks like perf trace record has broken and doesn't produce > > > output in newer perfs. It works on 6.5 and so a bisect is necessary. > > > > Bisect result: > > ``` > > 9925495d96efc14d885ba66c5696f664fe0e663c is the first bad commit > > commit 9925495d96efc14d885ba66c5696f664fe0e663c > > Author: Ian Rogers <irogers@google.com> > > Date: Thu Sep 14 14:19:45 2023 -0700 > > > > perf build: Default BUILD_BPF_SKEL, warn/disable for missing deps > > ... > > https://lore.kernel.org/r/20230914211948.814999-3-irogers@google.com > > ``` > > > > Now to do the bisect with BUILD_BPF_SKEL=1 on each make. > > This looks better (how could I be at fault :-) ): > ``` > 1836480429d173c01664a633b61e525b13d41a2a is the first bad commit > commit 1836480429d173c01664a633b61e525b13d41a2a > Author: Arnaldo Carvalho de Melo <acme@redhat.com> > Date: Wed Aug 16 13:53:26 2023 -0300 > > perf bpf_skel augmented_raw_syscalls: Cap the socklen parameter > using &= sizeof(saddr) > ... > Cc: Adrian Hunter <adrian.hunter@intel.com> > Cc: Ian Rogers <irogers@google.com> > Cc: Jiri Olsa <jolsa@kernel.org> > Cc: Namhyung Kim <namhyung@kernel.org> > Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> > ``` > No LKML link. So simple... ;-\ I've reproduced your steps and got to the same cset while testing on a recent distro kernel (6.6.13-200.fc39.x86_64), scratching my head now and trying to figure this out. Wonder if trying to run on an older kernel the problem would appear. Will try and add a perf test shell entry with a simple: root@number:~# perf trace record sleep 0.001 && perf script | head | wc -l [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.034 MB perf.data ] 0 root@number:~# Has to be 10 :-) Thanks, - Arnaldo
On Tue, Feb 13, 2024 at 10:37 PM Ian Rogers <irogers@google.com> wrote: > > Commit 91e467bc568f ("perf machine: Use hashtable for machine > threads") made the iteration of thread tids unordered. The perf trace > --summary output sorts and prints each hash bucket, rather than all > threads globally. Change this behavior by turn all threads into a > list, sort the list by number of trace events then by tids, finally > print the list. This also allows the rbtree in threads to be not > accessed outside of machine. > > Signed-off-by: Ian Rogers <irogers@google.com> > --- > tools/perf/builtin-trace.c | 41 +++++++++++++++++++++---------------- > tools/perf/util/rb_resort.h | 5 ----- > 2 files changed, 23 insertions(+), 23 deletions(-) > > diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c > index 109b8e64fe69..90eaff8c0f6e 100644 > --- a/tools/perf/builtin-trace.c > +++ b/tools/perf/builtin-trace.c > @@ -74,6 +74,7 @@ > #include <linux/err.h> > #include <linux/filter.h> > #include <linux/kernel.h> > +#include <linux/list_sort.h> > #include <linux/random.h> > #include <linux/stringify.h> > #include <linux/time64.h> > @@ -4312,34 +4313,38 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace) > return ttrace ? ttrace->nr_events : 0; > } > > -DEFINE_RESORT_RB(threads, > - (thread__nr_events(thread__priv(a->thread)) < > - thread__nr_events(thread__priv(b->thread))), > - struct thread *thread; > -) > +static int trace_nr_events_cmp(void *priv __maybe_unused, > + const struct list_head *la, > + const struct list_head *lb) > { > - entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; > + struct thread_list *a = list_entry(la, struct thread_list, list); > + struct thread_list *b = list_entry(lb, struct thread_list, list); > + unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread)); > + unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread)); > + > + if (a_nr_events != b_nr_events) > + return a_nr_events < b_nr_events ? -1 : 1; > + > + /* Identical number of threads, place smaller tids first. */ > + return thread__tid(a->thread) < thread__tid(b->thread) > + ? -1 > + : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0); I'm not sure if it can have a case where two different threads in the hash table can have the same tid. If not, it can simplify the last case. > } > > static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) > { > size_t printed = trace__fprintf_threads_header(fp); > - struct rb_node *nd; > - int i; > - > - for (i = 0; i < THREADS__TABLE_SIZE; i++) { > - DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); > + LIST_HEAD(threads); > > - if (threads == NULL) { > - fprintf(fp, "%s", "Error sorting output by nr_events!\n"); > - return 0; > - } > + if (machine__thread_list(trace->host, &threads) == 0) { > + struct thread_list *pos; > > - resort_rb__for_each_entry(nd, threads) > - printed += trace__fprintf_thread(fp, threads_entry->thread, trace); > + list_sort(NULL, &threads, trace_nr_events_cmp); Same concern, it'd be nice if we can use an array instead. Thanks, Namhyung > > - resort_rb__delete(threads); > + list_for_each_entry(pos, &threads, list) > + printed += trace__fprintf_thread(fp, pos->thread, trace); > } > + thread_list__delete(&threads); > return printed; > } > > diff --git a/tools/perf/util/rb_resort.h b/tools/perf/util/rb_resort.h > index 376e86cb4c3c..d927a0d25052 100644 > --- a/tools/perf/util/rb_resort.h > +++ b/tools/perf/util/rb_resort.h > @@ -143,9 +143,4 @@ struct __name##_sorted *__name = __name##_sorted__new > DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \ > __ilist->rblist.nr_entries) > > -/* For 'struct machine->threads' */ > -#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) \ > - DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_root, \ > - __machine->threads[hash_bucket].nr) > - > #endif /* _PERF_RESORT_RB_H_ */ > -- > 2.43.0.687.g38aa6559b0-goog >
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 109b8e64fe69..90eaff8c0f6e 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -74,6 +74,7 @@ #include <linux/err.h> #include <linux/filter.h> #include <linux/kernel.h> +#include <linux/list_sort.h> #include <linux/random.h> #include <linux/stringify.h> #include <linux/time64.h> @@ -4312,34 +4313,38 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace) return ttrace ? ttrace->nr_events : 0; } -DEFINE_RESORT_RB(threads, - (thread__nr_events(thread__priv(a->thread)) < - thread__nr_events(thread__priv(b->thread))), - struct thread *thread; -) +static int trace_nr_events_cmp(void *priv __maybe_unused, + const struct list_head *la, + const struct list_head *lb) { - entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread; + struct thread_list *a = list_entry(la, struct thread_list, list); + struct thread_list *b = list_entry(lb, struct thread_list, list); + unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread)); + unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread)); + + if (a_nr_events != b_nr_events) + return a_nr_events < b_nr_events ? -1 : 1; + + /* Identical number of threads, place smaller tids first. */ + return thread__tid(a->thread) < thread__tid(b->thread) + ? -1 + : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0); } static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) { size_t printed = trace__fprintf_threads_header(fp); - struct rb_node *nd; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); + LIST_HEAD(threads); - if (threads == NULL) { - fprintf(fp, "%s", "Error sorting output by nr_events!\n"); - return 0; - } + if (machine__thread_list(trace->host, &threads) == 0) { + struct thread_list *pos; - resort_rb__for_each_entry(nd, threads) - printed += trace__fprintf_thread(fp, threads_entry->thread, trace); + list_sort(NULL, &threads, trace_nr_events_cmp); - resort_rb__delete(threads); + list_for_each_entry(pos, &threads, list) + printed += trace__fprintf_thread(fp, pos->thread, trace); } + thread_list__delete(&threads); return printed; } diff --git a/tools/perf/util/rb_resort.h b/tools/perf/util/rb_resort.h index 376e86cb4c3c..d927a0d25052 100644 --- a/tools/perf/util/rb_resort.h +++ b/tools/perf/util/rb_resort.h @@ -143,9 +143,4 @@ struct __name##_sorted *__name = __name##_sorted__new DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \ __ilist->rblist.nr_entries) -/* For 'struct machine->threads' */ -#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) \ - DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_root, \ - __machine->threads[hash_bucket].nr) - #endif /* _PERF_RESORT_RB_H_ */