From 559128b1f1b3cfd82f46b8a1248cbb4de0463db1 Mon Sep 17 00:00:00 2001 From: "Schulz, Andreas" Date: Tue, 4 Nov 2025 18:50:02 +0000 Subject: [PATCH] Thread: Add name parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For debugging purposes, threads can be assigned a name, which eases distinguishing between them in e.g. htop or gdb. This uses a Linux-specific API for now which is limited to 15 characters (+ null terminator), so truncation is done and names for existing thread instantiations were chosen to be consise. [Kieran: Apply checkstyle suggestions, rebase on proxy rework] Signed-off-by: Schulz, Andreas Signed-off-by: Kieran Bingham Reviewed-by: Daniel Scally Reviewed-by: Barnabás Pőcze Signed-off-by: Kieran Bingham --- include/libcamera/base/thread.h | 4 +++- src/libcamera/base/thread.cpp | 11 +++++++++-- src/libcamera/camera_manager.cpp | 2 +- src/libcamera/pipeline/virtual/virtual.cpp | 2 +- src/libcamera/software_isp/software_isp.cpp | 3 ++- .../libcamera_templates/module_ipa_proxy.cpp.tmpl | 2 +- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h index dc1aca27..6aa36fa1 100644 --- a/include/libcamera/base/thread.h +++ b/include/libcamera/base/thread.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include @@ -30,7 +31,7 @@ class ThreadMain; class Thread { public: - Thread(); + Thread(std::string name = {}); virtual ~Thread(); void start(); @@ -74,6 +75,7 @@ private: void moveObject(Object *object, ThreadData *currentData, ThreadData *targetData); + std::string name_; std::thread thread_; std::unique_ptr data_; }; diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp index 338cbcc2..39209ec8 100644 --- a/src/libcamera/base/thread.cpp +++ b/src/libcamera/base/thread.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -144,6 +145,7 @@ class ThreadMain : public Thread { public: ThreadMain() + : Thread("libcamera-main") { data_->running_ = true; } @@ -234,8 +236,9 @@ ThreadData *ThreadData::current() /** * \brief Create a thread */ -Thread::Thread() - : data_(std::make_unique(this)) +Thread::Thread(std::string name) + : name_(std::move(name)), + data_(std::make_unique(this)) { } @@ -288,6 +291,10 @@ void Thread::startThread() data_->tid_ = syscall(SYS_gettid); currentThreadData = data_.get(); + if (!name_.empty()) + pthread_setname_np(thread_.native_handle(), + name_.substr(0, 15).c_str()); + run(); } diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index c203b08f..83510e06 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -39,7 +39,7 @@ LOG_DEFINE_CATEGORY(Camera) #ifndef __DOXYGEN_PUBLIC__ CameraManager::Private::Private() - : initialized_(false) + : Thread("CameraManager"), initialized_(false) { ipaManager_ = std::make_unique(this->configuration()); } diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index 23eae852..09e0afc9 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -115,7 +115,7 @@ private: VirtualCameraData::VirtualCameraData(PipelineHandler *pipe, const std::vector &supportedResolutions) - : Camera::Private(pipe) + : Camera::Private(pipe), Thread("VirtualCamera") { config_.resolutions = supportedResolutions; for (const auto &resolution : config_.resolutions) { diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp index fdadf79e..341c0352 100644 --- a/src/libcamera/software_isp/software_isp.cpp +++ b/src/libcamera/software_isp/software_isp.cpp @@ -75,7 +75,8 @@ LOG_DEFINE_CATEGORY(SoftwareIsp) */ SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor, ControlInfoMap *ipaControls) - : dmaHeap_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap | + : ispWorkerThread_("SWIspWorker"), + dmaHeap_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap | DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap | DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf) { diff --git a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl index 0d03c1df..e6e19b30 100644 --- a/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl +++ b/utils/codegen/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl @@ -46,7 +46,7 @@ namespace {{ns}} { {%- endif %} {{proxy_name}}Threaded::{{proxy_name}}Threaded(IPAModule *ipam, const GlobalConfiguration &configuration) - : {{proxy_name}}(ipam, configuration) + : {{proxy_name}}(ipam, configuration), thread_("{{proxy_name}}") { LOG(IPAProxy, Debug) << "initializing {{module_name}} proxy in thread: loading IPA from "