Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp568428pxv; Thu, 15 Jul 2021 10:24:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwd8Y+XEi1o91XPeyo620NBBzoJoXc6mCIqGL0DxuMwvUn1OK9tq5UKFYRf2NLJg8eRBuEt X-Received: by 2002:a05:6e02:1142:: with SMTP id o2mr3480196ill.277.1626369897589; Thu, 15 Jul 2021 10:24:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626369897; cv=none; d=google.com; s=arc-20160816; b=dzQjLvcuriZRLmNZyicPHH0m2uYFDw6Mcb6dFvAy/f5zRKuI9SjL3wZTMHQn95YhZY i5jHaJDSG6PI9R2OaD0puvl/SvXorfbBuuJ8K3EzujHzwZy6Iq3h17ObptavZZtQs2Ie dABSqwIOQ99LJ5SPd2XvGqUz8NZVbCOVdffcN8dAfCf0akh5vVzjRGkINijY3LFUWlVI /ikbMLsBXMFx1BdGv3eRj4NIXaPTJk+SxHh9YbmwZ4EP1dmKJPzbR/tq8w0SQtw5R8BX fn1y/tz141KMOT2H/+I+HTcjzHlMuRZ6TOLj8rupUAA9paq3GyhuTZIOxJ8dmlMO9gK5 /2Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=Ep2eMkNp2nRHtHvY85fIEvX7CnU3IX/8d7Feo7j4+0w=; b=02B4KmvduvlQLr02WyGrRQQevY51ldnmzJgqWG9na4/xKSbsF3RFr9nzwxyR/GlwCM JOTsXXcdIGfaUfgdm+yZorMoNwMRb3P630VvLYAXmzLLQB8+Wsuu1VRWJTeaMtmVNWVK /WWkzAjr6aGkaqHoDUnce0vZYJktfXoy06/7D0Pz7irNxiUiKb+xE+D0xWLY/ljmE1pN MXf1TyAImYfMmrrCn8LjgkrvFa5o6hQR7DWFW4MFRA7aV1p042CEWLjLTiPxJZoIS8nY 4iaNl9W1FP43Svbb3Mwh6TI2jmKjBlfEnlkFWarJRVfOVUcRRQW6aLesFutezp95nOIA HnKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=UvcwkoTX; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m8si7420467iow.20.2021.07.15.10.24.45; Thu, 15 Jul 2021 10:24:57 -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=@gmail.com header.s=20161025 header.b=UvcwkoTX; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231688AbhGOQwy (ORCPT + 99 others); Thu, 15 Jul 2021 12:52:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230164AbhGOQwy (ORCPT ); Thu, 15 Jul 2021 12:52:54 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E83FC06175F; Thu, 15 Jul 2021 09:50:00 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id f17so8664955wrt.6; Thu, 15 Jul 2021 09:50:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=Ep2eMkNp2nRHtHvY85fIEvX7CnU3IX/8d7Feo7j4+0w=; b=UvcwkoTXYKTRmVgtSvhkZ5PFMhYp1BTRg79G5ABgnpZl3Zc9xDktEVMrnMuB5P8aoZ 8Dk9wjEhMTEDKkidk5isRs1KAyQf5gpnFoUFDy4ZzsPBwxyXkQvzPKUZvLlHQlX0aLx2 RMKtM6B8vYuN12tHUSJKuT9fh8nKY1OBOuA7pmvT3H3B0rh93F7AkkR4Ku2XcomqNvdt UgBtTMIZk5Fr+ZjK7RXzXGf6I/PsQXCgzMukXzXnsQ+dTYpjswpS5Xr8SMzvWgHv9O0I 1yo4BP7Cwsa1sYm8NYUFjAa2iX+oB9F1xFp0TFMjOLs37Nlzlq9sEzk+BM7BnlCfwVwn 7uHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=Ep2eMkNp2nRHtHvY85fIEvX7CnU3IX/8d7Feo7j4+0w=; b=J8flYshOXlit7kTXo4PPoK1DiIwWibzqthfN3GW+A6OcIFbQV5G/thmZe+bbNVsHiq gWJRIKQmxIFAY7ktBo972yAFhdtxJObTV6THhGkzTC5kPT55t2vY2W76lC+k+AjyZsHv vcr16Yhrmvjsj5pZcx0/eTIQ7n0o8PYy+EgG1zOxshgESBjR2qDYcDM9E+W6sUtsh3g1 gjaG2fYUwFUX49efFtABXM7bgj2i1XmBp2iLSa28ck4+2FxACZCgeU8dpWH1nuQdN+4o j6YR6tDMedp8dgTVx2XsM/FA8I/tORm9EGHXW7K/T6QYtpQJ2iJ0fMgDeUzffzzQkQm8 wtcw== X-Gm-Message-State: AOAM5324ZzmfUhdNkzi/FIdkLOgXEqjb74tOLS3gZj/kbyEWCCXmwdMy UX+WhkT3gBSvMEBw/LTzoM4= X-Received: by 2002:a05:6000:1841:: with SMTP id c1mr6585305wri.423.1626367799030; Thu, 15 Jul 2021 09:49:59 -0700 (PDT) Received: from ?IPv6:2001:b07:6456:fd99:ced0:db1c:53e1:191e? ([2001:b07:6456:fd99:ced0:db1c:53e1:191e]) by smtp.gmail.com with ESMTPSA id v15sm9231249wmj.39.2021.07.15.09.49.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jul 2021 09:49:58 -0700 (PDT) Message-ID: Subject: Re: [RFC PATCH 06/10] perf workqueue: introduce workqueue struct From: Riccardo Mancini To: Arnaldo Carvalho de Melo Cc: Ian Rogers , Namhyung Kim , Peter Zijlstra , Ingo Molnar , Mark Rutland , Jiri Olsa , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Date: Thu, 15 Jul 2021 18:49:57 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.2 (3.40.2-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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 > > >