Show a patch.

GET /api/patches/21767/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 21767,
    "url": "https://patchwork.libcamera.org/api/patches/21767/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21767/",
    "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": "<20241029085837.3615699-2-chenghaoyang@chromium.org>",
    "date": "2024-10-29T08:57:55",
    "name": "[v7,1/1] libcamera: add method to set thread affinity",
    "commit_ref": "4d9db06d669044c0c461a2aed79c85c7fe32a502",
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "490eff589a9f9e390a29e0a1e4e6824bef1f69a4",
    "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/21767/mbox/",
    "series": [
        {
            "id": 4751,
            "url": "https://patchwork.libcamera.org/api/series/4751/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4751",
            "date": "2024-10-29T08:57:54",
            "name": "Add thread affinity",
            "version": 7,
            "mbox": "https://patchwork.libcamera.org/series/4751/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21767/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21767/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 99651C3220\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 29 Oct 2024 08:58:54 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2F4D0653A8;\n\tTue, 29 Oct 2024 09:58:49 +0100 (CET)",
            "from mail-oi1-x232.google.com (mail-oi1-x232.google.com\n\t[IPv6:2607:f8b0:4864:20::232])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 33C6065398\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 29 Oct 2024 09:58:46 +0100 (CET)",
            "by mail-oi1-x232.google.com with SMTP id\n\t5614622812f47-3e606cba08eso3065661b6e.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 29 Oct 2024 01:58:46 -0700 (PDT)",
            "from chenghaoyang-low.c.googlers.com.com\n\t(27.247.221.35.bc.googleusercontent.com. [35.221.247.27])\n\tby smtp.gmail.com with ESMTPSA id\n\t41be03b00d2f7-7edc89f2faesm7000856a12.60.2024.10.29.01.58.43\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 29 Oct 2024 01:58:44 -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=\"dyUmHfb4\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1730192324; x=1730797124;\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=GIpyMBbuJIOv8U6VsJd+oUR+ViHmb7sQ9ux1KQSBe28=;\n\tb=dyUmHfb4NYJEASOM+HYV6JdMPWgmI3wFEaiLWohh1KIe5jlBgkDK13lJCZcn1y21Va\n\tSo6neeAXd9k/82+BGnClNtUMrncPp8m/a+UnwDFTdG+Z6eM/fq/LPPT8nrfZijSPRrEm\n\tpCNA25JkJMs7TEaJ10OtqU44fDXMmpTHlRp9Q=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1730192324; x=1730797124;\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=GIpyMBbuJIOv8U6VsJd+oUR+ViHmb7sQ9ux1KQSBe28=;\n\tb=Lgjq2aKensWQ1brLKfRzRJbeTxMoW7gVet6KB2XW9SoOWAHN57r2rOboxf2NMEwrRf\n\t/PofOD7Kv0xCHqD+5sUgOdVywubjEwXQFzmzoQ3dNszCoo9gujEv6bagrdNf5LSGCmj9\n\tiLHUXM+IKMBYTGHcd+dIbdUIDzbax0CqXiFtgR3H6U0oK0SihmiEDd4Vaj9F/WfnrQyI\n\t4vrhUPERenjGNX1SMboJr+LKj3tFXpaqWhjlIvrtS9z27QRoSVeh3vFRULa6JcRVpj+e\n\tsCcL/fGkX5IJ9uc+tSsp9JDs5SZ7D4hUwRwdKGdSX3pi9pd9ESZL/JPearppjkOVv8ln\n\tQFCg==",
        "X-Gm-Message-State": "AOJu0YzPrXBKWfF8DZFtpHOtBiRE8w0/FRY5UovqIZ7hyKPdSgUKzwTR\n\tNk6Hxvapiy5E5Wi/PxjMwyhPG5LyhtD43/w65Tjs4uEoh+CcYvuunTCw0jsLSiFsmAq+rlAzz1Y\n\t=",
        "X-Google-Smtp-Source": "AGHT+IHekJZCCtYoZDpcGIHsNpkrKVm7nBLb9k+bn2LUFxeMio593Sizl03a6sRTrP/HiPXSJ8Tsvw==",
        "X-Received": "by 2002:a05:6808:228f:b0:3e2:9e62:37d5 with SMTP id\n\t5614622812f47-3e638239500mr8353236b6e.5.1730192324565; \n\tTue, 29 Oct 2024 01:58:44 -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 v7 1/1] libcamera: add method to set thread affinity",
        "Date": "Tue, 29 Oct 2024 08:57:55 +0000",
        "Message-ID": "<20241029085837.3615699-2-chenghaoyang@chromium.org>",
        "X-Mailer": "git-send-email 2.47.0.163.g1226f6d8fa-goog",
        "In-Reply-To": "<20241029085837.3615699-1-chenghaoyang@chromium.org>",
        "References": "<20241029085837.3615699-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   | 47 +++++++++++++++++++++++++++++++++\n test/threads.cpp                | 40 ++++++++++++++++++++++++++++\n 3 files changed, 92 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..f6322fe31 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_;\n };\n \n /**\n@@ -281,6 +284,8 @@ void Thread::startThread()\n \tdata_->tid_ = syscall(SYS_gettid);\n \tcurrentThreadData = data_;\n \n+\tsetThreadAffinityInternal();\n+\n \trun();\n }\n \n@@ -410,6 +415,48 @@ 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, this function won't modify the thread affinity and\n+ * will return an error.\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) << \"Invalid CPU \" << cpu << \"for thread affinity\";\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  *\ndiff --git a/test/threads.cpp b/test/threads.cpp\nindex ceb4fa0f2..8d6ee1510 100644\n--- a/test/threads.cpp\n+++ b/test/threads.cpp\n@@ -9,9 +9,11 @@\n #include <iostream>\n #include <memory>\n #include <pthread.h>\n+#include <sched.h>\n #include <thread>\n #include <time.h>\n \n+#include <libcamera/base/object.h>\n #include <libcamera/base/thread.h>\n \n #include \"test.h\"\n@@ -66,6 +68,27 @@ private:\n \tbool &cancelled_;\n };\n \n+class CpuSetTester : public Object\n+{\n+public:\n+\tCpuSetTester(unsigned int cpuset)\n+\t\t: cpuset_(cpuset) {}\n+\n+\tbool testCpuSet()\n+\t{\n+\t\tint ret = sched_getcpu();\n+\t\tif (static_cast<int>(cpuset_) != ret) {\n+\t\t\tcout << \"Invalid cpuset: \" << ret << \", expecting: \" << cpuset_ << endl;\n+\t\t\treturn false;\n+\t\t}\n+\n+\t\treturn true;\n+\t}\n+\n+private:\n+\tconst unsigned int cpuset_;\n+};\n+\n class ThreadTest : public Test\n {\n protected:\n@@ -165,6 +188,23 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\tconst unsigned int numCpus = std::thread::hardware_concurrency();\n+\t\tfor (unsigned int i = 0; i < numCpus; ++i) {\n+\t\t\tthread = std::make_unique<Thread>();\n+\t\t\tconst std::array<const unsigned int, 1> cpus{ i };\n+\t\t\tthread->setThreadAffinity(cpus);\n+\t\t\tthread->start();\n+\n+\t\t\tCpuSetTester tester(i);\n+\t\t\ttester.moveToThread(thread.get());\n+\n+\t\t\tif (!tester.invokeMethod(&CpuSetTester::testCpuSet, ConnectionTypeBlocking))\n+\t\t\t\treturn TestFail;\n+\n+\t\t\tthread->exit(0);\n+\t\t\tthread->wait();\n+\t\t}\n+\n \t\treturn TestPass;\n \t}\n \n",
    "prefixes": [
        "v7",
        "1/1"
    ]
}