Show a patch.

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

{
    "id": 21604,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/21604/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21604/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20241012124926.1049458-2-chenghaoyang@chromium.org>",
    "date": "2024-10-12T12:47:57",
    "name": "[v2,1/1] libcamera: add method to set thread affinity",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "5b3b9e61032101896f032d158c306e5214990a3c",
    "submitter": {
        "id": 117,
        "url": "https://patchwork.libcamera.org/api/1.1/people/117/?format=api",
        "name": "Cheng-Hao Yang",
        "email": "chenghaoyang@chromium.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21604/mbox/",
    "series": [
        {
            "id": 4690,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4690/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4690",
            "date": "2024-10-12T12:47:56",
            "name": "Add thread affinity",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/4690/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21604/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21604/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 5F190C32F3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 12 Oct 2024 12:49:38 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2C04A6537E;\n\tSat, 12 Oct 2024 14:49:37 +0200 (CEST)",
            "from mail-pf1-x429.google.com (mail-pf1-x429.google.com\n\t[IPv6:2607:f8b0:4864:20::429])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 21FD563538\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 12 Oct 2024 14:49:34 +0200 (CEST)",
            "by mail-pf1-x429.google.com with SMTP id\n\td2e1a72fcca58-71e579abb99so70618b3a.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 12 Oct 2024 05:49:34 -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\td2e1a72fcca58-71e2aa937cesm4074222b3a.132.2024.10.12.05.49.31\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSat, 12 Oct 2024 05:49:31 -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=\"eTFaXkqO\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1728737372; x=1729342172;\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=c1rd/5YnjV8toBF1fUisNrdZyaQgfqf0hhp8bLk1t/I=;\n\tb=eTFaXkqO8Rva2zsZBHH2G3FwczsWRAeGcRPZ9hYhaNx9nVLtH7Pu7zZNphteZgY/V8\n\tosySSX+UM0fkAegf5I5TYnjDzUbV/BBh2pnT+P/CZa6EAKCayomMwPn5l7sadRuSX6yZ\n\t3NDc+FplW5eyrderjNxtPDgF2sczY6pzPSDpo=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1728737372; x=1729342172;\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=c1rd/5YnjV8toBF1fUisNrdZyaQgfqf0hhp8bLk1t/I=;\n\tb=EGJxUlnHGGTEoEXhWrlph1dpecMYrOd4iGjvV+uqNOne8huGzp6Ote6PzTWA/ruDtS\n\tAHcbNLKppvIVV8uO30lHOLioMtq6tVZfXtSHeWeO3l2pTwb58rW9elpfPSSvn6FZVif0\n\ts38wHnwZ4wW17ZRWh5FRxmgslpJwshwW1AlzkLA9Cxw+2OLHCO+sNrJLklQTN9oTGug8\n\tAukI7xwyJA42LWkTj/sMQ+AOjpBTtuGeSDHlaxrWgO392h1lIl7Fi0kCPBmoSsdd9pes\n\tIS2FqgH0+k0oQgP/pDZhKxx1E/QvNbLawnsZTrFDszfi9f/K9e8akW8tJ/dl3dygMwaS\n\tr1Tg==",
        "X-Gm-Message-State": "AOJu0YwR30W2ngaTwiqq/yrExGluMe1Z9CsGhr10lEUdJOHWp12b56lF\n\tzs4QxuHDKZpEoFUIRi6CvysDMiiftJA0ceR3tOEoSJ1m1xTX/oFPdomeInoO3A86wy4XQXlo55Q\n\t=",
        "X-Google-Smtp-Source": "AGHT+IFuRUgcMUOFZqq22kPnD92ebkE1Ddz7gBVgvPBBCdaXZ2U3IahSYrOWvsYUHHxhdnCtWU9X8Q==",
        "X-Received": "by 2002:a05:6a00:1256:b0:71e:58be:3604 with SMTP id\n\td2e1a72fcca58-71e58be513dmr247292b3a.4.1728737372366; \n\tSat, 12 Oct 2024 05:49:32 -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 v2 1/1] libcamera: add method to set thread affinity",
        "Date": "Sat, 12 Oct 2024 12:47:57 +0000",
        "Message-ID": "<20241012124926.1049458-2-chenghaoyang@chromium.org>",
        "X-Mailer": "git-send-email 2.47.0.rc1.288.g06298d1525-goog",
        "In-Reply-To": "<20241012124926.1049458-1-chenghaoyang@chromium.org>",
        "References": "<20241012124926.1049458-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 |  6 +++++\n src/libcamera/base/thread.cpp   | 47 +++++++++++++++++++++++++++++++++\n 2 files changed, 53 insertions(+)",
    "diff": "diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h\nindex 4f33de63d..c74fe102b 100644\n--- a/include/libcamera/base/thread.h\n+++ b/include/libcamera/base/thread.h\n@@ -35,6 +35,8 @@ public:\n \tvoid exit(int code = 0);\n \tbool wait(utils::duration duration = utils::duration::max());\n \n+\tvoid setThreadAffinity(const std::vector<int> &cpus);\n+\n \tbool isRunning();\n \n \tSignal<> finished;\n@@ -54,6 +56,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 \n@@ -67,6 +71,8 @@ private:\n \n \tstd::thread thread_;\n \tThreadData *data_;\n+\n+\tstd::vector<int> cpus_;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp\nindex 8735670b8..de5423eda 100644\n--- a/src/libcamera/base/thread.cpp\n+++ b/src/libcamera/base/thread.cpp\n@@ -254,6 +254,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 +412,51 @@ 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+void Thread::setThreadAffinity(const std::vector<int> &cpus)\n+{\n+\tcpus_.clear();\n+\n+\tconst unsigned int num_cpus = std::thread::hardware_concurrency();\n+\tfor (const int &cpu : cpus) {\n+\t\tif (cpus_.size() == 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 >= static_cast<int>(num_cpus) || cpu < 0) {\n+\t\t\tLOG(Thread, Error) << \"Ignore an invalid cpu index: \" << cpu;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tcpus_.push_back(cpu);\n+\t}\n+\n+\tMutexLocker locker(data_->mutex_);\n+\tif (data_->running_)\n+\t\tsetThreadAffinityInternal();\n+}\n+\n+void Thread::setThreadAffinityInternal()\n+{\n+\tif (cpus_.empty())\n+\t\treturn;\n+\n+\tauto handle = thread_.native_handle();\n+\n+\tcpu_set_t cpuset;\n+\tCPU_ZERO(&cpuset);\n+\n+\tfor (auto cpu : cpus_)\n+\t\tCPU_SET(cpu, &cpuset);\n+\n+\tpthread_setaffinity_np(handle, sizeof(cpu_set_t), &cpuset);\n+}\n+\n /**\n  * \\brief Check if the thread is running\n  *\n",
    "prefixes": [
        "v2",
        "1/1"
    ]
}