Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6935705rdb; Tue, 2 Jan 2024 21:10:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbXbsqKSOhQqM1KYqXtD8Bz1UWvOvIbdoOkRqDX9JQ4hD9qzs+oVsri9rDUwBBgJUkVDYv X-Received: by 2002:a17:906:25d6:b0:a26:9d1c:a73f with SMTP id n22-20020a17090625d600b00a269d1ca73fmr3505552ejb.297.1704258605728; Tue, 02 Jan 2024 21:10:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704258605; cv=none; d=google.com; s=arc-20160816; b=iFjf1i9FzUhRaUzJNU3NK3/jctzOUj0iB4l7HKhZ7fljwcRyZdZRt7DFf23oDIZjpu qwSYQJYOXWsqzI88/M0q0w/jkQxu6BJOrEz6DNyKZ1cNn+cdyg0zy4juGUXQZ0jOdoK4 VfHPx/ECs6pJw9nD23OT8wliMWDQAK3yjJ5OrEM7Zg31HB1BRg6jAOsXDyEG5kdWxhO4 pDjNowhL0nxJNX+xKICj33tRedGF8rl6mprnTh/2OHpijGyHsi9mVsztFs+PgsDCtjG/ XkZkF72GjqFGqu9Twf7bTEXwpUCDgKKyN+S8zbkv+JlUD4KAdnPFEF+z2CQ8Jn9o4Rwb mwsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=Bi9+5j5nUNHjq8o6Sc1oAY+K6c1o1N3jOoBjl62w75E=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=s5M09iCko1Tt+UjJ6jIOEiWpdVnERVbbfV2VRW6qyQaT/7lHmWawgStrGMW4JJGMBT roJtHioOu1bd2+ll7MS8rHZ1IbZIZ7S/CeuYGKRcn5xEbm3AACd00z6hXDtb7GyEefZ8 g+0+R0yTyBL30jaKjnDxoarY/mHZMUKVIr7I+0zcbX1A1/QCGAd+N6L8dXMfHaB6IPnw Dj39FhlIeVpQSqHR6Ez7Tt0VqamVULB1jZ5GOSwBSW4TzSMtM0tkkoQinFX3iIR7Vnlz h43QunMc5edbayFZPg48V8GlFproKH0jKfacO4SUly06fmtaHmoSk0RgYrbhin1wStUp MiYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yDehJzo0; spf=pass (google.com: domain of linux-kernel+bounces-15169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15169-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id gi31-20020a1709070c9f00b00a234b6bc08csi10693578ejc.262.2024.01.02.21.10.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 21:10:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=yDehJzo0; spf=pass (google.com: domain of linux-kernel+bounces-15169-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15169-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4B6351F23A75 for ; Wed, 3 Jan 2024 05:10:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE2CD18E35; Wed, 3 Jan 2024 05:07:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yDehJzo0" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F8A818E05 for ; Wed, 3 Jan 2024 05:07:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5f03e3e39f5so63461977b3.3 for ; Tue, 02 Jan 2024 21:07:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704258430; x=1704863230; 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=Bi9+5j5nUNHjq8o6Sc1oAY+K6c1o1N3jOoBjl62w75E=; b=yDehJzo0ISFSpvp3tsFbRrOXUcmay5fcFoUF3GkGJLurmyvLCIFDP/J8z0tTlzfHUk 4bp1aQ/Ad5v7zy0uXf8s2cn+I/fmsMjzsRgdwF6vMXItLid80M55uKN2WK6mkgvK/gOF Lqfddf8lAxa5v4YkB3xa35kqbNUWrSWSXIRqL3cMByV0oRqVawcICyj5ev9PpLUyWfrd nuv4axfLL1b2ZWAv9jem/9rtK5xloRx4p751hBgSMJKntXeqhOcs/jdJp3uqkhYoCoPE wKmkB+L3/RSWG30BpxparPwWI4EXxollpMWDAw/RHuRpzbrPq3kswXadM/4sUleMX+cT ER1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704258430; x=1704863230; 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=Bi9+5j5nUNHjq8o6Sc1oAY+K6c1o1N3jOoBjl62w75E=; b=jOLe39kRMVUn+8+QSbA4yx184NrCmeasnUdt2+9bUOCWFNW/SAh07NWAXAqDzEzz3J 9Nq6CRkEFewOq4BouMAQT3YcdBVFzVt1YiEIU+kUAY1xBraOiL/GqiJO0S/gfNkwiNJB ugv+ht3RCRgh5Ki2GLin/6qTXZWLDoGu6tvBrRovA4AfEX4ltaBPXpRJ5p2FTaj21Vpl hNonKSaqSKwd24dwltpObgjf8B1yzggSyyUn075kbNNgXmzxl/7AoXGJsS0PWItck9aK dqawgeLWejIYzSedBINP1Yxhiqxgcvtmvj3MvntbYr6ReAe+UMRrlg/2ONxEC/6lbSmJ n5+Q== X-Gm-Message-State: AOJu0YyAgtArBXd4uJIEUJwCiYoqXvrvQMR0f7Ynxn2M2IUYOjxCmQRJ XgXy7RVIeoypCJsLRB2pwrjSm+pZtayKGmxX01w= X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:e2bd:f1f6:8ea6:8d6c]) (user=irogers job=sendgmr) by 2002:a05:690c:d1b:b0:5e8:e6d3:46a6 with SMTP id cn27-20020a05690c0d1b00b005e8e6d346a6mr9663854ywb.10.1704258429725; Tue, 02 Jan 2024 21:07:09 -0800 (PST) Date: Tue, 2 Jan 2024 21:06:20 -0800 In-Reply-To: <20240103050635.391888-1-irogers@google.com> Message-Id: <20240103050635.391888-11-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240103050635.391888-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Subject: [PATCH v7 10/25] 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" 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.472.g3155946c3a-goog