From nobody Sat Feb 7 11:05:08 2026 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 02FC511CAD for ; Wed, 14 Feb 2024 06:37:24 +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=1707892645; cv=none; b=s4Un6K6wHiSYI9K0c1XwBL1uM6esAFYdfodt0fjXvu6LwpaSe/4BJmnNLe9WNkCw6QRBl/fHGX/B8obFTkp5YW/wQ17uOO6C8iPZrYLAPZ06uKTf1p/3UGrZaXb5BKM4G5q9XJ3ndul5g5gN1Z9ysFPcAT30ZgfS4RC7zj+uZX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892645; c=relaxed/simple; bh=Xx+uvA43OduUIzjEu9tCr6C0ALQ8vcobIRZz2sQGsbI=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=p6Xz/bilSQvCIW8krviq+LDuEdTThxRxOsm7XkFGL9kvMcPFhL4w2lSOdUb7WKm1Jcthdd9bw3REQjGHL595e4u865cpbHbsTwz38VQ+YTZj5XqhtWR1o8Md4FX/aE5oYw9etGeYD4vgQNs6baxaV4lgXoapMqQ+62LiXfRGSWs= 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=ClilPw9h; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ClilPw9h" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-604832bcbd0so112577597b3.0 for ; Tue, 13 Feb 2024 22:37:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892643; x=1708497443; 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=rEAosGoN+Ps3V+fA/yuGNTVNjMqltIfkWiaUJr2st/A=; b=ClilPw9hbFO593oHaU0wDtjSBoKVcMmvDpvrWxkRA3nOYUg1hXiujiJb1nPVxWB5Qp Evl5IZixneiRGnjvHZuQNCraHio8eSLcNj8hsyZQLfxTi7G/A10GP4pMfz0GmeZYvCuG hXB70GbETPk4YbwCNtqe1DPIzoHNpaO3Vv0RNzgo3f5OU9nXRROwuxwG/pp/oIZ/REDB cO01HpIpvfQ1AeQ9av7htdGLfWcikiP3+HQHZYLDL7nUBtDx5j47++7YpUFpy5gaN9kb RB1AJ/d5wkkGJXWM4D34UJSnVgp0UKHW0jgfYgWeBdCTI3dvVxg/EyJOh3+KjVgLZCOg DOmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892643; x=1708497443; 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=rEAosGoN+Ps3V+fA/yuGNTVNjMqltIfkWiaUJr2st/A=; b=DItQGomUc4ZPYoK9iiSrqHjNKQ0O1OxguyqHf6vMvy9UVvcnrgKp1wd78K0+s3EZJ0 DWnQHcykaFeQBp6OgTqryM/i+zDSvkKjK9HnOkZLadPe26FoahKfEHi6FM63eUHZ7WSS DzdJJgzZ56L/qVjY2hQvynCTsI+003VchiM4070tpxqm8O/xUqY1//BxVf4urNie6qvQ MLVN6CFByv1hATi3NLzthXrSEmNZDlfgMA9E79rkZQiIAqv9vT9UNzIOMHtI9Ks8l55x c1LbRFG6xpS9TUU66g9pRVAazD+znow6b85RdBURGuqT55KJCn73EI9qZ2EeGYJEArBg NviQ== X-Forwarded-Encrypted: i=1; AJvYcCXYZ/Bd78iP0M7q0iQ3UdcKO37hplW6qM5uWcQcWsRgSaxzqQ/B7PzB/EHjO5cGZZXOGYxuP/cF9xQ6tSMN8zX053Kl5UuFv2JapqG3 X-Gm-Message-State: AOJu0YxTqWaRkkffsUMJ6OeJNT8fDEoXF4QvLND1yBwo/xaD9jO2OajF 7PVxHpzDitpEx9KEeb23eXS57+KOjv3jD6sRzvrnFmPwLWni/awOL4zwH7wPhJGxLeI/MaioKcW MBRTs/g== X-Google-Smtp-Source: AGHT+IEwDICX3tyoH43C3xaWXWn//YFpEz0C0PMp0ITUlqRXonJFY+wXhJ71KokqtkN0Gf1BRvfCgvPrvq1U X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a81:4fce:0:b0:604:9315:b547 with SMTP id d197-20020a814fce000000b006049315b547mr393295ywb.5.1707892643199; Tue, 13 Feb 2024 22:37:23 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:03 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 1/6] perf report: Sort child tasks by tid From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 91e467bc568f ("perf machine: Use hashtable for machine threads") made the iteration of thread tids unordered. The perf report --tasks output now shows child threads in an order determined by the hashing. For example, in this snippet tid 3 appears after tid 256 even though they have the same ppid 2: ``` $ perf report --tasks % pid tid ppid comm 0 0 -1 |swapper 2 2 0 | kthreadd 256 256 2 | kworker/12:1H-k 693761 693761 2 | kworker/10:1-mm 1301762 1301762 2 | kworker/1:1-mm_ 1302530 1302530 2 | kworker/u32:0-k 3 3 2 | rcu_gp ... ``` The output is easier to read if threads appear numerically increasing. To allow for this, read all threads into a list then sort with a comparator that orders by the child task's of the first common parent. The list creation and deletion are created as utilities on machine. The indentation is possible by counting the number of parents a child has. With this change the output for the same data file is now like: ``` $ perf report --tasks % pid tid ppid comm 0 0 -1 |swapper 1 1 0 | systemd 823 823 1 | systemd-journal 853 853 1 | systemd-udevd 3230 3230 1 | systemd-timesyn 3236 3236 1 | auditd 3239 3239 3236 | audisp-syslog 3321 3321 1 | accounts-daemon ... ``` Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 203 ++++++++++++++++++++---------------- tools/perf/util/machine.c | 30 ++++++ tools/perf/util/machine.h | 10 ++ 3 files changed, 155 insertions(+), 88 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 8e16fa261e6f..b48f1d5309e3 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -59,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -828,35 +829,6 @@ static void tasks_setup(struct report *rep) rep->tool.no_warn =3D true; } =20 -struct task { - struct thread *thread; - struct list_head list; - struct list_head children; -}; - -static struct task *tasks_list(struct task *task, struct machine *machine) -{ - struct thread *parent_thread, *thread =3D task->thread; - struct task *parent_task; - - /* Already listed. */ - if (!list_empty(&task->list)) - return NULL; - - /* Last one in the chain. */ - if (thread__ppid(thread) =3D=3D -1) - return task; - - parent_thread =3D machine__find_thread(machine, -1, thread__ppid(thread)); - if (!parent_thread) - return ERR_PTR(-ENOENT); - - parent_task =3D thread__priv(parent_thread); - thread__put(parent_thread); - list_add_tail(&task->list, &parent_task->children); - return tasks_list(parent_task, machine); -} - struct maps__fprintf_task_args { int indent; FILE *fp; @@ -900,89 +872,144 @@ static size_t maps__fprintf_task(struct maps *maps, = int indent, FILE *fp) return args.printed; } =20 -static void task__print_level(struct task *task, FILE *fp, int level) +static int thread_level(struct machine *machine, const struct thread *thre= ad) { - struct thread *thread =3D task->thread; - struct task *child; - int comm_indent =3D fprintf(fp, " %8d %8d %8d |%*s", - thread__pid(thread), thread__tid(thread), - thread__ppid(thread), level, ""); + struct thread *parent_thread; + int res; =20 - fprintf(fp, "%s\n", thread__comm_str(thread)); + if (thread__tid(thread) <=3D 0) + return 0; =20 - maps__fprintf_task(thread__maps(thread), comm_indent, fp); + if (thread__ppid(thread) <=3D 0) + return 1; =20 - if (!list_empty(&task->children)) { - list_for_each_entry(child, &task->children, list) - task__print_level(child, fp, level + 1); + parent_thread =3D machine__find_thread(machine, -1, thread__ppid(thread)); + if (!parent_thread) { + pr_err("Missing parent thread of %d\n", thread__tid(thread)); + return 0; } + res =3D 1 + thread_level(machine, parent_thread); + thread__put(parent_thread); + return res; } =20 -static int tasks_print(struct report *rep, FILE *fp) +static void task__print_level(struct machine *machine, struct thread *thre= ad, FILE *fp) { - struct perf_session *session =3D rep->session; - struct machine *machine =3D &session->machines.host; - struct task *tasks, *task; - unsigned int nr =3D 0, itask =3D 0, i; - struct rb_node *nd; - LIST_HEAD(list); + int level =3D thread_level(machine, thread); + int comm_indent =3D fprintf(fp, " %8d %8d %8d |%*s", + thread__pid(thread), thread__tid(thread), + thread__ppid(thread), level, ""); =20 - /* - * No locking needed while accessing machine->threads, - * because --tasks is single threaded command. - */ + fprintf(fp, "%s\n", thread__comm_str(thread)); =20 - /* Count all the threads. */ - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) - nr +=3D machine->threads[i].nr; + maps__fprintf_task(thread__maps(thread), comm_indent, fp); +} =20 - tasks =3D malloc(sizeof(*tasks) * nr); - if (!tasks) - return -ENOMEM; +static int task_list_cmp(void *priv, const struct list_head *la, const str= uct list_head *lb) +{ + struct machine *machine =3D priv; + struct thread_list *task_a =3D list_entry(la, struct thread_list, list); + struct thread_list *task_b =3D list_entry(lb, struct thread_list, list); + struct thread *a =3D task_a->thread; + struct thread *b =3D task_b->thread; + int level_a, level_b, res; + + /* Compare a and b to root. */ + if (thread__tid(a) =3D=3D thread__tid(b)) + return 0; =20 - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; + if (thread__tid(a) =3D=3D 0) + return -1; =20 - for (nd =3D rb_first_cached(&threads->entries); nd; - nd =3D rb_next(nd)) { - task =3D tasks + itask++; + if (thread__tid(b) =3D=3D 0) + return 1; =20 - task->thread =3D rb_entry(nd, struct thread_rb_node, rb_node)->thread; - INIT_LIST_HEAD(&task->children); - INIT_LIST_HEAD(&task->list); - thread__set_priv(task->thread, task); - } + /* If parents match sort by tid. */ + if (thread__ppid(a) =3D=3D thread__ppid(b)) { + return thread__tid(a) < thread__tid(b) + ? -1 + : (thread__tid(a) > thread__tid(b) ? 1 : 0); } =20 /* - * Iterate every task down to the unprocessed parent - * and link all in task children list. Task with no - * parent is added into 'list'. + * Find a and b such that if they are a child of each other a and b's + * tid's match, otherwise a and b have a common parent and distinct + * tid's to sort by. First make the depths of the threads match. */ - for (itask =3D 0; itask < nr; itask++) { - task =3D tasks + itask; - - if (!list_empty(&task->list)) - continue; - - task =3D tasks_list(task, machine); - if (IS_ERR(task)) { - pr_err("Error: failed to process tasks\n"); - free(tasks); - return PTR_ERR(task); + level_a =3D thread_level(machine, a); + level_b =3D thread_level(machine, b); + a =3D thread__get(a); + b =3D thread__get(b); + for (int i =3D level_a; i > level_b; i--) { + struct thread *parent =3D machine__find_thread(machine, -1, thread__ppid= (a)); + + thread__put(a); + if (!parent) { + pr_err("Missing parent thread of %d\n", thread__tid(a)); + thread__put(b); + return -1; } + a =3D parent; + } + for (int i =3D level_b; i > level_a; i--) { + struct thread *parent =3D machine__find_thread(machine, -1, thread__ppid= (b)); =20 - if (task) - list_add_tail(&task->list, &list); + thread__put(b); + if (!parent) { + pr_err("Missing parent thread of %d\n", thread__tid(b)); + thread__put(a); + return 1; + } + b =3D parent; + } + /* Search up to a common parent. */ + while (thread__ppid(a) !=3D thread__ppid(b)) { + struct thread *parent; + + parent =3D machine__find_thread(machine, -1, thread__ppid(a)); + thread__put(a); + if (!parent) + pr_err("Missing parent thread of %d\n", thread__tid(a)); + a =3D parent; + parent =3D machine__find_thread(machine, -1, thread__ppid(b)); + thread__put(b); + if (!parent) + pr_err("Missing parent thread of %d\n", thread__tid(b)); + b =3D parent; + if (!a || !b) + return !a && !b ? 0 : (!a ? -1 : 1); + } + if (thread__tid(a) =3D=3D thread__tid(b)) { + /* a is a child of b or vice-versa, deeper levels appear later. */ + res =3D level_a < level_b ? -1 : (level_a > level_b ? 1 : 0); + } else { + /* Sort by tid now the parent is the same. */ + res =3D thread__tid(a) < thread__tid(b) ? -1 : 1; } + thread__put(a); + thread__put(b); + return res; +} + +static int tasks_print(struct report *rep, FILE *fp) +{ + struct machine *machine =3D &rep->session->machines.host; + LIST_HEAD(tasks); + int ret; =20 - fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); + ret =3D machine__thread_list(machine, &tasks); + if (!ret) { + struct thread_list *task; =20 - list_for_each_entry(task, &list, list) - task__print_level(task, fp, 0); + list_sort(machine, &tasks, task_list_cmp); =20 - free(tasks); - return 0; + fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); + + list_for_each_entry(task, &tasks, list) + task__print_level(machine, task->thread, fp); + } + thread_list__delete(&tasks); + return ret; } =20 static int __cmd_report(struct report *rep) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 3da92f18814a..7872ce92c9fc 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -3261,6 +3261,36 @@ int machines__for_each_thread(struct machines *machi= nes, return rc; } =20 + +static int thread_list_cb(struct thread *thread, void *data) +{ + struct list_head *list =3D data; + struct thread_list *entry =3D malloc(sizeof(*entry)); + + if (!entry) + return -ENOMEM; + + entry->thread =3D thread__get(thread); + list_add_tail(&entry->list, list); + return 0; +} + +int machine__thread_list(struct machine *machine, struct list_head *list) +{ + return machine__for_each_thread(machine, thread_list_cb, list); +} + +void thread_list__delete(struct list_head *list) +{ + struct thread_list *pos, *next; + + list_for_each_entry_safe(pos, next, list, list) { + thread__zput(pos->thread); + list_del(&pos->list); + free(pos); + } +} + pid_t machine__get_current_tid(struct machine *machine, int cpu) { if (cpu < 0 || (size_t)cpu >=3D machine->current_tid_sz) diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 1279acda6a8a..b738ce84817b 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -280,6 +280,16 @@ int machines__for_each_thread(struct machines *machine= s, int (*fn)(struct thread *thread, void *p), void *priv); =20 +struct thread_list { + struct list_head list; + struct thread *thread; +}; + +/* Make a list of struct thread_list based on threads in the machine. */ +int machine__thread_list(struct machine *machine, struct list_head *list); +/* Free up the nodes within the thread_list list. */ +void thread_list__delete(struct list_head *list); + pid_t machine__get_current_tid(struct machine *machine, int cpu); int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, pid_t tid); --=20 2.43.0.687.g38aa6559b0-goog From nobody Sat Feb 7 11:05:08 2026 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 ; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y1ONCkJ2" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc4563611cso2980463276.3 for ; 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-Google-Smtp-Source: AGHT+IGvJVS96aABdEOS9tS4w8Q4cso9jIVj1Kv46sheeCV1N/KHtABTU2xUrbdiKuY6fwIfh+zW0nqes89z 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: List-Subscribe: List-Unsubscribe: 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 To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 threa= d_trace *ttrace) return ttrace ? ttrace->nr_events : 0; } =20 -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 =3D rb_entry(nd, struct thread_rb_node, rb_node)->thread; + struct thread_list *a =3D list_entry(la, struct thread_list, list); + struct thread_list *b =3D list_entry(lb, struct thread_list, list); + unsigned long a_nr_events =3D thread__nr_events(thread__priv(a->thread)); + unsigned long b_nr_events =3D thread__nr_events(thread__priv(b->thread)); + + if (a_nr_events !=3D 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); } =20 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) { size_t printed =3D trace__fprintf_threads_header(fp); - struct rb_node *nd; - int i; - - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); + LIST_HEAD(threads); =20 - if (threads =3D=3D NULL) { - fprintf(fp, "%s", "Error sorting output by nr_events!\n"); - return 0; - } + if (machine__thread_list(trace->host, &threads) =3D=3D 0) { + struct thread_list *pos; =20 - resort_rb__for_each_entry(nd, threads) - printed +=3D trace__fprintf_thread(fp, threads_entry->thread, trace); + list_sort(NULL, &threads, trace_nr_events_cmp); =20 - resort_rb__delete(threads); + list_for_each_entry(pos, &threads, list) + printed +=3D trace__fprintf_thread(fp, pos->thread, trace); } + thread_list__delete(&threads); return printed; } =20 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 =3D __name##_sorted__new DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \ __ilist->rblist.nr_entries) =20 -/* For 'struct machine->threads' */ -#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) = \ - DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_roo= t, \ - __machine->threads[hash_bucket].nr) - #endif /* _PERF_RESORT_RB_H_ */ --=20 2.43.0.687.g38aa6559b0-goog From nobody Sat Feb 7 11:05:08 2026 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 A729F12B8C for ; Wed, 14 Feb 2024 06:37:28 +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=1707892650; cv=none; b=cK/QtSw+81i40Fm9KI8NdBuIjL+K+JEYpa+z5lg5F2eWbCekEZNHmstL4v9dE4WZxFNnsf1TAF+wsiOoWuUm0febJPzfRwNA6gt/MoaTo9S42UpxaO5ruis9A9AZUTprriQoa9Cr4HY977ep/xPDQFPRaBhpwqpLVbkcrIJiZyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892650; c=relaxed/simple; bh=gi4txDdv0C9NQjjEzGpOMktXyzhWLm0PbyoiCg8lenM=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=CqttRdZG6qt2sAhVtOFvXLkaapbJI6RHlV+3L6u2tYLD+LUU7/tGlbYWqQABSAFhm/UAk+v/5YLge5d+SpA81LceybQFyw2Dr1xxh0QGSQ+dxYjJmoOIBRl65hD5Q/zHeyM7GiJGXVDTQlkzVa+k0Pzjz/BcbeP47TowPQgp2nA= 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=ESIxsA0b; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ESIxsA0b" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5f38d676cecso7062267b3.0 for ; Tue, 13 Feb 2024 22:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892648; x=1708497448; 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=CANQqurHGmF5Ru8GYcuqg+/9JMey73Sl/dxU7/wCOGk=; b=ESIxsA0biR3o1+9gLHaK9lU9NuyJ3HJw8dfsMYdRxzdEbvKJSKymj7wfdcGS6+pCnt 7uJr4q1gF9qcVGUd80IbaPzap8aTLBguMdLYiZMCgezMl9uN1P80Zzi2f90lhR6D3dJs v5Dwd3nT6LFpctpm2qV+bUkLMvp4gGjwsFraKvZ331HeaP2vCpzdEuqxIZ2a9iQfHs5z 79JnYSpUGAeKYxGGclHdY5F58gJYvYUKA+fDDZkhPwYfnEQoiXBBTr+lyc50wVdkdPGd K5ObK6hZZ1dEVMMvgpzZFE330LSO+AO7ht0FWy25dbS5aoqm96sxb/7Ix54W6f1mu/AM iCTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892648; x=1708497448; 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=CANQqurHGmF5Ru8GYcuqg+/9JMey73Sl/dxU7/wCOGk=; b=veOzy1zQ7A6KIZpO21eYDOUaeX+JHZKGZphInxzr1lAstX9qh9xKlpq1690OX1RVkp VPZaNOPsjnP+baAYwbro2dMEJB9e9IHSYhCjDFjmp/djk+jhUn5wANRIKvxAtwUuOaJZ mqHjST7tKxSkV7PJNTQZZ9YXQdmvlbqsDMcMaMkKefO+4G1RfgussJHAkTV8g9161pbg COHSjW54jgHrNgAvW/R8KDzP2UGzTpdt1hetDBZTos8Kt3A/KqOLcwPauWTMyRFh3Od9 87VeDzuBSUkkoBKs0Bi7XWMttMhTgvNLWMyeRBNYRHGBdmtwFnJDrGu1+XCO00eRPUlA RfBQ== X-Forwarded-Encrypted: i=1; AJvYcCWhFuHyb6q9kTIal1Kkud/YxgOZlzbbFhTyn0gVTYzXRz5a8/qEPaUx2rJIaOu7PvJKB3Jx/No34f2lccex6f1b+dp8at+UrB3qAxHS X-Gm-Message-State: AOJu0YxlLEZYOnskHVoE0T6nwABrUMJDeCtbV4nk0mtD/pvey9Fxfe1J jUMo8lY2am1lhzuaCYt2HdoBzHVXko02ESUOS1slJwm1KIXapxRAXRHycmEaxIU77nbUxLY0q9B LG6WDag== X-Google-Smtp-Source: AGHT+IHGxTR+p0CnuoOK+VdU0lHFhbRYRprRzVqsZvfUFCN+ScY3VDeSQ24bNsXgrrxVtSJ52mhmdBCQBk5J X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a81:4fc6:0:b0:604:125:f0cf with SMTP id d189-20020a814fc6000000b006040125f0cfmr232032ywb.2.1707892647799; Tue, 13 Feb 2024 22:37:27 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:05 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-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: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 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 , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; } =20 -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 =3D data; =20 - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; + /* TODO: handle fprintf errors. */ + args->printed +=3D thread__fprintf(thread, args->fp); + return 0; +} =20 - down_read(&threads->lock); +static size_t machine__threads_nr(const struct machine *machine) +{ + size_t nr =3D 0; =20 - ret =3D fprintf(fp, "Threads: %u\n", threads->nr); + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) + nr +=3D machine->threads[i].nr; =20 - for (nd =3D rb_first_cached(&threads->entries); nd; - nd =3D rb_next(nd)) { - struct thread *pos =3D rb_entry(nd, struct thread_rb_node, rb_node)->th= read; + return nr; +} =20 - ret +=3D thread__fprintf(pos, fp); - } +size_t machine__fprintf(struct machine *machine, FILE *fp) +{ + struct machine_fprintf_cb_args args =3D { + .fp =3D fp, + .printed =3D 0, + }; + size_t ret =3D fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)= ); =20 - up_read(&threads->lock); - } - return ret; + machine__for_each_thread(machine, machine_fprintf_cb, &args); + return ret + args.printed; } =20 static struct dso *machine__get_kernel(struct machine *machine) --=20 2.43.0.687.g38aa6559b0-goog From nobody Sat Feb 7 11:05:08 2026 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 7D66912E45 for ; Wed, 14 Feb 2024 06:37:30 +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=1707892653; cv=none; b=Mkr23uCgrwn70uErDMYGykBFfFu58SWPn4u/3JdtL4MlmGrVY2Hyproz1SpBCvjXIkr2MXiz0otxImZzjsXprqbQ63eLRDZBwWAQE555nfdvLbP6GUrPKFDv8bIePeaOcHX9NoQ3+0rGR/QlVzLcESyGuYE3k4fe3ILRH0faJsg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892653; c=relaxed/simple; bh=1q0Mzi1wXTH6Sb7R245+2O29oMhVzCi1GuhedrH/vXs=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=qrLGpAUox2zC2T2BuZcMF0jiqRr0tyHjpXiaXvlq0r6rCZm6k4k4FXCDDob0KNghIpSAeQgzg2C/sL91DGeJyy9ForhoV5+1JtW9ay7U9GKGqfU19m1SewmWNyW0dm7cHEo64qfvmw7Rpqj/6bSZ8k4vV6Mk4T4SVcw4d1PEuUw= 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=yrvA3GA0; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yrvA3GA0" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60665b5fabcso26920187b3.1 for ; Tue, 13 Feb 2024 22:37:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892650; x=1708497450; 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=Tle2JxgpvmKYL+8R8E/xIGik1x6V5IcyiIcZZhaAW8s=; b=yrvA3GA0Scp9f5c60hV93yIpQR6KsMW607qo9bGrcbztUbTEzlbO47qKsXYFziLHAp ZQWEAFOESxiEtwcV9K4K0RpTcO9ylHm8e9vF96fJnHM2QUQ3G0xi6/pB/oM9T8NFD5J9 qM6rkxcnmvYhjOIKwEKuYsP2jNIHICJcGFjQU+l9ffv4GtO3h5p/wtCAWxVj7B9bHpph N7WndFSPDriM8HdD8Tizir2HpinqznU/eqAck2MFypoIYSSEHUtC7Us+4MTlFIqI8X3i nX/ppIyTTqQOZ2w60enTv5gQqdwfJL8RCzRn30mD2Lxq3zx1W50DBI+zZ7c9t66IGczM T6Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892650; x=1708497450; 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=Tle2JxgpvmKYL+8R8E/xIGik1x6V5IcyiIcZZhaAW8s=; b=rMwnautCLWRZ41v2p2xfIQwPoXkPD2uosY1Rvpr9dzmSAsjFzThv0HnNROKE3fQ20f J4pkPTIWfKOxG9I0NTMYFJJ2+8MC+xwg1U9Nz5bdAuDb8/AZXy/0pfIszxbhAQ0jZD93 wvfiFg9Mf3eJ8+fRCbFNDwor4oOQfJiNKLFqMA14OT8ljlZIQapaD+MM6tNPEJJK6SWl JnddMVS9biAGiD/vt/yWVFdBG0zBJUixN3CbifpIFXHHO2MkDFJb8NsW8wDbnJOv5u6g xcTKKi6M9h3URFPXv6RnyOaV/vtuGJO8DZkmSGxlaEf8/ne3+dQI8odeboc6RoxVhJM9 kBBg== X-Forwarded-Encrypted: i=1; AJvYcCVadrk5rw9/5RV8Z9INYYFi4FRg5EwR0mXa2oS2wicPDjdCiwsegndyhm2tjzbFCTegRsNuHxuk4g7qKZdL85F2gxpu2+F5eCE6i2OU X-Gm-Message-State: AOJu0YyZYpceUF3Yn25S1+g11rerdw4XDKF/SXvSKG3nkFA9JDr0/yCg vfEeSJwvNnRgflZIyRsC9/jLOAwTiKszOUDdlaQrOZ8pK7AjpkF58g+LR/dZcnmyB8hedw+iMii 6PjUd3w== X-Google-Smtp-Source: AGHT+IHWlPmjxZVK4i1rc9v9CmkNmI9qVp5QQwf++9HP5qrq2xyr0S6NUUqBIQ20E5Iky4QsB1yQo/vPhPwa X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a0d:d784:0:b0:5e4:bb84:d171 with SMTP id z126-20020a0dd784000000b005e4bb84d171mr275047ywd.9.1707892650051; Tue, 13 Feb 2024 22:37:30 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:06 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-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: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 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 , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Locking discipline is tightened up in this change. 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 8027f450fa3e..a0e8cd68d490 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -71,6 +71,7 @@ perf-y +=3D ordered-events.o perf-y +=3D namespaces.o perf-y +=3D comm.o perf-y +=3D thread.o +perf-y +=3D threads.o perf-y +=3D thread_map.o perf-y +=3D parse-events-flex.o perf-y +=3D parse-events-bison.o diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_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, =20 /* do not update idle comm which contains CPU number */ if (pid) { - struct thread *t =3D __machine__findnew_thread(machine, /*pid=3D*/-1, p= id); + struct thread *t =3D machine__findnew_thread(machine, /*pid=3D*/-1, pid= ); =20 if (t =3D=3D NULL) return name; @@ -302,9 +302,9 @@ int lock_contention_read(struct lock_contention *con) return -1; =20 if (con->aggr_mode =3D=3D LOCK_AGGR_TASK) { - struct thread *idle =3D __machine__findnew_thread(machine, - /*pid=3D*/0, - /*tid=3D*/0); + struct thread *idle =3D machine__findnew_thread(machine, + /*pid=3D*/0, + /*tid=3D*/0); thread__set_comm(idle, "swapper", /*timestamp=3D*/0); } =20 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 =20 -static void __machine__remove_thread(struct machine *machine, struct threa= d_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); } =20 -static void machine__threads_init(struct machine *machine) -{ - int i; - - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; - threads->entries =3D RB_ROOT_CACHED; - init_rwsem(&threads->lock); - threads->nr =3D 0; - threads->last_match =3D NULL; - } -} - -static int thread_rb_node__cmp_tid(const void *key, const struct rb_node *= nd) -{ - int to_find =3D (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 =3D thread__tid(th); - struct rb_node *nd =3D 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 *r= oot_dir, pid_t pid) RB_CLEAR_NODE(&machine->rb_node); dsos__init(&machine->dsos); =20 - machine__threads_init(machine); + threads__init(&machine->threads); =20 machine->vdso_info =3D NULL; machine->env =3D NULL; @@ -221,27 +189,11 @@ static void dsos__exit(struct dsos *dsos) =20 void machine__delete_threads(struct machine *machine) { - struct rb_node *nd; - int i; - - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; - down_write(&threads->lock); - nd =3D rb_first_cached(&threads->entries); - while (nd) { - struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); - - nd =3D rb_next(nd); - __machine__remove_thread(machine, trb, trb->thread, false); - } - up_write(&threads->lock); - } + threads__remove_all_threads(&machine->threads); } =20 void machine__exit(struct machine *machine) { - int i; - if (machine =3D=3D NULL) return; =20 @@ -254,12 +206,7 @@ void machine__exit(struct machine *machine) zfree(&machine->current_tid); zfree(&machine->kallsyms_filename); =20 - machine__delete_threads(machine); - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; - - exit_rwsem(&threads->lock); - } + threads__exit(&machine->threads); } =20 void machine__delete(struct machine *machine) @@ -526,7 +473,7 @@ static void machine__update_thread_pid(struct machine *= machine, if (thread__pid(th) =3D=3D thread__tid(th)) return; =20 - leader =3D __machine__findnew_thread(machine, thread__pid(th), thread__pi= d(th)); + leader =3D machine__findnew_thread(machine, thread__pid(th), thread__pid(= th)); if (!leader) goto out_err; =20 @@ -560,160 +507,55 @@ static void machine__update_thread_pid(struct machin= e *machine, goto out_put; } =20 -/* - * 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 =3D threads->last_match; - if (th !=3D NULL) { - if (thread__tid(th) =3D=3D tid) { - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - thread__put(threads->last_match); - threads->last_match =3D NULL; - } - - return NULL; -} - -static struct thread* -threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) -{ - struct thread *th =3D NULL; - - if (perf_singlethreaded) - th =3D __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 =3D 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 =3D &threads->entries.rb_root.rb_node; - struct rb_node *parent =3D NULL; - struct thread *th; - struct thread_rb_node *nd; - bool leftmost =3D true; + struct thread *th =3D threads__find(&machine->threads, tid); + bool created; =20 - th =3D threads__get_last_match(threads, machine, pid, tid); - if (th) + if (th) { + machine__update_thread_pid(machine, th, pid); return th; - - while (*p !=3D NULL) { - parent =3D *p; - th =3D rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) =3D=3D tid) { - threads__set_last_match(threads, th); - machine__update_thread_pid(machine, th, pid); - return thread__get(th); - } - - if (tid < thread__tid(th)) - p =3D &(*p)->rb_left; - else { - p =3D &(*p)->rb_right; - leftmost =3D false; - } } - if (!create) return NULL; =20 - th =3D thread__new(pid, tid); - if (th =3D=3D NULL) - return NULL; - - nd =3D malloc(sizeof(*nd)); - if (nd =3D=3D NULL) { - thread__put(th); - return NULL; - } - nd->thread =3D 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 =3D 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); =20 -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pi= d, pid_t tid) -{ - return ____machine__findnew_thread(machine, machine__threads(machine, tid= ), pid, tid, true); + return th; } =20 -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 =3D machine__threads(machine, tid); - struct thread *th; - - down_write(&threads->lock); - th =3D __machine__findnew_thread(machine, pid, tid); - up_write(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=3D*/true); } =20 -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, pi= d_t tid) { - struct threads *threads =3D machine__threads(machine, tid); - struct thread *th; - - down_read(&threads->lock); - th =3D ____machine__findnew_thread(machine, threads, pid, tid, false); - up_read(&threads->lock); - return th; + return __machine__findnew_thread(machine, pid, tid, /*create=3D*/false); } =20 /* @@ -1127,23 +969,13 @@ static int machine_fprintf_cb(struct thread *thread,= void *data) return 0; } =20 -static size_t machine__threads_nr(const struct machine *machine) -{ - size_t nr =3D 0; - - for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) - nr +=3D machine->threads[i].nr; - - return nr; -} - size_t machine__fprintf(struct machine *machine, FILE *fp) { struct machine_fprintf_cb_args args =3D { .fp =3D fp, .printed =3D 0, }; - size_t ret =3D fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)= ); + size_t ret =3D fprintf(fp, "Threads: %zu\n", threads__nr(&machine->thread= s)); =20 machine__for_each_thread(machine, machine_fprintf_cb, &args); return ret + args.printed; @@ -2069,36 +1901,9 @@ int machine__process_mmap_event(struct machine *mach= ine, union perf_event *event return 0; } =20 -static void __machine__remove_thread(struct machine *machine, struct threa= d_rb_node *nd, - struct thread *th, bool lock) -{ - struct threads *threads =3D machine__threads(machine, thread__tid(th)); - - if (!nd) - nd =3D 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)) =3D=3D 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); } =20 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 =3D 0; - int i; - - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - threads =3D &machine->threads[i]; - for (nd =3D rb_first_cached(&threads->entries); nd; - nd =3D rb_next(nd)) { - struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); - - rc =3D fn(trb->thread, priv); - if (rc !=3D 0) - return rc; - } - } - return rc; + return threads__for_each_thread(&machine->threads, fn, priv); } =20 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" =20 struct addr_location; struct branch_stack; @@ -28,16 +29,6 @@ extern const char *ref_reloc_sym_names[]; =20 struct vdso_info; =20 -#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; }; =20 -static inline struct threads *machine__threads(struct machine *machine, pi= d_t tid) -{ - /* Cast it to handle tid =3D=3D -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 *a= rch); bool machine__normalized_is(struct machine *machine, const char *arch); int machine__nr_cpus_avail(struct machine *machine); =20 -struct thread *__machine__findnew_thread(struct machine *machine, pid_t pi= d, pid_t tid); struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,= pid_t tid); =20 struct dso *machine__findnew_dso_id(struct machine *machine, const char *f= ilename, 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 machi= ne *machine) if (pid =3D=3D thread__tid(thread) || pid =3D=3D -1) { thread__set_maps(thread, maps__new(machine)); } else { - struct thread *leader =3D __machine__findnew_thread(machine, pid, pid); + struct thread *leader =3D machine__findnew_thread(machine, pid, pid); =20 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 0df775b5c110..4b8f3e9e513b 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -3,7 +3,6 @@ #define __PERF_THREAD_H =20 #include -#include #include #include #include @@ -30,11 +29,6 @@ struct lbr_stitch { struct callchain_cursor_node *prev_lbr_cursor; }; =20 -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 =3D=3D -1 */ + return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; +} + +void threads__init(struct threads *threads) +{ + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + + table->entries =3D RB_ROOT_CACHED; + init_rwsem(&table->lock); + table->nr =3D 0; + table->last_match =3D NULL; + } +} + +void threads__exit(struct threads *threads) +{ + threads__remove_all_threads(threads); + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + + exit_rwsem(&table->lock); + } +} + +size_t threads__nr(struct threads *threads) +{ + size_t nr =3D 0; + + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + + down_read(&table->lock); + nr +=3D 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 =3D NULL; + + th =3D table->last_match; + if (th !=3D NULL) { + if (thread__tid(th) =3D=3D tid) + res =3D thread__get(th); + } + return res; +} + +static void __threads_table_entry__set_last_match(struct threads_table_ent= ry *table, + struct thread *th) +{ + thread__put(table->last_match); + table->last_match =3D 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 =3D threads__table(threads, tid); + struct rb_node **p; + struct thread *res =3D NULL; + + down_read(&table->lock); + res =3D __threads_table_entry__get_last_match(table, tid); + if (res) + return res; + + p =3D &table->entries.rb_root.rb_node; + while (*p !=3D NULL) { + struct rb_node *parent =3D *p; + struct thread *th =3D rb_entry(parent, struct thread_rb_node, rb_node)->= thread; + + if (thread__tid(th) =3D=3D tid) { + res =3D thread__get(th); + break; + } + + if (tid < thread__tid(th)) + p =3D &(*p)->rb_left; + else + p =3D &(*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 =3D threads__table(threads, tid); + struct rb_node **p; + struct rb_node *parent =3D NULL; + struct thread *res =3D NULL; + struct thread_rb_node *nd; + bool leftmost =3D true; + + *created =3D false; + down_write(&table->lock); + p =3D &table->entries.rb_root.rb_node; + while (*p !=3D NULL) { + struct thread *th; + + parent =3D *p; + th =3D rb_entry(parent, struct thread_rb_node, rb_node)->thread; + + if (thread__tid(th) =3D=3D tid) { + __threads_table_entry__set_last_match(table, th); + res =3D thread__get(th); + goto out_unlock; + } + + if (tid < thread__tid(th)) + p =3D &(*p)->rb_left; + else { + leftmost =3D false; + p =3D &(*p)->rb_right; + } + } + nd =3D malloc(sizeof(*nd)); + if (nd =3D=3D NULL) + goto out_unlock; + res =3D thread__new(pid, tid); + if (!res) + free(nd); + else { + *created =3D true; + nd->thread =3D 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 =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + struct rb_node *nd; + + down_write(&table->lock); + __threads_table_entry__set_last_match(table, NULL); + nd =3D rb_first_cached(&table->entries); + while (nd) { + struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); + + nd =3D 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 =3D=3D 0); + up_write(&table->lock); + } +} + +void threads__remove(struct threads *threads, struct thread *thread) +{ + struct rb_node **p; + struct threads_table_entry *table =3D threads__table(threads, thread__ti= d(thread)); + pid_t tid =3D 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 =3D &table->entries.rb_root.rb_node; + while (*p !=3D NULL) { + struct rb_node *parent =3D *p; + struct thread_rb_node *nd =3D rb_entry(parent, struct thread_rb_node, rb= _node); + struct thread *th =3D 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 =3D &(*p)->rb_left; + else + p =3D &(*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 =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + struct rb_node *nd; + + for (nd =3D rb_first_cached(&table->entries); nd; nd =3D rb_next(nd)) { + struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); + int rc =3D fn(trb->thread, data); + + if (rc !=3D 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 */ --=20 2.43.0.687.g38aa6559b0-goog From nobody Sat Feb 7 11:05:08 2026 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 529F2171C4 for ; Wed, 14 Feb 2024 06:37:33 +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=1707892655; cv=none; b=fPXFLn97fBUs9AwAy17JgcSqhrLCyaj28EBKvD4Z3Lbu2BM/1rhRy8GVVMuSmGMk9RB6ZWjUj+eJqcGocTQee+pGP4fAPYiAjPwYtcb8moZCoF3UxMDw79hH6Wa7vpF4xSIKMJ47Caq57JBt975yRBNLPmUxQu55QOJV53OGsOQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892655; c=relaxed/simple; bh=792Y2/R9dEdTkzaXADVPj0kpCyae3uNsLmkPUNZ1lNw=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=sVsr6QjlHjGIGPIZhIHvLva1Mukvsn4G8GRKS25uzbb+2QX7PMFZ7KfUQVywlX6JodcP/u0nDLfpPDbUlDOfxAtT8lxZ6LMs0IcT3VHDFtQgdEFSOt3VtQle6rhS++eGcURv/NhGbFtwZ8ET2Cp95gHXXzRPe01eZozcsvAqo6U= 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=rp3SkL3/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rp3SkL3/" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b267bf11so6283239276.2 for ; Tue, 13 Feb 2024 22:37:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892652; x=1708497452; 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=LrKC1v0yM6h/zxJwGsay7MbtcGdPOfBPN7PRv/fJOBQ=; b=rp3SkL3/xSSt1zY20iFf/e4GQ96s+RaDR35qqdvVsy0n9EWSXetnweWaCtlhGcunU4 wHc7FY6eDvyHNddD+Pxa33AHggOshoMANnChEiIXzngTwXAKu0ft/g+a5EZ2bXypdCo7 Lrnh5peXo1Pg6ZAX7Cd/MIoltz3nchMgTfh+8i7kkQjd/0nVY2SgyV+sUbnPc6eJCPf6 l2+sG+0CFCfbUr5Dr0IxaPaylOAXIiF0uCkQy4u72i8/Zz4V6YvJEzUg9vPzcp5L0VF0 aOVzEqbkZLTIQqNoCp0Mrgi8yJJ+OmPXH4pM0GL6M+IiWbDdEz1BFb/5XXxhfdTNp4LD 843A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892652; x=1708497452; 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=LrKC1v0yM6h/zxJwGsay7MbtcGdPOfBPN7PRv/fJOBQ=; b=laIGAdgVUFizeK9jIJCetjIB5EIINsccmF2/Sn+JYJqG1VIpN8LInrTAwgeLhSqqBg WiE3tcNUAYBJy8oso2kaPH3ALRIUE2UNK8hFVlP320hTVjZscRhQXWUwHfwQl4H4hppN NWY4K+F+/QK4Kf8fyugZB/y7Um5VhS+U4U7Lg2+X6kuLKxTyue7T5xE5LKRcGp/2iRiK hLSsxWWVQbXJginHFEzik315sBZk++QEPDuBlA3XPmMOp42dWDXAMZh6RgqFLPUjyzsg xcQvu/4QGI+VcjJUqZzvx8StDv5Gnj1RWQ7CO3roJKaFeUknehj2G5xmwwpHjKLS2QhS EGEQ== X-Forwarded-Encrypted: i=1; AJvYcCUV3yDSOJeXnKQCsb6/vaS/gzkkdFVf2SMnk7+7cyRFivXS+XLdYGYcyBxUEze63SNLYJVug9+724bX/iuENa3dTTT3x6C6sNgUWUxG X-Gm-Message-State: AOJu0Yx2dMMkBG93bVM4LRUylPzEN0VYKA9NsGIVn0MkHDRb/rVJL6Bg nOh4jF6bi8mGOdr/cxQIaBxbGPuWsY4YJfnlnQopy2VprE4MKH/AE9YzFxDOoaVAWr77fUpnklx NZldkkA== X-Google-Smtp-Source: AGHT+IFxpm66soxGF2R35nActxrpJcf2dLNCJZEbxqNkNmnj4iP/+1JYyGr/baG/Lhmmy5+gV4jUbpR3jj9V X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:100f:b0:dc7:3189:4e75 with SMTP id w15-20020a056902100f00b00dc731894e75mr46963ybt.3.1707892652306; Tue, 13 Feb 2024 22:37:32 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:07 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-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: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 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 , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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" =20 -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 =3D=3D -1 */ return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE]; } =20 +static size_t key_hash(long key, void *ctx __maybe_unused) +{ + /* The table lookup removes low bit entropy, but this is just ignored her= e. */ + return key; +} + +static bool key_equal(long key1, long key2, void *ctx __maybe_unused) +{ + return key1 =3D=3D key2; +} + void threads__init(struct threads *threads) { for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table =3D &threads->table[i]; =20 - table->entries =3D RB_ROOT_CACHED; + hashmap__init(&table->shard, key_hash, key_equal, NULL); init_rwsem(&table->lock); - table->nr =3D 0; table->last_match =3D NULL; } } @@ -32,6 +37,7 @@ void threads__exit(struct threads *threads) for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table =3D &threads->table[i]; =20 + hashmap__clear(&table->shard); exit_rwsem(&table->lock); } } @@ -44,7 +50,7 @@ size_t threads__nr(struct threads *threads) struct threads_table_entry *table =3D &threads->table[i]; =20 down_read(&table->lock); - nr +=3D table->nr; + nr +=3D 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 =3D threads__table(threads, tid); - struct rb_node **p; - struct thread *res =3D NULL; + struct thread *res; =20 down_read(&table->lock); res =3D __threads_table_entry__get_last_match(table, tid); - if (res) - return res; - - p =3D &table->entries.rb_root.rb_node; - while (*p !=3D NULL) { - struct rb_node *parent =3D *p; - struct thread *th =3D rb_entry(parent, struct thread_rb_node, rb_node)->= thread; - - if (thread__tid(th) =3D=3D tid) { - res =3D thread__get(th); - break; - } - - if (tid < thread__tid(th)) - p =3D &(*p)->rb_left; - else - p =3D &(*p)->rb_right; + if (!res) { + if (hashmap__find(&table->shard, tid, &res)) + res =3D 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 =3D threads__table(threads, tid); - struct rb_node **p; - struct rb_node *parent =3D NULL; struct thread *res =3D NULL; - struct thread_rb_node *nd; - bool leftmost =3D true; =20 *created =3D false; down_write(&table->lock); - p =3D &table->entries.rb_root.rb_node; - while (*p !=3D NULL) { - struct thread *th; - - parent =3D *p; - th =3D rb_entry(parent, struct thread_rb_node, rb_node)->thread; - - if (thread__tid(th) =3D=3D tid) { - __threads_table_entry__set_last_match(table, th); - res =3D thread__get(th); - goto out_unlock; - } - - if (tid < thread__tid(th)) - p =3D &(*p)->rb_left; - else { - leftmost =3D false; - p =3D &(*p)->rb_right; - } - } - nd =3D malloc(sizeof(*nd)); - if (nd =3D=3D NULL) - goto out_unlock; res =3D thread__new(pid, tid); - if (!res) - free(nd); - else { - *created =3D true; - nd->thread =3D 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 =3D NULL; + if (hashmap__find(&table->shard, tid, &res)) + res =3D thread__get(res); + } else { + res =3D thread__get(res); + *created =3D 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 *thre= ads) { for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table =3D &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur, *tmp; + size_t bkt; =20 down_write(&table->lock); __threads_table_entry__set_last_match(table, NULL); - nd =3D rb_first_cached(&table->entries); - while (nd) { - struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); - - nd =3D 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; =20 - free(trb); + hashmap__delete(&table->shard, cur->key, /*old_key=3D*/NULL, &old_value= ); + thread__put(old_value); } - assert(table->nr =3D=3D 0); up_write(&table->lock); } } =20 void threads__remove(struct threads *threads, struct thread *thread) { - struct rb_node **p; struct threads_table_entry *table =3D threads__table(threads, thread__ti= d(thread)); - pid_t tid =3D thread__tid(thread); + struct thread *old_value; =20 down_write(&table->lock); if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) __threads_table_entry__set_last_match(table, NULL); =20 - p =3D &table->entries.rb_root.rb_node; - while (*p !=3D NULL) { - struct rb_node *parent =3D *p; - struct thread_rb_node *nd =3D rb_entry(parent, struct thread_rb_node, rb= _node); - struct thread *th =3D 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 =3D &(*p)->rb_left; - else - p =3D &(*p)->rb_right; - } + hashmap__delete(&table->shard, thread__tid(thread), /*old_key=3D*/NULL, &= old_value); + thread__put(old_value); up_write(&table->lock); } =20 @@ -229,11 +171,11 @@ int threads__for_each_thread(struct threads *threads, { for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table =3D &threads->table[i]; - struct rb_node *nd; + struct hashmap_entry *cur; + size_t bkt; =20 - for (nd =3D rb_first_cached(&table->entries); nd; nd =3D rb_next(nd)) { - struct thread_rb_node *trb =3D rb_entry(nd, struct thread_rb_node, rb_n= ode); - int rc =3D fn(trb->thread, data); + hashmap__for_each_entry((&table->shard), cur, bkt) { + int rc =3D fn((struct thread *)cur->pvalue, data); =20 if (rc !=3D 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 =20 -#include +#include "hashmap.h" #include "rwsem.h" =20 struct thread; @@ -11,9 +11,9 @@ struct thread; #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) =20 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; }; =20 --=20 2.43.0.687.g38aa6559b0-goog From nobody Sat Feb 7 11:05:08 2026 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 ADF2017585 for ; Wed, 14 Feb 2024 06:37:35 +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=1707892657; cv=none; b=BKuF3gtHj0kb1xCBW8bgaNat95locRyRuOIKx1FfftM87ypAbC9eOwqHc4usxTBu1LmHfrDAqQrPWm1ZekIlYXeehO3GJ/RXmTMNmdfOqpT3wEmc0PTJXC84Tvq+zw8Yp4tz44oGeWF8YCu1oNuNA9PEMRiTPfk0jrJWGox6noY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892657; c=relaxed/simple; bh=UV01xgO1m+X1+w1j0tsdOivqonwgpzUr5TVWVuyCS+c=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=dTsZGbShoOgkSc9KVU+npfrjLHvVwA/tX/fMR4ihSCfpj34zSkhraANbCc7w+KkpuMQRTjc25CHQEvOJSybXKjXhXFZUJyuwfas+XqeAEX4KKoH79lcGnHfQ0mAosURBioSUBmawXfD6VcpXGU+e0+bSQLzlF46HM87IUZO5XiQ= 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=dNGnupZf; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dNGnupZf" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26eef6cso2588826276.3 for ; Tue, 13 Feb 2024 22:37:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892654; x=1708497454; 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=fauoScqMGb0T6W+waGwPLd0PTd+mrpoq3alj54i0j8Y=; b=dNGnupZf2D5yXYxrAoxIUNakQIi2pIcolGJADLKmjdaanYrjGbyFJGbA2Z18wCEyZi QmRcAtWjA0x0Y9ryuD2mTHRFsxAuj71EfkdBPfYyrap+WIKqyoWGJxzIAbvG/nAvUc5P 0Ilh3V3hwGbpMN20G/l/V+5TKBGgnsXdmJ7hu+JkEX9shCxK41AD3kuLfIcrpna67m9F Mcmg3zCBL+DqBKykhwSXYIi1oMiZUh2C3YjHb9f4Y8FE/3rIwz6eB8tatUyKylnCbNlp 4G5QJgsP7UUqV5sP5ZrrkllSDQP9ihbtSuy17djfN6m8ySPs6D93UeJumEP5zGy8vDce xz0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892654; x=1708497454; 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=fauoScqMGb0T6W+waGwPLd0PTd+mrpoq3alj54i0j8Y=; b=bKj5sQNKaSNvubBF1xWWwBOOQqwnZ5djerU1vqyrmX/IZ7cKqb2t8vmfUg9oQAG33g M5V0JyCkmwVvGJt7a7XCTA+QWENpfiP5SP47x0nLuqU6OnIalQ/ECKT+Pbh/BpTYdz30 /3Y55pAmQDStvKTM49mCeDVxN7bhiId4+mDn73BGjMMBUrgwiza7/JD+cIr5RzfFGLBt uJvmW/brqm21P+P5Xa7GPMXdplidr6V/apY7N9T6PoWQi6+AbIkqcUgSAMgGDX9sCBR1 2na/PArQeKZQt+vAdZwzLFDtINb88X67ElWkiIdYiLtPk8+wZESG3ZN1tNLt4K1sQiDJ 9UTg== X-Forwarded-Encrypted: i=1; AJvYcCUA83uLMcB/0FqPbc3tNlUusGnzQHbdQGOSnKW8xsJrjy1pp8uAU0r0ExQDgQIO9aRYEG2+GqseiUkYoL5qu8/Li1vA8uqSO8LluwBh X-Gm-Message-State: AOJu0Yz5PBgGADObwTBo17I/0oa9awOkbxOvEPZfBfpYl1OtGNFIj+Hq W4UREel+H3k80wR0EJOj1J3XF3tCiWw6YE6OlGNHGFjAIc4NN6IpY2IkPO1DcAm/HbSnqSNmSom +n+uOtA== X-Google-Smtp-Source: AGHT+IFi3zXQvwnC/z5gX6vCW3M3+FZqgPpuASOu1UAfp5HpZ8Z2Ob9CLJ9tDZc3bIk+YeJf9imFZQHz+HCI X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a05:6902:120a:b0:dbe:d0a9:2be8 with SMTP id s10-20020a056902120a00b00dbed0a92be8mr57048ybu.0.1707892654638; Tue, 13 Feb 2024 22:37:34 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:08 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-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: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 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 , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 @@ =20 struct thread; =20 -#define THREADS__TABLE_BITS 8 +#define THREADS__TABLE_BITS 3 #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) =20 struct threads_table_entry { --=20 2.43.0.687.g38aa6559b0-goog