Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp45127rdb; Wed, 18 Oct 2023 17:39:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH9ySaAha69t3RzOrJFVHSU5JA0VOryHQ8zHqgnJ/t/lj4d98ZDBA6+hBAMKy4s51qZ3tTp X-Received: by 2002:a17:903:887:b0:1ca:701:b0c8 with SMTP id kt7-20020a170903088700b001ca0701b0c8mr942700plb.8.1697675988045; Wed, 18 Oct 2023 17:39:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697675988; cv=none; d=google.com; s=arc-20160816; b=h2ONHUjr1zVVBe+LICUsyAZSS98IKFqEhkAb3kY89FjpPgElCsuFSH8PzDusji4EkS mjzG5LAO2himNpi4dMvDns5QfVFGuNIv5bcgys1gjyr5d4nEK0Nh24UAUbgrPp0STrNA 5s1lw1f9HpS4PHphZ3odLNflDiZqAz3pfHiS0cfyW0+hOXFyYnsqAZKISDxK94y5yeBh fEC+veBPLEqQbAqr7EMvWpmCAxhetdh57Qi71mTg85h6UzrddJJFTMP/gHsmzOZUBnVs tVk1NLdxHhkG7RSV++FhTf3zTriBEmJBZo5R2j+4ZAGvBwrhBxA26MXczH+Zi5/4fns4 9FZQ== 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 :dkim-signature; bh=iWzCLiLZGdTDQE6Mm3c5UmIIcTECp3y/VYTIRTvbYV4=; fh=nl88BES6xn7UI3tGEip4+WC3AAfutaQfoa4Vp0+Z3z8=; b=xBPhoIeslrxDrjXlVqXplExmfotmdzJ8BJLlzOcOKu0GTflC4Hiewit8PY3N2OSVIx 6rwOVfTSxeA6Xf5dliQgB8kL1TZhIzNNmQ4LSy06cRE2k/vbydg9Q0r2bQQR7LpQxaKY m2oIk+tivyeTcs2c7j4kMsC1eitjcvc7vqtMQFdDi074O9zP32n2DRSNdD1tGG2GrkWk dbMK/IbcfE7L+6F441+LvPjaKe8loCj4dAodZpEDm5BQlqnt5/3hn4l/IHW9e+FAgyJE 5MXRih6PYxiKvmmsfpuZLaOT9GHN4Ct9dT32Uaorne3k/TKpBZqVXIDd1PZCeLNcAFMy i0Mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="U6U/OWno"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id f18-20020a170902e99200b001c610ae885bsi1003379plb.59.2023.10.18.17.39.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 17:39:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="U6U/OWno"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 114308213F05; Wed, 18 Oct 2023 17:39:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231470AbjJSAji (ORCPT + 99 others); Wed, 18 Oct 2023 20:39:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbjJSAji (ORCPT ); Wed, 18 Oct 2023 20:39:38 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7104312B for ; Wed, 18 Oct 2023 17:39:35 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-40662119cd0so21685e9.1 for ; Wed, 18 Oct 2023 17:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697675974; x=1698280774; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=iWzCLiLZGdTDQE6Mm3c5UmIIcTECp3y/VYTIRTvbYV4=; b=U6U/OWnosDk6Gs1AtcU0u9pf31irMk4F8qLim6fgpQmQPsaT5xspNc4OZcQFnd6CNu f3YYjPYCiRudNUwZCCz9sDhqk5NGlCUR/MO00/NZjgcGG0ZSnwOK8Ml3W0+t9JVCtMzQ CWC1Sp2fn4vqOLXJHiuWIEO3BkYK7Fj61DM8mvCOTorb64G0FfKpn4R1SNxZBvAKGOHG PcbyJMimEQoZo5gvmDi3TSnnnTa2Tp/a9JRwVOu5t+4t8iwy09rUgJpDu5f/fVZLkg5P xNMUyEgMYnP9Sjas/KQBky8w4Z+wpsu8Nd6nNUBF6TjOIavFq3Fod3t2e3vOCXFBA1r9 JvwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697675974; x=1698280774; 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=iWzCLiLZGdTDQE6Mm3c5UmIIcTECp3y/VYTIRTvbYV4=; b=WXUuLpz9Yfai6RZPnsdvudyA0N+kj24qlNDSiEyfHlmvBKi6u4ajr34Dhh+jSvrFmJ zsnG6LfocxPHVYiwF6gX7TdUcP5rzgLXvURX9q8N86HaoN1fAYnZqoGStK6iJ6ZITMCD CuCBpyRX8evPohsLVb11B6s2LjRDNak1OeMSI9rAngLVVp+Q6bc8iCOE+1SbCfebA9Mx itNhubkY6FN0cSZvP61Vmv7Mv5Uksp3rvOeBmd3miY0cJylY+94syyFZpD9S0iotqSjq egj4dOKa/ULdAsLMw6YrP84pkSkCPN8LlCGXMQQOtfWYAacYm87M7+b8Og0wm7BM27Dz IrZw== X-Gm-Message-State: AOJu0YwB27B/EUpQLCfnlyP8+Y01NgfWfMTaFh6raIR/FIkFqwFsO4kR 2VO+ud+Om4Eljkfz3iHwqk2hQtQb9EhODVXLCeyvQg== X-Received: by 2002:a05:600c:1615:b0:3f7:3e85:36a with SMTP id m21-20020a05600c161500b003f73e85036amr38163wmn.7.1697675973589; Wed, 18 Oct 2023 17:39:33 -0700 (PDT) MIME-Version: 1.0 References: <20231012062359.1616786-1-irogers@google.com> <20231012062359.1616786-14-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Wed, 18 Oct 2023 17:39:21 -0700 Message-ID: Subject: Re: [PATCH v2 13/13] perf machine thread: Remove exited threads by default To: Namhyung Kim 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: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 17:39:47 -0700 (PDT) On Wed, Oct 18, 2023 at 4:30=E2=80=AFPM Namhyung Kim = wrote: > > On Wed, Oct 11, 2023 at 11:24=E2=80=AFPM 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 I did consider this but I think this order makes most sense. The only tool relying on access to dead threads is perf report, but its uses are questionable: - task printing - the tools wants to show all threads from a perf run and assumes they are in threads. By replacing tool.exit it would be easy to record dead threads for this, as the maps aren't required the memory overhead could be improved by just recording the necessary task's data. - offcpu - it would be very useful to have offcpu samples be real samples, rather than an aggregated sample at the end of the data file with a timestamp greater-than when the thread exited. These samples would happen before the exit event is processed and so the reason to keep dead threads around would be removed. I think we could do some kind of sample aggregation using BPF, but I think we need to think it through with exit events. Perhaps we can fix things in the short-term by generating BPF samples with aggregation when threads exit in the offcpu BPF code, but then again, if you have this going it seems as easy just to keep to record all offcpu events as distinct. Other commands like perf top and perf script don't currently have dependencies on dead threads and I'm kind of glad, for understandability, memory footprint, etc. Having the default be that dead threads linger on will just encourage the kind of issues we see currently in perf report and having to have every tool, perf script declare it doesn't want dead threads seems burdensome. Thanks, Ian > 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 th= at are in > > + * the data file. Off-cpu events are synthesized after other ev= ents and > > + * reference exited threads. > > + */ > > + symbol_conf.keep_exited_threads =3D true; > > + > > annotation_options__init(&report.annotation_opts); > > > > ret =3D 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 !=3D NULL) > > - thread__put(thread); > > - > > + if (thread !=3D NULL) { > > + if (symbol_conf.keep_exited_threads) > > + thread__set_exited(thread, /*exited=3D*/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_con= f.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 req= uire > > + * 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 th= read *thread) > > return &RC_CHK_ACCESS(thread)->refcnt; > > } > > > > +static inline void thread__set_exited(struct thread *thread, bool exit= ed) > > +{ > > + RC_CHK_ACCESS(thread)->exited =3D exited; > > +} > > + > > static inline bool thread__comm_set(const struct thread *thread) > > { > > return RC_CHK_ACCESS(thread)->comm_set; > > -- > > 2.42.0.609.gbb76f46606-goog > >