From patchwork Fri Dec 4 03:18:57 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Remy Alipoursafari X-Patchwork-Id: 29162 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 91BFEC3300 for ; Fri, 4 Dec 2026 03:20:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2D3086AB60; Fri, 4 Dec 2026 04:20:22 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=coolgi.dev header.i=@coolgi.dev header.b="b+8EA77t"; dkim=pass (2048-bit key; unprotected) header.d=coolgi.dev header.i=@coolgi.dev header.b="V3b+Uvj4"; dkim-atps=neutral Received: from mail.coolgi.dev (mail.coolgi.dev [185.193.17.204]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E8D116AB60 for ; Fri, 4 Dec 2026 04:20:20 +0100 (CET) From: Remy Alipoursafari DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=coolgi.dev; s=rsa-2026-08; t=1796354415; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p5ptxnSsKGdZxl7d9uNHX5lmkXbxZe6rjbZzvevngBo=; b=b+8EA77t36fBSJTzE1ZP5T/TyD56b+MiDOtO0z/N/SP2uVmaU+CoibLBP5cVDZAG+svl6B TlaaOb0uteJH8krmA2LHw+wZiDrDI1b9fzPEG/ZEXj7dEpeEnewj5OzhLsj2SOzGqWhrrP dthMC5lWAv1batkdjRgNwiUU1rg9r8WNUTgqrvlzFvKMdT+doawWdlycAqtmg4TSWplMim fXbBv9YwbcCKsKe8ANVMCdf/e0j4zD/iFyYhxuUp4NcLKVHh7KtvRLKqm06W6tm6fFr11b rrzmT27C2jkVS3jYBd+adPA2kuTrmXFoy0diA5GZi8RSfnq3Mx27MAoUgvLxYg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=coolgi.dev; s=rsa-2026-11; t=1796354415; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p5ptxnSsKGdZxl7d9uNHX5lmkXbxZe6rjbZzvevngBo=; b=V3b+Uvj4/N+Ywyjxq3HMeJ7eTl4CQHLi5I2jOIWnCdWGJw5ZRScOeI04LO3JfnO9TAwzEY eXphaN7p/0kLIjthPKUbKlRXUw1xbU4/7a0hJd6ZzlDkHnEka9B6GFfMXsQdzFv6Jh7SQm MBlMVboz7isSI6TtLL2lIfk0y+rCqS7KGJERw/XcOqotzHS0tkuL73+pkzkKRzeQbT7Pqh nVQUXEuhIaDYd0CpNEfvifc2jnXgpvxo9vKqpbfJJC2TSfLd7URkvIljwnMvf+845GPj12 6wSKFvMA4e5yVP6pZVHpgDvpDo4UAfSzHoo+/KWLjUk26cnj5dkAfYHpT9+NCw== To: libcamera-devel@lists.libcamera.org Cc: Remy Alipoursafari , =?utf-8?q?Lo?= =?utf-8?q?=C3=AFc_Molinari?= , Robert Mader Subject: [PATCH v2 5/6] software_isp: benchmark: Make benchmark into an abstract class Date: Fri, 4 Dec 2026 16:18:57 +1300 Message-ID: <20261204031914.117401-6-me@coolgi.dev> In-Reply-To: <20261204031914.117401-1-me@coolgi.dev> References: <20261204031914.117401-1-me@coolgi.dev> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org From: Remy Alipoursafari Makes the Benchmark class abstract, splitting off the CPU-specific timing code to a separate file to allow for different benchmarking methods to be added in the future. Signed-off-by: Remy Alipoursafari Reviewed-by: Robert Mader --- .../internal/software_isp/benchmark.h | 18 ++- .../internal/software_isp/benchmark_cpu.h | 37 ++++++ .../internal/software_isp/meson.build | 2 +- .../internal/software_isp/swstats_cpu.h | 5 +- src/libcamera/software_isp/benchmark.cpp | 108 ++++++++++++++---- src/libcamera/software_isp/benchmark_cpu.cpp | 74 ++++++++++++ src/libcamera/software_isp/debayer.h | 4 +- src/libcamera/software_isp/debayer_cpu.cpp | 1 + src/libcamera/software_isp/debayer_egl.cpp | 2 + src/libcamera/software_isp/debayer_egl.h | 1 - src/libcamera/software_isp/meson.build | 1 + src/libcamera/software_isp/swstats_cpu.cpp | 1 + 12 files changed, 220 insertions(+), 34 deletions(-) create mode 100644 include/libcamera/internal/software_isp/benchmark_cpu.h create mode 100644 src/libcamera/software_isp/benchmark_cpu.cpp diff --git a/include/libcamera/internal/software_isp/benchmark.h b/include/libcamera/internal/software_isp/benchmark.h index cc7f9d71..be2f6ca5 100644 --- a/include/libcamera/internal/software_isp/benchmark.h +++ b/include/libcamera/internal/software_isp/benchmark.h @@ -1,18 +1,18 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2024, Red Hat Inc. + * Copyright (C) 2026, Collabora Inc. * * Authors: * Hans de Goede + * Remy Alipoursafari * * Simple builtin benchmark to measure software ISP processing times */ #pragma once -#include #include -#include namespace libcamera { @@ -22,18 +22,26 @@ class Benchmark { public: Benchmark(const CameraManager &cm, const std::string &name); - ~Benchmark(); + virtual ~Benchmark(); void startFrame(void); void finishFrame(void); + void queryFrame(void); + +protected: + /* Cumulative time in nanoseconds */ + int64_t frameProcessTime_ = 0; + + virtual bool timeFrameStart(void) = 0; + virtual bool timeFrameEnd(void) = 0; + virtual bool timeFrameQuery(void) = 0; private: std::string name_; - timespec frameStartTime_; + /* Whether to process the current frame */ bool measure_; /* Skip 30 frames for things to stabilize then measure 30 frames */ unsigned int encounteredFrames_ = 0; - int64_t frameProcessTime_ = 0; unsigned int skipBeforeMeasure_ = 30; unsigned int framesToMeasure_ = 30; }; diff --git a/include/libcamera/internal/software_isp/benchmark_cpu.h b/include/libcamera/internal/software_isp/benchmark_cpu.h new file mode 100644 index 00000000..b61737d4 --- /dev/null +++ b/include/libcamera/internal/software_isp/benchmark_cpu.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Red Hat Inc. + * Copyright (C) 2026, Collabora Inc. + * + * Authors: + * Hans de Goede + * Remy Alipoursafari + * + * Simple builtin benchmark to measure software ISP CPU processing times + */ + +#pragma once + +#include +#include + +#include "libcamera/internal/software_isp/benchmark.h" + +namespace libcamera { + +class BenchmarkCpu : public Benchmark +{ +public: + BenchmarkCpu(const CameraManager &cm, const std::string &name); + ~BenchmarkCpu(); + +protected: + bool timeFrameStart(void) override; + bool timeFrameEnd(void) override; + bool timeFrameQuery(void) override; + +private: + timespec frameStartTime_; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/software_isp/meson.build b/include/libcamera/internal/software_isp/meson.build index df7c3b97..89161600 100644 --- a/include/libcamera/internal/software_isp/meson.build +++ b/include/libcamera/internal/software_isp/meson.build @@ -1,7 +1,7 @@ # SPDX-License-Identifier: CC0-1.0 libcamera_internal_headers += files([ - 'benchmark.h', + 'benchmark_cpu.h', 'debayer_params.h', 'software_isp.h', 'swisp_stats.h', diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h index 55187092..e7871e41 100644 --- a/include/libcamera/internal/software_isp/swstats_cpu.h +++ b/include/libcamera/internal/software_isp/swstats_cpu.h @@ -22,8 +22,7 @@ #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/shared_mem_object.h" #include "libcamera/internal/software_isp/swisp_stats.h" - -#include "benchmark.h" +#include "benchmark_cpu.h" namespace libcamera { @@ -123,7 +122,7 @@ private: std::vector stats_; SharedMemObject sharedStats_; - Benchmark bench_; + BenchmarkCpu bench_; }; } /* namespace libcamera */ diff --git a/src/libcamera/software_isp/benchmark.cpp b/src/libcamera/software_isp/benchmark.cpp index 96f0ae00..8f2fa84c 100644 --- a/src/libcamera/software_isp/benchmark.cpp +++ b/src/libcamera/software_isp/benchmark.cpp @@ -1,9 +1,11 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2024, Red Hat Inc. + * Copyright (C) 2026, Collabora Inc. * * Authors: * Hans de Goede + * Remy Alipoursafari * * Simple builtin benchmark to measure software ISP processing times */ @@ -35,38 +37,74 @@ Benchmark::Benchmark(const CameraManager &cm, const std::string &name) const GlobalConfiguration &configuration = cm._d()->configuration(); skipBeforeMeasure_ = configuration.option( - { "software_isp", "measure", "skip" }) - .value_or(skipBeforeMeasure_); + { "software_isp", "measure", "skip" }) + .value_or(skipBeforeMeasure_); framesToMeasure_ = configuration.option( { "software_isp", "measure", "number" }) - .value_or(framesToMeasure_); + .value_or(framesToMeasure_); } Benchmark::~Benchmark() { } -static inline int64_t timeDiff(timespec &after, timespec &before) -{ - return (after.tv_sec - before.tv_sec) * 1000000000LL + - (int64_t)after.tv_nsec - (int64_t)before.tv_nsec; -} +/** + * \var Benchmark::frameProcessTime_ + * \brief Cumulative time in nanoseconds + */ + +/** + * \fn Benchmark::timeFrameStart() + * \brief Start measuring process time for a single frame + * \return True on success, false on failure. On failure, the rest of the + * pipeline is skipped + * \sa timeFrameEnd() + * \sa timeFrameQuery() + */ + +/** + * \fn Benchmark::timeFrameEnd() + * \brief Finish measuring process time for a single frame + * \return True on success, false on failure. On failure, the rest of the + * pipeline is skipped + * \sa timeFrameStart() + * \sa timeFrameQuery() + */ + +/** + * \fn Benchmark::timeFrameQuery() + * \brief Query the processing time between the timer's start & end + * + * Queries for the time between the timer's start & end, and putting the result + * in Benchmark::frameProcessTime_. + * + * \return True on success, false on failure. On failure, the rest of the + * pipeline is skipped + * \sa timeFrameStart() + * \sa timeFrameEnd() + */ /** * \brief Start measuring process time for a single frame * * Call this function before processing frame data to start measuring * the process time for a frame. + * + * \sa finishFrame() + * \sa queryFrame() */ -void Benchmark::startFrame(void) +void Benchmark::startFrame() { measure_ = framesToMeasure_ > 0 && encounteredFrames_ < skipBeforeMeasure_ + framesToMeasure_ && ++encounteredFrames_ > skipBeforeMeasure_; if (measure_) { - frameStartTime_ = {}; - clock_gettime(CLOCK_MONOTONIC_RAW, &frameStartTime_); + if (!timeFrameStart()) { + LOG(Benchmark, Error) << name_ << " failed starting time benchmark"; + encounteredFrames_--; + measure_ = false; + } } } @@ -76,21 +114,47 @@ void Benchmark::startFrame(void) * Call this function after processing frame data to finish measuring * the process time for a frame. * + * \sa startFrame() + * \sa queryFrame() + */ +void Benchmark::finishFrame() +{ + if (measure_) { + if (!timeFrameEnd()) { + LOG(Benchmark, Error) << name_ << " failed ending time benchmark"; + encounteredFrames_--; + measure_ = false; + } + } +} + +/** + * \brief Query & potentially print the processing time for a single frame + * + * Call this function once Benchmark::startFrame() & Benchmark::finishFrame() + * have been called. + * * This function will log frame processing time information after - * Benchmark::kLastFrameToMeasure frames have been processed. + * (Benchmark::skipBeforeMeasure_ + Benchmark::framesToMeasure_) successful + * frames have been processed. + * + * \sa startFrame() + * \sa finishFrame() */ -void Benchmark::finishFrame(void) +void Benchmark::queryFrame() { if (measure_) { - timespec frameEndTime = {}; - clock_gettime(CLOCK_MONOTONIC_RAW, &frameEndTime); - frameProcessTime_ += timeDiff(frameEndTime, frameStartTime_); - if (encounteredFrames_ == skipBeforeMeasure_ + framesToMeasure_) { - LOG(Benchmark, Info) - << name_ << " processed " << framesToMeasure_ - << " frames in " << frameProcessTime_ / 1000 << "us, " - << frameProcessTime_ / (1000 * framesToMeasure_) - << " us/frame"; + if (timeFrameQuery()) { + if (encounteredFrames_ == skipBeforeMeasure_ + framesToMeasure_) { + LOG(Benchmark, Info) + << name_ << " processed " << framesToMeasure_ + << " frames in " << frameProcessTime_ / 1000 << "us, " + << frameProcessTime_ / (1000 * framesToMeasure_) + << " us/frame"; + } + } else { + LOG(Benchmark, Error) << name_ << " failed querying time benchmark"; + encounteredFrames_--; } } } diff --git a/src/libcamera/software_isp/benchmark_cpu.cpp b/src/libcamera/software_isp/benchmark_cpu.cpp new file mode 100644 index 00000000..6994cf23 --- /dev/null +++ b/src/libcamera/software_isp/benchmark_cpu.cpp @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Red Hat Inc. + * Copyright (C) 2026, Collabora Inc. + * + * Authors: + * Hans de Goede + * Remy Alipoursafari + * + * Simple builtin benchmark to measure software ISP CPU processing times + */ + +#include "libcamera/internal/software_isp/benchmark_cpu.h" + +namespace libcamera { + +/** + * \class BenchmarkCpu + * \brief Simple builtin benchmark for mesuring CPU time + * + * Simple builtin benchmark to measure software ISP CPU processing times. + */ + +/** + * \brief Constructs a BenchmarkCpu object + */ +BenchmarkCpu::BenchmarkCpu(const CameraManager &cm, const std::string &name) + : Benchmark(cm, name) +{ +} + +BenchmarkCpu::~BenchmarkCpu() +{ +} + +static inline int64_t timeDiff(timespec &after, timespec &before) +{ + return (after.tv_sec - before.tv_sec) * 1000000000LL + + (int64_t)after.tv_nsec - (int64_t)before.tv_nsec; +} + +/** + * \brief Start measuring process time for a single frame + */ +bool BenchmarkCpu::timeFrameStart() +{ + frameStartTime_ = {}; + return clock_gettime(CLOCK_MONOTONIC_RAW, &frameStartTime_) == 0; +} + +/** + * \brief Finish measuring process time for a single frame + */ +bool BenchmarkCpu::timeFrameEnd() +{ + timespec frameEndTime = {}; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &frameEndTime)) + return false; + + frameProcessTime_ += timeDiff(frameEndTime, frameStartTime_); + + return true; +} + +/** + * \brief Query the processing time between the timer's start & end + */ +bool BenchmarkCpu::timeFrameQuery() +{ + // Querying the processing time is done in BenchmarkCpu::timeFrameEnd() + return true; +} + +} /* namespace libcamera */ diff --git a/src/libcamera/software_isp/debayer.h b/src/libcamera/software_isp/debayer.h index e30a0527..c9423263 100644 --- a/src/libcamera/software_isp/debayer.h +++ b/src/libcamera/software_isp/debayer.h @@ -22,7 +22,7 @@ #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/dma_buf_allocator.h" -#include "libcamera/internal/software_isp/benchmark.h" +#include "libcamera/internal/software_isp/benchmark_cpu.h" #include "libcamera/internal/software_isp/debayer_params.h" namespace libcamera { @@ -80,7 +80,7 @@ public: PixelFormat inputPixelFormat_; PixelFormat outputPixelFormat_; bool swapRedBlueGains_; - Benchmark bench_; + BenchmarkCpu bench_; private: virtual Size patternSize(PixelFormat inputFormat) = 0; diff --git a/src/libcamera/software_isp/debayer_cpu.cpp b/src/libcamera/software_isp/debayer_cpu.cpp index 91867baa..23e519c3 100644 --- a/src/libcamera/software_isp/debayer_cpu.cpp +++ b/src/libcamera/software_isp/debayer_cpu.cpp @@ -1094,6 +1094,7 @@ void DebayerCpu::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output /* Measure before emitting signals */ bench_.finishFrame(); + bench_.queryFrame(); /* * Buffer ids are currently not used, so pass zeros as its parameter. diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index d94a06d1..ca99bdc2 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -672,6 +672,7 @@ void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output egl_.syncOutput(); bench_.finishFrame(); + bench_.queryFrame(); outputBufferReady.emit(output); inputBufferReady.emit(input); @@ -680,6 +681,7 @@ void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output error: bench_.finishFrame(); + bench_.queryFrame(); metadata.status = FrameMetadata::FrameError; return; } diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h index cbbc6a45..4185d32b 100644 --- a/src/libcamera/software_isp/debayer_egl.h +++ b/src/libcamera/software_isp/debayer_egl.h @@ -21,7 +21,6 @@ #include "libcamera/internal/egl.h" #include "libcamera/internal/mapped_framebuffer.h" -#include "libcamera/internal/software_isp/benchmark.h" #include "libcamera/internal/software_isp/swstats_cpu.h" #include diff --git a/src/libcamera/software_isp/meson.build b/src/libcamera/software_isp/meson.build index 15f9e403..f6ee6700 100644 --- a/src/libcamera/software_isp/meson.build +++ b/src/libcamera/software_isp/meson.build @@ -21,6 +21,7 @@ summary({'SoftISP GPU acceleration' : mesa_works}, libcamera_internal_sources += files([ 'benchmark.cpp', + 'benchmark_cpu.cpp', 'debayer.cpp', 'debayer_cpu.cpp', 'software_isp.cpp', diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 74633815..0f08b19e 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -632,6 +632,7 @@ void SwStatsCpu::processFrame(uint32_t frame, uint32_t bufferId, MappedFrameBuff (this->*processFrame_)(input); finishFrame(frame, bufferId); bench_.finishFrame(); + bench_.queryFrame(); } } /* namespace libcamera */