From patchwork Thu Oct 24 08:13:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21748 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 0E0BDC3213 for ; Thu, 24 Oct 2024 08:15:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B524A65397; Thu, 24 Oct 2024 10:15:35 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="O86A5t7w"; dkim-atps=neutral Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AD5AE6538A for ; Thu, 24 Oct 2024 10:15:32 +0200 (CEST) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-7ea8c4ce232so488191a12.0 for ; Thu, 24 Oct 2024 01:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729757731; x=1730362531; 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=4H3RJjobAMRuZ12/pEQ3Vt8fw7PU/0OGrMFY14T5nmE=; b=O86A5t7wuogzHBX6SuNyehBbBeW9ewQM3zxEqHFVb2grceC/fdSFELgXzpeD6To4nH TCdBSsE/WsrmimpIFr5PPuJRf7aEKdud/M1SP//KK/ZkatLDDVoO6RqH9mxZ+3Rf51PI W0Wg5eq43K9xr2E9c0wR4VwILOVEFicW13rvc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729757731; x=1730362531; 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=4H3RJjobAMRuZ12/pEQ3Vt8fw7PU/0OGrMFY14T5nmE=; b=hZvRvIaJQg8w520eoN5H3GdYHnfGvUbri8vy+SJGSlYD72JVLqoZLkS8kv++mlMVQk pIrr/XRmMfv4F/xEZiBBOVwGokXG0fpKrF8ZX7afvXnvDtUzg6BvPYif3kByMSNA56zn sg/3zul7cm5oSUM7mfVxGvWelt70rfGHfa0cErJg5lvv64GV0ES4qyrfEfD0RaCS5f1S uwdpTq3ghbemuFyTAgMy+B8OsecgFH/cj9n/Wx9hCz7rh5CdjhvSjXCjR7t/GniboRyf 3o1PZEHIQUBTWYKxrlAhyPOjGU0hjwSi2F9vxQQk12MnVDOG4iXzmbZY/SiHe8xYdYdt 9Now== X-Gm-Message-State: AOJu0YxoLPE2BMvxjJlW4dR7puGrzPXeEmdu4VD4nBkiIcf80KI4DeEC lhkejXWTtNnci+edrriYSlI92qlpNVzIJtmcivqytfvBlDcJr8ymcuCjZxEG/BUkGEkazVOw/bE = X-Google-Smtp-Source: AGHT+IHRofEB7SFIM4Q+uH2brg+6KhaR/B6Nrp5grY7UrmndcP7dm0VQEiwYo2a81AQPVbzUjrK8sg== X-Received: by 2002:a05:6a20:b40a:b0:1d9:261c:5937 with SMTP id adf61e73a8af0-1d978ba06eemr5895314637.33.1729757730808; Thu, 24 Oct 2024 01:15:30 -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 41be03b00d2f7-7eaeab1e5cdsm8068360a12.21.2024.10.24.01.15.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 01:15:29 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v6 1/1] libcamera: add method to set thread affinity Date: Thu, 24 Oct 2024 08:13:53 +0000 Message-ID: <20241024081520.2912819-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog In-Reply-To: <20241024081520.2912819-1-chenghaoyang@chromium.org> References: <20241024081520.2912819-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 | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 52 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 *