Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp699370pxv; Thu, 15 Jul 2021 13:49:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5pCKxa7KUqlojFK8K0mUx71h6zZaZDpeK2Z39JiRjx7J5X+qOWFaprIsn3Z22perlLbJX X-Received: by 2002:a05:6638:25c7:: with SMTP id u7mr5683376jat.26.1626382157901; Thu, 15 Jul 2021 13:49:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626382157; cv=none; d=google.com; s=arc-20160816; b=sWMpQmDZ2fJLU02T+ZVpbofLtBPhgDvUsoyQj99T+lPqZiL42QT9Vl+9etjToZh3+1 LtrLrm+FQPf91hbPLzBUwX7TPZQJHc1xn86cvnZURno4b7gDSckzg3bRf6qSZuPcrDtU MoJJqkAo11ioHDlyC0aG0PHJPiNfxLrjCZF5jMD9/72id0/fRsJkOl21NZjxDlv/gUB4 awl1NO7GdCqugEmxiBupoKfgNGgYCU3R7sj34hBuaYjLmIWP/Du94WgHdiivwBImYlrj rhPfpaWYxDDKg5PWQ73p+9oLE+SLzoUrTERpFgN8uXKomhsHYvH+x1+j6s1aRkuoH7nY UseQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=hLCSQDt1omuQyzDlpeB9FS+cKxrnYfAipg7xBNlQl8s=; b=HFvpn6ebDQeRenkNjPgZvLAryS4iSJ2vkjwRDz7cC0kQcWHgwvrTwehqUdH1G+jgUn eMFhKVNWpMM1O4sxPnI/VQ4Dst0K5sfkugvNv5MppWmuclue3O9oQSeZO0JUpZ+GLwS9 ZpFiah2ittlfN1li7mQeZv06qExCRIZMER0pj3Mnz00HlwsSanLGiMxUKpNnNPxt+UWE NA221RjI1yM6Wr8VyyXW8Lk9iKqdyc0271EhYd9uxeCyzco/CsUKjL4dTI+V3WksWg1c 77EXntmwnrOphE63ovJ1AnsruGV45bJJoXjuEbN9DAeYHZMLUw13aJsy7MmgNXCqXZZq mnvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a8OUzu2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z24si8050018jap.49.2021.07.15.13.49.05; Thu, 15 Jul 2021 13:49:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a8OUzu2Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235350AbhGOUt6 (ORCPT + 99 others); Thu, 15 Jul 2021 16:49:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:32918 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230165AbhGOUt6 (ORCPT ); Thu, 15 Jul 2021 16:49:58 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0B080613D4; Thu, 15 Jul 2021 20:47:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1626382024; bh=DzOqBO0a1W1oxkZNxSUywrONmNC/88IWEjkZxmu9HJQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=a8OUzu2YqoVlSZW4AQjc1EXlvWAhIBK2xn8WaPhW1KOVC7PkRNaPtDR9AMtKW9MMd 1xDpqqxoMd40KuGOZzUX7Hm0LNwPPO9icMROhvhzBJWF/obENSHxfM7VQERqIn77vO 41cSjpAZss7ZE3La05U9EFSRGRpuORtvJUDS5+ywUnO0LJ/+e8x3VUZZtCiAWx8i1O /LcbDtwRycsykCWYszYunyk6SuWy7IhUaOcRo9b+lbqeOivq/PA8n0yzBJskb6CUW+ sb00eoV2ITvuXLV4nljPgAa+gSvUNtdPgSprhgxOq/XEruKPq7zqaTlaM7YVGun2Fu MEkcUjPETWg5Q== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 6D24F403F2; Thu, 15 Jul 2021 17:47:01 -0300 (-03) Date: Thu, 15 Jul 2021 17:47:01 -0300 From: Arnaldo Carvalho de Melo To: Riccardo Mancini Cc: Arnaldo Carvalho de Melo , Ian Rogers , Namhyung Kim , Peter Zijlstra , Ingo Molnar , Mark Rutland , Jiri Olsa , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: Re: [RFC PATCH 06/10] perf workqueue: introduce workqueue struct Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Url: http://acmel.wordpress.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Thu, Jul 15, 2021 at 06:49:57PM +0200, Riccardo Mancini escreveu: > Hi Arnaldo, > thanks again for having a look at this patchset! > > On Wed, 2021-07-14 at 12:22 -0300, Arnaldo Carvalho de Melo wrote: > > Em Tue, Jul 13, 2021 at 02:11:17PM +0200, Riccardo Mancini escreveu: > > > This patch adds the workqueue definition, along with simple creation and > > > destruction functions. > > > Furthermore, a simple subtest is added. > > > > > > A workqueue is attached to a pool, on which it executes its workers. > > > Next patches will introduce workers. > > > > > > Signed-off-by: Riccardo Mancini > > > > > > > + > > > +/** > > > + * create_workqueue - create a workqueue associated to @pool > > > + * > > > + * Only one workqueue can execute on a pool at a time. > > > + */ > > > +struct workqueue_struct *create_workqueue(struct threadpool_struct *pool) > > > > I wonder if we should use the exact same kernel signature and not pass a > > threadpool, essentially having just one threadpool in tools/perf/ that > > is used by create_workqueue(void)? > > I wondered the same thing, but I thought that we'd need it to be dynamically > created to prevent spawning threads at the beginning that might not even be > used. > I think this could be a follow-up patch. I see your point. My practice is to use the perf convention for things that don't come from the kernel, and if we do as I suggested, tooling will probably not even see this threadpool struct, i.e. just the workqueue APIs are exposed and workqueue_create() will notice that a threadpool is not yet created, doing its creation behind tooling's back. - Arnaldo > Thanks, > Riccardo > > > > > > +{ > > > +???????int err; > > > +???????struct workqueue_struct *wq = malloc(sizeof(struct > > > workqueue_struct)); > > > + > > > + > > > +???????err = pthread_mutex_init(&wq->lock, NULL); > > > +???????if (err) > > > +???????????????goto out_free_wq; > > > + > > > +???????err = pthread_cond_init(&wq->idle_cond, NULL); > > > +???????if (err) > > > +???????????????goto out_destroy_mutex; > > > + > > > +???????wq->pool = NULL; > > > +???????INIT_LIST_HEAD(&wq->busy_list); > > > +???????INIT_LIST_HEAD(&wq->idle_list); > > > + > > > +???????INIT_LIST_HEAD(&wq->pending); > > > + > > > +???????err = pipe(wq->msg_pipe); > > > +???????if (err) > > > +???????????????goto out_destroy_cond; > > > + > > > +???????wq->task.fn = worker_thread; > > > + > > > +???????err = attach_threadpool_to_workqueue(wq, pool); > > > +???????if (err) > > > +???????????????goto out_destroy_cond; > > > + > > > +???????wq->status = WORKQUEUE_STATUS__READY; > > > + > > > +???????return wq; > > > + > > > +out_destroy_cond: > > > +???????pthread_cond_destroy(&wq->idle_cond); > > > +out_destroy_mutex: > > > +???????pthread_mutex_destroy(&wq->lock); > > > +out_free_wq: > > > +???????free(wq); > > > +???????return NULL; > > > +} > > > + > > > +/** > > > + * destroy_workqueue - stop @wq workers and destroy @wq > > > + */ > > > +int destroy_workqueue(struct workqueue_struct *wq) > > > +{ > > > +???????int err = 0, ret; > > > + > > > +???????ret = detach_threadpool_from_workqueue(wq); > > > +???????if (ret) { > > > +???????????????pr_err("workqueue: error detaching from threadpool.\n"); > > > +???????????????err = -1; > > > +???????} > > > + > > > +???????ret = pthread_mutex_destroy(&wq->lock); > > > +???????if (ret) { > > > +???????????????err = -1; > > > +???????????????pr_err("workqueue: error pthread_mutex_destroy: %s\n", > > > +???????????????????????strerror(errno)); > > > +???????} > > > + > > > +???????ret = pthread_cond_destroy(&wq->idle_cond); > > > +???????if (ret) { > > > +???????????????err = -1; > > > +???????????????pr_err("workqueue: error pthread_cond_destroy: %s\n", > > > +???????????????????????strerror(errno)); > > > +???????} > > > + > > > +???????ret = close(wq->msg_pipe[0]); > > > +???????if (ret) { > > > +???????????????err = -1; > > > +???????????????pr_err("workqueue: error close msg_pipe[0]: %s\n", > > > +???????????????????????strerror(errno)); > > > +???????} > > > + > > > +???????ret = close(wq->msg_pipe[1]); > > > +???????if (ret) { > > > +???????????????err = -1; > > > +???????????????pr_err("workqueue: error close msg_pipe[1]: %s\n", > > > +???????????????????????strerror(errno)); > > > +???????} > > > + > > > +???????free(wq); > > > + > > > +???????return err; > > > +} > > > + > > > +/** > > > + * workqueue_nr_threads - get size of threadpool underlying @wq > > > + */ > > > +int workqueue_nr_threads(struct workqueue_struct *wq) > > > +{ > > > +???????return threadpool_size(wq->pool); > > > +} > > > diff --git a/tools/perf/util/workqueue/workqueue.h > > > b/tools/perf/util/workqueue/workqueue.h > > > new file mode 100644 > > > index 0000000000000000..86ec1d69274f41db > > > --- /dev/null > > > +++ b/tools/perf/util/workqueue/workqueue.h > > > @@ -0,0 +1,24 @@ > > > +/* SPDX-License-Identifier: GPL-2.0 */ > > > +#ifndef __WORKQUEUE_WORKQUEUE_H > > > +#define __WORKQUEUE_WORKQUEUE_H > > > + > > > +#include > > > +#include > > > +#include > > > +#include "threadpool.h" > > > + > > > +struct work_struct; > > > +typedef void (*work_func_t)(struct work_struct *work); > > > + > > > +struct work_struct { > > > +???????struct list_head entry; > > > +???????work_func_t func; > > > +}; > > > + > > > +struct workqueue_struct; > > > + > > > +extern struct workqueue_struct *create_workqueue(struct threadpool_struct > > > *pool); > > > +extern int destroy_workqueue(struct workqueue_struct *wq); > > > + > > > +extern int workqueue_nr_threads(struct workqueue_struct *wq); > > > +#endif /* __WORKQUEUE_WORKQUEUE_H */ > > > -- > > > 2.31.1 > > > > > > > -- - Arnaldo