From patchwork Mon Oct 14 06:58:56 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: 21608 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 46520C32F4 for ; Mon, 14 Oct 2024 07:00:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 701916537B; Mon, 14 Oct 2024 09:00:10 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="b9fVVLFN"; dkim-atps=neutral Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5036A65369 for ; Mon, 14 Oct 2024 09:00:08 +0200 (CEST) Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-7cd8803fe0aso2642860a12.0 for ; Mon, 14 Oct 2024 00:00:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728889206; x=1729494006; 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=Y2Zc0vPz4qFQ7+53+wuFDEix1//hdzOc84hhW6Gj8GM=; b=b9fVVLFNHF4BkXBhvuUIvCuRhK0iFW8jUTtklsPg8wyda9my+jbwR42jVRqFfjrTVQ GvjQmPf95Sw2FmBrm36KjI+pZ7tYoPBmnO8+Hyg6Qo3AOR7sj2iu5RtCE6/2lpP4uk/G IPpitijitCxfQ7VBCJW4NyEjpOMowFDTSG4m4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728889206; x=1729494006; 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=Y2Zc0vPz4qFQ7+53+wuFDEix1//hdzOc84hhW6Gj8GM=; b=diaGS1Ac/1QQBNBQCi0MckNLFFYLbrVtwXobfJopXUETAGSZvke0asXYpOKh7yCpXE Pu1NXpZLgaxnHvgOHg8Pk4LtbAOOT4+LYSSMT3uX1wI3kROUXF3e8D4v9TTZw6HFckk7 +Sdu9Vvm6N3DhM4rO+GtjTcP2kzH9+B+lbV6mGgdRheMwM8AYvU4zLK1H8k51cpzcH5j G0JMEDoKTiu9YB0mztn4OpqpALka5+DpupUxj0hXIJaO0USRDnKkundYyE99FKht/Eps 8duVEFjTqNcpsATKwvQdD/5uIwhU3+JC25wPzB8sxDEyK7u1vEk78CrBdR1QJGi9tHLd zpCA== X-Gm-Message-State: AOJu0YzLh8+MF8lMrI40PN9fd8VPq2IhMeBSTxr5GoEb8CakctQzcOQE a5DbSYYbj5zp1qbVRVNd8xuOuk5D05HJUREjCKoE7vv+T5ai+M5IxOQFx8epIKeJOMOk60zn+KM = X-Google-Smtp-Source: AGHT+IF+ebTEdsKnvbC9kOYRl6D7FoTAQpbTM+BC9jNJmGERBUNMgz3MV4D2hd4ZkGh7DgXXx6BomQ== X-Received: by 2002:a05:6a21:505:b0:1d8:aa24:caeb with SMTP id adf61e73a8af0-1d8bcfc5932mr19100749637.49.1728889206446; Mon, 14 Oct 2024 00:00:06 -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 d2e1a72fcca58-71e675a9566sm1230461b3a.54.2024.10.14.00.00.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 00:00:06 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v3 1/1] libcamera: add method to set thread affinity Date: Mon, 14 Oct 2024 06:58:56 +0000 Message-ID: <20241014070000.658023-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241014070000.658023-1-chenghaoyang@chromium.org> References: <20241014070000.658023-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 | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index 4f33de63d..58cae341b 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()); + void 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..f40c7d8e7 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,53 @@ 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()), + * and the total number should not exceed `CPU_SETSIZE`. Invalid indices and + * extra indices will be ignored. + */ +void 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()); + + unsigned int count = 0; + for (const unsigned int &cpu : cpus) { + if (count == CPU_SETSIZE) { + LOG(Thread, Error) << "cpus_ already contains " << CPU_SETSIZE + << " cpus. Ignoring the rest."; + break; + } + + if (cpu >= numCpus) { + LOG(Thread, Error) << "Ignore an invalid cpu index: " << cpu; + continue; + } + + ++count; + CPU_SET(cpu, &data_->cpuset_.value()); + } + + if (data_->running_) + setThreadAffinityInternal(); +} + +void Thread::setThreadAffinityInternal() +{ + if (!data_->cpuset_) + return; + + auto handle = thread_.native_handle(); + + pthread_setaffinity_np(handle, sizeof(cpu_set_t), &data_->cpuset_.value()); +} + /** * \brief Check if the thread is running *