From patchwork Fri Aug 19 11:16:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 17181 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 AF3F7C327F for ; Fri, 19 Aug 2022 11:16:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6621661FC4; Fri, 19 Aug 2022 13:16:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660907798; bh=rbdPJmlX0jh6SOF3bqFbTjLN0VOVLWoGLtX0RjstgwE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=IGnezcy37PpPojES2zFlMUFykQVbY4MTWzqGBj5ZUSaLvXhEr7dtTsRokIuF0fVDT 9rppwr7J6VpFkwu+gNs4ZcgveFFRISlvL/jcRyVWXE/rXhJy6LjaaUVgsesyAv1NGh m6eS5vSXQFRtlI4D0bhfk7CKolx/GsqbTXANcY+SHCX/cY9W5fhHdnTOpaTA2TxGVX rcxF3y7LPw0FovHSu6uuPn4N0stGx2WINDNs+5y64bttFr4d44H/BgYUzZxSeHDQBj 7CuxgMxK8LVY5jTNK8yeLU3wWhJcf4mr2PtjypCks/PiDBrPy5hmdCGB8C9qqaHzaL Btle0d3bYGVIw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4A43A61FC9 for ; Fri, 19 Aug 2022 13:16:33 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Uod3iUNY"; dkim-atps=neutral Received: from deskari.lan (91-158-154-79.elisa-laajakaista.fi [91.158.154.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B8F143F1; Fri, 19 Aug 2022 13:16:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1660907793; bh=rbdPJmlX0jh6SOF3bqFbTjLN0VOVLWoGLtX0RjstgwE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Uod3iUNYYPOvTBRoMxIxAdSe/zpfeWbk+AJGJHTnytDxvBSvX1Ijabull7YnF4EPi nDhCxRSyQAaOkdLrEcPkFRrl4koTB0WOA/L7OSkhCOqmwe6As6N2eRFrICni8EmHum B+S3ZVodjn13ABSbuk/EUqHUAl5RAxW3Zch1qveQ= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Fri, 19 Aug 2022 14:16:15 +0300 Message-Id: <20220819111615.39814-7-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220819111615.39814-1-tomi.valkeinen@ideasonboard.com> References: <20220819111615.39814-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 6/6] py: Switch to non-blocking eventfd 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: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Blocking wait can be easily implemented on top in Python, so rather than supporting only blocking reads, or supporting both non-blocking and blocking reads, let's support only non-blocking reads. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/examples/simple-cam.py | 5 +++-- src/py/examples/simple-capture.py | 12 +++++++++-- src/py/examples/simple-continuous-capture.py | 5 +++-- src/py/libcamera/py_camera_manager.cpp | 22 +++++++++++++++----- src/py/libcamera/py_camera_manager.h | 2 +- test/py/unittests.py | 7 +++++++ 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/py/examples/simple-cam.py b/src/py/examples/simple-cam.py index 2b81bb65..b3e97ca7 100755 --- a/src/py/examples/simple-cam.py +++ b/src/py/examples/simple-cam.py @@ -19,8 +19,9 @@ TIMEOUT_SEC = 3 def handle_camera_event(cm): - # cm.get_ready_requests() will not block here, as we know there is an event - # to read. + # cm.get_ready_requests() returns the ready requests, which in our case + # should almost always return a single Request, but in some cases there + # could be multiple or none. reqs = cm.get_ready_requests() diff --git a/src/py/examples/simple-capture.py b/src/py/examples/simple-capture.py index a6a9b33e..5f93574f 100755 --- a/src/py/examples/simple-capture.py +++ b/src/py/examples/simple-capture.py @@ -14,6 +14,7 @@ import argparse import libcamera as libcam +import selectors import sys # Number of frames to capture @@ -107,11 +108,18 @@ def main(): # The main loop. Wait for the queued Requests to complete, process them, # and re-queue them again. + sel = selectors.DefaultSelector() + sel.register(cm.event_fd, selectors.EVENT_READ) + while frames_done < TOTAL_FRAMES: - # cm.get_ready_requests() blocks until there is an event and returns - # all the ready requests. Here we should almost always get a single + # cm.get_ready_requests() does not block, so we use a Selector to wait + # for a camera event. Here we should almost always get a single # Request, but in some cases there could be multiple or none. + events = sel.select() + if not events: + continue + reqs = cm.get_ready_requests() for req in reqs: diff --git a/src/py/examples/simple-continuous-capture.py b/src/py/examples/simple-continuous-capture.py index fe78a2dd..26a8060b 100755 --- a/src/py/examples/simple-continuous-capture.py +++ b/src/py/examples/simple-continuous-capture.py @@ -88,8 +88,9 @@ class CaptureContext: camera_contexts: list[CameraCaptureContext] = [] def handle_camera_event(self): - # cm.get_ready_requests() will not block here, as we know there is an event - # to read. + # cm.get_ready_requests() returns the ready requests, which in our case + # should almost always return a single Request, but in some cases there + # could be multiple or none. reqs = self.cm.get_ready_requests() diff --git a/src/py/libcamera/py_camera_manager.cpp b/src/py/libcamera/py_camera_manager.cpp index ea4ae825..9ccb7aad 100644 --- a/src/py/libcamera/py_camera_manager.cpp +++ b/src/py/libcamera/py_camera_manager.cpp @@ -24,7 +24,7 @@ PyCameraManager::PyCameraManager() cameraManager_ = std::make_unique(); - int fd = eventfd(0, EFD_CLOEXEC); + int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); if (fd == -1) throw std::system_error(errno, std::generic_category(), "Failed to create eventfd"); @@ -62,7 +62,13 @@ py::list PyCameraManager::cameras() std::vector PyCameraManager::getReadyRequests() { - readFd(); + int ret = readFd(); + + if (ret == -EAGAIN) + return std::vector(); + + if (ret != 0) + throw std::system_error(-ret, std::generic_category()); std::vector py_reqs; @@ -96,12 +102,18 @@ void PyCameraManager::writeFd() LOG(Python, Fatal) << "Unable to write to eventfd"; } -void PyCameraManager::readFd() +int PyCameraManager::readFd() { uint8_t buf[8]; - if (read(eventFd_.get(), buf, 8) != 8) - throw std::system_error(errno, std::generic_category()); + ssize_t ret = read(eventFd_.get(), buf, 8); + + if (ret == 8) + return 0; + else if (ret < 0) + return -errno; + else + return -EIO; } void PyCameraManager::pushRequest(Request *req) diff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h index 56bea13d..3525057d 100644 --- a/src/py/libcamera/py_camera_manager.h +++ b/src/py/libcamera/py_camera_manager.h @@ -39,7 +39,7 @@ private: LIBCAMERA_TSA_GUARDED_BY(completedRequestsMutex_); void writeFd(); - void readFd(); + int readFd(); void pushRequest(Request *req); std::vector getCompletedRequests(); }; diff --git a/test/py/unittests.py b/test/py/unittests.py index 9adc4337..6dea80fc 100755 --- a/test/py/unittests.py +++ b/test/py/unittests.py @@ -207,9 +207,16 @@ class SimpleCaptureMethods(CameraTesterBase): reqs = None gc.collect() + sel = selectors.DefaultSelector() + sel.register(cm.event_fd, selectors.EVENT_READ) + reqs = [] while True: + events = sel.select() + if not events: + continue + ready_reqs = cm.get_ready_requests() reqs += ready_reqs