From patchwork Tue Oct 15 06:54:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21627 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 02466C0F2A for ; Tue, 15 Oct 2024 06:55:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4505A65385; Tue, 15 Oct 2024 08:55:46 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ewikc5uK"; dkim-atps=neutral Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 28AFD6537D for ; Tue, 15 Oct 2024 08:55:42 +0200 (CEST) Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2e2e88cb0bbso2701082a91.3 for ; Mon, 14 Oct 2024 23:55:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1728975340; x=1729580140; 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=jcQ/uYmurIcrmbh97DA61AFT7ZM0z5RxJmrf5o+Rlhk=; b=Ewikc5uKXU4lR/E89a4oNbgkp+ezd3J17eIwGthdLJPZzOS/onObbRXCbTqvynUUbK +LPhO/GzDT2+uSG01+XOkPaknWUvFmyJb9OKg/0nNvY7D5q2xWh4w5OTDpU3sIXKde/P 38DtJ2+bSK2W19MvDcTgfUBsL+LoCi5/vZxhg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728975340; x=1729580140; 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=jcQ/uYmurIcrmbh97DA61AFT7ZM0z5RxJmrf5o+Rlhk=; b=VK3/XD4Yo+TNWROIK3rPQS6tuCzgpuN0OsjjcH9dBLVheOBtI5gT5q8OsqIssEAJY9 SrpSHf+IxUcNdF8hmljwFBpTBso3F5FxYD/3KUtq4FqfSxdJmLYnnbf3C1O+jkjPVhas lQGjTrd9phphayzkJZlRILvMtqbGwGaC4Veyh+b4akQprd1vUKjRdJ0hVbQnH2ob7dnj BU3Qb1TcfFem+1RVDdEU2d6MxAhznoLCqqMB43RrJR3PtwNL5k0VFHDJKc8UHygkSOJL VAWr9aPRBUaep0lr9CIyy5hi3F7LnvYIPByq/YS9sVlJTv88FWgfMZZ7SlMAHISGySV9 UIfA== X-Gm-Message-State: AOJu0YwlHrgCYQMJYJH4X+zBgrHcCJPgMa32QNo1LUhl976gMNkkAzOl ZG4oXJZF3DQZL3364376Arz7/8GshW4L98UQFWsaDTpu2mRhBlyjSmrzO7WnI57qqyp4Qi4X8Pc = X-Google-Smtp-Source: AGHT+IFcuIALwMYQ65nz7LW67kaIA5SoM6f1V4CFoqhilM5qxxz9oWo29o3BjTCVzpQMcZvgsHvHrw== X-Received: by 2002:a17:90a:6886:b0:2e2:d16e:8769 with SMTP id 98e67ed59e1d1-2e2f0aa8914mr16648865a91.15.1728975339945; Mon, 14 Oct 2024 23:55:39 -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 98e67ed59e1d1-2e392f75624sm790215a91.53.2024.10.14.23.55.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 23:55:39 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v4 1/1] libcamera: add method to set thread affinity Date: Tue, 15 Oct 2024 06:54:36 +0000 Message-ID: <20241015065533.356466-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog In-Reply-To: <20241015065533.356466-1-chenghaoyang@chromium.org> References: <20241015065533.356466-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 | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 58 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..5491e3f90 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,54 @@ 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(data_->cpuset_.value()), + &data_->cpuset_.value()); +} + /** * \brief Check if the thread is running *