Patch Detail
Show a patch.
GET /api/patches/21636/?format=api
{ "id": 21636, "url": "https://patchwork.libcamera.org/api/patches/21636/?format=api", "web_url": "https://patchwork.libcamera.org/patch/21636/", "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": "<20241016084449.3566589-2-chenghaoyang@chromium.org>", "date": "2024-10-16T08:43:44", "name": "[v5,1/1] libcamera: add method to set thread affinity", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "918563e4fb6ee330c5a2370cf23ed2d5ed8dd124", "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/21636/mbox/", "series": [ { "id": 4703, "url": "https://patchwork.libcamera.org/api/series/4703/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4703", "date": "2024-10-16T08:43:43", "name": "Add thread affinity", "version": 5, "mbox": "https://patchwork.libcamera.org/series/4703/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/21636/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/21636/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 264A8C32FA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 16 Oct 2024 08:45:04 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 40E5565386;\n\tWed, 16 Oct 2024 10:45:03 +0200 (CEST)", "from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com\n\t[IPv6:2607:f8b0:4864:20::102e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E3AAA6537E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 16 Oct 2024 10:44:57 +0200 (CEST)", "by mail-pj1-x102e.google.com with SMTP id\n\t98e67ed59e1d1-2e28b75dbd6so4456128a91.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 16 Oct 2024 01:44:57 -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\t98e67ed59e1d1-2e392f03756sm3508553a91.29.2024.10.16.01.44.54\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 16 Oct 2024 01:44:55 -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=\"gcirX33h\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1729068296; x=1729673096;\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=dTKZLNnerUEia6r5PzeYdUu978NY/XyOHsE/wH/qwkM=;\n\tb=gcirX33h3QGyf1rozjC+FrEA2xcO9lp9SPyR3OdNacSQ+XRrwf9SCUgO3v2GMv0Nwv\n\tmlMNYgEr0CB/tkaNgFjwtx8eH+EWQOpEeE3kwKnmJXq6iksZ6oZ3cjupWYFAPKLTV/Nm\n\tNE9GgeDfgQwbNrUTO7D/1MYZAM3jdrz/Yx4Zk=", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1729068296; x=1729673096;\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=dTKZLNnerUEia6r5PzeYdUu978NY/XyOHsE/wH/qwkM=;\n\tb=aQ0QEBfjXZDmVkhnfYdSrOabfsYwjdnph27OZc8lWP93NrnjfSRWTfKxa6jbfLX86u\n\ty0/kLBqe7LOQVnjV1vsUKaHB/AQVNVmdr75YbjymabfqYNcWRrEVuof5/AqbI3RH+U2d\n\tY+PoBmvMwCHvYytyBujupGndl6nYIABQ7KzabaOdnWitiqb6wjmVnDLpB15mS4OA7ctp\n\tFh1CWGvwBZV8YqPeR4j+MV6k6yHx/ZIWMe7Cr1eRlMcnm7tfbHYi8OBvCxF/mwlKz0LS\n\tCoGtyKTLQg3IeWY+59R3tCHBjEHW5nHJcY+DVs1CDj4A6J6jwioIBjdWlKwgvjgr9027\n\tZ+4g==", "X-Gm-Message-State": "AOJu0YwGvWUoDsnhDiIi9GwFyEczD6377vQt5rWR+Nq4oA+HZ11W0/Pn\n\tFikGMhiH6Mv/PzYM/kIOisaaCCe1D4+xENfZMe+Ktqqs5F+70ZKD/SXieiPzQFgYWmsqSOmSlLk\n\t=", "X-Google-Smtp-Source": "AGHT+IHtN986iICbj8Q98rfrjGxLfKYkRvtWv5zu085Ka7K42gmq/NCwW3OPdC76bPztZKhdmPZu0Q==", "X-Received": "by 2002:a17:90b:4c43:b0:2e2:f044:caaa with SMTP id\n\t98e67ed59e1d1-2e31538fb87mr18464509a91.37.1729068296059; \n\tWed, 16 Oct 2024 01:44:56 -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 v5 1/1] libcamera: add method to set thread affinity", "Date": "Wed, 16 Oct 2024 08:43:44 +0000", "Message-ID": "<20241016084449.3566589-2-chenghaoyang@chromium.org>", "X-Mailer": "git-send-email 2.47.0.rc1.288.g06298d1525-goog", "In-Reply-To": "<20241016084449.3566589-1-chenghaoyang@chromium.org>", "References": "<20241016084449.3566589-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 | 46 +++++++++++++++++++++++++++++++++\n 2 files changed, 51 insertions(+)", "diff": "diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h\nindex 4f33de63d..3209d4f7c 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+\tint 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..5dc787dba 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,47 @@ 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+ * If any index is invalid, no cpu indices will be set.\n+ *\n+ * \\return 0 if all indices are valid, -EINVAL otherwise\n+ */\n+int 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+\tfor (const unsigned int &cpu : cpus) {\n+\t\tif (cpu >= numCpus) {\n+\t\t\tLOG(Thread, Error) << \"Ignore an invalid cpu index: \" << cpu;\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tCPU_SET(cpu, &data_->cpuset_.value());\n+\t}\n+\n+\tif (data_->running_)\n+\t\tsetThreadAffinityInternal();\n+\n+\treturn 0;\n+}\n+\n+void Thread::setThreadAffinityInternal()\n+{\n+\tif (!data_->cpuset_)\n+\t\treturn;\n+\n+\tconst cpu_set_t &cpuset = data_->cpuset_.value();\n+\tpthread_setaffinity_np(thread_.native_handle(), sizeof(cpuset), &cpuset);\n+}\n+\n /**\n * \\brief Check if the thread is running\n *\n", "prefixes": [ "v5", "1/1" ] }