Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1363604rdb; Wed, 6 Dec 2023 17:22:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjgT0CK9N09+lBP3oG23UKX2KIDWvxvlRmbZ0vNW1x+Czz3FH7Ah2OF7ozZ6pjicBV+4KZ X-Received: by 2002:a05:6870:ed90:b0:1fb:75a:de67 with SMTP id fz16-20020a056870ed9000b001fb075ade67mr2120427oab.85.1701912148660; Wed, 06 Dec 2023 17:22:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701912148; cv=none; d=google.com; s=arc-20160816; b=FuxsMbm3U59sKz9gcpFG2plWvCO/0xVpzADs6dA6A8FfpsJipXMJkOrCIe5JUZYEWf vgvOSiLnGtltwsSzfnNxE8lD0Llwop7fFRdf/CMkzj/a80G9QiS6f8Ls2cyKskLo3SvV To66+NseQPmDIOzc0JQ4yP39BYaZLtFQSHhgnExUX/NZRr+/7No0yGmcPVLJzhMLtRN5 Xk5ee3EMv2qxwubErUbWZyHCYSSRMDXQOTu4plCzQ8GUYHrClL0sHz8g+CUwzafXRZtH AsBl0xYfGLXB05TfPJmYhNxlhrc8BOzbws80KNnC+W6IU2ryOxg4NcKnrHrnXYgDg3qg 8w4g== 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=OPdxRcwQgxtYBVUuxZI3Rs6EMAh++C462IioMxtmXNk=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=kFTAmCmOYvqFDuMXWI//BYsX4FhMuMyCXQ17JKYdjOQZvTsLW+z8dRrOWEBJFToCXD 2vI2jTGtN827pIJJ4GNziDSBeeWp9rHO8HquwLB5LRykWls6Yfx/0h40eYbbEXo+sQ6x XGrAHlUvNxnYoFOXFvheuq9fikD/PYXX1TLjiG5/nRP3WwTU2xWwFslbcUYMmd+X2+Ba PM2VNM+M6iVNT9cG/XSF87oDTjVtcRva9LQ7fvv+EILoKf64CEwufVd2+AyQZre6s2Qp FbjZAaYjZ8dU7HjrhCjF+4ccOoy/2wNUforFQITFMXZiHV15ErQtGls4R1jn1wVGLlf7 md7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jMrDVgfm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id i11-20020a63e90b000000b005c67a42a55fsi206946pgh.732.2023.12.06.17.22.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:22:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jMrDVgfm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id F2E498411884; Wed, 6 Dec 2023 17:22:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443130AbjLGBTz (ORCPT + 99 others); Wed, 6 Dec 2023 20:19:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442957AbjLGBTJ (ORCPT ); Wed, 6 Dec 2023 20:19:09 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF6C21990 for ; Wed, 6 Dec 2023 17:18:42 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d340a9cf07so1750087b3.3 for ; Wed, 06 Dec 2023 17:18:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911922; x=1702516722; 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=OPdxRcwQgxtYBVUuxZI3Rs6EMAh++C462IioMxtmXNk=; b=jMrDVgfmdz8cUs/jIwy7W2ngjBeog09RGtnAHduDyCVUwlKTYL6CgdtTncCFxeH5Kk jU0V25otZl6oBgUD2xkPCTTtPlx5kweuXSNzMp7tORYfmtyxqXHgazDCkW1PJbYF0FLh 1uljYIZB+/bUC+JA2PGPOOdNqX7PxRj2Fo8Tn48ZrIstYWTt5l59ljcDuKnCYJFiG1BJ t5AhOP3u7VW4XeEvIerk++mNejuK50N9DSfUEPPswemSbBGktYuT6jtIRCxiRusrTIwd AprCTIUHhmbXQL3R1cP1wBzLU0+GsA8PvmMKi/dTKoC72XofyCg0xmHYPTBz95CY6Wqe nz9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911922; x=1702516722; 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=OPdxRcwQgxtYBVUuxZI3Rs6EMAh++C462IioMxtmXNk=; b=l+iGdr0RePoAvFlElbrILI6Ia28iJpMomNn4ha9gIDFkYRVZPTu0KA/CjDqz81c3Ak jME9Lrg7XUM1L3sgdAV34ehC+KwmPyW9DLlpZsS3BUpjUU+7GP1ED6cZdiQA+eBX0fts QZmz9L8ZmmCflYYOWm9d1iSvvxw2Tk64FRw9DBhAFMu1CXc3+XC66fjVNpOjnofrzJC4 +u4wSzo3THGDA+IQM6B+WRQwIVlVR8QC9AfVWi3157yljQJbR4KRdo4WxxH6L4o2x/lW yTl0YhHAtJs/ylHE0rSxReNizkUfy3+PjRa4dcHMYQRs/57qgIKT0jPgo7R5BMif9yPE tWyw== X-Gm-Message-State: AOJu0YwTmVf7sZmbOZKM2rqUj6NZ+YGPpsRKPtD1IQtgRFgFa+WKi3ii fQhLWcmZN8cyQyP5BAvRbORjIAQtqeZo X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:99d0:0:b0:5d4:3013:25d4 with SMTP id q199-20020a8199d0000000b005d4301325d4mr31875ywg.5.1701911921468; Wed, 06 Dec 2023 17:18:41 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:06 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-33-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 32/47] 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, Guilherme Amadio 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 howler.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 (howler.vger.email [0.0.0.0]); Wed, 06 Dec 2023 17:22:05 -0800 (PST) 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 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; } -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.43.0.rc2.451.g8631bc7472-goog