Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2186572rda; Tue, 24 Oct 2023 15:28:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHPW1/ybqgsEkli/wGPGl4q/Q9luc6XkxqIl4aqDI087JykMJeYx2txPoG0bUTrwHlgX3+s X-Received: by 2002:a05:6a00:1595:b0:6be:22db:7a13 with SMTP id u21-20020a056a00159500b006be22db7a13mr11583751pfk.25.1698186530233; Tue, 24 Oct 2023 15:28:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186530; cv=none; d=google.com; s=arc-20160816; b=A8UHmi0+Roeem5uPHHMD2+FovhYCtsZicW/rjTmNrZ8gLRgMUMMBmb51ASxUz//OEZ L5GRsYfnMXupY49T4J+S2h2ZJvpMSZbho3EX0PToAMbUHNxkduMLNy4ok4zFB8XUhzjI M82VBXJWdU8CNxSnGLTfP5VmQQEtwN4UdgPAI6NbhimkZk7VRwQDmCQPDikZVX0bSgyf UgjAf9/U1BhmCElp7M/qKw/dszgt5cRA0O6YaU+Avzp/JthSshGdVJz4QF8XA+wjouhD S//j4nGryMnrvGegI2yI0sK5vxlv3D9KzfGPsFeQOGDJq46BybzLxEYG4L6gxxWyAAF3 e/TA== 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=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=GnFbOL/kjk56Gib6VPayw1S03Sa6ViHHVCdIScF/10wzspiWrSe7PaLutkwH0xtkr4 QaS6xskUMFlNlfrQHlIJmP37cgnq++RKfdFUlglHOKC+VqNVc9d3q3FiP0OQtUAi7cc9 gmJYnWhOS37Tc3R0U9XA6fePrflhZ9OEyuPjkXIPkgoyDJ2jsCKqgCiJ3zvs6pKJTRIj jbhFn3KbVm9S2q2Tbxdc4iCRisqkMv4qzno5BR38eFiSfH4Th9dAReWwB+JE0dx77lKs Ppn5VRMkzS9JIpfa14ZEewfxXy34WyW744S1DHrvmc7eM08RR7pMas08O5+hFKA1Ro58 vxug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpYDtR5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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. [23.128.96.37]) by mx.google.com with ESMTPS id p22-20020a637f56000000b00577448019b5si8743966pgn.276.2023.10.24.15.28.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:28:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=rpYDtR5J; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 7349480CF529; Tue, 24 Oct 2023 15:28:49 -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 S1344478AbjJXW2k (ORCPT + 99 others); Tue, 24 Oct 2023 18:28:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344627AbjJXW2S (ORCPT ); Tue, 24 Oct 2023 18:28:18 -0400 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 9FCFC1708 for ; Tue, 24 Oct 2023 15:25:59 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a909b4e079so58031037b3.2 for ; Tue, 24 Oct 2023 15:25:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186359; x=1698791159; 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=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; b=rpYDtR5JPG1CYbQSVc3Eb9sUIcTDJspUBSSZMewAlkB5arZfbDpqaRHs6oQzoNAgrV xUqP8OuuDuRIr7wXrw7xfA4ypmlBYjgjIN5aRM2Y22OAd0Bq57s62GdCmRK4yNVQ+wrd f48AtAiNsuawowuSE/4fov4a6DmCAogKssgkp9eNQpjPgZkC6u761+mYulbmMpF4dcRE GQweSo5W246hfvF2ryMJRdsNh9OL5UxQCRfAINfh9DsQvwGnnR//kl0nRocC7qcJf4uv IYGi7ApvaMN+xBxMXKEbH/gFEofhyPEb6RDr9JAj4n4g7F6lIQhKpoa6+jXjhgnwAoXi k7bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186359; x=1698791159; 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=NbxTM76xqJorkOJkA8b8csCodDzMUvbQpbWTOjkDtY8=; b=ldp2qI4xMkTMJ0QLPWyugcTeNI/hrHdkALfZzglV4VTHBmtWFvcboeOViNdgSMClmj pKtO9mw50X7b9Ojs2IJoJA9EPptWJ2+2QOSK9Mgc9R1I6kKrhmuL0PGPAMArkJ8NNzkD UHwu6dVmeH528pYAXKQt+kmhnTs4qy/yEFLUHrghoCuUdMi8Km93rg/9T2s/owKMphU6 blaWkHwHNVLMSVsW7IqNJr8w3m2wmREQWLH0hRoHO9cMBDVsgXzYskb9dr9ZAFeHrBls OxG4Qv9+ePTQWJoi2+DzRpdOyqsP2MagIUezZ1gMXtjzrmpoSWRLxsucZ3aJZNuk+XCz WHeQ== X-Gm-Message-State: AOJu0YzLiTyPISrEhuuCfSYbAkWFDJUFsIs6tRTGJdyO8yDVotpl+PE7 lWISOLnc0Ol4S73cnQ6rgOW+r9wbKHcP X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:c5c1:0:b0:d9a:ce79:6298 with SMTP id v184-20020a25c5c1000000b00d9ace796298mr231994ybe.2.1698186358799; Tue, 24 Oct 2023 15:25:58 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:50 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-48-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 47/50] 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 , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 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]); Tue, 24 Oct 2023 15:28:49 -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.758.gaed0368e0e-goog