From patchwork Tue Feb 27 22:01:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 207460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2989528dyb; Tue, 27 Feb 2024 14:03:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW0qw7iKMC1pOrFtlf3qjlltjB5TGaEqQQWYLp6KrqTqls2MsfuE91VJ60L2DfNzLvnI+PfK5jppjuu7flP3HDINaCFiQ== X-Google-Smtp-Source: AGHT+IHEYzjC6uKBYxqAx6sv8wnFC85LWE1im+SuhuUKGBCTniY+stRewLwfmrJy08+zPZ8BwJQr X-Received: by 2002:a05:6a00:1ca4:b0:6e3:b4a5:4b53 with SMTP id y36-20020a056a001ca400b006e3b4a54b53mr11457265pfw.24.1709071421874; Tue, 27 Feb 2024 14:03:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709071421; cv=pass; d=google.com; s=arc-20160816; b=drdbACEx+5g/U6zLednySEJ0F3YXJurCxGpOhz1+Y+uyNJyCcpYVwUxn1KVATDu9aT eZSqYWIA1KbqUJ4JZCbvzJ7hbaF5gi7br9UohZZxnBsbb49RT6hYr/DzBmvsWEq8WD5F iTvBNU3FWDaaYn1j+Iz6lmMlJB9W6cYLezhEj/TBMExCY2eWI3aX9Di/hfo/A/kGpNUf qGsYG2S10eGLeQIq8266XLlrA1002GbO7gb2OYw/ywsCbA7oWn5UZyfDzQgLhfhYYcSo 6tV3ANZccMn7lBgK8NcBAk6hr9R9op3VeQtEFmWKuVY4F0QRfPve5lfaIhnJN2JfpjNr U88Q== 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=/FMLMbsrzbdkn9O9XsKWx3S12UuVwdsAjVLGeB/eBZI=; fh=KR8WpDK4eRHNmDZ2AzJon79UgK3uJAsB/I9iLRS70jQ=; b=dfy/U1muydd+PxlCEgnRWVSYDv0TzCBKsSPvu9nlNNmh2uVD6IUPAdVp6JraoRkj3Q 4lj16UiwuRC5/QYEsXbgSaibJ0GPe4c0GUMjxCN0BT578bPPIp4kxWTWrALpEJJvupT5 XCBAJ7h4WGOFO2U/C6BWrtd5V8ZHLCRoZpt6CdmbO3Bb4tCbE7n8RDKHdhhwh/2A/bVn 4eutyB1Gl02TDxd/+WiKC/22rznIBxuEnbNJW/3V6mQGNEAafMFJTM0VrQIkLr6ELW9J 1UIV6xeHm6SPJHqlRWahVt2TPLjBeioMv5N9RQS/MHVPknisjhd0LMZmPSmQJ+QXvAU2 F7aw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2+ezsV03; 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-84081-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84081-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s39-20020a056a0017a700b006e5571cfb33si698831pfg.311.2024.02.27.14.03.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:03:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84081-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2+ezsV03; 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-84081-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84081-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 4923D285E1E for ; Tue, 27 Feb 2024 22:03:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 91E8A155A39; Tue, 27 Feb 2024 22:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2+ezsV03" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 006C8151CD9 for ; Tue, 27 Feb 2024 22:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071328; cv=none; b=KQiNNqCEjICtn4Pz9ff/j/DFEcVtcL1gLSNFnIWLWcvmEEzCE52e1qbAG10T3WlE4U3xKDt4Hg+mK6BAWtMwmcjDaXNMgwd8vFZItLJ3IdF2tr4m6PDJmUibeKR5cgMwkm6T9xgsJpgjoIs2++VhQXHzFe0PvFMTGR7mr1ZEwXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071328; c=relaxed/simple; bh=louA5ITf0PB9gTEbmjxtVgELJEkk9dAW1fzHeRrbwTQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=J3nS7AHnbYs4nGVEwpuFL/5+ViUMdl8/tMuy1L6A3Zxf3yTY7KVAT29AJ0KAhwDzB6SyPqMxgAypU6ZYn0ebEtaeywfohUHEM/Q1Icoy5FESoQcdQqtv06i36ZGh+iRN6wAJow3d0CcrB8J7oqCy2PGZS4wLwNeaKosKecEdY68= 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=2+ezsV03; arc=none smtp.client-ip=209.85.219.202 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-f202.google.com with SMTP id 3f1490d57ef6-dcf22e5b70bso8605973276.1 for ; Tue, 27 Feb 2024 14:02:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709071326; x=1709676126; 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=/FMLMbsrzbdkn9O9XsKWx3S12UuVwdsAjVLGeB/eBZI=; b=2+ezsV035YYGdi1fQZHX7QXaKBcwAriZoTOm1Sw5aUUGe11VbnONJEpYnrxzRBBpMI 7XnFs5/BAIKzXCWsVn0eGJd+XMsXToE666mzs7qrh2LvC75PUwEqEDOschKvFabrIwVw jxEXQe/4v763Le9x3rOLQirCC1CZzYKcYNum2o0SOEYRo77jGnEAMHGhP3dETMKEeifx wgXhasDLkcqJde9u5QKillgECiqwXCsabDYG/3BoETwLijhHQvLL2A6g9g8tsGZA8ciV I5JLi8SAN25d9vBP/m+kQh/+XxV+Vm66FEslpn8lo/GQJcm0iih/jKtsAhnIMbs5Dgkr 0W4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709071326; x=1709676126; 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=/FMLMbsrzbdkn9O9XsKWx3S12UuVwdsAjVLGeB/eBZI=; b=OKEHgZvZZ6tsgClrVipgT1b+DppbMcnx2zfmpuepx9+9HWXE3pDejg6L/Lq4jj5ZXQ jzf+4Sp3fo/E+P2P5pf0dvNPGRVK2fGTJdTdkKwmRqBozJ7eq04qhbv8LGogLFq/L/8T qUVc4cq4/QyzZ8A+NR4hYGwc69SwL/gTa/QBTzFUhHD36jXhUnTnvTm82pvkZw41Wtku BfWa9A5hIKJqdFtpycW+VVOfXED1aDjbly9XqmIFpgbq/r/0zJ7kZaEhIDTUJvTUyKSD nR81OjFy2h/7QPcC39K7RyHD91an9wdNec+s8ejhsQiHhGC2noRXzR2vH1PqErRaEJa2 ChTg== X-Forwarded-Encrypted: i=1; AJvYcCUiKVt9jeu4bZHyAsMZDlCAPd6YQYV+xJfv9UADWnG3tIS+7ZcfzSE+EZkBhcobFrB9c+bPFqLCHa0n9e9qBaDa5vka0QynpZtT+rxy X-Gm-Message-State: AOJu0Yx3ca20WTyVUq315RaZX0VCgLJm2+Oso6rztZNBmBolOKN5DFzM pToj4TOP2P0R7RrXHguXmzFGtdiegeYOkdjGTvrsmYW3+YLAG/M+57KzI86c3ebV2QvjaH5ltsV EgRwYvQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ff1:8af6:9e1a:6382]) (user=irogers job=sendgmr) by 2002:a05:6902:150a:b0:dc6:d9eb:6422 with SMTP id q10-20020a056902150a00b00dc6d9eb6422mr32279ybu.10.1709071326081; Tue, 27 Feb 2024 14:02:06 -0800 (PST) Date: Tue, 27 Feb 2024 14:01:46 -0800 In-Reply-To: <20240227220150.3876198-1-irogers@google.com> Message-Id: <20240227220150.3876198-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240227220150.3876198-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Subject: [PATCH v2 2/6] perf trace: Ignore thread hashing in summary From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792091275035654140 X-GMAIL-MSGID: 1792091275035654140 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 --- 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 #include #include +#include #include #include #include @@ -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_ */ From patchwork Tue Feb 27 22:01:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 207459 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2989515dyb; Tue, 27 Feb 2024 14:03:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXpGG7mT8CjiVBzHjqCA5ZNJ+b0yTfGStP4iio61UhmzWadLvBtbOd+w3U60bC2Oh2PehRROL0oJ4tmOQBwRWswco2RBg== X-Google-Smtp-Source: AGHT+IGvM5zlS9CgEBkcTh9uPJH7sH2R8XlXH31wk/RiDuLXt/Ggm2AtT16zb2m/rkInC+MGOGHo X-Received: by 2002:aa7:d31a:0:b0:565:ddc7:6dcd with SMTP id p26-20020aa7d31a000000b00565ddc76dcdmr4856923edq.8.1709071420416; Tue, 27 Feb 2024 14:03:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709071420; cv=pass; d=google.com; s=arc-20160816; b=MSloCQEIwbZwJc/vkNs+FCJiXlidTr4U4KlM7JtSawS16vXMpj+daWYpeCirFfcbgD uOCkveW3aayijRV3QO9VVvi0j2sg/sgg7N7ilYvcsozkByoec5ywzKVMvcrH1gQjvBlr 4JxlEux1i7M9kfljPc2KMoYWwCuTn4zALzQ401M60Yo0PbH1mgfMvb53EJjESY1OtCgA oltmgcH6BylBW7e4a+xeTWl0VH/CpIBQEEhtULvEgsqACLXjlkUlv5yt+mbuVBXYsfsf XdrVz48Eg4rGXApyrmz319DGbAcK9b15ErPWWBnuF8CA0iLNVxKxlRWiHrrGt2d6/+Bv v0Jw== 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=LLvFhK5uL/QCeGE47QDYifrDggRxdFdeVaa+ektv4Ks=; fh=GHmQRVs8L8NxvEJ0xnpj2CC3EKYjA7+TPePUpeFlE9I=; b=KoJ95exulOb929tLFG9hmgAVNmGWfE/vR8ppl6JRPWaNcIE1kXKgnfh8UvjxAl6b17 CTSxWS5vw3n6diOzANA6op6/qtw3HAE0Xbc/1Bw/aweJ3B3BMnvhs/M3wQ25KKsR7rBO +QVDp0U0nvVZMtLJJiYs3TuVHmG+BVYCv9+qgXYlt2WaAu6q66UF8dFjDrrzAvNdDUEU JZhJSGlM5rGQOV2iapmkIYdQIO7OGM2ge/kAhAevBHiky7BgNtWf6DFzU9i0PZzda3Ev mCzZ6zd7/gvYcD92pWFV1RQi1hB0I8zy5QNiUP3TjyPXhziIm8X/f4BTzghgCLEsJc4M iX9Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4dleXVlg; 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-84082-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84082-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id w11-20020aa7d28b000000b00565ac35bfbesi1073433edq.44.2024.02.27.14.03.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:03:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84082-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4dleXVlg; 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-84082-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84082-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9534E1F2962C for ; Tue, 27 Feb 2024 22:03:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5ACCE157E9C; Tue, 27 Feb 2024 22:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4dleXVlg" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 A7AA4153BCD for ; Tue, 27 Feb 2024 22:02:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071332; cv=none; b=HeFDIi/eDCln2Hid5BZ2qG+nivS8ZG18iYQstb+yiALw5DrUywk88CWNfEewC2lsuz1nULwPzjWLRwyriatzxSvfZ6BY1yJuH4/pKm25/OC59fuotg1Vv2XvfR70vw2OTOa2Bz/5m0vvNtB0T1kN9d/btAqjo4CKl0IzA2CUQTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071332; c=relaxed/simple; bh=/yGeJM1MC3xrnGlw+YrOzjUeVSCpkNh+lS2W25QBImY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=D64A8+mMkY3LzB25EwDAQWyaazk5RJo7v9vlKS6i+vAlVnyscg5Ge3RHv9lOLH0+q+5lk00G6jGmDDbuQRJoPqlohJ3e75I+d1RgHGBi6kNQmmXNVvi6YNvB/XmEX1dHfPfT1SPEmsFr/TynYB/ZFHZzsSs8LIM2+6VY4M77lFk= 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=4dleXVlg; arc=none smtp.client-ip=209.85.128.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-yw1-f201.google.com with SMTP id 00721157ae682-608ad239f8fso82021337b3.0 for ; Tue, 27 Feb 2024 14:02:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709071329; x=1709676129; 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=LLvFhK5uL/QCeGE47QDYifrDggRxdFdeVaa+ektv4Ks=; b=4dleXVlggqGls83VLDgVmaAYqUTq7J/uiSL0iJc+KNyKLNICbsipEW97LvJweK32Hv XCGViPHcDRN0RZ9MzTlYCKWgog99NebLaguxMTvpcQJxNJf/Nwo7FWdzlMiE0VSMyvut RcNvasddobo2cv9jO/pwsLSS5swYhqMc1N/EhvgjCp7YmAyg/nkN8JGo82rYbHN+EKGa H3sdYTUZPFJdYf0nJOi8GloAulR6WFLIo114Gm1Ovwt5e7K6/iXsHgXSbm+N2VpDXrkN Sa0jJEbxXjdsnmU0bpuECXAJkR0DYXJT+5lKwsBNkRRtJ+Qo5Aq7NXTALOoh0FZxcOHH 2stA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709071329; x=1709676129; 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=LLvFhK5uL/QCeGE47QDYifrDggRxdFdeVaa+ektv4Ks=; b=UchK9s/7rOwtSBbuy32P9LElPH4slwQdYwGl1EpjARRq2CASkUzIBhXLOiBYkDmcIm EQbJAXU36MUesYOZZIoja7d8hd3qqeq4uqF9kb8/D8xEBP+pxGeIbGVzC6y7IUrqq/5z TsNrPNwcXwULEB6XZhNnPV5jUIZ5ZxNplzX0zZ8C1of4sNZPvJ9L13LRxXxBFYe8WAwx PsrgqAYc6mRXz8TudcWJvdCMtipmcdBQlVNSpdPqymZrX0jiMUJ58dyLkDMymQvOtpV7 amJ9wa7EDE1isrHES1TbYEVDAl4NFRQIT8KgZtW12DrjbUGuk6ztp4TgrzNaW2sWOJ3N BjeA== X-Forwarded-Encrypted: i=1; AJvYcCVmCLFl7BqQowC3P0Pvw47Fi4ZYn1MLrr1u35EAnbeXolhK54WRzflyScP0dKq/TZBgWp/WXncYojP7AdP90TdtarwCpWAeIiLcIKdX X-Gm-Message-State: AOJu0YwSHi8AHnCZKlbxfIveBTlDa4KGbMJlGLI8EXWESCENHVT0kyw9 veHBioEvmCzReQX4JL6y27f2E1RBfAd9RhHD6ZbSmSRHjDbTyNdHm2A3btm13QimhLTTrIb9Uw/ rxeidjg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ff1:8af6:9e1a:6382]) (user=irogers job=sendgmr) by 2002:a81:a08f:0:b0:608:e711:5a5a with SMTP id x137-20020a81a08f000000b00608e7115a5amr807524ywg.1.1709071328756; Tue, 27 Feb 2024 14:02:08 -0800 (PST) Date: Tue, 27 Feb 2024 14:01:47 -0800 In-Reply-To: <20240227220150.3876198-1-irogers@google.com> Message-Id: <20240227220150.3876198-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240227220150.3876198-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Subject: [PATCH v2 3/6] perf machine: Move fprintf to for_each loop and a callback From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792091273700671840 X-GMAIL-MSGID: 1792091273700671840 Avoid exposing the threads data structure by switching to the callback machine__for_each_thread approach. machine__fprintf is only used in tests and verbose >3 output so don't turn to list and sort. Add machine__threads_nr to be refactored later. Note, all existing *_fprintf routines ignore fprintf errors. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 43 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 7872ce92c9fc..e072b2115b64 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1113,29 +1113,40 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) return printed; } -size_t machine__fprintf(struct machine *machine, FILE *fp) +struct machine_fprintf_cb_args { + FILE *fp; + size_t printed; +}; + +static int machine_fprintf_cb(struct thread *thread, void *data) { - struct rb_node *nd; - size_t ret; - int i; + struct machine_fprintf_cb_args *args = data; - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; + /* TODO: handle fprintf errors. */ + args->printed += thread__fprintf(thread, args->fp); + return 0; +} - down_read(&threads->lock); +static size_t machine__threads_nr(const struct machine *machine) +{ + size_t nr = 0; - ret = fprintf(fp, "Threads: %u\n", threads->nr); + for (int i = 0; i < THREADS__TABLE_SIZE; i++) + nr += machine->threads[i].nr; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - struct thread *pos = rb_entry(nd, struct thread_rb_node, rb_node)->thread; + return nr; +} - ret += thread__fprintf(pos, fp); - } +size_t machine__fprintf(struct machine *machine, FILE *fp) +{ + struct machine_fprintf_cb_args args = { + .fp = fp, + .printed = 0, + }; + size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)); - up_read(&threads->lock); - } - return ret; + machine__for_each_thread(machine, machine_fprintf_cb, &args); + return ret + args.printed; } static struct dso *machine__get_kernel(struct machine *machine) From patchwork Tue Feb 27 22:01:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 207483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3005039dyb; Tue, 27 Feb 2024 14:42:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXXni5TW40XMSG1wjyctO/LWQY9rDBesEr6W4dfxQ2bBbefql0w4+iSLcC4RrMMFLnMXWIIxe3oreCJMYZye8JwgPoP3g== X-Google-Smtp-Source: AGHT+IEL7R+AhTLwvW5LC0w7NDUtWztHnsWK6BZKsypG1BjEElSKQWPXACkMeEudCHIcHqDPBTJc X-Received: by 2002:a17:90a:ea8c:b0:299:a69:1f8b with SMTP id h12-20020a17090aea8c00b002990a691f8bmr9208692pjz.23.1709073773681; Tue, 27 Feb 2024 14:42:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709073773; cv=pass; d=google.com; s=arc-20160816; b=N2r8gx/HoXUmx2AuXqGNCEH0V8dqGAMW5dbqUHxhQoP92GSrvHvKJqjEJTOpgiwEQk ejyu1ib84B92mqDf/oOgRmBf1fKRpsdMc9mnSdXJVjJmfbf0hfqFtkgjoLx2nXA6kE1E 0HVdOGa7eYbzItPpCYFeQpWIdXjLdck6OPb/LFDIPJ2I6jL4eL4olt06XqT5R0d0C9+M DJFu4S/Jrwh6bhDmPaaUOgKzTx9g1MiF5CYaUFRLd6jHC8BZHvl7sTSPXeCBi2Io7eki 5mPaL7BDiZNxgIDHQXr9MZfdVajfcj7jtG63s2m9wZx4564iAbAriYGEOrr7JpD79xTL TFHQ== 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=ImEDViDBOcqCHKqHKXmTVraxu3CYSvL5VkrRz+GpsCo=; fh=yBHWAiPKkO9h21e7yUmtXg1BhGjdVNv1BiH+84YU6+c=; b=EN1dHjEs/IWYlNJTSye6TIt8jcy6xginJUxukAMCUN2ZN7YnV/UFpso+znABb5Fy+Q g1lXwrhwtOTKKi7U4V8Boj3LNDEMAHMpsDVtdPbIkK3tdbrbbvAPCtZemHZZb7i/qM2t RUnjzUbggrNeGcN/hF3HKo7oSnUcZy5M+pt+6hTP23eCddqBL7o20JTy2oJ9fO31Dywo DE3l7Jo3pXlzWOYxU+lxPHxsZctlSg5oNZ3d/H6nzF35oIsN2b/KEYyvy0z2zUM3ONxI VtW1VudZYb8Fzd5CB3oxAMAWDMwB0ioQIvsNKKwy/lKHw6iJqGdjWN+CtosgeqGq1EOJ xBdA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=RL532pKU; 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-84083-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84083-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id si2-20020a17090b528200b0029ac787c384si104744pjb.123.2024.02.27.14.42.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:42:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84083-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=RL532pKU; 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-84083-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84083-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 9C7E0B2B3E2 for ; Tue, 27 Feb 2024 22:04:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D7AE158D91; Tue, 27 Feb 2024 22:02:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RL532pKU" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 16396156963 for ; Tue, 27 Feb 2024 22:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071334; cv=none; b=gLXB0F9j7hyhREZjlbuRvlhwgqmkvBilk467MwMsdwq4N5YZieY7XR5o083+iYV4lV7wdqagU6cJeK+t/p3lWSqhjwUb/xnZlRlvJyJKoMutOaYOjHvr4fTnB9Y9NpyqoPZqc1updrfnZWQcaM7MsmLDFhgTpznOeCFL7sVqO7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071334; c=relaxed/simple; bh=BwcV35MQ9IkUkXfZ67mXHHWsr4b/gkiZgBHtVuRfRT8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=d5OZocfkhKYTVFNptfVEJ4V0xGsisxnFnqAzUIvUkn8XywswYJDR5DEKi1uXa+RLDIUy2l9UUCAfJCuOzF6O++uhhs/KUjzyNky7Mg4a3VxkBo4JgYT/QIVMi3v7RKCb/RdtOz26F7p+QK0DrB04qe+dMEL0QM0b6II3pppevn8= 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=RL532pKU; arc=none smtp.client-ip=209.85.128.202 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-yw1-f202.google.com with SMTP id 00721157ae682-607cd6c11d7so75668517b3.0 for ; Tue, 27 Feb 2024 14:02:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709071331; x=1709676131; 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=ImEDViDBOcqCHKqHKXmTVraxu3CYSvL5VkrRz+GpsCo=; b=RL532pKU8yYvYtM1DYrnK0nqiCSzXF4pq4zwV9m63odZJJxR3dnA8GCuF/eUvGdjUT xP1e02aH+ZlyhkWkJQz5wt+QqPIQibrJ9O7ZmqE10XiwMxbRE679X5aMXdeaxBX1fN4S hMQxQy0dZJEpmO2dH9lxXPmRr3M46BBmvHyKBpEgnu4wMpM3+0/rRftg6IoXEONpMmiV Pe/l0uddR2ST0stQ03jQCDtKULMX9bHJgcBTCwzkbLO1mKM3oVMLCVlUsJ5Yo8KCXibY iyGBNns5NLyjD0kwPt9ttOjyjOxqy6KYsUmhno9ToCWgZ22yUBgWJRDgelJ4nsmad2ZK s9tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709071331; x=1709676131; 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=ImEDViDBOcqCHKqHKXmTVraxu3CYSvL5VkrRz+GpsCo=; b=Kf326XqIomuik85iAjzUEEgzraJqZ8ieRxz8pV0oh+11EfjRV7wcTxcKUn0WZrX8lM pOPbmbzUVUtC+POTfWpzziH8isDs0zNMwj5S7GzqceOQL0BvzF9bjyTp9gyAQwz3aOr9 4OaX/jKa5wv0GRpDXWADzjfBF2ygIN1N8FMEaQX6jb+5/2rrCLkNu9XSNSVmc1qUdIzh HRhXWOezOwY+DaNpr3S/qtCBnrWVAuOHJFuSfFID52q9Uh1Y/AYPH9lpRs59+vatKx4t 0UQAKN/SDRTzHEwkMgxMLmKjh8MJF+jJn2nDXmDTAJxqlofaC6TAKH4ubNo+7Nwi5Ji7 v2Dw== X-Forwarded-Encrypted: i=1; AJvYcCUNXm21yWjLkvr0QnDOe1a18tUDgMHEz9EToUdDn+Uwz4aX5zlCt7OiD48SQij6YIY9MAJsl/EU8LW9wzkGomxi33gF0hQ+NMqAenW1 X-Gm-Message-State: AOJu0Yyt4ZJ2Qq3/Utziu7LLcvkRuE0caxCrctg7ITrooA3NnzaPtlfV LhtbL0TQTr9InZN9HiRS79sUlOl/BlCqePoViunw4G4BMhEKa1rxoerbVfa0JSZLad16cux3kTM qA2hw1g== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ff1:8af6:9e1a:6382]) (user=irogers job=sendgmr) by 2002:a25:d8d3:0:b0:dcc:4785:b51e with SMTP id p202-20020a25d8d3000000b00dcc4785b51emr54008ybg.12.1709071331258; Tue, 27 Feb 2024 14:02:11 -0800 (PST) Date: Tue, 27 Feb 2024 14:01:48 -0800 In-Reply-To: <20240227220150.3876198-1-irogers@google.com> Message-Id: <20240227220150.3876198-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240227220150.3876198-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Subject: [PATCH v2 4/6] perf threads: Move threads to its own files From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792093741724764066 X-GMAIL-MSGID: 1792093741724764066 Move threads out of machine and move thread_rb_node into the C file. This hides the implementation of threads from the rest of the code allowing for it to be refactored. Note, the implementation here is removed in the next patch in the series which moves to a hashtable. Locking discipline is tightened up in this change. When a function is added to threads.c care is taken that the threads lock is held appropriately. This replaces somewhat adhoc use of the lock in machine, and adds the lock in places it was missed primarily threads__nr. In the previous code it was the caller's responsibility to hold the right lock before calling an internal "__" version of the function. As the lock is now encapsulated in threads, the findnew function requires holding it (as it already did in machine). Rather than do conditionals with locks based on whether the thread should be created (which could potentially be error prone with a read lock match with a write unlock), have a separate threads__find that won't create the thread and only holds the read lock. This effectively duplicates the findnew logic, with the existing findnew logic only operating under a write lock assuming creation is necessary as a previous find failed. The creation may still fail with the write lock due to another thread. The duplication is removed in the next patch that delegates the implementation to hashtable. Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/bpf_lock_contention.c | 8 +- tools/perf/util/machine.c | 287 ++++---------------------- tools/perf/util/machine.h | 20 +- tools/perf/util/thread.c | 2 +- tools/perf/util/thread.h | 6 - tools/perf/util/threads.c | 244 ++++++++++++++++++++++ tools/perf/util/threads.h | 35 ++++ 8 files changed, 325 insertions(+), 278 deletions(-) create mode 100644 tools/perf/util/threads.c create mode 100644 tools/perf/util/threads.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 2cbeeb79b6ef..e0a723e24503 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -72,6 +72,7 @@ perf-y += ordered-events.o perf-y += namespaces.o perf-y += comm.o perf-y += thread.o +perf-y += threads.o perf-y += thread_map.o perf-y += parse-events-flex.o perf-y += parse-events-bison.o diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 31ff19afc20c..3992c8a9fd96 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -210,7 +210,7 @@ static const char *lock_contention_get_name(struct lock_contention *con, /* do not update idle comm which contains CPU number */ if (pid) { - struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid); + struct thread *t = machine__findnew_thread(machine, /*pid=*/-1, pid); if (t == NULL) return name; @@ -302,9 +302,9 @@ int lock_contention_read(struct lock_contention *con) return -1; if (con->aggr_mode == LOCK_AGGR_TASK) { - struct thread *idle = __machine__findnew_thread(machine, - /*pid=*/0, - /*tid=*/0); + struct thread *idle = machine__findnew_thread(machine, + /*pid=*/0, + /*tid=*/0); thread__set_comm(idle, "swapper", /*timestamp=*/0); } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index e072b2115b64..e668a97255f8 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -43,9 +43,6 @@ #include #include -static void __machine__remove_thread(struct machine *machine, struct thread_rb_node *nd, - struct thread *th, bool lock); - static struct dso *machine__kernel_dso(struct machine *machine) { return map__dso(machine->vmlinux_map); @@ -58,35 +55,6 @@ static void dsos__init(struct dsos *dsos) init_rwsem(&dsos->lock); } -static void machine__threads_init(struct machine *machine) -{ - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - threads->entries = RB_ROOT_CACHED; - init_rwsem(&threads->lock); - threads->nr = 0; - threads->last_match = NULL; - } -} - -static int thread_rb_node__cmp_tid(const void *key, const struct rb_node *nd) -{ - int to_find = (int) *((pid_t *)key); - - return to_find - (int)thread__tid(rb_entry(nd, struct thread_rb_node, rb_node)->thread); -} - -static struct thread_rb_node *thread_rb_node__find(const struct thread *th, - struct rb_root *tree) -{ - pid_t to_find = thread__tid(th); - struct rb_node *nd = rb_find(&to_find, tree, thread_rb_node__cmp_tid); - - return rb_entry(nd, struct thread_rb_node, rb_node); -} - static int machine__set_mmap_name(struct machine *machine) { if (machine__is_host(machine)) @@ -120,7 +88,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) RB_CLEAR_NODE(&machine->rb_node); dsos__init(&machine->dsos); - machine__threads_init(machine); + threads__init(&machine->threads); machine->vdso_info = NULL; machine->env = NULL; @@ -221,27 +189,11 @@ static void dsos__exit(struct dsos *dsos) void machine__delete_threads(struct machine *machine) { - struct rb_node *nd; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - down_write(&threads->lock); - nd = rb_first_cached(&threads->entries); - while (nd) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - nd = rb_next(nd); - __machine__remove_thread(machine, trb, trb->thread, false); - } - up_write(&threads->lock); - } + threads__remove_all_threads(&machine->threads); } void machine__exit(struct machine *machine) { - int i; - if (machine == NULL) return; @@ -254,12 +206,7 @@ void machine__exit(struct machine *machine) zfree(&machine->current_tid); zfree(&machine->kallsyms_filename); - machine__delete_threads(machine); - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; - - exit_rwsem(&threads->lock); - } + threads__exit(&machine->threads); } void machine__delete(struct machine *machine) @@ -526,7 +473,7 @@ static void machine__update_thread_pid(struct machine *machine, if (thread__pid(th) == thread__tid(th)) return; - leader = __machine__findnew_thread(machine, thread__pid(th), thread__pid(th)); + leader = machine__findnew_thread(machine, thread__pid(th), thread__pid(th)); if (!leader) goto out_err; @@ -560,160 +507,55 @@ static void machine__update_thread_pid(struct machine *machine, goto out_put; } -/* - * Front-end cache - TID lookups come in blocks, - * so most of the time we dont have to look up - * the full rbtree: - */ -static struct thread* -__threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) -{ - struct thread *th; - - th = threads->last_match; - if (th != NULL) { - if (thread__tid(th) == tid) { - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - thread__put(threads->last_match); - threads->last_match = NULL; - } - - return NULL; -} - -static struct thread* -threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) -{ - struct thread *th = NULL; - - if (perf_singlethreaded) - th = __threads__get_last_match(threads, machine, pid, tid); - - return th; -} - -static void -__threads__set_last_match(struct threads *threads, struct thread *th) -{ - thread__put(threads->last_match); - threads->last_match = thread__get(th); -} - -static void -threads__set_last_match(struct threads *threads, struct thread *th) -{ - if (perf_singlethreaded) - __threads__set_last_match(threads, th); -} - /* * Caller must eventually drop thread->refcnt returned with a successful * lookup/new thread inserted. */ -static struct thread *____machine__findnew_thread(struct machine *machine, - struct threads *threads, - pid_t pid, pid_t tid, - bool create) +static struct thread *__machine__findnew_thread(struct machine *machine, + pid_t pid, + pid_t tid, + bool create) { - struct rb_node **p = &threads->entries.rb_root.rb_node; - struct rb_node *parent = NULL; - struct thread *th; - struct thread_rb_node *nd; - bool leftmost = true; + struct thread *th = threads__find(&machine->threads, tid); + bool created; - th = threads__get_last_match(threads, machine, pid, tid); - if (th) + if (th) { + machine__update_thread_pid(machine, th, pid); return th; - - while (*p != NULL) { - parent = *p; - th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - threads__set_last_match(threads, th); - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else { - p = &(*p)->rb_right; - leftmost = false; - } } - if (!create) return NULL; - th = thread__new(pid, tid); - if (th == NULL) - return NULL; - - nd = malloc(sizeof(*nd)); - if (nd == NULL) { - thread__put(th); - return NULL; - } - nd->thread = th; - - rb_link_node(&nd->rb_node, parent, p); - rb_insert_color_cached(&nd->rb_node, &threads->entries, leftmost); - /* - * We have to initialize maps separately after rb tree is updated. - * - * The reason is that we call machine__findnew_thread within - * thread__init_maps to find the thread leader and that would screwed - * the rb tree. - */ - if (thread__init_maps(th, machine)) { - pr_err("Thread init failed thread %d\n", pid); - rb_erase_cached(&nd->rb_node, &threads->entries); - RB_CLEAR_NODE(&nd->rb_node); - free(nd); - thread__put(th); - return NULL; - } - /* - * It is now in the rbtree, get a ref - */ - threads__set_last_match(threads, th); - ++threads->nr; - - return thread__get(th); -} + th = threads__findnew(&machine->threads, pid, tid, &created); + if (created) { + /* + * We have to initialize maps separately after rb tree is + * updated. + * + * The reason is that we call machine__findnew_thread within + * thread__init_maps to find the thread leader and that would + * screwed the rb tree. + */ + if (thread__init_maps(th, machine)) { + pr_err("Thread init failed thread %d\n", pid); + threads__remove(&machine->threads, th); + thread__put(th); + return NULL; + } + } else + machine__update_thread_pid(machine, th, pid); -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) -{ - return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true); + return th; } -struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, - pid_t tid) +struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) { - struct threads *threads = machine__threads(machine, tid); - struct thread *th; - - down_write(&threads->lock); - th = __machine__findnew_thread(machine, pid, tid); - up_write(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=*/true); } -struct thread *machine__find_thread(struct machine *machine, pid_t pid, - pid_t tid) +struct thread *machine__find_thread(struct machine *machine, pid_t pid, pid_t tid) { - struct threads *threads = machine__threads(machine, tid); - struct thread *th; - - down_read(&threads->lock); - th = ____machine__findnew_thread(machine, threads, pid, tid, false); - up_read(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=*/false); } /* @@ -1127,23 +969,13 @@ static int machine_fprintf_cb(struct thread *thread, void *data) return 0; } -static size_t machine__threads_nr(const struct machine *machine) -{ - size_t nr = 0; - - for (int i = 0; i < THREADS__TABLE_SIZE; i++) - nr += machine->threads[i].nr; - - return nr; -} - size_t machine__fprintf(struct machine *machine, FILE *fp) { struct machine_fprintf_cb_args args = { .fp = fp, .printed = 0, }; - size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)); + size_t ret = fprintf(fp, "Threads: %zu\n", threads__nr(&machine->threads)); machine__for_each_thread(machine, machine_fprintf_cb, &args); return ret + args.printed; @@ -2069,36 +1901,9 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event return 0; } -static void __machine__remove_thread(struct machine *machine, struct thread_rb_node *nd, - struct thread *th, bool lock) -{ - struct threads *threads = machine__threads(machine, thread__tid(th)); - - if (!nd) - nd = thread_rb_node__find(th, &threads->entries.rb_root); - - if (threads->last_match && RC_CHK_EQUAL(threads->last_match, th)) - threads__set_last_match(threads, NULL); - - if (lock) - down_write(&threads->lock); - - BUG_ON(refcount_read(thread__refcnt(th)) == 0); - - thread__put(nd->thread); - rb_erase_cached(&nd->rb_node, &threads->entries); - RB_CLEAR_NODE(&nd->rb_node); - --threads->nr; - - free(nd); - - if (lock) - up_write(&threads->lock); -} - void machine__remove_thread(struct machine *machine, struct thread *th) { - return __machine__remove_thread(machine, NULL, th, true); + return threads__remove(&machine->threads, th); } int machine__process_fork_event(struct machine *machine, union perf_event *event, @@ -3232,23 +3037,7 @@ int machine__for_each_thread(struct machine *machine, int (*fn)(struct thread *thread, void *p), void *priv) { - struct threads *threads; - struct rb_node *nd; - int rc = 0; - int i; - - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - threads = &machine->threads[i]; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - rc = fn(trb->thread, priv); - if (rc != 0) - return rc; - } - } - return rc; + return threads__for_each_thread(&machine->threads, fn, priv); } int machines__for_each_thread(struct machines *machines, diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index b738ce84817b..e28c787616fe 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -7,6 +7,7 @@ #include "maps.h" #include "dsos.h" #include "rwsem.h" +#include "threads.h" struct addr_location; struct branch_stack; @@ -28,16 +29,6 @@ extern const char *ref_reloc_sym_names[]; struct vdso_info; -#define THREADS__TABLE_BITS 8 -#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) - -struct threads { - struct rb_root_cached entries; - struct rw_semaphore lock; - unsigned int nr; - struct thread *last_match; -}; - struct machine { struct rb_node rb_node; pid_t pid; @@ -48,7 +39,7 @@ struct machine { char *root_dir; char *mmap_name; char *kallsyms_filename; - struct threads threads[THREADS__TABLE_SIZE]; + struct threads threads; struct vdso_info *vdso_info; struct perf_env *env; struct dsos dsos; @@ -69,12 +60,6 @@ struct machine { bool trampolines_mapped; }; -static inline struct threads *machine__threads(struct machine *machine, pid_t tid) -{ - /* Cast it to handle tid == -1 */ - return &machine->threads[(unsigned int)tid % THREADS__TABLE_SIZE]; -} - /* * The main kernel (vmlinux) map */ @@ -220,7 +205,6 @@ bool machine__is(struct machine *machine, const char *arch); bool machine__normalized_is(struct machine *machine, const char *arch); int machine__nr_cpus_avail(struct machine *machine); -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid); struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index c59ab4d79163..1aa8962dcf52 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -26,7 +26,7 @@ int thread__init_maps(struct thread *thread, struct machine *machine) if (pid == thread__tid(thread) || pid == -1) { thread__set_maps(thread, maps__new(machine)); } else { - struct thread *leader = __machine__findnew_thread(machine, pid, pid); + struct thread *leader = machine__findnew_thread(machine, pid, pid); if (leader) { thread__set_maps(thread, maps__get(thread__maps(leader))); diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index df344262eaee..8b4a3c69bad1 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -3,7 +3,6 @@ #define __PERF_THREAD_H #include -#include #include #include #include @@ -29,11 +28,6 @@ struct lbr_stitch { struct callchain_cursor_node *prev_lbr_cursor; }; -struct thread_rb_node { - struct rb_node rb_node; - struct thread *thread; -}; - DECLARE_RC_STRUCT(thread) { /** @maps: mmaps associated with this thread. */ struct maps *maps; diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c new file mode 100644 index 000000000000..d984ec939c7b --- /dev/null +++ b/tools/perf/util/threads.c @@ -0,0 +1,244 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "threads.h" +#include "machine.h" +#include "thread.h" + +struct thread_rb_node { + struct rb_node rb_node; + struct thread *thread; +}; + +static struct threads_table_entry *threads__table(struct threads *threads, pid_t tid) +{ + /* Cast it to handle tid == -1 */ + return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; +} + +void threads__init(struct threads *threads) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + table->entries = RB_ROOT_CACHED; + init_rwsem(&table->lock); + table->nr = 0; + table->last_match = NULL; + } +} + +void threads__exit(struct threads *threads) +{ + threads__remove_all_threads(threads); + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + exit_rwsem(&table->lock); + } +} + +size_t threads__nr(struct threads *threads) +{ + size_t nr = 0; + + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + + down_read(&table->lock); + nr += table->nr; + up_read(&table->lock); + } + return nr; +} + +/* + * Front-end cache - TID lookups come in blocks, + * so most of the time we dont have to look up + * the full rbtree: + */ +static struct thread *__threads_table_entry__get_last_match(struct threads_table_entry *table, + pid_t tid) +{ + struct thread *th, *res = NULL; + + th = table->last_match; + if (th != NULL) { + if (thread__tid(th) == tid) + res = thread__get(th); + } + return res; +} + +static void __threads_table_entry__set_last_match(struct threads_table_entry *table, + struct thread *th) +{ + thread__put(table->last_match); + table->last_match = thread__get(th); +} + +static void threads_table_entry__set_last_match(struct threads_table_entry *table, + struct thread *th) +{ + down_write(&table->lock); + __threads_table_entry__set_last_match(table, th); + up_write(&table->lock); +} + +struct thread *threads__find(struct threads *threads, pid_t tid) +{ + struct threads_table_entry *table = threads__table(threads, tid); + struct rb_node **p; + struct thread *res = NULL; + + down_read(&table->lock); + res = __threads_table_entry__get_last_match(table, tid); + if (res) + return res; + + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct rb_node *parent = *p; + struct thread *th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; + + if (thread__tid(th) == tid) { + res = thread__get(th); + break; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else + p = &(*p)->rb_right; + } + up_read(&table->lock); + if (res) + threads_table_entry__set_last_match(table, res); + return res; +} + +struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created) +{ + struct threads_table_entry *table = threads__table(threads, tid); + struct rb_node **p; + struct rb_node *parent = NULL; + struct thread *res = NULL; + struct thread_rb_node *nd; + bool leftmost = true; + + *created = false; + down_write(&table->lock); + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct thread *th; + + parent = *p; + th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; + + if (thread__tid(th) == tid) { + __threads_table_entry__set_last_match(table, th); + res = thread__get(th); + goto out_unlock; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else { + leftmost = false; + p = &(*p)->rb_right; + } + } + nd = malloc(sizeof(*nd)); + if (nd == NULL) + goto out_unlock; + res = thread__new(pid, tid); + if (!res) + free(nd); + else { + *created = true; + nd->thread = thread__get(res); + rb_link_node(&nd->rb_node, parent, p); + rb_insert_color_cached(&nd->rb_node, &table->entries, leftmost); + ++table->nr; + __threads_table_entry__set_last_match(table, res); + } +out_unlock: + up_write(&table->lock); + return res; +} + +void threads__remove_all_threads(struct threads *threads) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + struct rb_node *nd; + + down_write(&table->lock); + __threads_table_entry__set_last_match(table, NULL); + nd = rb_first_cached(&table->entries); + while (nd) { + struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); + + nd = rb_next(nd); + thread__put(trb->thread); + rb_erase_cached(&trb->rb_node, &table->entries); + RB_CLEAR_NODE(&trb->rb_node); + --table->nr; + + free(trb); + } + assert(table->nr == 0); + up_write(&table->lock); + } +} + +void threads__remove(struct threads *threads, struct thread *thread) +{ + struct rb_node **p; + struct threads_table_entry *table = threads__table(threads, thread__tid(thread)); + pid_t tid = thread__tid(thread); + + down_write(&table->lock); + if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) + __threads_table_entry__set_last_match(table, NULL); + + p = &table->entries.rb_root.rb_node; + while (*p != NULL) { + struct rb_node *parent = *p; + struct thread_rb_node *nd = rb_entry(parent, struct thread_rb_node, rb_node); + struct thread *th = nd->thread; + + if (RC_CHK_EQUAL(th, thread)) { + thread__put(nd->thread); + rb_erase_cached(&nd->rb_node, &table->entries); + RB_CLEAR_NODE(&nd->rb_node); + --table->nr; + free(nd); + break; + } + + if (tid < thread__tid(th)) + p = &(*p)->rb_left; + else + p = &(*p)->rb_right; + } + up_write(&table->lock); +} + +int threads__for_each_thread(struct threads *threads, + int (*fn)(struct thread *thread, void *data), + void *data) +{ + for (int i = 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table = &threads->table[i]; + struct rb_node *nd; + + for (nd = rb_first_cached(&table->entries); nd; nd = rb_next(nd)) { + struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); + int rc = fn(trb->thread, data); + + if (rc != 0) + return rc; + } + } + return 0; + +} diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h new file mode 100644 index 000000000000..ed67de627578 --- /dev/null +++ b/tools/perf/util/threads.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_THREADS_H +#define __PERF_THREADS_H + +#include +#include "rwsem.h" + +struct thread; + +#define THREADS__TABLE_BITS 8 +#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) + +struct threads_table_entry { + struct rb_root_cached entries; + struct rw_semaphore lock; + unsigned int nr; + struct thread *last_match; +}; + +struct threads { + struct threads_table_entry table[THREADS__TABLE_SIZE]; +}; + +void threads__init(struct threads *threads); +void threads__exit(struct threads *threads); +size_t threads__nr(struct threads *threads); +struct thread *threads__find(struct threads *threads, pid_t tid); +struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created); +void threads__remove_all_threads(struct threads *threads); +void threads__remove(struct threads *threads, struct thread *thread); +int threads__for_each_thread(struct threads *threads, + int (*fn)(struct thread *thread, void *data), + void *data); + +#endif /* __PERF_THREADS_H */ From patchwork Tue Feb 27 22:01:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 207462 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2989982dyb; Tue, 27 Feb 2024 14:04:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWUIqnXv8y2vIr/HzpMxFs1PoffSHjb7eudKHqHuPnit6dn8rJ/oTGZEShl48hscStRfNQqVrFPSBgaxieVOBFiPJMreg== X-Google-Smtp-Source: AGHT+IF2Z1Te45BFgVTrzpT6FZ3PwmTvmr/fzQ69aqgD9EFXvUeop+278y8xG/JufSlG9aQnX/mi X-Received: by 2002:a05:6808:bd4:b0:3c0:3189:d9e2 with SMTP id o20-20020a0568080bd400b003c03189d9e2mr3850260oik.20.1709071474668; Tue, 27 Feb 2024 14:04:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709071474; cv=pass; d=google.com; s=arc-20160816; b=iuDwdI0S1/zYAQ4JiF36O+MPGR7Brtjg9ZATfb3ZNW9xbIW0pPF8LeWa9xn1a80HcN 2/8OZoACu0vzGvVmcALQMp/laZ25QeDK0xIOXqA4HF8O7iSvDkpkD4AjkuyObMPX8pcZ aK7eNAR2BEzctEb9ssTCtAMw4gmNqXSNIF4lx8EpG6+e6HpF6cXo/PO6cbtoTGrHKlM1 4XOsI30TPgCtiXduZGgeK0d0FquQdJFBWRKIvRaWklf5zC7m5SuJffsR0YJ3bg+bF9OR s6YgcE5QtkgP5xlvcj7NP388N70vrjteum+GU6cb/vOKBPkP7dA/hbqUoIgKYthYrtnK HqvQ== 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=wba+QvoL7K11DwHxYWfqulLAN6tkIyxzlgcJ5v9C6VE=; fh=ADTIWxoa2lnLJ0JAxDZtYCoWC4MWV/2EBqmxgq9e5bc=; b=SBm/7ySDaDfNdImsktCpfSdqGtr4PT4Fob7Gx5cAPORchIalvT/CpWjEr4SrQbYWva FZ27bF5Z/krn4+Eqql0vh5bolcBGmUVKfUqyXP6eanzi6pb6Knd5WbuA0U7euv3yrg6b 0G7LBo3nJxg/gBSMRhhm0orDuMNHvaLDaZrIHoESHnrglAh/cL0QG/KV5MHTkF54Ko08 JAWSDQQLP8o5yXrbv1QgmCkUKyj7jGN9SKebJGMNETHTVzqoS5H9PhIhiwakQvPT6kk+ I3jQdgpW0Jsf1NU5E4bUFyMRgnFUVYpMGhSDblx5SI9HrT0fdEqMPG9L/H2ZxC2hglzI OeGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GVYTn6Tx; 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-84085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84085-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c3-20020aa781c3000000b006e4e6509ce2si6107858pfn.332.2024.02.27.14.04.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:04:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GVYTn6Tx; 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-84085-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84085-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9BF8D2855CE for ; Tue, 27 Feb 2024 22:04:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9DF39158D9B; Tue, 27 Feb 2024 22:02:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GVYTn6Tx" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 4CFD4157E65 for ; Tue, 27 Feb 2024 22:02:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071338; cv=none; b=lvBCPy6LOtkiIKApHm3/+EO+5vX3+aEg0K2s2Jlhh+18x2T01/nYt67qSFDkiVmYQMIyaSrSPOZdI3rI1kxA1UrSd2hKnICV78CcevLeVAI9B+RgzhpM9hqtjFaY7MTAHO/YgZvKEggPfYwM95dEZ24Drbc1YnzC690+BhwH2so= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071338; c=relaxed/simple; bh=8uQDXFmaIiRJ6Hg6w6jtncylaSx91Lkrl/drWddSSDA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=QYdjyO4yj/oPwRKxavFuCnrjj8dfXiQAJLHDnj6XnKmbGvmQlbP7xyXXKKiO4oBComBJAb8icv081mz/Obew9uMwiPB7WosgG4g+ggTcmz4YLZ7pKz7HYjB7dGYVy+4OtYyTwUqBzSyZzmI6e5wlxRcqsFtOqkuqbxgBZuDBc5M= 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=GVYTn6Tx; arc=none smtp.client-ip=209.85.128.202 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-yw1-f202.google.com with SMTP id 00721157ae682-608b648044fso88532087b3.3 for ; Tue, 27 Feb 2024 14:02:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709071333; x=1709676133; 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=wba+QvoL7K11DwHxYWfqulLAN6tkIyxzlgcJ5v9C6VE=; b=GVYTn6TxJEypgiw8cYUM50IdLB0JV+gqF6e3HhFkS6L8+C1MR/h0xe1zfanAkOhtej /RKnakt//SLyy41x1YK/XOkYUP0Y5UQm6gOqIwL7r6jt0u1dXyFli6kENjVnjDTUmcQz 6tov3X9oh+Jn+N3XfWdZg3DorBO8oFSadTK4L6pQ7dFaFwBkwHW40astGgPXLqHDzaQE qSiGmpFNd8A1tBq3yjzG20eJ4nKmtpvjdTty4iR1zy8OmnYotXCKRi4IRFgyMTg+JXP3 VbrOmtHR+6gqVlnlDJuugOuqYixjYrJ4uvHei3NF3Pq6fe0STYgG6agjeMH2pDN3wJ71 Tg7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709071333; x=1709676133; 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=wba+QvoL7K11DwHxYWfqulLAN6tkIyxzlgcJ5v9C6VE=; b=xKbKOt2Cl4PDBttCHjkYr/EorVkqAuTlm+U54yfyPuAED/q0+CEu9IR+SDCaK+6cML zj0DyTXPCQnXHYxKUAaREV48lknazUHCggDC5rbUIOSvaxeRvC1Kbpo4ho8Dk9J0WZRA 1ChwlPSm4iTOBqsg3/zIgB0m/UYbN2sdQ6y5ZBdfM1Y6ICfqr42IAOjhlXuVjFm+nERh 0Zr8oV5Urc85Qe8h6J3WmYfpfuhJAbCop3TMtJF+/O/rgGFqxuzZY99k5CyrtduDImTs PHNhtt0V3rwl8bePjiU7sWPxCbqSZ2umsQuiOQtosuH2whg779VrTTkqs3apVzH3VQZC 5+WA== X-Forwarded-Encrypted: i=1; AJvYcCWOFA3M7oRwg9gkTs/rDedynwAkD9tb0HpEcVqVp7AG6AxPG8OPDK7f9G3lrP/86U2G0a9e6tq+ssz/NrD78M092cE552xFVY1znLSW X-Gm-Message-State: AOJu0YzzBohq0yzOdn9irMdL8zZywj7W8z4TK2A5xVwjWRhTk30afiRh iihD1WdrtHHxw+9VPkqbHO22X8+5eEZBhRYF2Ax7e+tlT0xTTpOLBHTM591uLad68qA2hpbUS/N TZWhPsg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ff1:8af6:9e1a:6382]) (user=irogers job=sendgmr) by 2002:a81:6c56:0:b0:608:ba07:3093 with SMTP id h83-20020a816c56000000b00608ba073093mr780129ywc.1.1709071333554; Tue, 27 Feb 2024 14:02:13 -0800 (PST) Date: Tue, 27 Feb 2024 14:01:49 -0800 In-Reply-To: <20240227220150.3876198-1-irogers@google.com> Message-Id: <20240227220150.3876198-6-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240227220150.3876198-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Subject: [PATCH v2 5/6] perf threads: Switch from rbtree to hashmap From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792091330565244532 X-GMAIL-MSGID: 1792091330565244532 The rbtree provides a sorting on entries but this is unused. Switch to using hashmap for O(1) rather than O(log n) find/insert/remove complexity. Signed-off-by: Ian Rogers --- tools/perf/util/threads.c | 146 ++++++++++++-------------------------- tools/perf/util/threads.h | 6 +- 2 files changed, 47 insertions(+), 105 deletions(-) diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c index d984ec939c7b..55923be53180 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -3,25 +3,30 @@ #include "machine.h" #include "thread.h" -struct thread_rb_node { - struct rb_node rb_node; - struct thread *thread; -}; - static struct threads_table_entry *threads__table(struct threads *threads, pid_t tid) { /* Cast it to handle tid == -1 */ return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; } +static size_t key_hash(long key, void *ctx __maybe_unused) +{ + /* The table lookup removes low bit entropy, but this is just ignored here. */ + return key; +} + +static bool key_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 == key2; +} + void threads__init(struct threads *threads) { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - table->entries = RB_ROOT_CACHED; + hashmap__init(&table->shard, key_hash, key_equal, NULL); init_rwsem(&table->lock); - table->nr = 0; table->last_match = NULL; } } @@ -32,6 +37,7 @@ void threads__exit(struct threads *threads) for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; + hashmap__clear(&table->shard); exit_rwsem(&table->lock); } } @@ -44,7 +50,7 @@ size_t threads__nr(struct threads *threads) struct threads_table_entry *table = &threads->table[i]; down_read(&table->lock); - nr += table->nr; + nr += hashmap__size(&table->shard); up_read(&table->lock); } return nr; @@ -86,28 +92,13 @@ static void threads_table_entry__set_last_match(struct threads_table_entry *tabl struct thread *threads__find(struct threads *threads, pid_t tid) { struct threads_table_entry *table = threads__table(threads, tid); - struct rb_node **p; - struct thread *res = NULL; + struct thread *res; down_read(&table->lock); res = __threads_table_entry__get_last_match(table, tid); - if (res) - return res; - - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct rb_node *parent = *p; - struct thread *th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - res = thread__get(th); - break; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else - p = &(*p)->rb_right; + if (!res) { + if (hashmap__find(&table->shard, tid, &res)) + res = thread__get(res); } up_read(&table->lock); if (res) @@ -118,49 +109,25 @@ struct thread *threads__find(struct threads *threads, pid_t tid) struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created) { struct threads_table_entry *table = threads__table(threads, tid); - struct rb_node **p; - struct rb_node *parent = NULL; struct thread *res = NULL; - struct thread_rb_node *nd; - bool leftmost = true; *created = false; down_write(&table->lock); - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct thread *th; - - parent = *p; - th = rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) == tid) { - __threads_table_entry__set_last_match(table, th); - res = thread__get(th); - goto out_unlock; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else { - leftmost = false; - p = &(*p)->rb_right; - } - } - nd = malloc(sizeof(*nd)); - if (nd == NULL) - goto out_unlock; res = thread__new(pid, tid); - if (!res) - free(nd); - else { - *created = true; - nd->thread = thread__get(res); - rb_link_node(&nd->rb_node, parent, p); - rb_insert_color_cached(&nd->rb_node, &table->entries, leftmost); - ++table->nr; - __threads_table_entry__set_last_match(table, res); + if (res) { + if (hashmap__add(&table->shard, tid, res)) { + /* Add failed. Assume a race so find other entry. */ + thread__put(res); + res = NULL; + if (hashmap__find(&table->shard, tid, &res)) + res = thread__get(res); + } else { + res = thread__get(res); + *created = true; + } + if (res) + __threads_table_entry__set_last_match(table, res); } -out_unlock: up_write(&table->lock); return res; } @@ -169,57 +136,32 @@ void threads__remove_all_threads(struct threads *threads) { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur, *tmp; + size_t bkt; down_write(&table->lock); __threads_table_entry__set_last_match(table, NULL); - nd = rb_first_cached(&table->entries); - while (nd) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - - nd = rb_next(nd); - thread__put(trb->thread); - rb_erase_cached(&trb->rb_node, &table->entries); - RB_CLEAR_NODE(&trb->rb_node); - --table->nr; + hashmap__for_each_entry_safe((&table->shard), cur, tmp, bkt) { + struct thread *old_value; - free(trb); + hashmap__delete(&table->shard, cur->key, /*old_key=*/NULL, &old_value); + thread__put(old_value); } - assert(table->nr == 0); up_write(&table->lock); } } void threads__remove(struct threads *threads, struct thread *thread) { - struct rb_node **p; struct threads_table_entry *table = threads__table(threads, thread__tid(thread)); - pid_t tid = thread__tid(thread); + struct thread *old_value; down_write(&table->lock); if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) __threads_table_entry__set_last_match(table, NULL); - p = &table->entries.rb_root.rb_node; - while (*p != NULL) { - struct rb_node *parent = *p; - struct thread_rb_node *nd = rb_entry(parent, struct thread_rb_node, rb_node); - struct thread *th = nd->thread; - - if (RC_CHK_EQUAL(th, thread)) { - thread__put(nd->thread); - rb_erase_cached(&nd->rb_node, &table->entries); - RB_CLEAR_NODE(&nd->rb_node); - --table->nr; - free(nd); - break; - } - - if (tid < thread__tid(th)) - p = &(*p)->rb_left; - else - p = &(*p)->rb_right; - } + hashmap__delete(&table->shard, thread__tid(thread), /*old_key=*/NULL, &old_value); + thread__put(old_value); up_write(&table->lock); } @@ -229,11 +171,11 @@ int threads__for_each_thread(struct threads *threads, { for (int i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table = &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur; + size_t bkt; - for (nd = rb_first_cached(&table->entries); nd; nd = rb_next(nd)) { - struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node); - int rc = fn(trb->thread, data); + hashmap__for_each_entry((&table->shard), cur, bkt) { + int rc = fn((struct thread *)cur->pvalue, data); if (rc != 0) return rc; diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index ed67de627578..d03bd91a7769 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -2,7 +2,7 @@ #ifndef __PERF_THREADS_H #define __PERF_THREADS_H -#include +#include "hashmap.h" #include "rwsem.h" struct thread; @@ -11,9 +11,9 @@ struct thread; #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) struct threads_table_entry { - struct rb_root_cached entries; + /* Key is tid, value is struct thread. */ + struct hashmap shard; struct rw_semaphore lock; - unsigned int nr; struct thread *last_match; }; From patchwork Tue Feb 27 22:01:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 207461 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2989744dyb; Tue, 27 Feb 2024 14:04:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUQYXA31VkHiLnwy+HsnJ2tzGyGacCr2VdmJg8eAan5WqFfiaMDjuB5D24J4GDTi/oTK8L7U4tGczsbV6tST5jI2WpWUA== X-Google-Smtp-Source: AGHT+IHIgAeXzebpZQWjORvzizNAwmn4yultTScAO7zinRIsz1jV67PjTeieqE/MT+SKhNFIwUEv X-Received: by 2002:a05:6512:4019:b0:513:16cd:137d with SMTP id br25-20020a056512401900b0051316cd137dmr1056659lfb.2.1709071447324; Tue, 27 Feb 2024 14:04:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709071447; cv=pass; d=google.com; s=arc-20160816; b=yNp5/DUxnJh9DVY6M0S31aK4QKXXITTHLkL+aFVrGQ2ThM2xexiMe5QTbJEz+lGP7h T5kl27BkmmS0vSrxBugs/M+j+UT4srezVo3KAbkehBQLNUZ3DklufsdXLgKon5WdN6pL J29SefqnAQhDwwwWQ3DEcWMx9sd+UxCRcUhl6jCQG7CFAVwUcTusjFjzH7MaypsXeo/h 6tr6S8l857e4cd+eBT/usBJhObHQoQTE66mb2Wt8YlFjrTHCcJGsG1cDDXO1Yck73u3p Eu4qfScOjRu1BZ9MV/9cD9RM7ZLqtGImRo1mN/rIogRA+r6+hAUWQzP9QHNkEYknsv3C xGqQ== 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=vCV6R+NKRLMYoVC59mA+bYuzdEe0EtrTOitlrlnnMsA=; fh=xLLQE/8BOjVtiCjPI50zoW78VQbrAP4zRBr+KFNfKlM=; b=BndWTC0xXhAjYqVp8P/GiUFUFhvvg6ZYQj6zBNClc9MBPjx3Rgu7siDH0EuBP3PhiE iy5v1e+F+JdlMwswq7SdrWWjpBe6wY/Z+UJTQ1H6DPDyeYnS/4hoYt8hlE1hxSjo0ARp N7WMd1Foi89P+aemYlZesvUzMaXpr4jTKtm2Tioxs+4NFWwYtf2LtU+JqGqN9VgroF6Q OjjOyStEH3dreRGyRajqHfaFKYDODVrIXSg5XW9/CJwxvzzeClDIdl+Ng0LGOzBOmKcY jxuXMiavJ0cWDDx6da0t3lzmHycIPJWT5XsJORKYwWnfGeMeGwllfi5iqW+ztCAR8qsp FAuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=s9gcvNa9; 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-84084-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84084-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n24-20020a170906089800b00a438cbd7cd3si1073836eje.190.2024.02.27.14.04.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 14:04:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84084-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=s9gcvNa9; 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-84084-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84084-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 am.mirrors.kernel.org (Postfix) with ESMTPS id E67DD1F2443B for ; Tue, 27 Feb 2024 22:04:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07BDA158D8C; Tue, 27 Feb 2024 22:02:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="s9gcvNa9" 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 E2A421586D8 for ; Tue, 27 Feb 2024 22:02:16 +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=1709071338; cv=none; b=lBKbvWSwESAOEvOaZtGEY4uhbbEtwudtfcPm2UrfdDdA7CzPB3BBBzPbzRje+T343GNSzHukU2madJN8cgJkf53+1VRL024+/dS5oIc6blsGBP7lgVOEEITjyIqbFeb84r61Rwszzmijxc5/rMclk7eeFn0PBrBgWNTkDrpEpYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709071338; c=relaxed/simple; bh=upLg+yhe8A2OZg6p8OFlrhH2yLDrE7BI//l8P1Vylms=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=pqyLHTrrjGbQ00RchUxraYd6c6gAvL0DBjILF6gWxAlW2psDGK3kutau1ZdVPGmOTyE62sY5kb5IXMu8o1QdYrPhjhR8id/bYvWAzAnY5xqFxiZKiMDRvXpR+NCUT/lS+9sXL5r0O6PAUbhBqWHq3nBnvvM1Ae134HZ24IwzDCg= 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=s9gcvNa9; 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-dc6b26ce0bbso9048506276.1 for ; Tue, 27 Feb 2024 14:02:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709071336; x=1709676136; 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=vCV6R+NKRLMYoVC59mA+bYuzdEe0EtrTOitlrlnnMsA=; b=s9gcvNa9lW/PIzWihQTBqJP0VhTkZpQ2bDTsbEkPcpPLqGv6NTqfIF9gX+jC0MYySl osPkLoPBNizf3K41BZSSLigRaSaGT1Qgn9qYm59CQODGL5LG0zYf6iSoQuyc1c5XWaTT TGaQukHZ1EJmX6+JatByB8EPKyCCGxF/6W4NW2krDMsZyfSS0Puzq9ziNCM9koeX4+bd a3m6oPYQDeFKyHAykaE2jJN3Ub+EOmpJqxC+j5xjSvgVUIi+U8m0pv6MByvY5wiwAWxH thikawX9Nj/Nm83tyMYSPhDXLrrW1TrwDhQ0TjXJs84drugGC6t1l/sekw811eMQguc/ ZjEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709071336; x=1709676136; 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=vCV6R+NKRLMYoVC59mA+bYuzdEe0EtrTOitlrlnnMsA=; b=TuCGVLZGBMMjIzrwm5aTz8fvWfUzCGFDtb1tkLAQlyNJ9RgUitKxkxEFzhZ0zj8Zjb sW5hmNOJFx4PntZBuNqKFTSdg4G1TNDjFxqoGty2rjjo5gizHGiaizj+PJrMD8cLInjK C610vH0gbGU8cOxhPlZCjBbnbbdXCx5g3hVSejI2zVy1C7liUcpASJf4z40MiXVvUra8 nYETtHvchs1eL2ZjAJHO49SrG4TzA/eqnYL4qDMrdsu9+P0cWA8AjHQZGdlt26uTGJgI DDnvTq63LoQ2pVO0H2bxtGhUNJxVdBA76kQwZ8HceZm4P2S7LeZLsFndPOwx3MaAos/E RaSQ== X-Forwarded-Encrypted: i=1; AJvYcCXWGOFdr6iIIcn9SkUvWRCkzR7cn4SYo/p7nEDmG1QouUEADxUQMSPCqCLTEKIYKM1LKjekI34vBA85UpmrU1yniVp7/jNqO1zto6u3 X-Gm-Message-State: AOJu0YwXcQaVN8i6kAmF3TsrYyukjn3Pgnf8Nyy18E+r3cfU3k4uYtzC +xuOjoISYUNIfHiUAJVJ3UCoDrn9ty/PimqSK5NhcPYO5L3uJ963OqZS15mm+zM1BIK/k2PIW3j yO9ECGA== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:4ff1:8af6:9e1a:6382]) (user=irogers job=sendgmr) by 2002:a05:6902:1204:b0:dc6:cd85:bcd7 with SMTP id s4-20020a056902120400b00dc6cd85bcd7mr220673ybu.3.1709071335967; Tue, 27 Feb 2024 14:02:15 -0800 (PST) Date: Tue, 27 Feb 2024 14:01:50 -0800 In-Reply-To: <20240227220150.3876198-1-irogers@google.com> Message-Id: <20240227220150.3876198-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240227220150.3876198-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Subject: [PATCH v2 6/6] perf threads: Reduce table size from 256 to 8 From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792091301859650774 X-GMAIL-MSGID: 1792091301859650774 The threads data structure is an array of hashmaps, previously rbtrees. The two levels allows for a fixed outer array where access is guarded by rw_semaphores. Commit 91e467bc568f ("perf machine: Use hashtable for machine threads") sized the outer table at 256 entries to avoid future scalability problems, however, this means the threads struct is sized at 30,720 bytes. As the hashmaps allow O(1) access for the common find/insert/remove operations, lower the number of entries to 8. This reduces the size overhead to 960 bytes. Signed-off-by: Ian Rogers --- tools/perf/util/threads.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index d03bd91a7769..da68d2223f18 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -7,7 +7,7 @@ struct thread; -#define THREADS__TABLE_BITS 8 +#define THREADS__TABLE_BITS 3 #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) struct threads_table_entry {