From patchwork Wed Oct 16 08:43:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21636 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 264A8C32FA for ; Wed, 16 Oct 2024 08:45:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 40E5565386; Wed, 16 Oct 2024 10:45:03 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="gcirX33h"; dkim-atps=neutral Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E3AAA6537E for ; Wed, 16 Oct 2024 10:44:57 +0200 (CEST) Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2e28b75dbd6so4456128a91.0 for ; Wed, 16 Oct 2024 01:44:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729068296; x=1729673096; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dTKZLNnerUEia6r5PzeYdUu978NY/XyOHsE/wH/qwkM=; b=gcirX33h3QGyf1rozjC+FrEA2xcO9lp9SPyR3OdNacSQ+XRrwf9SCUgO3v2GMv0Nwv mlMNYgEr0CB/tkaNgFjwtx8eH+EWQOpEeE3kwKnmJXq6iksZ6oZ3cjupWYFAPKLTV/Nm NE9GgeDfgQwbNrUTO7D/1MYZAM3jdrz/Yx4Zk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729068296; x=1729673096; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dTKZLNnerUEia6r5PzeYdUu978NY/XyOHsE/wH/qwkM=; b=aQ0QEBfjXZDmVkhnfYdSrOabfsYwjdnph27OZc8lWP93NrnjfSRWTfKxa6jbfLX86u y0/kLBqe7LOQVnjV1vsUKaHB/AQVNVmdr75YbjymabfqYNcWRrEVuof5/AqbI3RH+U2d Y+PoBmvMwCHvYytyBujupGndl6nYIABQ7KzabaOdnWitiqb6wjmVnDLpB15mS4OA7ctp Fh1CWGvwBZV8YqPeR4j+MV6k6yHx/ZIWMe7Cr1eRlMcnm7tfbHYi8OBvCxF/mwlKz0LS CoGtyKTLQg3IeWY+59R3tCHBjEHW5nHJcY+DVs1CDj4A6J6jwioIBjdWlKwgvjgr9027 Z+4g== X-Gm-Message-State: AOJu0YwGvWUoDsnhDiIi9GwFyEczD6377vQt5rWR+Nq4oA+HZ11W0/Pn FikGMhiH6Mv/PzYM/kIOisaaCCe1D4+xENfZMe+Ktqqs5F+70ZKD/SXieiPzQFgYWmsqSOmSlLk = X-Google-Smtp-Source: AGHT+IHtN986iICbj8Q98rfrjGxLfKYkRvtWv5zu085Ka7K42gmq/NCwW3OPdC76bPztZKhdmPZu0Q== X-Received: by 2002:a17:90b:4c43:b0:2e2:f044:caaa with SMTP id 98e67ed59e1d1-2e31538fb87mr18464509a91.37.1729068296059; Wed, 16 Oct 2024 01:44:56 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (199.211.81.34.bc.googleusercontent.com. [34.81.211.199]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392f03756sm3508553a91.29.2024.10.16.01.44.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2024 01:44:55 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v5 1/1] libcamera: add method to set thread affinity Date: Wed, 16 Oct 2024 08:43:44 +0000 Message-ID: <20241016084449.3566589-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241016084449.3566589-1-chenghaoyang@chromium.org> References: <20241016084449.3566589-1-chenghaoyang@chromium.org> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Han-Lin Chen Add method to set thread affinity to Thread class. Signed-off-by: Han-Lin Chen Co-developed-by: Harvey Yang Signed-off-by: Harvey Yang --- include/libcamera/base/thread.h | 5 ++++ src/libcamera/base/thread.cpp | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index 4f33de63d..3209d4f7c 100644 --- a/include/libcamera/base/thread.h +++ b/include/libcamera/base/thread.h @@ -15,6 +15,7 @@ #include #include +#include #include namespace libcamera { @@ -35,6 +36,8 @@ public: void exit(int code = 0); bool wait(utils::duration duration = utils::duration::max()); + int setThreadAffinity(const Span &cpus); + bool isRunning(); Signal<> finished; @@ -54,6 +57,8 @@ private: void startThread(); void finishThread(); + void setThreadAffinityInternal(); + void postMessage(std::unique_ptr msg, Object *receiver); void removeMessages(Object *receiver); diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index 8735670b8..5dc787dba 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -128,6 +129,8 @@ private: int exitCode_; MessageQueue messages_; + + std::optional cpuset_ = std::nullopt; }; /** @@ -254,6 +257,8 @@ void Thread::start() data_->exit_.store(false, std::memory_order_relaxed); thread_ = std::thread(&Thread::startThread, this); + + setThreadAffinityInternal(); } void Thread::startThread() @@ -410,6 +415,47 @@ bool Thread::wait(utils::duration duration) return hasFinished; } +/** + * \brief Set the CPU affinity mask of the thread + * \param[in] cpus The list of CPU indices that the thread is set affinity to + * + * The CPU indices should be within [0, std::thread::hardware_concurrency()). + * If any index is invalid, no cpu indices will be set. + * + * \return 0 if all indices are valid, -EINVAL otherwise + */ +int Thread::setThreadAffinity(const Span &cpus) +{ + const unsigned int numCpus = std::thread::hardware_concurrency(); + + MutexLocker locker(data_->mutex_); + data_->cpuset_ = cpu_set_t(); + CPU_ZERO(&data_->cpuset_.value()); + + for (const unsigned int &cpu : cpus) { + if (cpu >= numCpus) { + LOG(Thread, Error) << "Ignore an invalid cpu index: " << cpu; + return -EINVAL; + } + + CPU_SET(cpu, &data_->cpuset_.value()); + } + + if (data_->running_) + setThreadAffinityInternal(); + + return 0; +} + +void Thread::setThreadAffinityInternal() +{ + if (!data_->cpuset_) + return; + + const cpu_set_t &cpuset = data_->cpuset_.value(); + pthread_setaffinity_np(thread_.native_handle(), sizeof(cpuset), &cpuset); +} + /** * \brief Check if the thread is running *