Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp382801rdh; Tue, 13 Feb 2024 22:38:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW020y1V9warfVmDRQkcGMB/WSpYYZMGmgMigWNs8trzOOd3/AaAAyAzqOUKDbgWAirSybaGiyNXVCbP55tJPgHEMG9lH2bCb5hMJAKaA== X-Google-Smtp-Source: AGHT+IHOGC6SQbJPJVOUwQ3kBIxYFstnuaGULFKUB59n9z7dw1vaNzg24XwJXyOyobSxv8cBBj7n X-Received: by 2002:a05:6870:638a:b0:219:198b:ade9 with SMTP id t10-20020a056870638a00b00219198bade9mr1745372oap.36.1707892737887; Tue, 13 Feb 2024 22:38:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707892737; cv=pass; d=google.com; s=arc-20160816; b=h2Fs53A9mMbSx36X0ydPagteup8PleObk1S2qpatPeAN/PL4iPa+d9c4iDdJRLH+13 j6puki7lZeqU53dL1U5IQ0FDqMgMd4CgiPzMxhOhOWcjmYg31Ugt9Do8TzMOC17K3zBm 3J3VpKrMjJhXV8fa39dey91cvbm01b+FsKvN40xYkQGddCnq0lpuOdASqCPtUh7gbm0p qSF6w7cC+b5vOj9Qr8qDW50vx4J/Dr2LI8QiYqR3R16oRjEjby1WWs+/G/b017Rrfu4h 6HhdOgxzL5A1WKPRNY+BQOIGItfIgWLzkpNpIGOwSorljDz6T8b8/75z7X6EU7aQX5cS pRog== ARC-Message-Signature: i=2; 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=CANQqurHGmF5Ru8GYcuqg+/9JMey73Sl/dxU7/wCOGk=; fh=yDR9JE2iiIIYJGUvJyodXkmKUUpIL1y7qDvGbYxukzQ=; b=mhyBtW2Gi0IUxZOhyzPaeJ3w/4YPgjWNT7z0+Ee/n4C3VHneMQvdl5sdvFieAW9mSo 57iHazElbulEmmQBLx4zhiXCMegYwZ9bMl3hNgMqscYoYeaMzqQBaCA1VhsbzOdeQS6a Rb7acpbU7O8W4kLW8bZP3RoXQN7xqZOLqYAmVVEbEohh50hqy97UQ5ae+Y9gE6kksEq7 JVdOBinLhduyLpvCbn87U60pNh6z7Ac4+viVE38z9StcdzL5CJOSxihERmMGfV2Vomrb vwemNMgjv88EqHHPy7l2ZHJnvAq6Dr8StE04GZjpgeGRL3ET2hURgRy8gGpkNjY79/V5 uxGg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ESIxsA0b; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64798-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVGlglMbDskV0jxOHI/z/EGUxcSDAfw3YjVhVYTQm2f87tqXb8Rx/YEYYpsmeLhjOiFiyq4aH5055RrLChAjoh8pFu4HASnb+EPnQDZ3g== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l185-20020a6388c2000000b005dc87994572si2059039pgd.346.2024.02.13.22.38.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 22:38:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64798-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ESIxsA0b; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-64798-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64798-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 82DD42851C3 for ; Wed, 14 Feb 2024 06:38:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3D97915AE0; Wed, 14 Feb 2024 06:37:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ESIxsA0b" 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 A729F12B8C for ; Wed, 14 Feb 2024 06:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892650; cv=none; b=cK/QtSw+81i40Fm9KI8NdBuIjL+K+JEYpa+z5lg5F2eWbCekEZNHmstL4v9dE4WZxFNnsf1TAF+wsiOoWuUm0febJPzfRwNA6gt/MoaTo9S42UpxaO5ruis9A9AZUTprriQoa9Cr4HY977ep/xPDQFPRaBhpwqpLVbkcrIJiZyk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707892650; c=relaxed/simple; bh=gi4txDdv0C9NQjjEzGpOMktXyzhWLm0PbyoiCg8lenM=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=CqttRdZG6qt2sAhVtOFvXLkaapbJI6RHlV+3L6u2tYLD+LUU7/tGlbYWqQABSAFhm/UAk+v/5YLge5d+SpA81LceybQFyw2Dr1xxh0QGSQ+dxYjJmoOIBRl65hD5Q/zHeyM7GiJGXVDTQlkzVa+k0Pzjz/BcbeP47TowPQgp2nA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ESIxsA0b; arc=none smtp.client-ip=209.85.128.202 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-5f38d676cecso7062267b3.0 for ; Tue, 13 Feb 2024 22:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707892648; x=1708497448; 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=CANQqurHGmF5Ru8GYcuqg+/9JMey73Sl/dxU7/wCOGk=; b=ESIxsA0biR3o1+9gLHaK9lU9NuyJ3HJw8dfsMYdRxzdEbvKJSKymj7wfdcGS6+pCnt 7uJr4q1gF9qcVGUd80IbaPzap8aTLBguMdLYiZMCgezMl9uN1P80Zzi2f90lhR6D3dJs v5Dwd3nT6LFpctpm2qV+bUkLMvp4gGjwsFraKvZ331HeaP2vCpzdEuqxIZ2a9iQfHs5z 79JnYSpUGAeKYxGGclHdY5F58gJYvYUKA+fDDZkhPwYfnEQoiXBBTr+lyc50wVdkdPGd K5ObK6hZZ1dEVMMvgpzZFE330LSO+AO7ht0FWy25dbS5aoqm96sxb/7Ix54W6f1mu/AM iCTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707892648; x=1708497448; 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=CANQqurHGmF5Ru8GYcuqg+/9JMey73Sl/dxU7/wCOGk=; b=veOzy1zQ7A6KIZpO21eYDOUaeX+JHZKGZphInxzr1lAstX9qh9xKlpq1690OX1RVkp VPZaNOPsjnP+baAYwbro2dMEJB9e9IHSYhCjDFjmp/djk+jhUn5wANRIKvxAtwUuOaJZ mqHjST7tKxSkV7PJNTQZZ9YXQdmvlbqsDMcMaMkKefO+4G1RfgussJHAkTV8g9161pbg COHSjW54jgHrNgAvW/R8KDzP2UGzTpdt1hetDBZTos8Kt3A/KqOLcwPauWTMyRFh3Od9 87VeDzuBSUkkoBKs0Bi7XWMttMhTgvNLWMyeRBNYRHGBdmtwFnJDrGu1+XCO00eRPUlA RfBQ== X-Forwarded-Encrypted: i=1; AJvYcCWhFuHyb6q9kTIal1Kkud/YxgOZlzbbFhTyn0gVTYzXRz5a8/qEPaUx2rJIaOu7PvJKB3Jx/No34f2lccex6f1b+dp8at+UrB3qAxHS X-Gm-Message-State: AOJu0YxlLEZYOnskHVoE0T6nwABrUMJDeCtbV4nk0mtD/pvey9Fxfe1J jUMo8lY2am1lhzuaCYt2HdoBzHVXko02ESUOS1slJwm1KIXapxRAXRHycmEaxIU77nbUxLY0q9B LG6WDag== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:6d92:85eb:9adc:66dd]) (user=irogers job=sendgmr) by 2002:a81:4fc6:0:b0:604:125:f0cf with SMTP id d189-20020a814fc6000000b006040125f0cfmr232032ywb.2.1707892647799; Tue, 13 Feb 2024 22:37:27 -0800 (PST) Date: Tue, 13 Feb 2024 22:37:05 -0800 In-Reply-To: <20240214063708.972376-1-irogers@google.com> Message-Id: <20240214063708.972376-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240214063708.972376-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v1 3/6] perf machine: Move fprintf to for_each loop and a callback From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org 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.687.g38aa6559b0-goog