From patchwork Wed Feb 14 06:37:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 200827 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1025357dyb; Tue, 13 Feb 2024 22:39:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVh30lYNmsKEHEIZASGm3UKuPIUKkk14hBQvlcKIBkQhrSlU4aeWxasCFysu5rYAEaZk24Lfvp8FC9j7JKI1pFg/9e14g== X-Google-Smtp-Source: AGHT+IGFUVIrCnsTnQUAgZkVcGM1oPUQiQ8vWtEIqVhGi5aYB/30GOdvNRqzIND8JSKBfew3eHBJ X-Received: by 2002:a17:906:3bc5:b0:a3c:9ad4:b8ff with SMTP id v5-20020a1709063bc500b00a3c9ad4b8ffmr1122473ejf.49.1707892756350; Tue, 13 Feb 2024 22:39:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707892756; cv=pass; d=google.com; s=arc-20160816; b=qZ2lgxA7/G2PZ6Ku1pvhulbWU9xX7dgdszOlMI3ZtNKZCEfr7Uz3VRDOJ+Ss3iogOv MIPva3umAvxYRfOc7D4CEOonY0g0Zk7oX+T4QwbX+WZZAp3kB4RTckZ0PeTkBgM+1GaT Mj2RKdiuOPtqL1B6gB0wsPCjzC5jjtF1PoP0hO5wUP1xpi3l/Xh4oB718KSvGr1xrdNy 2A6XWcOpe7Prz+747SmehX81VFuknTF1KIs00uhC3Gly/d8ck6+AkgiCCUCw3Couu2Q5 xdG21ETYBSdMhU0N8Eqs0c8nq9ksdJZA+gb1YfDXNtbpgvdkJyVvqjCLMES52UFLIYY4 XOyg== 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=Uy25Dkh9sAcF6Rhan2EjhyJq1ba6maH2cFFX8uGor+k=; b=ldytyvJYW96Ln4SKngMGd3E6RiAGohNB391qyttX/d2ckY+IjYhIOZ4MKYI/JR9bMO f9j/HbUp+kinIXDdIs7fsMh6qdYV4VT3muT8pnoHe9TGt64c/rsYUEHaHWSFfW1Ogwqe V5n+RtCJb+sjRQJbOwR/G12pyAsFqGvTKjDX7GKv31vHT7sQ513wHw+HNNcn5vbCspI+ gCIMVhAk/hFCbFBJS3xabjPXQPpKlfItldDmH5L8aBN1VUDFHtbvJfpM+aplU5Xdh+CW 0nA3BeEPCnPbWGXtW587F5Q0N+wGUEimuHoOWPDt8Ns66Z0Zhn9A8uGLT31LGjO+vPA1 ZL2Q==; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64798-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXB+eCCgEgx54OWWQJ6Z6bfHU0IOiM8xtNUBmEKSf110Z9YBjuNgGf44lDX6noeAo6P7Wir3ht1rNw7/2b/jbB/G/BDMg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id dx5-20020a170906a84500b00a3bf06a9c9esi1948700ejb.694.2024.02.13.22.39.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 22:39:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64798-ouuuleilei=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=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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64798-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 am.mirrors.kernel.org (Postfix) with ESMTPS id F23521F2782A for ; Wed, 14 Feb 2024 06:39:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A091171C1; Wed, 14 Feb 2024 06:37:33 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790855354854266623 X-GMAIL-MSGID: 1790855354854266623 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)