[{"id":22614,"web_url":"https://patchwork.libcamera.org/comment/22614/","msgid":"<Yk12ZCsGJdGlFkCO@pendragon.ideasonboard.com>","date":"2022-04-06T11:15:48","subject":"Re: [libcamera-devel] [PATCH v4 1/3] libcamera: v4l2_videodevice:\n\tAdd a dequeue timer","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Naush,\n\nThank you for the patch.\n\nOn Wed, Apr 06, 2022 at 08:58:57AM +0100, Naushir Patuck wrote:\n> From: Naushir Patuck via libcamera-devel <libcamera-devel@lists.libcamera.org>\n\nThis looks wrong. I'll fix it locally. Did you apply the patch to your\ntree back from the mailing list ?\n\n> Add a timer that gets reset on every buffer dequeue event. If the timeout\n> expires, optionally call a slot in the pipeline handler to handle this\n> condition. This may be useful in detecting and handling stalls in either the\n> hardware or device driver.\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  include/libcamera/internal/v4l2_videodevice.h | 10 ++++\n>  src/libcamera/v4l2_videodevice.cpp            | 55 +++++++++++++++++++\n>  2 files changed, 65 insertions(+)\n> \n> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\n> index cfeae7bd6c52..9c9493cc16ed 100644\n> --- a/include/libcamera/internal/v4l2_videodevice.h\n> +++ b/include/libcamera/internal/v4l2_videodevice.h\n> @@ -20,7 +20,9 @@\n>  #include <libcamera/base/class.h>\n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/signal.h>\n> +#include <libcamera/base/timer.h>\n>  #include <libcamera/base/unique_fd.h>\n> +#include <libcamera/base/utils.h>\n>  \n>  #include <libcamera/color_space.h>\n>  #include <libcamera/framebuffer.h>\n> @@ -217,6 +219,9 @@ public:\n>  \tint streamOn();\n>  \tint streamOff();\n>  \n> +\tvoid setDequeueTimeout(utils::Duration timeout);\n> +\tSignal<> dequeueTimeout;\n> +\n>  \tstatic std::unique_ptr<V4L2VideoDevice>\n>  \tfromEntityName(const MediaDevice *media, const std::string &entity);\n>  \n> @@ -253,6 +258,8 @@ private:\n>  \tvoid bufferAvailable();\n>  \tFrameBuffer *dequeueBuffer();\n>  \n> +\tvoid watchdogExpired();\n> +\n>  \tV4L2Capability caps_;\n>  \tV4L2DeviceFormat format_;\n>  \tconst PixelFormatInfo *formatInfo_;\n> @@ -266,6 +273,9 @@ private:\n>  \tEventNotifier *fdBufferNotifier_;\n>  \n>  \tState state_;\n> +\n> +\tTimer watchdog_;\n> +\tutils::Duration watchdogDuration_;\n>  };\n>  \n>  class V4L2M2MDevice\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index 0830be80c553..dc5020df5c5c 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -539,6 +539,7 @@ V4L2VideoDevice::V4L2VideoDevice(const std::string &deviceNode)\n>  V4L2VideoDevice::V4L2VideoDevice(const MediaEntity *entity)\n>  \t: V4L2VideoDevice(entity->deviceNode())\n>  {\n> +\twatchdog_.timeout.connect(this, &V4L2VideoDevice::watchdogExpired);\n>  }\n>  \n>  V4L2VideoDevice::~V4L2VideoDevice()\n> @@ -1726,6 +1727,9 @@ FrameBuffer *V4L2VideoDevice::dequeueBuffer()\n>  \t\treturn nullptr;\n>  \t}\n>  \n> +\tif (watchdogDuration_.count())\n> +\t\twatchdog_.start(std::chrono::duration_cast<std::chrono::milliseconds>(watchdogDuration_));\n> +\n>  \tcache_->put(buf.index);\n>  \n>  \tFrameBuffer *buffer = it->second;\n> @@ -1828,6 +1832,8 @@ int V4L2VideoDevice::streamOn()\n>  \t}\n>  \n>  \tstate_ = State::Streaming;\n> +\tif (watchdogDuration_.count())\n> +\t\twatchdog_.start(std::chrono::duration_cast<std::chrono::milliseconds>(watchdogDuration_));\n>  \n>  \treturn 0;\n>  }\n> @@ -1852,6 +1858,9 @@ int V4L2VideoDevice::streamOff()\n>  \tif (state_ != State::Streaming && queuedBuffers_.empty())\n>  \t\treturn 0;\n>  \n> +\tif (watchdogDuration_.count())\n> +\t\twatchdog_.stop();\n> +\n>  \tret = ioctl(VIDIOC_STREAMOFF, &bufferType_);\n>  \tif (ret < 0) {\n>  \t\tLOG(V4L2, Error)\n> @@ -1879,6 +1888,52 @@ int V4L2VideoDevice::streamOff()\n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\brief Set the dequeue timeout value\n> + * \\param[in] timeout The timeout value to be used\n> + *\n> + * Sets a timeout value, given by \\a timeout, that will be used by a watchdog timer\n\nI'll reflow this as the line got longer with the parameter rename.\n\n> + * to ensure buffer dequeue events are periodically occurring when the device is\n> + * streaming. The watchdog timer is only active when the device is streaming, so\n> + * it is not necessary to disable it when the device stops streaming. The timeout\n> + * value can be safely updated at any time.\n> + *\n> + * If the timer expires, the \\ref V4L2VideoDevice::dequeueTimeout signal is\n> + * emitted. This can typically be used by pipeline handlers to be notified of\n> + * stalled devices.\n> + *\n> + * Set \\a timeout to 0 to disable the watchdog timer.\n> + */\n> +void V4L2VideoDevice::setDequeueTimeout(utils::Duration timeout)\n> +{\n> +\twatchdogDuration_ = timeout;\n> +\n> +\twatchdog_.stop();\n> +\tif (watchdogDuration_.count() && state_ == State::Streaming) {\n> +\t\twatchdog_.start(std::chrono::duration_cast<std::chrono::milliseconds>(timeout));\n> +\t}\n\nNo need for curly braces, I'll drop the locally.\n\n> +}\n> +\n> +/**\n> + * \\var V4L2VideoDevice::dequeueTimeout\n> + * \\brief A Signal emitted when the dequeue watchdog timer expires\n> + */\n> +\n> +/**\n> + * \\brief Slot to handle an expired dequeue timer\n> + *\n> + * When this slot is called, the time between successive dequeue events is over\n> + * the required timeout. Emit the \\ref V4L2VideoDevice::dequeueTimeout signal.\n> + */\n> +void V4L2VideoDevice::watchdogExpired()\n> +{\n> +\tLOG(V4L2, Warning)\n> +\t\t<< \"Dequeue timer of \" << watchdogDuration_\n> +\t\t<< \" has expired!\";\n\nAnd this now holds on a single line, I'll handle it locally too.\n\n> +\n> +\tdequeueTimeout.emit();\n> +}\n> +\n>  /**\n>   * \\brief Create a new video device instance from \\a entity in media device\n>   * \\a media","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 BB62AC0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 Apr 2022 11:15:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 16622633A4;\n\tWed,  6 Apr 2022 13:15:54 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7A343604B8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Apr 2022 13:15:52 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(117.145-247-81.adsl-dyn.isp.belgacom.be [81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EF565482;\n\tWed,  6 Apr 2022 13:15:51 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1649243754;\n\tbh=QU8MwU/ii3lD7iXu4gfovsK5qfThN5i/3hM2m/k0R9o=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=1zVlG85rjWFNW5Le9u3nSzEW/EUnb+TXWNgY5p3aAJi8BwmFBvgl/1eErnLRgjC3m\n\taK7vOymWA2kip0XEFut3CeYVQns/IrKXG2qcDcN9rGB0ImJBECs03MZPnoEcney3S9\n\tT2X9ABWpmDPdMGcdVwVgtcqCKo0uGHOh1DUCYZoq2p8FERN9le2/O/au3navmAMaRR\n\tDhMgO+y/bBK3iQzIQ0qB9CfioYWohOzRoALfngHTNOFTz12hsrWY5jzctPPSP+EPG6\n\txEAqIyYFBsIU7LODIwJm5qnzoQQseCqgk1dA344nAisM0R5592i6c4mMRVc3mXdfq2\n\tiyHNf3k24vrgw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1649243752;\n\tbh=QU8MwU/ii3lD7iXu4gfovsK5qfThN5i/3hM2m/k0R9o=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=nqeTx31V1MLUj5ijruD/6LcOl25cpPYUpd0elGpD6EGGgHZ4DdMOqub4k9Iu58eYi\n\trKNbPHa3Omji6m2Sbr3cLdzkAbNYs5QN7PhC4oueoBgrHy8moRWAACJ+Uy30l+oP6m\n\tUjxIKRFxX8BwPVi1VPODusRN3T7fY2UtwDclSrn4="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"nqeTx31V\"; dkim-atps=neutral","Date":"Wed, 6 Apr 2022 14:15:48 +0300","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<Yk12ZCsGJdGlFkCO@pendragon.ideasonboard.com>","References":"<20220406075859.3857121-1-naush@raspberrypi.com>\n\t<20220406075859.3857121-2-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220406075859.3857121-2-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v4 1/3] libcamera: v4l2_videodevice:\n\tAdd a dequeue timer","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]