[{"id":22627,"web_url":"https://patchwork.libcamera.org/comment/22627/","msgid":"<3e0c8be7-53e5-e5b6-5455-d4967f1e7274@ideasonboard.com>","date":"2022-04-06T14:47:41","subject":"Re: [libcamera-devel] [PATCH v5 1/3] libcamera: v4l2_videodevice:\n\tAdd a dequeue timer","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/people/86/","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"content":"Hi Naush,\n\nOn 4/6/22 18:05, Naushir Patuck via libcamera-devel wrote:\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            | 53 +++++++++++++++++++\n>   2 files changed, 63 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\nThis ended up being used un-initialised which in turn polluted the log: \nhttps://paste.debian.net/1236996/\n\nI have posted a patch \"libcamera: v4l2_videodevice: Fix uninitialised \nwatchdogDuration_\" to fix it\n\n>   };\n>   \n>   class V4L2M2MDevice\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index 0830be80c553..c3a1b6c42e19 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,50 @@ 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\n> + * timer to ensure buffer dequeue events are periodically occurring when the\n> + * device is streaming. The watchdog timer is only active when the device is\n> + * streaming, so it is not necessary to disable it when the device stops\n> + * streaming. The timeout 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> +}\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_ << \" has expired!\";\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 B612EC3256\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 Apr 2022 14:47:51 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6F6E065643;\n\tWed,  6 Apr 2022 16:47:51 +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 05BE4604B8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Apr 2022 16:47:49 +0200 (CEST)","from [192.168.1.110] (unknown [27.57.186.178])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D899A482;\n\tWed,  6 Apr 2022 16:47:47 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1649256471;\n\tbh=TJJ8Zt0mnOACI85SAsB8JiISOpFMpPLe+CqCDSNP53I=;\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:\n\tFrom;\n\tb=eZMLB9DT3meW3im+tbRGGqIHqZS0TBvgwMk7tSiU6Jayqs06x+O35/tPUbguu0apS\n\td4Pxp2zBik2a/gOIUIfKhpSlnnBFkQONPw90h6Xy/aU5YKVp3egjD1JcqZV8XD+ZUo\n\tiU3hK0UlI9YZxPx3rI9obY9YoSYvrUyithDoadKSJm2Evwr58YqoJQFbVKgNpdt2fX\n\tqeA42LYgDLHmyQkpJy1X56YHRDfgIm9otVn8EV+o89SUzrWwLCRm//tAQAHBOC/Qtt\n\tq7QQrFb825Ek08XkAxBW+W/Walz3i6J/tAX0TAqBcFNpauDw7n5mGu0rR16SeUVcE4\n\tgOsEkZrTbZZ8A==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1649256468;\n\tbh=TJJ8Zt0mnOACI85SAsB8JiISOpFMpPLe+CqCDSNP53I=;\n\th=Date:Subject:To:References:From:In-Reply-To:From;\n\tb=FnEsqDLSmhn0GxILHBiWSDTyst6moHUv7N9Sz9a0RRBG1L4qUGR6ERnvd6WpRxWa7\n\tX9CgZ6OBs99gDapxaixiAj6pIXkZLjAFsMqa+XtCKJbEhhAyLSkuWye4wugM/Ek/gA\n\tDmIjoSRZEDTqUvyPp6KV1+dGt/AQW08rX7438yr8="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"FnEsqDLS\"; dkim-atps=neutral","Message-ID":"<3e0c8be7-53e5-e5b6-5455-d4967f1e7274@ideasonboard.com>","Date":"Wed, 6 Apr 2022 20:17:41 +0530","MIME-Version":"1.0","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101\n\tThunderbird/91.4.1","Content-Language":"en-US","To":"Naushir Patuck <naush@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20220406123554.4042163-1-naush@raspberrypi.com>\n\t<20220406123554.4042163-2-naush@raspberrypi.com>","In-Reply-To":"<20220406123554.4042163-2-naush@raspberrypi.com>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","Subject":"Re: [libcamera-devel] [PATCH v5 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":"Umang Jain via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Umang Jain <umang.jain@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]