Patch Detail
Show a patch.
GET /api/patches/21608/?format=api
{ "id": 21608, "url": "https://patchwork.libcamera.org/api/patches/21608/?format=api", "web_url": "https://patchwork.libcamera.org/patch/21608/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20241014070000.658023-2-chenghaoyang@chromium.org>", "date": "2024-10-14T06:58:56", "name": "[v3,1/1] libcamera: add method to set thread affinity", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "b5b7af4716a9c1e52daf2ebfe0e46e01e59ea0cc", "submitter": { "id": 117, "url": "https://patchwork.libcamera.org/api/people/117/?format=api", "name": "Cheng-Hao Yang", "email": "chenghaoyang@chromium.org" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/21608/mbox/", "series": [ { "id": 4693, "url": "https://patchwork.libcamera.org/api/series/4693/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4693", "date": "2024-10-14T06:58:55", "name": "Add thread affinity", "version": 3, "mbox": "https://patchwork.libcamera.org/series/4693/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/21608/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/21608/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 46520C32F4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 14 Oct 2024 07:00:13 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 701916537B;\n\tMon, 14 Oct 2024 09:00:10 +0200 (CEST)", "from mail-pg1-x534.google.com (mail-pg1-x534.google.com\n\t[IPv6:2607:f8b0:4864:20::534])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5036A65369\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Oct 2024 09:00:08 +0200 (CEST)", "by mail-pg1-x534.google.com with SMTP id\n\t41be03b00d2f7-7cd8803fe0aso2642860a12.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Oct 2024 00:00:08 -0700 (PDT)", "from chenghaoyang-low.c.googlers.com.com\n\t(199.211.81.34.bc.googleusercontent.com. [34.81.211.199])\n\tby smtp.gmail.com with ESMTPSA id\n\td2e1a72fcca58-71e675a9566sm1230461b3a.54.2024.10.14.00.00.05\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 14 Oct 2024 00:00:06 -0700 (PDT)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"b9fVVLFN\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1728889206; x=1729494006;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=Y2Zc0vPz4qFQ7+53+wuFDEix1//hdzOc84hhW6Gj8GM=;\n\tb=b9fVVLFNHF4BkXBhvuUIvCuRhK0iFW8jUTtklsPg8wyda9my+jbwR42jVRqFfjrTVQ\n\tGvjQmPf95Sw2FmBrm36KjI+pZ7tYoPBmnO8+Hyg6Qo3AOR7sj2iu5RtCE6/2lpP4uk/G\n\tIPpitijitCxfQ7VBCJW4NyEjpOMowFDTSG4m4=", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1728889206; x=1729494006;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=Y2Zc0vPz4qFQ7+53+wuFDEix1//hdzOc84hhW6Gj8GM=;\n\tb=diaGS1Ac/1QQBNBQCi0MckNLFFYLbrVtwXobfJopXUETAGSZvke0asXYpOKh7yCpXE\n\tPu1NXpZLgaxnHvgOHg8Pk4LtbAOOT4+LYSSMT3uX1wI3kROUXF3e8D4v9TTZw6HFckk7\n\t+Sdu9Vvm6N3DhM4rO+GtjTcP2kzH9+B+lbV6mGgdRheMwM8AYvU4zLK1H8k51cpzcH5j\n\tG0JMEDoKTiu9YB0mztn4OpqpALka5+DpupUxj0hXIJaO0USRDnKkundYyE99FKht/Eps\n\t8duVEFjTqNcpsATKwvQdD/5uIwhU3+JC25wPzB8sxDEyK7u1vEk78CrBdR1QJGi9tHLd\n\tzpCA==", "X-Gm-Message-State": "AOJu0YzLh8+MF8lMrI40PN9fd8VPq2IhMeBSTxr5GoEb8CakctQzcOQE\n\ta5DbSYYbj5zp1qbVRVNd8xuOuk5D05HJUREjCKoE7vv+T5ai+M5IxOQFx8epIKeJOMOk60zn+KM\n\t=", "X-Google-Smtp-Source": "AGHT+IF+ebTEdsKnvbC9kOYRl6D7FoTAQpbTM+BC9jNJmGERBUNMgz3MV4D2hd4ZkGh7DgXXx6BomQ==", "X-Received": "by 2002:a05:6a21:505:b0:1d8:aa24:caeb with SMTP id\n\tadf61e73a8af0-1d8bcfc5932mr19100749637.49.1728889206446; \n\tMon, 14 Oct 2024 00:00:06 -0700 (PDT)", "From": "Harvey Yang <chenghaoyang@chromium.org>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Han-Lin Chen <hanlinchen@chromium.org>,\n\tHarvey Yang <chenghaoyang@chromium.org>", "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", "Content-Transfer-Encoding": "8bit", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "From: Han-Lin Chen <hanlinchen@chromium.org>\n\nAdd method to set thread affinity to Thread class.\n\nSigned-off-by: Han-Lin Chen <hanlinchen@chromium.org>\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\nSigned-off-by: Harvey Yang <chenghaoyang@chromium.org>\n---\n include/libcamera/base/thread.h | 5 ++++\n src/libcamera/base/thread.cpp | 52 +++++++++++++++++++++++++++++++++\n 2 files changed, 57 insertions(+)", "diff": "diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h\nindex 4f33de63d..58cae341b 100644\n--- a/include/libcamera/base/thread.h\n+++ b/include/libcamera/base/thread.h\n@@ -15,6 +15,7 @@\n \n #include <libcamera/base/message.h>\n #include <libcamera/base/signal.h>\n+#include <libcamera/base/span.h>\n #include <libcamera/base/utils.h>\n \n namespace libcamera {\n@@ -35,6 +36,8 @@ public:\n \tvoid exit(int code = 0);\n \tbool wait(utils::duration duration = utils::duration::max());\n \n+\tvoid setThreadAffinity(const Span<const unsigned int> &cpus);\n+\n \tbool isRunning();\n \n \tSignal<> finished;\n@@ -54,6 +57,8 @@ private:\n \tvoid startThread();\n \tvoid finishThread();\n \n+\tvoid setThreadAffinityInternal();\n+\n \tvoid postMessage(std::unique_ptr<Message> msg, Object *receiver);\n \tvoid removeMessages(Object *receiver);\n \ndiff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\nindex 8735670b8..f40c7d8e7 100644\n--- a/src/libcamera/base/thread.cpp\n+++ b/src/libcamera/base/thread.cpp\n@@ -9,6 +9,7 @@\n \n #include <atomic>\n #include <list>\n+#include <optional>\n #include <sys/syscall.h>\n #include <sys/types.h>\n #include <unistd.h>\n@@ -128,6 +129,8 @@ private:\n \tint exitCode_;\n \n \tMessageQueue messages_;\n+\n+\tstd::optional<cpu_set_t> cpuset_ = std::nullopt;\n };\n \n /**\n@@ -254,6 +257,8 @@ void Thread::start()\n \tdata_->exit_.store(false, std::memory_order_relaxed);\n \n \tthread_ = std::thread(&Thread::startThread, this);\n+\n+\tsetThreadAffinityInternal();\n }\n \n void Thread::startThread()\n@@ -410,6 +415,53 @@ bool Thread::wait(utils::duration duration)\n \treturn hasFinished;\n }\n \n+/**\n+ * \\brief Set the CPU affinity mask of the thread\n+ * \\param[in] cpus The list of CPU indices that the thread is set affinity to\n+ *\n+ * The cpu indices should be within [0, std::thread::hardware_concurrency()),\n+ * and the total number should not exceed `CPU_SETSIZE`. Invalid indices and\n+ * extra indices will be ignored.\n+ */\n+void Thread::setThreadAffinity(const Span<const unsigned int> &cpus)\n+{\n+\tconst unsigned int numCpus = std::thread::hardware_concurrency();\n+\n+\tMutexLocker locker(data_->mutex_);\n+\tdata_->cpuset_ = cpu_set_t();\n+\tCPU_ZERO(&data_->cpuset_.value());\n+\n+\tunsigned int count = 0;\n+\tfor (const unsigned int &cpu : cpus) {\n+\t\tif (count == CPU_SETSIZE) {\n+\t\t\tLOG(Thread, Error) << \"cpus_ already contains \" << CPU_SETSIZE\n+\t\t\t\t\t << \" cpus. Ignoring the rest.\";\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (cpu >= numCpus) {\n+\t\t\tLOG(Thread, Error) << \"Ignore an invalid cpu index: \" << cpu;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\t++count;\n+\t\tCPU_SET(cpu, &data_->cpuset_.value());\n+\t}\n+\n+\tif (data_->running_)\n+\t\tsetThreadAffinityInternal();\n+}\n+\n+void Thread::setThreadAffinityInternal()\n+{\n+\tif (!data_->cpuset_)\n+\t\treturn;\n+\n+\tauto handle = thread_.native_handle();\n+\n+\tpthread_setaffinity_np(handle, sizeof(cpu_set_t), &data_->cpuset_.value());\n+}\n+\n /**\n * \\brief Check if the thread is running\n *\n", "prefixes": [ "v3", "1/1" ] }