Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp8244lqg; Thu, 29 Feb 2024 17:36:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXve9O494C2hU9a+GvueM7x6KmzObErFi6mXGlAgblro03RQGJnwSdOJKLjf+ZCbnxiaGRZJMkki9PWUn8WlQpSr+PhcykrOm7JEDYWew== X-Google-Smtp-Source: AGHT+IFlNV62BY7/6dlxDaRLL4mv1tA9J+KW9QAMEXXZfzTk51G1rD2cXwcAmoXHRWfRFQj1zqS5 X-Received: by 2002:a05:622a:1809:b0:42e:6ee7:9598 with SMTP id t9-20020a05622a180900b0042e6ee79598mr225324qtc.66.1709256998025; Thu, 29 Feb 2024 17:36:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709256998; cv=pass; d=google.com; s=arc-20160816; b=hpwJkZlA1uWh4l1ER1flswbgK8QNTDv+0WjgSJN0pWs79gXZq8wx/DU87dw6da8QtY EnW4CggCN/VAPqjc5UhfEnmPzePxEToeXbVFHp6UzSMPOQCwHbzxgrsBLHEwT4nNn4L4 6UMos7U1lbJYGCdSWEUQ9YkwEXWV3RKfL5+8hnzZygjmJZwKfDqRIhYnW7bMgEJtpvn+ Gqxm9moKQTSOorNirj0V9Vif7CO4WF0dicIPFEQkh9/0sJLW8u/VkrZQilh4vGu4wMTa l5ZTGvJ7itQaNO36b2ghdlkap0l3K7q+9ic/g2Yj8PAJ8QrH9MHMjKIhh+4McdgO12Qk lEzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=XOjQrfFkf1F9ouFasWOllB0AHilUi/DGdhN7vfozvZA=; fh=2Zn2jnPSKWQtzYpt9gNuTv2wrpEClrmlfUOJOiWLKGc=; b=n/TMQ6ZK7AIbJG9E3UpLegU348cSi21R/H/AfQDqgtX6eXyPyE1sxF/hdv/br6kjDH pzWBHAYlO3W3sC4QFwJPIAPo4wi6T4m0/9f+RVQMgi5ro2akFkGW7bAXIB5e2zVqCpJB j3QNff/n3cGNJmlfKM+I9sKcg7yTOfmEpL+3uSxA/Wir/XcmT9nEbKr401kfmDRSiRBG 5AYt+1ApTpwXH7tiGDZaY9vHT7AoQRSZi1udMwvoplqRCKD8V+sw+56CiD/tvi/eEtiL Mo0T2uR58pAcbNNXjcZS0piNGLI2bhXgbD/MsaPYMLnN4vuu63PEio6bKfAeaEgaXo+9 p+lw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-87780-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87780-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d14-20020ac8534e000000b0042ec2024746si1582885qto.389.2024.02.29.17.36.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 17:36:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87780-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-87780-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87780-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 7092C1C20CAD for ; Fri, 1 Mar 2024 01:36:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 21803374EC; Fri, 1 Mar 2024 01:36:19 +0000 (UTC) Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 3A80BA92E; Fri, 1 Mar 2024 01:36:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256978; cv=none; b=EuSuP4jOgH/iDKKjXd8cnPLCk0QBSiQh8iQkw+DSBgQEi98fxlq070uSIRwWXJsoFX/CWD5RIihFzjCZgOJ2DVwIp4NtlrGcOJJ06WB+r4yS4j9MsYy7na19xcl+woAzN1wpzc4EBsZS9UHw4z90qNWuT1wiLxVYL8qC3fnf8yM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709256978; c=relaxed/simple; bh=wVXrnBTi3TycX+Jfqc1qPh0gqtHz5YmTj8yDDOMQ//w=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=mMpNR10qFaNimHzJSfeyuzSZYJahZK5HqiiJ1Dg0WPUYxFdGA941mBTG0KhTFHbSPA1QR5ijTw5Z0CyGCP0PcFFla0382yBGWg/qPoeaQdPEQ3Fyb8p0B19Cv4TS5ehxCKTYQ9Q3egjVuAjkhv1xQTnCpqGeIh9LkLtQx2e991A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5ce2aada130so1419726a12.1; Thu, 29 Feb 2024 17:36:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709256976; x=1709861776; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XOjQrfFkf1F9ouFasWOllB0AHilUi/DGdhN7vfozvZA=; b=FsIEuyZLHqt4rG6WF/tm1uNZBu9OgFxklg7+xa8yIZO1zuKbHflLlzasemgFfAlmRJ 93S20SOaWMCsNcTpX3kHB/W7E5w65x65NQjGiNDpi0Y1qNL8njZQXwpuZxy9wCBro0rg s8OoxByFKMJ4dcVSfhUh1sXBDekUhvTheD/lNcO6MLUJHFzo+n2kBsiijzeBXGyQ5G50 JowW1c9zDgiY1VqlnVlHKFX1aRzwzq27wlBv529T/2gpQrfYy08K9O4mz4Amk+0bjc7e +4Cs3ZWriC+y+xb1R5g1yjOsDhnuscbHmvePc5MJDwoq6f4MQEm2fuDjY2OpPhFAz4O1 Cz0Q== X-Forwarded-Encrypted: i=1; AJvYcCVs7g5uibY5svYtxUWIYbUms9Ku5SNfUCQoOLuboKQG95wOAfVqytCEyFikX2yqpXROYdkU9JApQoTx6SPsvXc6RyvZaaB+KCv9btN2HQYkUFUo9rfe1t1plFCUCaOTKOE5C32CSO60ON4V1Zp2Cw== X-Gm-Message-State: AOJu0Yzpa8Sc8NnQrS+lNVIql2BXOLWQI42onuME/VBcLdW9mrSj/qs/ GgDlcouqdJ0rRa5ugz8tlVZybb56ETEaEM0Ydz6q7ogtzthDxTs2vDxARvpx8vSiCQnhwVqFZwe JE5Ns1gBJz2myaWCwEhWVR5MS6SI= X-Received: by 2002:a05:6a20:aea8:b0:1a1:34dd:7d92 with SMTP id do40-20020a056a20aea800b001a134dd7d92mr106040pzb.59.1709256976409; Thu, 29 Feb 2024 17:36:16 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240229063253.561838-1-irogers@google.com> <20240229063253.561838-5-irogers@google.com> In-Reply-To: <20240229063253.561838-5-irogers@google.com> From: Namhyung Kim Date: Thu, 29 Feb 2024 17:36:05 -0800 Message-ID: Subject: Re: [PATCH v3 4/7] perf machine: Move machine's threads into its own abstraction To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Oliver Upton , Yang Jihong , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Feb 28, 2024 at 10:33=E2=80=AFPM Ian Rogers wr= ote: > > Move thread_rb_node into the machine.c file. This hides the > implementation of threads from the rest of the code allowing for it to > be refactored. > > Locking discipline is tightened up in this change. As the lock is now > encapsulated in threads, the findnew function requires holding it (as > it already did in machine). Rather than do conditionals with locks > based on whether the thread should be created (which could potentially > be error prone with a read lock match with a write unlock), have a > separate threads__find that won't create the thread and only holds the > read lock. This effectively duplicates the findnew logic, with the > existing findnew logic only operating under a write lock assuming > creation is necessary as a previous find failed. The creation may > still fail with the write lock due to another thread. The duplication > is removed in a later next patch that delegates the implementation to > hashtable. > > Signed-off-by: Ian Rogers Thanks for doing this! A nit below.. > --- [SNIP] > @@ -3228,27 +3258,31 @@ int thread__resolve_callchain(struct thread *thre= ad, > return ret; > } > > -int machine__for_each_thread(struct machine *machine, > - int (*fn)(struct thread *thread, void *p), > - void *priv) > +int threads__for_each_thread(struct threads *threads, > + int (*fn)(struct thread *thread, void *data)= , > + void *data) > { > - struct threads *threads; > - struct rb_node *nd; > - int rc =3D 0; > - int i; > + for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { > + struct threads_table_entry *table =3D &threads->table[i]; > + struct rb_node *nd; > > - for (i =3D 0; i < THREADS__TABLE_SIZE; i++) { > - threads =3D &machine->threads[i]; > - for (nd =3D rb_first_cached(&threads->entries); nd; > - nd =3D rb_next(nd)) { > + for (nd =3D rb_first_cached(&table->entries); nd; nd =3D = rb_next(nd)) { > struct thread_rb_node *trb =3D rb_entry(nd, struc= t thread_rb_node, rb_node); > + int rc =3D fn(trb->thread, data); > > - rc =3D fn(trb->thread, priv); > if (rc !=3D 0) > return rc; > } > } > - return rc; > + return 0; Don't we need locking in this function? Thanks, Namhyung > + > +} > + > +int machine__for_each_thread(struct machine *machine, > + int (*fn)(struct thread *thread, void *p), > + void *priv) > +{ > + return threads__for_each_thread(&machine->threads, fn, priv); > } >