From nobody Fri Dec 26 21:32:41 2025 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yDehJzo0" 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-Google-Smtp-Source: AGHT+IEDijvBcMosL0o4vT8FVyZhsL7c/ItJiPzS7LcGkR6zBRPRPz+s7gqRh7DsQZKq594/PkTvuPrhyAH+ 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-Transfer-Encoding: quoted-printable 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; } =20 -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 =3D data; =20 - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { - struct threads *threads =3D &machine->threads[i]; + /* TODO: handle fprintf errors. */ + args->printed +=3D thread__fprintf(thread, args->fp); + return 0; +} =20 - down_read(&threads->lock); +static size_t machine__threads_nr(const struct machine *machine) +{ + size_t nr =3D 0; =20 - ret =3D fprintf(fp, "Threads: %u\n", threads->nr); + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) + nr +=3D machine->threads[i].nr; =20 - for (nd =3D rb_first_cached(&threads->entries); nd; - nd =3D rb_next(nd)) { - struct thread *pos =3D rb_entry(nd, struct thread_rb_node, rb_node)->th= read; + return nr; +} =20 - ret +=3D thread__fprintf(pos, fp); - } +size_t machine__fprintf(struct machine *machine, FILE *fp) +{ + struct machine_fprintf_cb_args args =3D { + .fp =3D fp, + .printed =3D 0, + }; + size_t ret =3D fprintf(fp, "Threads: %zu\n", machine__threads_nr(machine)= ); =20 - up_read(&threads->lock); - } - return ret; + machine__for_each_thread(machine, machine_fprintf_cb, &args); + return ret + args.printed; } =20 static struct dso *machine__get_kernel(struct machine *machine) --=20 2.43.0.472.g3155946c3a-goog