{"id":21593,"url":"https://patchwork.libcamera.org/api/patches/21593/?format=json","web_url":"https://patchwork.libcamera.org/patch/21593/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20241011171930.3225255-2-chenghaoyang@chromium.org>","date":"2024-10-11T17:00:48","name":"[1/1] libcamera: add method to set thread affinity","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"8c91b77a39432a5b7ab23880da21f1561d133e9d","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21593/mbox/","series":[{"id":4686,"url":"https://patchwork.libcamera.org/api/series/4686/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4686","date":"2024-10-11T17:00:47","name":"Add thread affinity","version":1,"mbox":"https://patchwork.libcamera.org/series/4686/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21593/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21593/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 66EDEC32F1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 11 Oct 2024 17:19:43 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 801FA6537F;\n\tFri, 11 Oct 2024 19:19:41 +0200 (CEST)","from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com\n\t[IPv6:2607:f8b0:4864:20::62d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 67FBD6537D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 11 Oct 2024 19:19:38 +0200 (CEST)","by mail-pl1-x62d.google.com with SMTP id\n\td9443c01a7336-20cb47387ceso3679045ad.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 11 Oct 2024 10:19:38 -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\td9443c01a7336-20c8badc3b2sm25571975ad.12.2024.10.11.10.19.35\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 11 Oct 2024 10:19:36 -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=\"kNA3pOW+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1728667176; x=1729271976;\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=wtAlbItorfTrz/hKhS8f5b3FIZdquXtIFAS810zOKIM=;\n\tb=kNA3pOW+SCyxvWyBWbMa0NJiH9jFAPDvvbCCMTUX0TKt9vJLo9PmP/m8gmoAABvjLa\n\tgUkVglzE24dn8te61F3IV22BjeC9a/3kaKQwo3HcFDtSVni9DA2CJoWXvjxAzGeMqtNq\n\tMZbE2MnvKwgJi14THU5YeH1ZwepZ86yuuUkBc=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1728667176; x=1729271976;\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=wtAlbItorfTrz/hKhS8f5b3FIZdquXtIFAS810zOKIM=;\n\tb=LG6FmLwF46af0NeW+DGWBgmLcGuiF09zS5puFATdDFZ9gl9NkQt0TE2Foid/WqSRFh\n\tOnByziUeE/BByPA68mUp+DvEIkCXhJTDB5Wr4G2AG7jocWkL3KCdXrp9y566ox0ou5m0\n\tbHbrgT5c5L3h3x8KEo/11lkCJUVQZP7LGYnPvgSdm+7wBgFX25JhrDSFweWtcC9/62kW\n\tzR9zIRnfJdT3S/JTzayMZc21aheH6VKCMLRHojpVtWylvVtWofOhqlhnjqDRV0tQOMlm\n\t1TXGI1O8EZyXP5ebULCi7TIE/uqAANL9C8WMngwq2uCiy5rl1S05Qn9LPCDoG1xpn6Mt\n\tJiXA==","X-Gm-Message-State":"AOJu0YwmNEEAjT1yVKLQ6A1o8N+cSlMg0IXGCuRVu4s8lRcai3ZwYWMP\n\tS7r/TNslGCpfUL2aUjyXzYlGhJW73OQ3VJ+3Uiypp7Fq/QMCO2ltgj6Ysd9AiHAjN2NqedPBJ44\n\t=","X-Google-Smtp-Source":"AGHT+IEEfhaKa7weHsLbca3B155tYjQ3gbMsTkLOb+PTegONrWnkd63/RczBLOd4M5wf2KHJ/ETaKg==","X-Received":"by 2002:a17:903:2348:b0:20c:8cf9:6134 with SMTP id\n\td9443c01a7336-20ca142475fmr47981375ad.4.1728667176625; \n\tFri, 11 Oct 2024 10:19:36 -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 1/1] libcamera: add method to set thread affinity","Date":"Fri, 11 Oct 2024 17:00:48 +0000","Message-ID":"<20241011171930.3225255-2-chenghaoyang@chromium.org>","X-Mailer":"git-send-email 2.47.0.rc1.288.g06298d1525-goog","In-Reply-To":"<20241011171930.3225255-1-chenghaoyang@chromium.org>","References":"<20241011171930.3225255-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   | 31 +++++++++++++++++++++++++++++++\n 2 files changed, 37 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..2417043a3 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,35 @@ 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_ = cpus;\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":["1/1"]}