Show a patch.

GET /api/1.1/patches/14966/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 14966,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/14966/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/14966/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20211201142936.107405-7-jacopo@jmondi.org>",
    "date": "2021-12-01T14:29:31",
    "name": "[libcamera-devel,v4,06/11] test: fence: Add test for the Fence class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "c6589213775b96c4fcdd71e9649369f3ecc80b16",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": {
        "id": 15,
        "url": "https://patchwork.libcamera.org/api/1.1/users/15/?format=api",
        "username": "jmondi",
        "first_name": "Jacopo",
        "last_name": "Mondi",
        "email": "jacopo@jmondi.org"
    },
    "mbox": "https://patchwork.libcamera.org/patch/14966/mbox/",
    "series": [
        {
            "id": 2795,
            "url": "https://patchwork.libcamera.org/api/1.1/series/2795/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2795",
            "date": "2021-12-01T14:29:25",
            "name": "libcamera: Add support for Fence",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/2795/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/14966/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/14966/checks/",
    "tags": {},
    "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 3C646C3251\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  1 Dec 2021 14:28:57 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E2B53607CE;\n\tWed,  1 Dec 2021 15:28:56 +0100 (CET)",
            "from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net\n\t[217.70.183.198])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7393760738\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  1 Dec 2021 15:28:55 +0100 (CET)",
            "(Authenticated sender: jacopo@jmondi.org)\n\tby relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 7F4E3C0002;\n\tWed,  1 Dec 2021 14:28:54 +0000 (UTC)"
        ],
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Wed,  1 Dec 2021 15:29:31 +0100",
        "Message-Id": "<20211201142936.107405-7-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.33.1",
        "In-Reply-To": "<20211201142936.107405-1-jacopo@jmondi.org>",
        "References": "<20211201142936.107405-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 06/11] test: fence: Add test for the\n\tFence class",
        "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>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Add a test for the Fence class by testing a Fence failure case, and\nby testing a successfully signalled fence capture cycle.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n test/fence.cpp   | 340 +++++++++++++++++++++++++++++++++++++++++++++++\n test/meson.build |   1 +\n 2 files changed, 341 insertions(+)\n create mode 100644 test/fence.cpp",
    "diff": "diff --git a/test/fence.cpp b/test/fence.cpp\nnew file mode 100644\nindex 000000000000..ee1c8343e01c\n--- /dev/null\n+++ b/test/fence.cpp\n@@ -0,0 +1,340 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * fence.cpp - Fence test\n+ */\n+\n+#include <iostream>\n+#include <memory>\n+#include <sys/eventfd.h>\n+#include <unistd.h>\n+\n+#include <libcamera/base/event_dispatcher.h>\n+#include <libcamera/base/thread.h>\n+#include <libcamera/base/timer.h>\n+#include <libcamera/base/unique_fd.h>\n+#include <libcamera/base/utils.h>\n+\n+#include <libcamera/fence.h>\n+#include <libcamera/framebuffer_allocator.h>\n+\n+#include \"camera_test.h\"\n+#include \"test.h\"\n+\n+using namespace std::chrono_literals;\n+using namespace libcamera;\n+using namespace std;\n+\n+class FenceTest : public CameraTest, public Test\n+{\n+public:\n+\tFenceTest();\n+\n+protected:\n+\tint init() override;\n+\tint run() override;\n+\tvoid cleanup() override;\n+\n+private:\n+\tint validateExpiredRequest(Request *request);\n+\tvoid requestComplete(Request *request);\n+\tvoid signalFence();\n+\n+\tstd::unique_ptr<Fence> fence_;\n+\tEventDispatcher *dispatcher_;\n+\tUniqueFD eventFd_;\n+\tTimer fenceTimer_;\n+\n+\tstd::vector<std::unique_ptr<Request>> requests_;\n+\tstd::unique_ptr<CameraConfiguration> config_;\n+\tFrameBufferAllocator *allocator_;\n+\n+\tStream *stream_;\n+\n+\tbool expectedCompletionResult_ = true;\n+\tbool expectFenceFailure_ = true;\n+\n+\tunsigned int completedRequest_;\n+\n+\tunsigned int signalledRequestId_;\n+\tunsigned int expiredRequestId_;\n+\tunsigned int nbuffers_;\n+\n+\tint efd2_;\n+\tint efd_;\n+};\n+\n+FenceTest::FenceTest()\n+\t: CameraTest(\"platform/vimc.0 Sensor B\")\n+{\n+}\n+\n+int FenceTest::init()\n+{\n+\tdispatcher_ = Thread::current()->eventDispatcher();\n+\n+\teventFd_ = UniqueFD(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));\n+\tif (!eventFd_.isValid()) {\n+\t\tcerr << \"Unable to create eventfd\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\t/*\n+\t * Keep a copy of the original fd value to validate that an expired\n+\t * fence has the same fd value.\n+\t */\n+\tefd_ = eventFd_.get();\n+\n+\tconfig_ = camera_->generateConfiguration({ StreamRole::Viewfinder });\n+\tif (!config_ || config_->size() != 1) {\n+\t\tcerr << \"Failed to generate default configuration\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tif (camera_->acquire()) {\n+\t\tcerr << \"Failed to acquire the camera\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tif (camera_->configure(config_.get())) {\n+\t\tcerr << \"Failed to set default configuration\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tStreamConfiguration &cfg = config_->at(0);\n+\tstream_ = cfg.stream();\n+\n+\tallocator_ = new FrameBufferAllocator(camera_);\n+\tif (allocator_->allocate(stream_) < 0)\n+\t\treturn TestFail;\n+\n+\tnbuffers_ = allocator_->buffers(stream_).size();\n+\tif (nbuffers_ < 2) {\n+\t\tcerr << \"Not enough buffers available\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tsignalledRequestId_ = nbuffers_ - 2;\n+\texpiredRequestId_ = nbuffers_ - 1;\n+\n+\treturn TestPass;\n+}\n+\n+int FenceTest::validateExpiredRequest(Request *request)\n+{\n+\tFrameBuffer *buffer = request->buffers().begin()->second;\n+\n+\tstd::unique_ptr<Fence> fence = buffer->releaseFence();\n+\tif (!fence) {\n+\t\tcerr << \"The expired fence should be present\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tif (!fence->isValid()) {\n+\t\tcerr << \"The expired fence should be valid\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tUniqueFD fd = fence->release();\n+\tif (fd.get() != efd_) {\n+\t\tcerr << \"The expired fence file descriptor should not change\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\texpectFenceFailure_ = false;\n+\n+\treturn 0;\n+}\n+\n+/* Callback to signal a fence waiting on the eventfd file descriptor. */\n+void FenceTest::signalFence()\n+{\n+\tuint64_t value = 1;\n+\twrite(efd2_, &value, sizeof(value));\n+\tdispatcher_->processEvents();\n+}\n+\n+void FenceTest::requestComplete(Request *request)\n+{\n+\tuint64_t cookie = request->cookie();\n+\tcompletedRequest_ = cookie;\n+\n+\t/* We expect the last request to fail, if it doesn't happen, test fails. */\n+\tif (expectFenceFailure_ && cookie == expiredRequestId_ &&\n+\t    request->status() != Request::RequestCancelled) {\n+\t\tcerr << \"The last request should have failed: \" << cookie << endl;\n+\n+\t\texpectedCompletionResult_ = false;\n+\t\tdispatcher_->interrupt();\n+\t\treturn;\n+\t}\n+\n+\t/* All other requests but the last one should be completed. */\n+\tif (expectFenceFailure_ && cookie < expiredRequestId_ &&\n+\t    request->status() != Request::RequestComplete) {\n+\t\tcerr << \"All requests but last should complete: \" << cookie << endl;\n+\n+\t\texpectedCompletionResult_ = false;\n+\t\tdispatcher_->interrupt();\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * If the last request has failed already, all the ones queued after\n+\t * that shall complete.\n+\t */\n+\tif (!expectFenceFailure_ &&\n+\t    request->status() != Request::RequestComplete) {\n+\t\tcerr << \"Unexpected request failure: \" << cookie << endl;\n+\n+\t\texpectedCompletionResult_ = false;\n+\t\tdispatcher_->interrupt();\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * The last request has failed.\n+\t * Validate its fence status and do not re-queue it.\n+\t */\n+\tif (cookie == expiredRequestId_) {\n+\t\tint ret = validateExpiredRequest(request);\n+\t\tif (ret)\n+\t\t\texpectedCompletionResult_ = false;\n+\n+\t\tdispatcher_->interrupt();\n+\t\treturn;\n+\t}\n+\n+\tconst Request::BufferMap &buffers = request->buffers();\n+\tconst Stream *stream = buffers.begin()->first;\n+\tFrameBuffer *buffer = buffers.begin()->second;\n+\n+\t/* A succesfully completed request should have the Fence closed. */\n+\tstd::unique_ptr<Fence> bufferFence = buffer->releaseFence();\n+\tif (bufferFence) {\n+\t\tcerr << \"Unexpected valid fence in completed request\" << endl;\n+\n+\t\texpectedCompletionResult_ = false;\n+\t\tdispatcher_->interrupt();\n+\t\treturn;\n+\t}\n+\n+\trequest->reuse();\n+\n+\tif (cookie == signalledRequestId_) {\n+\t\t/*\n+\t\t * The second time this request is about to be queued add\n+\t\t * a fence to it.\n+\t\t *\n+\t\t * The main loop should signal it by using a timer to write to\n+\t\t * the eventfd file descriptor before the fence expires.\n+\t\t */\n+\t\tUniqueFD eventFd2(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));\n+\t\tif (!eventFd2.isValid()) {\n+\t\t\tcerr << \"Unable to create eventfd\" << endl;\n+\t\t\texpectedCompletionResult_ = false;\n+\t\t\treturn;\n+\t\t}\n+\t\tefd2_ = eventFd2.get();\n+\n+\t\tstd::unique_ptr<Fence> fence =\n+\t\t\tstd::make_unique<Fence>(std::move(eventFd2));\n+\t\trequest->addBuffer(stream, buffer, std::move(fence));\n+\t} else {\n+\t\t/* All the other requests continue to operate without fences. */\n+\t\trequest->addBuffer(stream, buffer);\n+\t}\n+\n+\tcamera_->queueRequest(request);\n+\n+\t/*\n+\t * Interrupt the dispatcher to return control to the main loop and\n+\t * activate the fenceTimer\n+\t . */\n+\tdispatcher_->interrupt();\n+}\n+\n+int FenceTest::run()\n+{\n+\tfor (const auto &[i, buffer] : utils::enumerate(allocator_->buffers(stream_))) {\n+\t\tstd::unique_ptr<Request> request = camera_->createRequest(i);\n+\t\tif (!request) {\n+\t\t\tcerr << \"Failed to create request\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tint ret;\n+\t\tif (i == expiredRequestId_) {\n+\t\t\t/* This request will have a fence, and it will expire. */\n+\t\t\tfence_ = std::make_unique<Fence>(std::move(eventFd_));\n+\t\t\tif (!fence_->isValid()) {\n+\t\t\t\tcerr << \"Fence should be valid\" << endl;\n+\t\t\t\treturn TestFail;\n+\t\t\t}\n+\n+\t\t\tret = request->addBuffer(stream_, buffer.get(), std::move(fence_));\n+\t\t} else {\n+\t\t\t/* All other requests will have no Fence. */\n+\t\t\tret = request->addBuffer(stream_, buffer.get());\n+\t\t}\n+\n+\t\tif (ret) {\n+\t\t\tcerr << \"Failed to associate buffer with request\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\trequests_.push_back(std::move(request));\n+\t}\n+\n+\tcamera_->requestCompleted.connect(this, &FenceTest::requestComplete);\n+\n+\tif (camera_->start()) {\n+\t\tcerr << \"Failed to start camera\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\tfor (std::unique_ptr<Request> &request : requests_) {\n+\t\tif (camera_->queueRequest(request.get())) {\n+\t\t\tcerr << \"Failed to queue request\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\t}\n+\n+\texpectedCompletionResult_ = true;\n+\n+\t/* This timer serves to signal fences associated with \"signalledRequestId_\" */\n+\tTimer fenceTimer;\n+\tfenceTimer.timeout.connect(this, &FenceTest::signalFence);\n+\n+\t/* Loop for one second. */\n+\tTimer timer;\n+\ttimer.start(1000);\n+\twhile (timer.isRunning() && expectedCompletionResult_) {\n+\t\tif (completedRequest_ == signalledRequestId_)\n+\t\t\t/*\n+\t\t\t * signalledRequestId_ has just completed and it has\n+\t\t\t * been re-queued with a fence. Start the timer to\n+\t\t\t * signal the fence in 10 msec.\n+\t\t\t */\n+\t\t\tfenceTimer.start(10);\n+\n+\t\tdispatcher_->processEvents();\n+\t}\n+\n+\tcamera_->requestCompleted.disconnect();\n+\n+\tif (camera_->stop()) {\n+\t\tcerr << \"Failed to stop camera\" << endl;\n+\t\treturn TestFail;\n+\t}\n+\n+\treturn expectedCompletionResult_ ? TestPass : TestFail;\n+}\n+\n+void FenceTest::cleanup()\n+{\n+\tdelete allocator_;\n+}\n+\n+TEST_REGISTER(FenceTest)\ndiff --git a/test/meson.build b/test/meson.build\nindex daaa3862cdd6..2c9487e203e2 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -39,6 +39,7 @@ internal_tests = [\n     ['event',                           'event.cpp'],\n     ['event-dispatcher',                'event-dispatcher.cpp'],\n     ['event-thread',                    'event-thread.cpp'],\n+    ['fence',                           'fence.cpp'],\n     ['file',                            'file.cpp'],\n     ['flags',                           'flags.cpp'],\n     ['hotplug-cameras',                 'hotplug-cameras.cpp'],\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "06/11"
    ]
}