From nobody Sat Feb 7 10:16:12 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 1CD3A51016 for ; Fri, 1 Mar 2024 05:36:54 +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=1709271416; cv=none; b=mPem6hdNqE+PxbYGzup5ePUJQXt5M2UsB+TnsDhCMjhxjBH/9fix0Q7SmuEHv+JplFg8jcMVOagQwiEW64ZSUyt0lt3amILGTmm2+RwEUgzA4Y1WxZ84+jNdmQxRtcIcRIxU/oQUo+mkRpzmYxHiU2sCxid1Ytkox0DhkZA+eQY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271416; c=relaxed/simple; bh=5Vfpowhmbi0TZhAFP5b/eNfo07AUeLjsdG6M3wyq6EU=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=QoNiK+d1+90eZJypV609GQXUsVV4vcMv+d9M/zpLbxVb52wJOCeL4wYm+zJsxVnqIwMBPxGGMCd2Cb2J4L7VGXUsZL25euk4e3dOHVV1mxnMI9LnnAFOr2zmuLKZOOd6NmTbdXu2hghMLaJ5soKxpBidN3jGSNnGFauApaNBmak= 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=ebeClS+Q; 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="ebeClS+Q" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6093f75fc81so31044867b3.1 for ; Thu, 29 Feb 2024 21:36:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271413; x=1709876213; 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=yQeIrRq9VG1C1m8G7JjY7O5qpsoUL6eU/Y9t6ioRzeU=; b=ebeClS+Q7qbXccRBIH/lVBhgQUMUM1JWewUf2qTGIW+MySiMN8eepnaKLsDWdLtH1l tV2Bs23lnR2I2ZVtVmlwmkGEIAN0lKATEZehsHSWOcn7xVEDfgUXRG9MRpgLhqUa839V 6ocsYIll6VF4QZSuRVrZYmP1H7XkIutQOq+1dVKf2ScEReR04qiJo/PakNRa2/a2y9FU 9lgnDjO+HkDlj4CFyagESdSIvRPdI8oAYiWt083bmC2j+wClvzY8jWsaocQOsUyssZQ7 PKGCczOAW4KlbFEuIZv6TdKYeKSt4zOs1nJlKGvPpBtOVSls7+upyXcebo7h3O2Mg0lP +OYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271413; x=1709876213; 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=yQeIrRq9VG1C1m8G7JjY7O5qpsoUL6eU/Y9t6ioRzeU=; b=hxL5eFVgooVSkm10FOCtIL25oz4MpGY3F5V7S9/pP8QH3cXHX9Ju0bXkPuLo2s3Ep2 InMGVpi2j0lnYknlavLSaK9FPIP8uT4AJPRqXGZissCUCY9iu79/OtXcbLPA4+Y8083H tJCV8amcijJp7aXXOsSXR0XluG1LqdghfUWfku0G0qTJbosYjyAiBtdXz5eG4MfiZ4vY rF6Be3xKMaH4CU/xwIfFjg9/3o+5aUligSoVMP4A+PFEyOzMtnTyJNKLiyE5DYtPz6Xj N+2ruOshhRajsRnZOMo8Hxl7lNA8P5SMOrr8E/Y9s1XUkGkeMVs6M6+xjnAk43bosuBT joWA== X-Forwarded-Encrypted: i=1; AJvYcCUNDqx7cAG6YipYkmBuQNcnJ1Q555NUdP1WbNZle79xOmOZTkO9dbTe5WKE2nBlgMGF55rSku/AFqejyKrXxGyxkFhUbw7bKrHKUiZs X-Gm-Message-State: AOJu0YyWKIs+UKWe9nEctL4pxJ4UB8OyPUzBBaVVIl1dOZaN0I7ryXnO WI6Mjb1DUiOebHvDyDxYGNk6BUrfhgokidIJEJQGeeJYDKYS402LAqqy9EAtCRFWLHpd+98g0i3 99ykOLg== X-Google-Smtp-Source: AGHT+IHzY4vAeCAbNgLDBlMYcUGRB47pTsaxTuaJfsxTor6Gkb2Y+CWHZqehh8mBUqmH6OogzwB27cv1pYZ5 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a5b:f05:0:b0:dca:33b8:38d7 with SMTP id x5-20020a5b0f05000000b00dca33b838d7mr141664ybr.11.1709271413380; Thu, 29 Feb 2024 21:36:53 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:39 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 1/7] 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 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 Acked-by: Namhyung Kim --- tools/perf/builtin-report.c | 217 +++++++++++++++++++++--------------- tools/perf/util/machine.c | 30 +++++ tools/perf/util/machine.h | 10 ++ 3 files changed, 168 insertions(+), 89 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 8e16fa261e6f..dcd93ee5fc24 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,156 @@ 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; +/* + * Sort two thread list nodes such that they form a tree. The first node i= s the + * root of the tree, its children are ordered numerically after it. If a c= hild + * has children itself then they appear immediately after their parent. For + * example, the 4 threads in the order they'd appear in the list: + * - init with a TID 1 and a parent of 0 + * - systemd with a TID 3000 and a parent of init/1 + * - systemd child thread with TID 4000, the parent is 3000 + * - NetworkManager is a child of init with a TID of 3500. + */ +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; + + /* Same thread? */ + 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]; + /* Compare a and b to root. */ + 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 : 1; =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) { + /* Handle missing parent (unexpected) with some sanity. */ + thread__put(a); + thread__put(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; +} =20 - fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); +static int tasks_print(struct report *rep, FILE *fp) +{ + struct machine *machine =3D &rep->session->machines.host; + LIST_HEAD(tasks); + int ret; =20 - list_for_each_entry(task, &list, list) - task__print_level(task, fp, 0); + ret =3D machine__thread_list(machine, &tasks); + if (!ret) { + struct thread_list *task; =20 - free(tasks); - return 0; + list_sort(machine, &tasks, task_list_cmp); + + 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.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 B48AE524DC for ; Fri, 1 Mar 2024 05:36:56 +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=1709271418; cv=none; b=Efr6sdheOtF3iqmS8mAQBxWx5DgFQk4OFMV+UWd8sFzlXFUlh07UYfdvwj2QKNf6z2URytk3CYWxbms54GT/Xi2sJZZV6funUqJ9xYvw/Rmbnvf8W5HHgy1YZTwe0U1zaCNTeqkC+6Q6x6AXnLYjYa+ooBOpz8pkhHNsVCfEHt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271418; c=relaxed/simple; bh=soASIs7D8iQcX2U2s2M4JC6RaqIvyHatWg6UktdKnSI=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=H4YqecEeGXRwKqR43M6kApAMjx05amQfi3IonNhTM2nGocxWUURPlTTI1rp4eLRwrOzoHBoObMkfkVSKZCX5X4Y7PpihUwaH5HgyLgLClrvjzX1hsAW9YIyWRRZjVSvx5AYY+sZ9umzpXlWLkUSwB7/7Gy3MYJaMdy8j+/o2AJU= 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=yN7KMJk/; 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="yN7KMJk/" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-607e8e8c2f1so29944327b3.3 for ; Thu, 29 Feb 2024 21:36:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271416; x=1709876216; 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=eHNbX5Xyi/VqeebJOw+lK9eWaYZye24FQe0AyXZlgyM=; b=yN7KMJk/IHZ+SdUXNslVSy2elnT6d/xwvd0JH4r6vu66eSR5ydG4HVIi75O3BlwTNL Uwbo94ENq5eW5S90oqq09DQW7csC+fcbD8dqOhDKP+bAdOquKxYNlDsJ31AA4iWYxHpY W1D5+P9j0ImqaXsUgkbPKGJg5swOOEbWF1oFAqSRG8eOem/TFaqy555JwMvYf7qncDJW mI5KM92xfgaLSUDX+B0xaPBjQxAs6Q7kDh8IO3LLUEfRQIlmdfWFnw3ZChKthuNRBMV3 c4dy8zILmVp+xFYbA4kvg5SYvHtRNbJbPpYaBYqmP8yn3yAQfZ6+1nc3fNnWEp54qiX1 teIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271416; x=1709876216; 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=eHNbX5Xyi/VqeebJOw+lK9eWaYZye24FQe0AyXZlgyM=; b=FTPQegILu1gsxmPtnbo4EjqimJSzQCvc5wJFw5Z7bxQNW9THP0RdOV6URwq9sPaKKV 9Dwu3f1PRU+KH9d692AJnwDOf2fEJA+icVTu5iPAJMRl/uxslgEPhVbmM5AAneXV2mpN ic+t1R+kFbh2ebQZlk5O/sS2Jqrdbu1nz/Y/oc47CfSy41FJaEIBAu0ONDkyOlfc+U+N KwOdilSyFU0zQXwps7DoWR8kn6heEgTqs22cNLMtxv3+2cglRD/XhxQVDkxZ0hCsuGlJ R4GTz5fg7CHA20gdaj6KonnUWAhs3oRU8c3bg4FNLEdTz8SEfGiWbUJYdjYs853eInNF qcww== X-Forwarded-Encrypted: i=1; AJvYcCVEjQUkwaoFf60f18mIiWN2g8XZwfKrj5XqWUM8GMVyUq5tuKdBtvXTBYApCYY1mT/tRcimS+jUYNkH+1e05OFKBWof0zv8XJMg6rMh X-Gm-Message-State: AOJu0YywelTtjFm9mOf7gs7neqOj8bxSnglr9EfU9Egzy3yUOYM1vE7W nJh6D0qAZIGl9yszksspJamhRmIaUi1aG1MVWuEGfb0ZM48w+NgNNUedKS/bpySo2SEaeiNKeZ5 yExcXpQ== X-Google-Smtp-Source: AGHT+IGn8HoPMAtiLjnOrLidczbr8rHmWkK6qo4EV/60cj9vlFmzMLPXquVyPOjzZ/F3/NRBJNMVTERB35x2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a81:9950:0:b0:609:4d6f:7c0b with SMTP id q77-20020a819950000000b006094d6f7c0bmr114933ywg.4.1709271415925; Thu, 29 Feb 2024 21:36:55 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:40 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 2/7] 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 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 Acked-by: Namhyung Kim --- 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.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 40DD55336D for ; Fri, 1 Mar 2024 05:36:59 +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=1709271420; cv=none; b=Ws8cgAR746/4M/3fQlNKk4OE49I3AeHcSa8hNeOHZJLT+3Z9y+I9Nsc+R/UeaV1OoveHyXX8BbxdELA0OzzA02SEYnzP5kiWhNvjkVYU5sRQfpKgqr/oWD+bW+zdGsXCiypoLaCgPbSgJ+rIfsillPTuXnyv07e+f7iQ4Q8+MtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271420; c=relaxed/simple; bh=BBmU04u6tU5hlUeOKBhv7F/E4iRu0Xd0+OelCnDu9d4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Q6CDZGPCobSCGPClnsWL3cfotBxifgp3HzszehT1GzLEcr3IWP9+MjWNzOknFx76O4iTVf74+TVaNNsGDL0H50g+sEjFCCJMaksIIZwcn758XF2rUig6SLD6ScneNtb6Nb0VUhqRXWnEG7W5nBcd7Nyf8L3wQwUZ4FowbUR/Oc8= 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=ro/PVEIb; 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="ro/PVEIb" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ade10cb8so3571474276.0 for ; Thu, 29 Feb 2024 21:36:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271418; x=1709876218; 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=TRPVI6egL+2XI2pKzd6EAQy7fDTiYP04TRmL/KfQbAc=; b=ro/PVEIbA41Q4SdR4B2QPlPKNtMHCyuFGJUvsvuk/AtEFRo+N5U4X9+p246GpEtZFS oTardnH8fIDYNTWFC/+L/HxZ/67Zgh4sNVLkl0w8PiPltPr2H/Ithsi411awv1BTsyjs 9uCLZN6j0VOemEVMEGV3H8r9YD+tUCx0TdOgblvCNpX9eeMHtJSfHgIJwbll876fnKmZ a9JdMWVIby1jPAo0k7fuD5ID25qi1xp0E7vflnjVZV+wqUp7vfYF3VzCDJz32u720K3D wkW0uwob92PUw9+3uKARnAUpyqCGH9fsRICo/Xz9PVIMdNmsRC7DlQL2h+qld4EQh9Rv 5GPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271418; x=1709876218; 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=TRPVI6egL+2XI2pKzd6EAQy7fDTiYP04TRmL/KfQbAc=; b=K68DOdOiasclhsjjHMQ8c5H9Af+RmGvuuSWMD7Nu0GpSFbueqVz+FCgbix+q1T4pW1 u2QtHjNRk8skIx2aJ36D1/zZydB6CjVHrmRK9zkJ5QD33I7mex5yTnYc+KAzdDCziSIH jo5qSv7Cbmqpyan8lDi1IKL6xo4ir/niPkQuM9oNLzcJLifGNM4kg/kZ0R8sQ8DV+dia KgofFFPUzFiOVvcchMry9WP9h7pmzZo6MxB6/AMHtDGU9R9oMTSGvqrQNeN3DlY6Ot8D Hw4oKJGzBSyK+e1SCO5o0YfY82ayAkd47BY7o7G3y16TMfJ3KVL0jquQXyQ+/FNvgj4H eT5Q== X-Forwarded-Encrypted: i=1; AJvYcCWygP7CYQBJnjp66QSyzQ/quCdWvpBA7QLkljoae5cazsADi8SygAii/txS/gxow0EJTzAi7UTMSU8qUIhGNMlob7OOF/bgmKSLQe4j X-Gm-Message-State: AOJu0YyBfmE0uhJrEymJVL/BFMldIFCs2Ei8eAMU0E25mslq0i1cfrtn Swz6GkrL1mTq1pT6nkj3kDNgxIQJ+96CHEpeXKiuMlepV7AnM0BYSH2Ml+6UlQ64GyfTgLvchiX 8BCm5cQ== X-Google-Smtp-Source: AGHT+IGRLMrQFbbox60wk3jGuHwsiDSFz/GgzyTXEcf4rK5AC6vMv1Mi7Tmq1hBxxVIampmyF3fsHTa+P71N X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a25:b292:0:b0:dc7:5925:92d2 with SMTP id k18-20020a25b292000000b00dc7592592d2mr757438ybj.1.1709271418281; Thu, 29 Feb 2024 21:36:58 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:41 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 3/7] 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 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 Acked-by: Namhyung Kim --- 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.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 B70A7537F0 for ; Fri, 1 Mar 2024 05:37:01 +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=1709271423; cv=none; b=Qk3g4iX68vefml2MOLGEjJnVT2twNBj9kAs7xMA1fnblJxXdRIRXJUB5ZTuUQXboxwC8GUWLnA1+f22waVryIEgfuZ1FDyCToPlsviXhubBxLGA8W9RXHMIVmxhZJ+c0XAQfKFr4tGcD43c15Rm8QTf7VF0VL1zHGvpFsiud9yw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271423; c=relaxed/simple; bh=fBqMdgvBqvnAASN42/FlvI+/RXHkBdraQJ/XUtdnEI0=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=e8P+g6wfvrYWkeS7KnqArE/Igujv+cwYLECS4usibdge7cKnkVcopPEO2Vnx48N2iIOAj8GdH/eXRcmC5gn2qB49Rg1pJPFp3feI8efd9gJaeP4mHhKYPoBtdHyfiTl3EO4zev8hsiEJVAboXWs2pQ3PQvhQ++x8MF8GGds88F0= 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=20uIoqtE; 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="20uIoqtE" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ceade361so2986939276.0 for ; Thu, 29 Feb 2024 21:37:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271421; x=1709876221; 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=Yj/vjiBWUOYkXpVUJ7tJ/QYC18JIl7FM2VJjItok3Cs=; b=20uIoqtEdLVwH+SH8ZxBPQ7FRu6fI9GkAKR9aNsJ02ebh/7o5oiRHK5jF73gBjaUDA IeuSkQkRoQladik3h/BY+YQppf/B5NJ2fZ2dVfrPRTaKG9MlvZfLbkZ7w5T+pSx7pbTh thMNTKFKJziDC3DCqK9MbubSabryixtoyRCOloLdrQbJ93C93aIbJBk8bk3JwjM1v3j2 /s1L36ssMu9w6bw/8wBjyukND9CGlsqhkMV5qeZ0lezyJhV/TLVsMqPYloiBPY9PTnLR VnSxI7zDVro2TjDno+mxTqxc/K/yY3mMAG86JmaD9iVuFWLpYGrPQglFQMFlX19yNKur FPWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271421; x=1709876221; 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=Yj/vjiBWUOYkXpVUJ7tJ/QYC18JIl7FM2VJjItok3Cs=; b=PMm6BkMPBQ3ALxq1b9EEPuSAUWnLuIaQI4DGkO7Yf4SvMdA6BYwhbqaKS79sjWcEGB G/Vuvkj4CeU6db4D4xHx8FArEGfQF9BZGu7wrFbJ1gx/oDXoJ3djORmVW2+xIYMllO3b zC14JIl2Cj6ltsHRN1uhLZVDwK/oP6wvDjEC0wj4zTzXRqX+SvO5lWSD26nIMeFGZzhM wxf9Hq6G9DnC84zw00GweT4AfP1X4QuL/nML6qJk9hz49hmOwWICD4NGPl2IoWmhAEEK yZALskzAaNU/dgh35063gPf7cLKVZ8Vw5ce0/GT592APEsOKTN1UiVVQuQtxmh/wObia iEfA== X-Forwarded-Encrypted: i=1; AJvYcCWcLfGJBDHjgjVYcnNKY/y9G2BRQImvqLTnKjhFmQA0g2GGhcPDLuDPGdnb3K/liJsVkXFHXwgpH8e3TxD/oa1kheTTViDyboIMpr73 X-Gm-Message-State: AOJu0YxJNOSZOueDeaM+aXO68l1cATEc2H03qfvGXwLEjK/WT35oB04q akIm8kC2H2iatX2rzBDcYoH2doKJE5wxlMzlcA5j3zi/pA3Oo35Jc9UyjczxEwRCoZ4a5dEBOzn ensvY5w== X-Google-Smtp-Source: AGHT+IEaHoc5FS/aXnmu4l9AbUxfirmqvnksQO/BwYEX8z8lTz4eIYHpeD6QiKMzA0tuN9nBMiaFEJaOLd4t X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a05:6902:100a:b0:dc7:5aad:8965 with SMTP id w10-20020a056902100a00b00dc75aad8965mr157192ybt.0.1709271420887; Thu, 29 Feb 2024 21:37:00 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:42 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 4/7] perf machine: Move machine's threads into its own abstraction 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move thread_rb_node into the machine.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. 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 a later next patch that delegates the implementation to hashtable. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/bpf_lock_contention.c | 4 +- tools/perf/util/machine.c | 408 ++++++++++++++------------ tools/perf/util/machine.h | 26 +- tools/perf/util/thread.c | 2 +- tools/perf/util/thread.h | 6 - 5 files changed, 243 insertions(+), 203 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lo= ck_contention.c index 3549180c7885..b4cb3fe5cc25 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -328,7 +328,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; @@ -422,7 +422,7 @@ int lock_contention_read(struct lock_contention *con) account_end_timestamp(con); =20 if (con->aggr_mode =3D=3D LOCK_AGGR_TASK) { - struct thread *idle =3D __machine__findnew_thread(machine, + struct thread *idle =3D machine__findnew_thread(machine, /*pid=3D*/0, /*tid=3D*/0); thread__set_comm(idle, "swapper", /*timestamp=3D*/0); diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index e072b2115b64..224b53b4bfe2 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -43,8 +43,16 @@ #include #include =20 -static void __machine__remove_thread(struct machine *machine, struct threa= d_rb_node *nd, - struct thread *th, bool lock); +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 struct dso *machine__kernel_dso(struct machine *machine) { @@ -58,35 +66,18 @@ static void dsos__init(struct dsos *dsos) init_rwsem(&dsos->lock); } =20 -static void machine__threads_init(struct machine *machine) +void threads__init(struct threads *threads) { - int i; + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; =20 - 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; + table->entries =3D RB_ROOT_CACHED; + init_rwsem(&table->lock); + table->nr =3D 0; + table->last_match =3D NULL; } } =20 -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 +111,7 @@ int machine__init(struct machine *machine, const char *= root_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; @@ -219,29 +210,51 @@ static void dsos__exit(struct dsos *dsos) exit_rwsem(&dsos->lock); } =20 -void machine__delete_threads(struct machine *machine) +static void __threads_table_entry__set_last_match(struct threads_table_ent= ry *table, + struct thread *th); + +void threads__remove_all_threads(struct threads *threads) { - struct rb_node *nd; - int i; + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + struct rb_node *nd; =20 - 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); + 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); =20 nd =3D rb_next(nd); - __machine__remove_thread(machine, trb, trb->thread, false); + thread__put(trb->thread); + rb_erase_cached(&trb->rb_node, &table->entries); + RB_CLEAR_NODE(&trb->rb_node); + --table->nr; + + free(trb); } - up_write(&threads->lock); + assert(table->nr =3D=3D 0); + up_write(&table->lock); } } =20 -void machine__exit(struct machine *machine) +void machine__delete_threads(struct machine *machine) { - int i; + threads__remove_all_threads(&machine->threads); +} + +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); + } +} =20 +void machine__exit(struct machine *machine) +{ if (machine =3D=3D NULL) return; =20 @@ -254,12 +267,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 +534,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 @@ -565,78 +573,88 @@ static void machine__update_thread_pid(struct machine= *machine, * 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) +static struct thread *__threads_table_entry__get_last_match(struct threads= _table_entry *table, + pid_t tid) { - struct thread *th; + struct thread *th, *res =3D NULL; =20 - th =3D threads->last_match; + th =3D table->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; + if (thread__tid(th) =3D=3D tid) + res =3D thread__get(th); } - - return NULL; + return res; } =20 -static struct thread* -threads__get_last_match(struct threads *threads, struct machine *machine, - int pid, int tid) +static void __threads_table_entry__set_last_match(struct threads_table_ent= ry *table, + struct thread *th) { - struct thread *th =3D NULL; - - if (perf_singlethreaded) - th =3D __threads__get_last_match(threads, machine, pid, tid); - - return th; + thread__put(table->last_match); + table->last_match =3D thread__get(th); } =20 -static void -__threads__set_last_match(struct threads *threads, struct thread *th) +static void threads_table_entry__set_last_match(struct threads_table_entry= *table, + struct thread *th) { - thread__put(threads->last_match); - threads->last_match =3D thread__get(th); + down_write(&table->lock); + __threads_table_entry__set_last_match(table, th); + up_write(&table->lock); } =20 -static void -threads__set_last_match(struct threads *threads, struct thread *th) +struct thread *threads__find(struct threads *threads, pid_t tid) { - if (perf_singlethreaded) - __threads__set_last_match(threads, th); + 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; } =20 -/* - * 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) +struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t = tid, bool *created) { - struct rb_node **p =3D &threads->entries.rb_root.rb_node; + struct threads_table_entry *table =3D threads__table(threads, tid); + struct rb_node **p; struct rb_node *parent =3D NULL; - struct thread *th; + struct thread *res =3D NULL; struct thread_rb_node *nd; bool leftmost =3D true; =20 - th =3D threads__get_last_match(threads, machine, pid, tid); - if (th) - return th; - + *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; =20 if (thread__tid(th) =3D=3D tid) { - threads__set_last_match(threads, th); - machine__update_thread_pid(machine, th, pid); - return thread__get(th); + __threads_table_entry__set_last_match(table, th); + res =3D thread__get(th); + goto out_unlock; } =20 if (tid < thread__tid(th)) @@ -646,74 +664,76 @@ static struct thread *____machine__findnew_thread(str= uct machine *machine, leftmost =3D false; } } + 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; +} =20 - if (!create) - return NULL; - - th =3D thread__new(pid, tid); - if (th =3D=3D NULL) - return NULL; +/* + * Caller must eventually drop thread->refcnt returned with a successful + * lookup/new thread inserted. + */ +static struct thread *__machine__findnew_thread(struct machine *machine, + pid_t pid, + pid_t tid, + bool create) +{ + struct thread *th =3D threads__find(&machine->threads, tid); + bool created; =20 - nd =3D malloc(sizeof(*nd)); - if (nd =3D=3D NULL) { - thread__put(th); - return NULL; + if (th) { + machine__update_thread_pid(machine, th, pid); + return th; } - nd->thread =3D th; =20 - 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); + if (!create) return NULL; - } - /* - * It is now in the rbtree, get a ref - */ - threads__set_last_match(threads, th); - ++threads->nr; =20 - 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 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,13 +1147,17 @@ 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 threads__nr(struct threads *threads) { size_t nr =3D 0; =20 - for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) - nr +=3D machine->threads[i].nr; + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; =20 + down_read(&table->lock); + nr +=3D table->nr; + up_read(&table->lock); + } return nr; } =20 @@ -1143,7 +1167,7 @@ size_t machine__fprintf(struct machine *machine, FILE= *fp) .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 +2093,42 @@ int machine__process_mmap_event(struct machine *mac= hine, 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) +void threads__remove(struct threads *threads, struct thread *thread) { - struct threads *threads =3D machine__threads(machine, thread__tid(th)); - - if (!nd) - nd =3D thread_rb_node__find(th, &threads->entries.rb_root); + struct rb_node **p; + struct threads_table_entry *table =3D threads__table(threads, thread__ti= d(thread)); + pid_t tid =3D thread__tid(thread); =20 - if (threads->last_match && RC_CHK_EQUAL(threads->last_match, th)) - threads__set_last_match(threads, NULL); + down_write(&table->lock); + if (table->last_match && RC_CHK_EQUAL(table->last_match, thread)) + __threads_table_entry__set_last_match(table, NULL); =20 - 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); + 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; + } =20 - if (lock) - up_write(&threads->lock); + if (tid < thread__tid(th)) + p =3D &(*p)->rb_left; + else + p =3D &(*p)->rb_right; + } + up_write(&table->lock); } =20 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, @@ -3228,27 +3258,35 @@ int thread__resolve_callchain(struct thread *thread, return ret; } =20 -int machine__for_each_thread(struct machine *machine, - int (*fn)(struct thread *thread, void *p), - void *priv) +int threads__for_each_thread(struct threads *threads, + int (*fn)(struct thread *thread, void *data), + void *data) { - struct threads *threads; - struct rb_node *nd; - int rc =3D 0; - int i; + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { + struct threads_table_entry *table =3D &threads->table[i]; + struct rb_node *nd; =20 - 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)) { + down_read(&table->lock); + 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); =20 - rc =3D fn(trb->thread, priv); - if (rc !=3D 0) + if (rc !=3D 0) { + up_read(&table->lock); return rc; + } } + up_read(&table->lock); } - return rc; + return 0; + +} + +int machine__for_each_thread(struct machine *machine, + int (*fn)(struct thread *thread, void *p), + void *priv) +{ + 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..5b425b70140e 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -31,13 +31,28 @@ struct vdso_info; #define THREADS__TABLE_BITS 8 #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) =20 -struct threads { +struct threads_table_entry { struct rb_root_cached entries; struct rw_semaphore lock; unsigned int nr; struct thread *last_match; }; =20 +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); + struct machine { struct rb_node rb_node; pid_t pid; @@ -48,7 +63,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 +84,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 +229,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 df344262eaee..8b4a3c69bad1 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 @@ -29,11 +28,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; --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 195C453807 for ; Fri, 1 Mar 2024 05:37:03 +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=1709271426; cv=none; b=QZyDqIpac+cjjhlrvXGuYxeOKK9KutgU5Mz6kZI8lR8QLBsMNKcBKeSb0r4CTgp7Jg30OiK8QNdXIHBzvdD5nOZ/Exb/Mjzhk+EXgO40HeEHwtDGMrFEUgl9ajNqMu41MA8M0jlglF6CMPqzhw4Ds5YCoDvnhjQqUc/gBdajQ4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271426; c=relaxed/simple; bh=aOKvNCw0mP/aSzwrAr6tjIXHYZ6WOqSdyo+aZpChxCA=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=QMGyKrckwudfKh9C0coZYunaWAY1DKoO60Q/YMXFMUlw1chHZvwzE9j/2Bsh7NYYILt8yzoDgXm0zVI/wxlO15XPJ9xxWY/+yI+V5+FaPMO96U+f2dNzbwO0XfbsL7LnleOsUcMKBnS0y0gFeIVCF00yeG80TS2LuOfdDulF60o= 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=roPXcHyq; 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="roPXcHyq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6093f75fc81so31046637b3.1 for ; Thu, 29 Feb 2024 21:37:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271423; x=1709876223; 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=ROSXDwUomvqQuML9PMkfq2AQvT85kKoIeIu9nBJaVks=; b=roPXcHyqgy8Gk6nnFgvnnRHDuYdbOT9ZL4WhuO3V+sUOYDZCBsVHyn0rE4XLrt30XH 9gT3Dh2Ya8OP+jBsgjeVbDqinInIOXajpjMrDggRRue0mNvuU6m37V9afQwHemaRSPcP DU7YSE1NbXqA00uXJ2wMX6zSnatmseXQ/ym7KAfIosa4z0D5v4PUMT6UwcmQp1A0qRNZ RD7Q/XVrRnBuYE3BojWXQDZ3gKzv3e3tFz/JwmeiWNNo56c8E63jPlsXioU48j7rFGdk uF5yOR2bU/P5gwOkc2/e7wZ/Z5Fvj2IDj7YHrkWPLpnqfDn11+NIcNlVSLkbVLxRDNyU tpWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271423; x=1709876223; 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=ROSXDwUomvqQuML9PMkfq2AQvT85kKoIeIu9nBJaVks=; b=m+gf5Xzvmisp9XINGfz3Zhdf1ay05UorYF6zXittuPx3UxIE3x+mjvAWn3N/XSsxOd 0mTHMZGmYBd3q+u90dp6lLEHRU+aR6+2Q+yG/KUsYeNgTf8yW4GBsF4WkMxhRy9EFpw/ iPJgsbPbeocVNLOIqEBxXlVNegHWCZp1nLHynetDCzZ1vksYdm3X+fBKRof8ZoIAbEOc yP1FcTrlToAw8Be509ZVNnHpKtLvouebwE5VphC1xIubLT2jWH+3BXMMNtfzxRKyjpkb 38rY1QOLp7Ctiy3exFYBIyjQvME7V9mtL4/MfOTFPyhpXzP2ryCHt/b83fbwRdHJU1WM 8cQw== X-Forwarded-Encrypted: i=1; AJvYcCVOoWbDt1UxcZuXyiRL3DlQT9fOdEryuo4xoC3vMaktrjvfqkCTSANWjwA6h82syKKYRaxDhAub3Cay83fbDGpWV7xG22PYEVcg3tGO X-Gm-Message-State: AOJu0YwFQtMkVkxKtMVVOEdmPDyFQen4EhShACExMBvpOzOglg0fGqpr GDPrjChglPbZ1YwmGbEVSsDxhp5DgTY6R2f2XhRIqlvlqQ+p5LVKGWWdCN1wmmWIsRiwc6OsSC1 ONC2FIA== X-Google-Smtp-Source: AGHT+IHzQdeM1X0crAbAfCGu8ncW89Nm1yPNhZsTC2Nfd9aa23Z5xJAhmN2erL3pTCl9ayhj96utvGNJOn0J X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a05:690c:c86:b0:608:ce23:638c with SMTP id cm6-20020a05690c0c8600b00608ce23638cmr174942ywb.4.1709271423168; Thu, 29 Feb 2024 21:37:03 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:43 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 5/7] 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move threads out of machine and into its own file. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/perf/util/Build | 1 + tools/perf/util/machine.c | 248 -------------------------------------- tools/perf/util/machine.h | 26 +--- tools/perf/util/threads.c | 248 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/threads.h | 35 ++++++ 5 files changed, 285 insertions(+), 273 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 +=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/machine.c b/tools/perf/util/machine.c index 224b53b4bfe2..527517db3182 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -43,17 +43,6 @@ #include #include =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]; -} - static struct dso *machine__kernel_dso(struct machine *machine) { return map__dso(machine->vmlinux_map); @@ -66,18 +55,6 @@ static void dsos__init(struct dsos *dsos) init_rwsem(&dsos->lock); } =20 -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; - } -} - static int machine__set_mmap_name(struct machine *machine) { if (machine__is_host(machine)) @@ -210,49 +187,11 @@ static void dsos__exit(struct dsos *dsos) exit_rwsem(&dsos->lock); } =20 -static void __threads_table_entry__set_last_match(struct threads_table_ent= ry *table, - struct thread *th); - -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 machine__delete_threads(struct machine *machine) { threads__remove_all_threads(&machine->threads); } =20 -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); - } -} - void machine__exit(struct machine *machine) { if (machine =3D=3D NULL) @@ -568,121 +507,6 @@ static void machine__update_thread_pid(struct machine= *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_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 { - p =3D &(*p)->rb_right; - leftmost =3D false; - } - } - 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; -} - /* * Caller must eventually drop thread->refcnt returned with a successful * lookup/new thread inserted. @@ -699,7 +523,6 @@ static struct thread *__machine__findnew_thread(struct = machine *machine, machine__update_thread_pid(machine, th, pid); return th; } - if (!create) return NULL; =20 @@ -1147,20 +970,6 @@ static int machine_fprintf_cb(struct thread *thread, = void *data) return 0; } =20 -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; -} - size_t machine__fprintf(struct machine *machine, FILE *fp) { struct machine_fprintf_cb_args args =3D { @@ -2093,39 +1902,6 @@ int machine__process_mmap_event(struct machine *mach= ine, union perf_event *event return 0; } =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); - - 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); -} - void machine__remove_thread(struct machine *machine, struct thread *th) { return threads__remove(&machine->threads, th); @@ -3258,30 +3034,6 @@ int thread__resolve_callchain(struct thread *thread, return ret; } =20 -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; - - down_read(&table->lock); - 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) { - up_read(&table->lock); - return rc; - } - } - up_read(&table->lock); - } - return 0; - -} - int machine__for_each_thread(struct machine *machine, int (*fn)(struct thread *thread, void *p), void *priv) diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 5b425b70140e..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,31 +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_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); - struct machine { struct rb_node rb_node; pid_t pid; diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c new file mode 100644 index 000000000000..db52d233c2de --- /dev/null +++ b/tools/perf/util/threads.c @@ -0,0 +1,248 @@ +// 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; + + down_read(&table->lock); + 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) { + up_read(&table->lock); + return rc; + } + } + up_read(&table->lock); + } + 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.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 6C7C854737 for ; Fri, 1 Mar 2024 05:37: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=1709271428; cv=none; b=XeyjPkeYD/efwzHcsOMv3iFm4UtlnmfXAbuMCybNx0ZnzTR+FYCaz4b0n2KGlij0pLxhH1x+dx7ppD9FPahf/zRBQE+zdzyZwXP219vB8IuiIhpIxOxJdKHCenqrnLCyysiQsXOHonfbuPyL9KKdkRAbdF0fJGZPGN6Z0UqX/C0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271428; c=relaxed/simple; bh=SCdyCRIW2q68p5P5XIiezVRdNMVzHuD+AJIgIjGCWXw=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=FiDmKpcYgUOYVhjPrqdKROaYDT9eI1I0haAH37pf5KGXZlWxxKqdWSjb9Z/r7E7ElvKuPqOQsU7Ig2U/zHvm1pwqZjxfTQQ01lVgDid+xvjMMHhewQA2efTMqYoo/kCuvT7ZeXjqXGO1cmb6XMhGZKSQ20hOrGkbaaN2s0B09jg= 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=JsDqkn//; 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="JsDqkn//" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc693399655so3255764276.1 for ; Thu, 29 Feb 2024 21:37:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271425; x=1709876225; 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=G4IHp+rWZ6m2tdq1gZNE1l7uOaHQfZu8fsR6pOr+kRs=; b=JsDqkn//YlCBTmY+LxfyxyBNgms6u3MC0PQDXERBEVsj08UmXLaTdbQtXUGUmdumo7 Vg52LO9qSLQoE827beREGu8hvcMA8P1ZwPeETtwK2OQkojV0SWCgTg/5HZk/tOJhnnKt Nti+co1hamuw0ZuXhKmW6v7GBL2v8XewKtAugbbqbbDift03Pc0tAbub3FT+ucCgic5J 71AxLqXFjUFyJkHktTwvPplFw84LtEwKzuAkAceqmPA+V6WfXdhT5YrBBeHYVKlijzHU nIj4Kq8cQMpGsFdHtX7E4xzVTQYGx8DyK1s/3m6UVINGmd89OBRSqjyzrlWu1yrVa7aI YD8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271425; x=1709876225; 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=G4IHp+rWZ6m2tdq1gZNE1l7uOaHQfZu8fsR6pOr+kRs=; b=S6hcSD1Bq+3/sE04rp+X05io6zgkbeikYqLf1izVvhMCOTDi43qIKgkjdUGGoJKEyB +yvWhPhNkvQQgsBpg6Y4/MjKs98+As+4iuLMWcX4Q6Ss1FwCp9E675bvc9afwLmV71Qc Fj2QUdJ1FN0nKX28izK4+80SvjL75g6GljwvyVt1C7cl3nqOiKqsn7rtrTYS8NImOdla z9eCDMFBwq/SDjgfoSlYRLfrTQUAC826rx9Sca29NnypMf2xrvDCvBwwpDz1pyE97Opt eXG+vYNQ5Ac5IMs5u+CkInT8bfThclqG/n4GCj5n6Dcjr1Ge5HqqJVrz2wNorsthtOTA XNDg== X-Forwarded-Encrypted: i=1; AJvYcCUn3U4bpoVuLv3A6NEnukiQnRh9VZq/ly/hxp7cAQNKnvddBUk/KJ4XLvqTTg+DlbXlPL+P1HppbtyHt1F9wSVqfN4iCMrlrJU5YPf0 X-Gm-Message-State: AOJu0YxEvhPne/vjS/Hl1Z8JdGNpKizTaH/IPVjvWuHqq0DQvcayt8p7 nRxNUs9QccsNSr9U1q7unp67P6QePGfMP2cjcVyzftC0/AUKD52WWvBYKKr2UvEGI3u30xXNs/S a/smWLg== X-Google-Smtp-Source: AGHT+IHDnJcerb+c7ZPfEF4j9tDeF/0/h6s4fjz+tv+cgtygpZuNEk0geTQ5pUO7WIcjdj5Uc3q5l8xOwCm2 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a05:6902:154d:b0:dcf:411d:67b9 with SMTP id r13-20020a056902154d00b00dcf411d67b9mr137956ybu.5.1709271425461; Thu, 29 Feb 2024 21:37:05 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:44 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-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: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 6/7] 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 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 Acked-by: Namhyung Kim --- 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 db52d233c2de..ff2b169e0085 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,12 +171,12 @@ 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 down_read(&table->lock); - 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) { up_read(&table->lock); 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.44.0.278.ge034bb2e1d-goog From nobody Sat Feb 7 10:16:12 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 B298554BFC for ; Fri, 1 Mar 2024 05:37:08 +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=1709271430; cv=none; b=KjWWl29lRotJbmej+aXiWnhZCpHkoIqn5w6FxKV1z3d34noQLCgYjYFV4AHMejKkNjD8qvSsz0gpgBs37ctlu6TtM7PSWGE2GxNwpDVQonnOGpnWLK3k/H+5Bjk37U+9svxTLy7r71o5Q99wH9+gMXPKUJxFD5aVfWQpHN7I0Jg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709271430; c=relaxed/simple; bh=GIegngHqtLjHARUsBjYE1nUvvYzurvCdwBU55T25Zws=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=tfJmZsougO3t0uGs7ymIe4kf0sHMfA1TuD4Ymg/grxcW03l2htl1RxGlZ+z2Sf2byy2lywubZ3/TMoa9PtQXsfGzHF8ZF3mwDcj47+lUqRDya0Ds7v9yjnOliQM73t/9zWC1El7+ECcONOG1R0+eoA2+F9tqRgWwNdmp4ilGG1w= 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=xQLSYKDJ; 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="xQLSYKDJ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6087ffdac8cso26288767b3.2 for ; Thu, 29 Feb 2024 21:37:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709271427; x=1709876227; 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=h3EeOnj2LJFuh4Gzgf1odB7yRFHxb61zXSqdlmTf/sY=; b=xQLSYKDJmoaE+yJNvdjYjShfCm7eK04b3GLGi2U3N5GvAS9YcB8vCk7OpmItFobhts 195HdHsABF1tJrM4Bh1DlxbnjDClsKPi/EBzuwfmvOo2JpRXoF6pGIB1DgwxZ8eJJ0N7 CklgfszI1bqzu19u3ftFqEZB1edr00ki94uK8WaTnVcVmj51booCGqpfjzrwLhF2pBsV xxgQT3Ip7mi5zeAtgP9o8sYqF6IvW8WEzWDWFd1HELnTgHOtJwA1HwMmfpY8gADj7T0P obIEV+00Ihr42PS5GNDphI2YNA+7meByKQ/Wc9Mn4APnr9JFJ0C44WW3hlikRxzXIPSF DmQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709271427; x=1709876227; 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=h3EeOnj2LJFuh4Gzgf1odB7yRFHxb61zXSqdlmTf/sY=; b=Lalglhs8nVshL2VQhlSodGKppLRUkWN4i+HauC4sXreSNFAR3wOIAoatyr05CXPGVX Bu5x0Qyq7pXEoCqsvYmyI/EzZaNslTXwfpQ9UrnSXuoTWrWgyyIh6oNrLIHaH1C8axie Z2iyghCRrpmLeOhuRi4/zmkRZBoTDc6T7goqYG6etMkS7nUfTTtx+ajRt9gUpwhnNImq 7Olpg6PyUk6wVlGjaC0Nv+NEi3D4b8hr7rPFXHtYT79Sd8Dbh7ekSimMWgmZBheMmDjs 6+1fdZPP9ycndS7y/WpGm4Anxxi2i9iaRFKzPsjOjmltjiBCpo/IuYhKMU+IgvO1O/+h o0Pg== X-Forwarded-Encrypted: i=1; AJvYcCXc/9LijPozSai6GdZiPpawDZpPxM5wM4AgaWIqnVx4iQmd0B8zScBLksOzrE9WTFNVlCoTASA0S4js7I56cLztW430Xzghu0DmWZi9 X-Gm-Message-State: AOJu0Yy1wnTYkCGjfyD11rqh52XCYz/Gq9Cfk8UR7DrG+Oj1ir2GYMgL IbeWgST3SLTHIZfN0fCQRvksVWvh5XYkeARqfFhOW0KbVDNxFTrCpRDrrM5Vi1+CDacSUvvnuyq c6lLbDg== X-Google-Smtp-Source: AGHT+IEu09qcHpz8XVF0bJYUs01j219s9OVd3W8pW3osKdThUV9pFvDfs+miQGs9atOQylZEGB8D3SP3TfJK X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:af4b:7fc1:b7be:fcb7]) (user=irogers job=sendgmr) by 2002:a05:6902:72f:b0:dc7:68b5:4f3d with SMTP id l15-20020a056902072f00b00dc768b54f3dmr130791ybt.11.1709271427758; Thu, 29 Feb 2024 21:37:07 -0800 (PST) Date: Thu, 29 Feb 2024 21:36:45 -0800 In-Reply-To: <20240301053646.1449657-1-irogers@google.com> Message-Id: <20240301053646.1449657-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240301053646.1449657-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v4 7/7] 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 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 Acked-by: Namhyung Kim --- 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.44.0.278.ge034bb2e1d-goog