From patchwork Tue Oct 29 08:57:55 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: 21767 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 99651C3220 for ; Tue, 29 Oct 2024 08:58:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2F4D0653A8; Tue, 29 Oct 2024 09:58:49 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="dyUmHfb4"; dkim-atps=neutral Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 33C6065398 for ; Tue, 29 Oct 2024 09:58:46 +0100 (CET) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3e606cba08eso3065661b6e.0 for ; Tue, 29 Oct 2024 01:58:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1730192324; x=1730797124; 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=GIpyMBbuJIOv8U6VsJd+oUR+ViHmb7sQ9ux1KQSBe28=; b=dyUmHfb4NYJEASOM+HYV6JdMPWgmI3wFEaiLWohh1KIe5jlBgkDK13lJCZcn1y21Va So6neeAXd9k/82+BGnClNtUMrncPp8m/a+UnwDFTdG+Z6eM/fq/LPPT8nrfZijSPRrEm pCNA25JkJMs7TEaJ10OtqU44fDXMmpTHlRp9Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730192324; x=1730797124; 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=GIpyMBbuJIOv8U6VsJd+oUR+ViHmb7sQ9ux1KQSBe28=; b=Lgjq2aKensWQ1brLKfRzRJbeTxMoW7gVet6KB2XW9SoOWAHN57r2rOboxf2NMEwrRf /PofOD7Kv0xCHqD+5sUgOdVywubjEwXQFzmzoQ3dNszCoo9gujEv6bagrdNf5LSGCmj9 iLHUXM+IKMBYTGHcd+dIbdUIDzbax0CqXiFtgR3H6U0oK0SihmiEDd4Vaj9F/WfnrQyI 4vrhUPERenjGNX1SMboJr+LKj3tFXpaqWhjlIvrtS9z27QRoSVeh3vFRULa6JcRVpj+e sCcL/fGkX5IJ9uc+tSsp9JDs5SZ7D4hUwRwdKGdSX3pi9pd9ESZL/JPearppjkOVv8ln QFCg== X-Gm-Message-State: AOJu0YzPrXBKWfF8DZFtpHOtBiRE8w0/FRY5UovqIZ7hyKPdSgUKzwTR Nk6Hxvapiy5E5Wi/PxjMwyhPG5LyhtD43/w65Tjs4uEoh+CcYvuunTCw0jsLSiFsmAq+rlAzz1Y = X-Google-Smtp-Source: AGHT+IHekJZCCtYoZDpcGIHsNpkrKVm7nBLb9k+bn2LUFxeMio593Sizl03a6sRTrP/HiPXSJ8Tsvw== X-Received: by 2002:a05:6808:228f:b0:3e2:9e62:37d5 with SMTP id 5614622812f47-3e638239500mr8353236b6e.5.1730192324565; Tue, 29 Oct 2024 01:58:44 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7edc89f2faesm7000856a12.60.2024.10.29.01.58.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 01:58:44 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v7 1/1] libcamera: add method to set thread affinity Date: Tue, 29 Oct 2024 08:57:55 +0000 Message-ID: <20241029085837.3615699-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog In-Reply-To: <20241029085837.3615699-1-chenghaoyang@chromium.org> References: <20241029085837.3615699-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 Reviewed-by: Kieran Bingham --- include/libcamera/base/thread.h | 5 ++++ src/libcamera/base/thread.cpp | 47 +++++++++++++++++++++++++++++++++ test/threads.cpp | 40 ++++++++++++++++++++++++++++ 3 files changed, 92 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..f6322fe31 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_; }; /** @@ -281,6 +284,8 @@ void Thread::startThread() data_->tid_ = syscall(SYS_gettid); currentThreadData = data_; + setThreadAffinityInternal(); + run(); } @@ -410,6 +415,48 @@ 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, this function won't modify the thread affinity and + * will return an error. + * + * \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) << "Invalid CPU " << cpu << "for thread affinity"; + 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 * diff --git a/test/threads.cpp b/test/threads.cpp index ceb4fa0f2..8d6ee1510 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include +#include #include #include "test.h" @@ -66,6 +68,27 @@ private: bool &cancelled_; }; +class CpuSetTester : public Object +{ +public: + CpuSetTester(unsigned int cpuset) + : cpuset_(cpuset) {} + + bool testCpuSet() + { + int ret = sched_getcpu(); + if (static_cast(cpuset_) != ret) { + cout << "Invalid cpuset: " << ret << ", expecting: " << cpuset_ << endl; + return false; + } + + return true; + } + +private: + const unsigned int cpuset_; +}; + class ThreadTest : public Test { protected: @@ -165,6 +188,23 @@ protected: return TestFail; } + const unsigned int numCpus = std::thread::hardware_concurrency(); + for (unsigned int i = 0; i < numCpus; ++i) { + thread = std::make_unique(); + const std::array cpus{ i }; + thread->setThreadAffinity(cpus); + thread->start(); + + CpuSetTester tester(i); + tester.moveToThread(thread.get()); + + if (!tester.invokeMethod(&CpuSetTester::testCpuSet, ConnectionTypeBlocking)) + return TestFail; + + thread->exit(0); + thread->wait(); + } + return TestPass; }