From patchwork Wed Jan 3 05:06:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 184614 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp4837799dyb; Tue, 2 Jan 2024 21:11:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFpC/hUUf8y9+LUowuDjVpnXNzQquqPD7sIcG+F8aITa9md6HN1W65RmIddp2o6MZx4W1P/ X-Received: by 2002:a9d:68d9:0:b0:6dc:7ec0:c71a with SMTP id i25-20020a9d68d9000000b006dc7ec0c71amr316661oto.42.1704258670434; Tue, 02 Jan 2024 21:11:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704258670; cv=none; d=google.com; s=arc-20160816; b=zNo+4WwI6KZoXdGkai4lGYMwFELtIVWkwQ5ziBPlwa3Yn333Fy8C46lkr4xvchjsKO 78BZqEDDKmie4i1scbNDVX2sD4tLg39lUvIxlhyheqIkFX/i/juEEQlczB6+ld11XqGm mkbM6I2HITqItRNvzi6M+VfHuZb0MPzr7dA2LHAGYUQxEaRcAh6wHfRjPH7vSEULEnci ayQU/MPOhi6B3m00ka8jNES62/eN0LbgtiFqQVAZAn+OsnnB3ifS6YePoqcV6UBGSwJb RXfOP5zfzQL1GLL10XVz/nUGeVpMWL846Uc8JTrgWwzPlL+Qb6PgbrENak6lcQJv1s4p aPgg== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15169-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id v7-20020a056a00148700b006d9bf501963si10995417pfu.229.2024.01.02.21.11.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 21:11:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15169-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15169-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B545AB22216 for ; Wed, 3 Jan 2024 05:10:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7755D18EAB; Wed, 3 Jan 2024 05:07:14 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787044739673606582 X-GMAIL-MSGID: 1787044739673606582 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)