From patchwork Sat Oct 12 12:47:57 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: 21604 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 5F190C32F3 for ; Sat, 12 Oct 2024 12:49:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2C04A6537E; Sat, 12 Oct 2024 14:49:37 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="eTFaXkqO"; dkim-atps=neutral Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 21FD563538 for ; Sat, 12 Oct 2024 14:49:34 +0200 (CEST) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-71e579abb99so70618b3a.2 for ; Sat, 12 Oct 2024 05:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728737372; x=1729342172; 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=c1rd/5YnjV8toBF1fUisNrdZyaQgfqf0hhp8bLk1t/I=; b=eTFaXkqO8Rva2zsZBHH2G3FwczsWRAeGcRPZ9hYhaNx9nVLtH7Pu7zZNphteZgY/V8 osySSX+UM0fkAegf5I5TYnjDzUbV/BBh2pnT+P/CZa6EAKCayomMwPn5l7sadRuSX6yZ 3NDc+FplW5eyrderjNxtPDgF2sczY6pzPSDpo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728737372; x=1729342172; 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=c1rd/5YnjV8toBF1fUisNrdZyaQgfqf0hhp8bLk1t/I=; b=EGJxUlnHGGTEoEXhWrlph1dpecMYrOd4iGjvV+uqNOne8huGzp6Ote6PzTWA/ruDtS AHcbNLKppvIVV8uO30lHOLioMtq6tVZfXtSHeWeO3l2pTwb58rW9elpfPSSvn6FZVif0 s38wHnwZ4wW17ZRWh5FRxmgslpJwshwW1AlzkLA9Cxw+2OLHCO+sNrJLklQTN9oTGug8 AukI7xwyJA42LWkTj/sMQ+AOjpBTtuGeSDHlaxrWgO392h1lIl7Fi0kCPBmoSsdd9pes IS2FqgH0+k0oQgP/pDZhKxx1E/QvNbLawnsZTrFDszfi9f/K9e8akW8tJ/dl3dygMwaS r1Tg== X-Gm-Message-State: AOJu0YwR30W2ngaTwiqq/yrExGluMe1Z9CsGhr10lEUdJOHWp12b56lF zs4QxuHDKZpEoFUIRi6CvysDMiiftJA0ceR3tOEoSJ1m1xTX/oFPdomeInoO3A86wy4XQXlo55Q = X-Google-Smtp-Source: AGHT+IFuRUgcMUOFZqq22kPnD92ebkE1Ddz7gBVgvPBBCdaXZ2U3IahSYrOWvsYUHHxhdnCtWU9X8Q== X-Received: by 2002:a05:6a00:1256:b0:71e:58be:3604 with SMTP id d2e1a72fcca58-71e58be513dmr247292b3a.4.1728737372366; Sat, 12 Oct 2024 05:49:32 -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 d2e1a72fcca58-71e2aa937cesm4074222b3a.132.2024.10.12.05.49.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Oct 2024 05:49:31 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v2 1/1] libcamera: add method to set thread affinity Date: Sat, 12 Oct 2024 12:47:57 +0000 Message-ID: <20241012124926.1049458-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241012124926.1049458-1-chenghaoyang@chromium.org> References: <20241012124926.1049458-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 | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 53 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..de5423eda 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,51 @@ 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_.clear(); + + const unsigned int num_cpus = std::thread::hardware_concurrency(); + for (const int &cpu : cpus) { + if (cpus_.size() == CPU_SETSIZE) { + LOG(Thread, Error) << "cpus_ already contains " << CPU_SETSIZE + << " cpus. Ignoring the rest."; + break; + } + + if (cpu >= static_cast(num_cpus) || cpu < 0) { + LOG(Thread, Error) << "Ignore an invalid cpu index: " << cpu; + continue; + } + + cpus_.push_back(cpu); + } + + 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 *