Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp106824rdg; Wed, 11 Oct 2023 23:25:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+DIrT9FYNCLFUrdfy6yaGr1FUhmaBNDKujlJJ/qKMvDSdA5iy9mZEbrZ3HCypPaBy50b5 X-Received: by 2002:a05:6a20:54a4:b0:159:b7ba:74bd with SMTP id i36-20020a056a2054a400b00159b7ba74bdmr24381151pzk.50.1697091946462; Wed, 11 Oct 2023 23:25:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697091946; cv=none; d=google.com; s=arc-20160816; b=bkPBpeNGKahfOox2dPwesxYcLnoq6byT8k0dC+s/0wpNb6osJS8lwHZgfU0c4XE2wz /8/dyI3OYBq9IqkC+B9hTp8wjXfw5uZHx0zm4iHRMDAjT9qwu5aZpQepSPvQbt6KfOTw UOK15clHPUBIwi6PVTRYjy2KI1/9nHu7nWZKMqlWzWijN7xw3QxiI+FqhT99KC+g1RDR 0jX0r9+2vwKnSSnklv002uCAI8nm8bwB464k8u/lTAPCS5XH2vS2Gml0NTxCW/CNeYrp D4subvRtNseFHkc3SVqHMelkRX5EFJIb3Cr739mTPGIK5Wcf3TZLF7QDwZdoAQX5tw70 a2sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=RTK1vjQioOgb7+3VfL3Txgo28p8CKodFJzCpLv4Rtj4=; fh=d2c3GDvCFnEMjeV5/UdzkMgvIdbP06sIBDmJP5CjgvI=; b=coLr+6FEQ8SxHMSKCHP10/ZfvYkxwTzqYPY8D0XpA8vvsqCBxLDNSeq/rUNVj9hpQY y9Dyt0+6iW+KnZvHzGCcJ7ucOhEZu9Fd2c0+QB3A69rHesWs5UuZ+zvkCZReHyulYmz5 z85w5Qy5ihJxQEKjSa6F0+i3ExBytLnydHPJ/xga5qZNPoKnDw1sTXiD3LfQw1K2u1F8 w8LPqdGx3EJFTemDkAGFCWg67SRfFvkBgxgI+BfRJqp1DsaK7F8HYq9onLPqsMNeET0m lSmAaQTYNF9YZyHdOn6wnQecJVbHqnXHA5v6JCD2F0GCmBl/JzL/gT+5BTq7MTKh2mbK dnPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uv6JeNPi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p7-20020a170902e74700b001bb0ff2b354si1647087plf.425.2023.10.11.23.25.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 23:25:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=uv6JeNPi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 44B808040924; Wed, 11 Oct 2023 23:25:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347125AbjJLGZ0 (ORCPT + 99 others); Thu, 12 Oct 2023 02:25:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377894AbjJLGYw (ORCPT ); Thu, 12 Oct 2023 02:24:52 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0FD5D67 for ; Wed, 11 Oct 2023 23:24:38 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a60a104b6so1502777276.1 for ; Wed, 11 Oct 2023 23:24:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697091878; x=1697696678; 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=RTK1vjQioOgb7+3VfL3Txgo28p8CKodFJzCpLv4Rtj4=; b=uv6JeNPij7TT4XS0q0/yoo6k2/t4vvrRQzLGGY5rFdmj4u0CQaXGgBbB1/NaeKmhgW UIlozJYzTUXmPNYb86Xubwt/GdWVlm65dwXKiQtwrGxD0Q0elKxj1APbB2E00VMKJ4D8 92taAuQSd+mrWsT6vfz/I+osKMd5YBHHLIaxf6qc/FXAR2zuYuF3YXuoVGP2Y3e+4SPA 0ivN5hSFC4514goeouq9RPkvblO2VtaBqMOiPs6pnkI62WBMbsHCrWVNWNzcchl7dyCa QpKKWDod1Jkp/GWd9lI9q23HWSRjvYwSj9D+mTlK2AhkzhijkzULcXBMP9YzmioKqaZi a57A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697091878; x=1697696678; 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=RTK1vjQioOgb7+3VfL3Txgo28p8CKodFJzCpLv4Rtj4=; b=Kib9GX7QcqCKT4gGqfnpUGTS/ht75JNd2zQ50U8rZGmK710ddrvhTBhOiscmDTD0q6 03qj4Fjuuqdkax50cdz3Ml/e0SRuEwc0a/GCuw4coQRV72p81qWCsJOAaEFbzI1r5Wvp WYGr2h8RdE2/u7QZw+QrasgBBhZP364Fe6iOb4KPmnQijjv4aW1B1oFmAF6ld/C8gGRh 4zs+t7v20TsduzrX3NPGDtJ/JyhGcPkPrpqaLqO8y6JPtJVHe8ITrjoFKEJTFEeVNB8+ ArcV9CWmT8m2uEjSJudoHwUly78iOsMblKlTeTDgLQjvtvHQI9KidxhsQlDXeS9XOKaa iefA== X-Gm-Message-State: AOJu0YxJKX6vB0svcPZJgSRRH/7cnV/y2DTWqI5IyyoWc/teWt3eYB7n 6waATuq7yfqmXVmCufLQo+o9itX9gVW3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7be5:14d2:880b:c5c9]) (user=irogers job=sendgmr) by 2002:a25:abc9:0:b0:d9a:6360:485b with SMTP id v67-20020a25abc9000000b00d9a6360485bmr169468ybi.2.1697091877832; Wed, 11 Oct 2023 23:24:37 -0700 (PDT) Date: Wed, 11 Oct 2023 23:23:59 -0700 In-Reply-To: <20231012062359.1616786-1-irogers@google.com> Message-Id: <20231012062359.1616786-14-irogers@google.com> Mime-Version: 1.0 References: <20231012062359.1616786-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v2 13/13] perf machine thread: Remove exited threads by default From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Song Liu , Sandipan Das , Anshuman Khandual , James Clark , Liam Howlett , Miguel Ojeda , Leo Yan , German Gomez , Ravi Bangoria , Artem Savkov , Athira Rajeev , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 11 Oct 2023 23:25:44 -0700 (PDT) struct thread values hold onto references to mmaps, dsos, etc. When a thread exits it is necessary to clean all of this memory up by removing the thread from the machine's threads. Some tools require this doesn't happen, such as perf report if offcpu events exist or if a task list is being generated, so add a symbol_conf value to make the behavior optional. When an exited thread is left in the machine's threads, mark it as exited. This change relates to commit 40826c45eb0b ("perf thread: Remove notion of dead threads"). Dead threads were removed as they had a reference count of 0 and were difficult to reason about with the reference count checker. Here a thread is removed from threads when it exits, unless via symbol_conf the exited thread isn't remove and is marked as exited. Reference counting behaves as it normally does. Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 7 +++++++ tools/perf/util/machine.c | 10 +++++++--- tools/perf/util/symbol_conf.h | 3 ++- tools/perf/util/thread.h | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index dcedfe00f04d..749246817aed 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -1411,6 +1411,13 @@ int cmd_report(int argc, const char **argv) if (ret < 0) goto exit; + /* + * tasks_mode require access to exited threads to list those that are in + * the data file. Off-cpu events are synthesized after other events and + * reference exited threads. + */ + symbol_conf.keep_exited_threads = true; + annotation_options__init(&report.annotation_opts); ret = perf_config(report__config, &report); diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 6ca7500e2cf4..5cda47eb337d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2157,9 +2157,13 @@ int machine__process_exit_event(struct machine *machine, union perf_event *event if (dump_trace) perf_event__fprintf_task(event, stdout); - if (thread != NULL) - thread__put(thread); - + if (thread != NULL) { + if (symbol_conf.keep_exited_threads) + thread__set_exited(thread, /*exited=*/true); + else + machine__remove_thread(machine, thread); + } + thread__put(thread); return 0; } diff --git a/tools/perf/util/symbol_conf.h b/tools/perf/util/symbol_conf.h index 2b2fb9e224b0..6040286e07a6 100644 --- a/tools/perf/util/symbol_conf.h +++ b/tools/perf/util/symbol_conf.h @@ -43,7 +43,8 @@ struct symbol_conf { disable_add2line_warn, buildid_mmap2, guest_code, - lazy_load_kernel_maps; + lazy_load_kernel_maps, + keep_exited_threads; const char *vmlinux_name, *kallsyms_name, *source_prefix, diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index e79225a0ea46..0df775b5c110 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -36,13 +36,22 @@ struct thread_rb_node { }; DECLARE_RC_STRUCT(thread) { + /** @maps: mmaps associated with this thread. */ struct maps *maps; pid_t pid_; /* Not all tools update this */ + /** @tid: thread ID number unique to a machine. */ pid_t tid; + /** @ppid: parent process of the process this thread belongs to. */ pid_t ppid; int cpu; int guest_cpu; /* For QEMU thread */ refcount_t refcnt; + /** + * @exited: Has the thread had an exit event. Such threads are usually + * removed from the machine's threads but some events/tools require + * access to dead threads. + */ + bool exited; bool comm_set; int comm_len; struct list_head namespaces_list; @@ -189,6 +198,11 @@ static inline refcount_t *thread__refcnt(struct thread *thread) return &RC_CHK_ACCESS(thread)->refcnt; } +static inline void thread__set_exited(struct thread *thread, bool exited) +{ + RC_CHK_ACCESS(thread)->exited = exited; +} + static inline bool thread__comm_set(const struct thread *thread) { return RC_CHK_ACCESS(thread)->comm_set; -- 2.42.0.609.gbb76f46606-goog