Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp560485rdb; Thu, 2 Nov 2023 11:08:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6YpfkUVZ4ZefcK3PuvEYlwavQqLVR4nsoDpXNNDdz5gHbi+IdkdepOr4i0SeZR+vfV1Jc X-Received: by 2002:a17:903:442:b0:1ca:8541:e1fe with SMTP id iw2-20020a170903044200b001ca8541e1femr16038348plb.62.1698948504023; Thu, 02 Nov 2023 11:08:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948504; cv=none; d=google.com; s=arc-20160816; b=B/nYn6sYkHJeidt1QsANGO66yx6ntwsLmwVXCYVBXAwalXSkjZo7T4Sk8tMWEHXu9w gZpYOaz+2Xgt+dnyUZ03nnpjg8YoCaMBOSYS5umVwdrCHQ0rckanQX06VZp4qa1n7Vnm jtkKtFKIKyxjNMH5jeg4jjEW95WW5CqRfmqpi4eeacezWbdruW9Bx7IxcDDG/HfK58aS 0FADKPW90CZPvNQRNTArB1e2fahSf+GXIHOXCqRZzvgIPyiZfwlMPrXVzCnTV+GRQY0K nQpmpIEcFULDOirVRtSu9nuTltmCVxn2vrhgXeSC93a9oZg735LObuffKfyO8aavSVh7 QltA== 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=jyqaewET4n3xW99qLn64pqtlx1KxaY6rTSlGiEWPc/8=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=nPvx+nBsaQwRB2KliN9eaALovTCLF5idfdxeaWABYgT57fBcXUebBBlbiICxJH4U+I j5O274Z4nCbK5IZx3uVjSIzrJzz4cY2uK34TH094mOJLCEJ+0B3FwfD4FvFFjtopba71 PQLo8eYc2nWiPtNzc/vbpJ9hSI/M1yE6124YwBbBZJuE6iN2wIjNKAIKeDHPtj1j7VIq +LiyUq/++wDlhKv1u0Qs8kF98pCsGeErwdto6PX+dEJEu8VFZUZFlDfCjO6jUhx4Xrkr whhQ8DD3r69jvFAv82SLxtY0vUQQ5ait26mhCuLcjppeudsJKRSHMZ/pr13PV9vQNRjo otpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JylFuvJV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id t9-20020a17090340c900b001c09d893c94si54813pld.612.2023.11.02.11.08.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:08:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=JylFuvJV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 932CB806D7C0; Thu, 2 Nov 2023 11:07:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344655AbjKBSEj (ORCPT + 99 others); Thu, 2 Nov 2023 14:04:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235313AbjKBSER (ORCPT ); Thu, 2 Nov 2023 14:04:17 -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 EB4C21BE1 for ; Thu, 2 Nov 2023 10:59:21 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da3a891bf3aso1525995276.1 for ; Thu, 02 Nov 2023 10:59:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947961; x=1699552761; 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=jyqaewET4n3xW99qLn64pqtlx1KxaY6rTSlGiEWPc/8=; b=JylFuvJVKZTu05aAc+DFdRzttQe/w+toVo46ht2/XVqiAY1PUrNCUZjB003Q3IO2zW EtSTVD6jYbVK97iiT7HU2XMBq+Ximd5eBaPnW1s9d2frRjVoQ4H16r2LY2qxn5mNfTIb 1Xj0d2RDk5NtW+T6MiuwbcUOkWCb+SoQW6ol881oudyhUrS9cXm3sMnXZVulMUAOAN3C Wj50r+G4lMeQWBwao76qbOi7Xg2ZgWEXVI5Gkk+HMDidEml6FprtkxiF37ePLdM9Cy2m 0D7SCegK8EsuQq48JXChb3cG3UrJXtfExdpqqTcsaUkRd8ofsU3n3NkpDoKTj4ylw+d7 QpGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947961; x=1699552761; 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=jyqaewET4n3xW99qLn64pqtlx1KxaY6rTSlGiEWPc/8=; b=IGJDpalgAvGZ/5RaovYs0vGJrr+gNBdZldFxZMIcy1xFky5zaqDJl7VM0uNs3FwbP6 Vne6823NNqzbhWayLWJ+mga2jREOVI5mTr417NuDjCMIkqyEjDvMrF0Pa//Na36HGw8G 0gkEEk+grrruojhVG5GnDncWkY4w0HLzfTjQExOBUIWal0S3/gyeosQpAO0mR2hVb9Hq S34nxGQyIhz5Pmw33TvJo3n8A2tkO5CdCG7pTOaYVhq+dtdl/+UBl5g4GJZj+MBokE4I NoemE20h4uSgpxsEQHasVDsOhlXwBqWPbzMIK0z+SLZI42s1/3orDbSEwkh1FGeJ7b65 UkJw== X-Gm-Message-State: AOJu0YxVg+RtOcyYFLE4TEYodoeT2MatCQRlqSfDnFpn0gxupSxaRzyW NVf0TteMVJgQziwemWGg3Yd6mV4McaCC X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a25:e082:0:b0:d9a:ec95:9687 with SMTP id x124-20020a25e082000000b00d9aec959687mr389437ybg.11.1698947961035; Thu, 02 Nov 2023 10:59:21 -0700 (PDT) Date: Thu, 2 Nov 2023 10:57:19 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-38-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 37/53] perf machine: Move fprintf to for_each loop and a callback 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 , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@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,T_SCC_BODY_TEXT_LINE, 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 groat.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 (groat.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:07:59 -0700 (PDT) Avoid exposing the threads data structure by switching to the callback machine__for_each_thread approach. machine__fprintf is only used in tests and verbose >3 output so don't turn to list and sort. Add machine__threads_nr to be refactored later. Note, all existing *_fprintf routines ignore fprintf errors. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 43 ++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 6d7a505850c8..7e19303d1aa6 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1114,29 +1114,40 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) return printed; } -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 = data; - for (i = 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads = &machine->threads[i]; + /* TODO: handle fprintf errors. */ + args->printed += thread__fprintf(thread, args->fp); + return 0; +} - down_read(&threads->lock); +static size_t machine__threads_nr(const struct machine *machine) +{ + size_t nr = 0; - ret = fprintf(fp, "Threads: %u\n", threads->nr); + for (int i = 0; i < THREADS__TABLE_SIZE; i++) + nr += machine->threads[i].nr; - for (nd = rb_first_cached(&threads->entries); nd; - nd = rb_next(nd)) { - struct thread *pos = rb_entry(nd, struct thread_rb_node, rb_node)->thread; + return nr; +} - ret += thread__fprintf(pos, fp); - } +size_t machine__fprintf(struct machine *machine, FILE *fp) +{ + struct machine_fprintf_cb_args args = { + .fp = fp, + .printed = 0, + }; + size_t ret = fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)); - up_read(&threads->lock); - } - return ret; + machine__for_each_thread(machine, machine_fprintf_cb, &args); + return ret + args.printed; } static struct dso *machine__get_kernel(struct machine *machine) -- 2.42.0.869.gea05f2083d-goog