Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3429284rdh; Mon, 27 Nov 2023 14:13:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHfsgN+lfeEhqMFVnpU9WEVEhuWIbN8W++BSusplRScFDveRGZzuAaC43d85TorOLkOB+JU X-Received: by 2002:a17:90b:4a0b:b0:283:2873:8882 with SMTP id kk11-20020a17090b4a0b00b0028328738882mr15158105pjb.10.1701123232177; Mon, 27 Nov 2023 14:13:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123232; cv=none; d=google.com; s=arc-20160816; b=xmv0FXONenuoSYe0ZDXUuqLd9bfudh2N2Ifav9nPHVfIEZF1i1hTZEfBLMeXOTkbVs nmEjk194iE0ydYMdXGW7bn9/SL7p5myKeYhWO+ELfNlLzEuycbrGMk90wNdwEIOv+bfn MZ9nLqd74BH6Ir+yvdQxMpfKoSK370ZItHDjj5uC1lC6ipIcbt38ttGcOAEktwv/vhF2 SLu/IdXLqpWlZZX7oF83uNY/6WjpP/enbQxBxz+Xu0ixuJLD2VgqYpXmWZIB7FT6L3XX p2ijHhf96DIa08X1qFdcbRHN8svcmDjXVyjPI0s20C9SeuJOhpja+shmzzTLsIOUlUSG zBxw== 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=DE11asDQk9rpQtwkVS6wVBngrVh7woxSSqgHs8uGQL0=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=hV7ZmFbuFOFjr6N7DyFzY+Zn6u2IlR3crDL2HROqsHx1L/qplyhkuvpyAqTIjbcDnu SWPuqjbbYye63tbvA8IQc9Q8Tugp7a8rJZ7tX/+lneysgSWIDbKHiN77Ax2zAv6+a850 twZCQke6cUUcjqhDrBoq+TtFs6YXgYP2UFmsBshHfLstNctH2ootFqH1fXQ9pNTy/Otb d8oE5+70yI+0L1gjytbn9wVSNbycdgVjCA3HMdLJYtk3CMsyy91sdNvUjhpdz1ALL7Ds wXrPbbnsVxegESZKEd9ZMp+1CPsRr1IJGymPLf9V88RNMPOGcxceOZJ7v6+CbdL+VPJi Cr0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Szaeqf7V; 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 g5-20020a17090ace8500b0027d113631f0si10392967pju.24.2023.11.27.14.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:13:52 -0800 (PST) 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=Szaeqf7V; 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 8845A80A5FAF; Mon, 27 Nov 2023 14:13:50 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233629AbjK0WNf (ORCPT + 99 others); Mon, 27 Nov 2023 17:13:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233989AbjK0WMm (ORCPT ); Mon, 27 Nov 2023 17:12:42 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9FDD30D0 for ; Mon, 27 Nov 2023 14:10:42 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d032ab478fso22191407b3.0 for ; Mon, 27 Nov 2023 14:10:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123042; x=1701727842; 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=DE11asDQk9rpQtwkVS6wVBngrVh7woxSSqgHs8uGQL0=; b=Szaeqf7V1DZHccV6SqWWH+R5bYAinpW4i6wG8xdUriunnpMJchdnjappz1wzO/gVNw USFAl6eawT7IufaTcAXvdR6/VYFBvd9mG+62df61ZjxtL8TXTsdy8jknviYFc8hm6ucs Wbh/6S9Vyg3GvWmyuVgZkemoHEJdb1KUEv3X7nTBBv2GP9/ZzXt31PnNN9J+oI6L9qaX O/Exx/U7BzxkvUOBxPD5el+mHS5dfJGcnGRYVirFbtYbGEtg/wDqdWwN9+HaX9X+0o9N sF7GI9+PLoEoX4ETJO6TkZ0a2v1gdEVtxEWh0yjgpdiKq6R5/0K34CKi4XyoaI3ofKvw p7lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123042; x=1701727842; 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=DE11asDQk9rpQtwkVS6wVBngrVh7woxSSqgHs8uGQL0=; b=xVP73KXP/iOxfl93QZs7ZQwxUlk6c1sQM5ioRyov6pYNlPPWkx2mQa+XpTkWPZ135/ BkVp1M3u5B7vjx5P0MMoswFJrEiioFFOqoYP8d4pHMrUkyLvG86Osg+951FgReDhs/4u qWpx+WhWMVaQMuLP4lfuQMmoJz2Hi0dpmDt7sGt4uRkGnb8bTX8vC+A/ZhPrLsZb0UQF rnOpfXd6/gYC7sySCUrOncbQ3tizFVhbnEUtLvOAeNPHNFXfC/h6rKuII9ti351jrC+g ZTcMC0KVpCCbY8lIGBtpvxOZnUFv9kwsFCkOdjRaqoAcxFcFnS4c2FLrtFmfdO3kdzGj cAkQ== X-Gm-Message-State: AOJu0YwurZouJCGZlExR4j6WOhnhNbJQNvYJ0uIZvk9A5nT/81btIKIQ UUiWBXJoMITHI9ZLSFNJ8iclV6fY45no X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:ec5:b0:5ca:321f:a368 with SMTP id cs5-20020a05690c0ec500b005ca321fa368mr517772ywb.7.1701123041929; Mon, 27 Nov 2023 14:10:41 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:46 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-35-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 34/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 , 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=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham 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]); Mon, 27 Nov 2023 14:13:50 -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 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.43.0.rc1.413.gea7ed67945-goog