Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp19707rdb; Wed, 18 Oct 2023 16:31:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZCqS3TALq3xfiHBe/yhWuChdKXj7ktQcBQy//gRjvA+NyL/uwLaXrxoK5a9cYOFplhWlU X-Received: by 2002:a05:6870:3c8b:b0:1e9:efa9:1199 with SMTP id gl11-20020a0568703c8b00b001e9efa91199mr1066329oab.4.1697671888176; Wed, 18 Oct 2023 16:31:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697671888; cv=none; d=google.com; s=arc-20160816; b=tmDtVmfKY718w+GD8T3/IaMmnFfhsbTk0AtULfJRIBXu9mPhq6F5u0/GvWCnapy3hU znObKUeEH7IEGVnR1D5FXXpPCRE9qSA0eQBMu2SXrkI0L1IrHfYxq9EsR9zO5FQwYp4c 3OTEBaKBC+Ko3WSgSEw9ChROAAmk4c1hw/CVpa9+t8SOoQQVA+fx0JNNRtcsW95C7LIE L7VyR1ILzdu6m0PdF/b7VKs6RptbpaYc8xkWbN/P08IVEwWclVflzBuBk3xr9FWRQvS3 uMTCSFplfIKbMSz8J/Z6KOukn3lawXFuNylaYYohoh+0+aEN+mgUVvsLjE0rR071cG+d HzbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=o7W/dGNGO+mFpsAMrd1euJ7qDWYXqvZKfCYDsxxIQu4=; fh=E10VZPe6Co8ezsBgErBfA67AzM7mDmfOH0ZUqd+UkZw=; b=zMsbA/clbASempngK0gJlnkcukGIrYnEZKycIf5I8ESsAPjNQmRrNxGq5d/8vGZQTl YkDsACH2ExrPaeTNBAdFD0Ao4Hi88p+IQxk3BWQfDNz9Dn8jJ/qRs52t7Psw30hErIlI 4phcl6AfMecdnrOwqtEKGcjK7OjgtVJ234BidBSSC782iq1T/7RMAozVXxnvLlu2a8r6 58fbIRyVusHnKUmHAq6KgTu6OBzSwUYtuvPDQTSX2t/9Vjx1ACHHulfTv4R7zo6XwsUn rEg4iVa6pm3QoXyRa2/bdaXZRd0y4CTnCG8VTP7bkhU+/kjMoNKeOUqBm1BAxi1MEXpL RWSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id l133-20020a633e8b000000b0058974d3c292si3184901pga.191.2023.10.18.16.31.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 16:31:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3733780A139B; Wed, 18 Oct 2023 16:30: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 S229726AbjJRXac convert rfc822-to-8bit (ORCPT + 99 others); Wed, 18 Oct 2023 19:30:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231548AbjJRXa3 (ORCPT ); Wed, 18 Oct 2023 19:30:29 -0400 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFD4C113; Wed, 18 Oct 2023 16:30:26 -0700 (PDT) Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-584a761b301so5436316a12.3; Wed, 18 Oct 2023 16:30:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697671826; x=1698276626; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6x4MOo5/gvvDjdQbTbVRFsm6OAJqGdIH7cIh4FO9aMI=; b=ai65WB9rtwIQ6y2fgJt5kn0OAUlTebSVaAVswjLRdPvLS/yM11Uu8JAhuBEv87bTlC ZMUHmpzk/orNqAIzpyKUWfoxedM4hlL9k6eoNynYP9pr0z1r2N8dK2mYveYiBE0hAE/T BlMHq3lujsWXEp/Wrx/SVgoe8phY4JU+yMrt8WNzv+WCrZ/I7KnynwMYwO1rDNUmAATL +r+I4NESdP1ywE1spyhdClIkq74VAMv5vB4sxyO8CCyqZwspZoOvvalJxrm1K0qrx5bJ uxoF4if2Eb/3GXpg68njvBPcE5XWOsvB6EAUn6VgkKQCJBi2LzQOUNtGFk/fMhoSaKrM Bq4g== X-Gm-Message-State: AOJu0YxFKP1HEUT9LYtpvQgPnvdMCl9+egC432E1r0TW02FDv35SHUjS R8/FaueE0Pgd68EVIXi/Lyj+UfcJYpJuPDut3bg= X-Received: by 2002:a05:6a21:6da0:b0:14c:d494:77c5 with SMTP id wl32-20020a056a216da000b0014cd49477c5mr679241pzb.13.1697671826126; Wed, 18 Oct 2023 16:30:26 -0700 (PDT) MIME-Version: 1.0 References: <20231012062359.1616786-1-irogers@google.com> <20231012062359.1616786-14-irogers@google.com> In-Reply-To: <20231012062359.1616786-14-irogers@google.com> From: Namhyung Kim Date: Wed, 18 Oct 2023 16:30:15 -0700 Message-ID: Subject: Re: [PATCH v2 13/13] perf machine thread: Remove exited threads by default To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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, 18 Oct 2023 16:30:44 -0700 (PDT) On Wed, Oct 11, 2023 at 11:24 PM Ian Rogers wrote: > > 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. Maybe we can do it the other way around. IOW tools can access dead threads for whatever reason if they are dealing with a data file. And I guess the main concern is perf top to reduce memory footprint, right? Then we can declare to remove the dead threads for perf top case only IMHO. Thanks, Namhyung > > 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 >