From patchwork Fri Oct 11 17:00:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21593 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 66EDEC32F1 for ; Fri, 11 Oct 2024 17:19:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 801FA6537F; Fri, 11 Oct 2024 19:19:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="kNA3pOW+"; dkim-atps=neutral Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 67FBD6537D for ; Fri, 11 Oct 2024 19:19:38 +0200 (CEST) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-20cb47387ceso3679045ad.1 for ; Fri, 11 Oct 2024 10:19:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728667176; x=1729271976; 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=wtAlbItorfTrz/hKhS8f5b3FIZdquXtIFAS810zOKIM=; b=kNA3pOW+SCyxvWyBWbMa0NJiH9jFAPDvvbCCMTUX0TKt9vJLo9PmP/m8gmoAABvjLa gUkVglzE24dn8te61F3IV22BjeC9a/3kaKQwo3HcFDtSVni9DA2CJoWXvjxAzGeMqtNq MZbE2MnvKwgJi14THU5YeH1ZwepZ86yuuUkBc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728667176; x=1729271976; 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=wtAlbItorfTrz/hKhS8f5b3FIZdquXtIFAS810zOKIM=; b=LG6FmLwF46af0NeW+DGWBgmLcGuiF09zS5puFATdDFZ9gl9NkQt0TE2Foid/WqSRFh OnByziUeE/BByPA68mUp+DvEIkCXhJTDB5Wr4G2AG7jocWkL3KCdXrp9y566ox0ou5m0 bHbrgT5c5L3h3x8KEo/11lkCJUVQZP7LGYnPvgSdm+7wBgFX25JhrDSFweWtcC9/62kW zR9zIRnfJdT3S/JTzayMZc21aheH6VKCMLRHojpVtWylvVtWofOhqlhnjqDRV0tQOMlm 1TXGI1O8EZyXP5ebULCi7TIE/uqAANL9C8WMngwq2uCiy5rl1S05Qn9LPCDoG1xpn6Mt JiXA== X-Gm-Message-State: AOJu0YwmNEEAjT1yVKLQ6A1o8N+cSlMg0IXGCuRVu4s8lRcai3ZwYWMP S7r/TNslGCpfUL2aUjyXzYlGhJW73OQ3VJ+3Uiypp7Fq/QMCO2ltgj6Ysd9AiHAjN2NqedPBJ44 = X-Google-Smtp-Source: AGHT+IEEfhaKa7weHsLbca3B155tYjQ3gbMsTkLOb+PTegONrWnkd63/RczBLOd4M5wf2KHJ/ETaKg== X-Received: by 2002:a17:903:2348:b0:20c:8cf9:6134 with SMTP id d9443c01a7336-20ca142475fmr47981375ad.4.1728667176625; Fri, 11 Oct 2024 10:19:36 -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 d9443c01a7336-20c8badc3b2sm25571975ad.12.2024.10.11.10.19.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 10:19:36 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH 1/1] libcamera: add method to set thread affinity Date: Fri, 11 Oct 2024 17:00:48 +0000 Message-ID: <20241011171930.3225255-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241011171930.3225255-1-chenghaoyang@chromium.org> References: <20241011171930.3225255-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 | 6 ++++++ src/libcamera/base/thread.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index 4f33de63d..c74fe102b 100644 --- a/include/libcamera/base/thread.h +++ b/include/libcamera/base/thread.h @@ -35,6 +35,8 @@ public: void exit(int code = 0); bool wait(utils::duration duration = utils::duration::max()); + void setThreadAffinity(const std::vector &cpus); + bool isRunning(); Signal<> finished; @@ -54,6 +56,8 @@ private: void startThread(); void finishThread(); + void setThreadAffinityInternal(); + void postMessage(std::unique_ptr msg, Object *receiver); void removeMessages(Object *receiver); @@ -67,6 +71,8 @@ private: std::thread thread_; ThreadData *data_; + + std::vector cpus_; }; } /* namespace libcamera */ diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index 8735670b8..2417043a3 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -254,6 +254,8 @@ void Thread::start() data_->exit_.store(false, std::memory_order_relaxed); thread_ = std::thread(&Thread::startThread, this); + + setThreadAffinityInternal(); } void Thread::startThread() @@ -410,6 +412,35 @@ 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 + */ +void Thread::setThreadAffinity(const std::vector &cpus) +{ + cpus_ = cpus; + + MutexLocker locker(data_->mutex_); + if (data_->running_) + setThreadAffinityInternal(); +} + +void Thread::setThreadAffinityInternal() +{ + if (cpus_.empty()) + return; + + auto handle = thread_.native_handle(); + + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + + for (auto cpu : cpus_) + CPU_SET(cpu, &cpuset); + + pthread_setaffinity_np(handle, sizeof(cpu_set_t), &cpuset); +} + /** * \brief Check if the thread is running *