Show a patch.

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

{
    "id": 2877,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/2877/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/2877/",
    "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": "<20200224193601.1040770-4-niklas.soderlund@ragnatech.se>",
    "date": "2020-02-24T19:36:00",
    "name": "[libcamera-devel,v2,3/4] test: v4l2_videodevice: Add test for V4L2BufferCache",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "ecb2ec1870be9e9901be251c7b427d173bd5eb90",
    "submitter": {
        "id": 5,
        "url": "https://patchwork.libcamera.org/api/1.1/people/5/?format=api",
        "name": "Niklas Söderlund",
        "email": "niklas.soderlund@ragnatech.se"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/2877/mbox/",
    "series": [
        {
            "id": 691,
            "url": "https://patchwork.libcamera.org/api/1.1/series/691/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=691",
            "date": "2020-02-24T19:35:57",
            "name": "libcamera: V4L2BufferCache: Improve cache eviction strategy",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/691/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/2877/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/2877/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<niklas.soderlund@ragnatech.se>",
        "Received": [
            "from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6779C6042F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Feb 2020 20:36:26 +0100 (CET)",
            "from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de\n\t[79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid ef150f55-573c-11ea-9f40-0050569116f7;\n\tMon, 24 Feb 2020 20:36:21 +0100 (CET)"
        ],
        "X-Halon-ID": "ef150f55-573c-11ea-9f40-0050569116f7",
        "Authorized-sender": "niklas@soderlund.pp.se",
        "From": "=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Mon, 24 Feb 2020 20:36:00 +0100",
        "Message-Id": "<20200224193601.1040770-4-niklas.soderlund@ragnatech.se>",
        "X-Mailer": "git-send-email 2.25.0",
        "In-Reply-To": "<20200224193601.1040770-1-niklas.soderlund@ragnatech.se>",
        "References": "<20200224193601.1040770-1-niklas.soderlund@ragnatech.se>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 3/4] test: v4l2_videodevice: Add test\n\tfor V4L2BufferCache",
        "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>",
        "X-List-Received-Date": "Mon, 24 Feb 2020 19:36:27 -0000"
    },
    "content": "Add test to test the different modes and situations the V4L2BufferCache\ncan be put in. The tests verify that a FrameBuffer used with the cache\nresults in a V4L2 video device index, and that the cache implementation\nis capable of keeping buffers in a hot state.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n* Changes since v1\n- Update comments in code.\n- Use std::mt19937 PRNG instead of rand()\n- Print randomize and print std::mt19937 initial seed to be able to\n  reproduce a test run with the same random sequences.\n- Add const std::vector<std::unique_ptr<FrameBuffer>> &buffers \"alias\"\n  for source.buffers() to make code more readable.\n- Make use of libtest common implementation of BufferSource.\n---\n test/v4l2_videodevice/buffer_cache.cpp | 215 +++++++++++++++++++++++++\n test/v4l2_videodevice/meson.build      |   1 +\n 2 files changed, 216 insertions(+)\n create mode 100644 test/v4l2_videodevice/buffer_cache.cpp",
    "diff": "diff --git a/test/v4l2_videodevice/buffer_cache.cpp b/test/v4l2_videodevice/buffer_cache.cpp\nnew file mode 100644\nindex 0000000000000000..0a8cb0d28ca9b204\n--- /dev/null\n+++ b/test/v4l2_videodevice/buffer_cache.cpp\n@@ -0,0 +1,215 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * Test the buffer cache different operation modes\n+ */\n+\n+#include <iostream>\n+#include <random>\n+#include <vector>\n+\n+#include <libcamera/stream.h>\n+\n+#include \"buffer_source.h\"\n+\n+#include \"test.h\"\n+\n+using namespace libcamera;\n+\n+namespace {\n+\n+class BufferCacheTest : public Test\n+{\n+public:\n+\t/*\n+\t * Test that a cache with the same size as there are buffers results in\n+\t * a sequential run over; 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ...\n+\t *\n+\t * The test is only valid when the cache size is as least as big as the\n+\t * number of buffers.\n+\t */\n+\tint testSequential(V4L2BufferCache *cache,\n+\t\t\t   const std::vector<std::unique_ptr<FrameBuffer>> &buffers)\n+\t{\n+\t\tfor (unsigned int i = 0; i < buffers.size() * 100; i++) {\n+\t\t\tint nBuffer = i % buffers.size();\n+\t\t\tint index = cache->get(*buffers[nBuffer].get());\n+\n+\t\t\tif (index != nBuffer) {\n+\t\t\t\tstd::cout << \"Expected index \" << nBuffer\n+\t\t\t\t\t  << \" got \" << index << std::endl;\n+\t\t\t\treturn TestFail;\n+\t\t\t}\n+\n+\t\t\tcache->put(index);\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\t/*\n+\t * Test that randomly putting buffers to the cache always results in a\n+\t * valid index.\n+\t */\n+\tint testRandom(V4L2BufferCache *cache,\n+\t\t       const std::vector<std::unique_ptr<FrameBuffer>> &buffers)\n+\t{\n+\t\tstd::uniform_int_distribution<> dist(0, buffers.size() - 1);\n+\n+\t\tfor (unsigned int i = 0; i < buffers.size() * 100; i++) {\n+\t\t\tint nBuffer = dist(generator_);\n+\t\t\tint index = cache->get(*buffers[nBuffer].get());\n+\n+\t\t\tif (index < 0) {\n+\t\t\t\tstd::cout << \"Failed lookup from cache\"\n+\t\t\t\t\t  << std::endl;\n+\t\t\t\treturn TestFail;\n+\t\t\t}\n+\n+\t\t\tcache->put(index);\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\t/*\n+\t * Test that using a buffer more frequently keeps it hot in the cache at\n+\t * all times.\n+\t */\n+\tint testHot(V4L2BufferCache *cache,\n+\t\t    const std::vector<std::unique_ptr<FrameBuffer>> &buffers,\n+\t\t    unsigned int hotFrequency)\n+\t{\n+\t\t/* Run the random test on the cache to make it messy. */\n+\t\tif (testRandom(cache, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tstd::uniform_int_distribution<> dist(0, buffers.size() - 1);\n+\n+\t\t/* Pick a hot buffer at random and store its index. */\n+\t\tint hotBuffer = dist(generator_);\n+\t\tint hotIndex = cache->get(*buffers[hotBuffer].get());\n+\t\tcache->put(hotIndex);\n+\n+\t\t/*\n+\t\t * Queue hot buffer at the requested frequency and make sure\n+\t\t * it stays hot.\n+\t\t */\n+\t\tfor (unsigned int i = 0; i < buffers.size() * 100; i++) {\n+\t\t\tint nBuffer, index;\n+\t\t\tbool hotQueue = i % hotFrequency == 0;\n+\n+\t\t\tif (hotQueue)\n+\t\t\t\tnBuffer = hotBuffer;\n+\t\t\telse\n+\t\t\t\tnBuffer = dist(generator_);\n+\n+\t\t\tindex = cache->get(*buffers[nBuffer].get());\n+\n+\t\t\tif (index < 0) {\n+\t\t\t\tstd::cout << \"Failed lookup from cache\"\n+\t\t\t\t\t  << std::endl;\n+\t\t\t\treturn TestFail;\n+\t\t\t}\n+\n+\t\t\tif (hotQueue && index != hotIndex) {\n+\t\t\t\tstd::cout << \"Hot buffer got cold\"\n+\t\t\t\t\t  << std::endl;\n+\t\t\t\treturn TestFail;\n+\t\t\t}\n+\n+\t\t\tcache->put(index);\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\tint init() override\n+\t{\n+\t\tstd::random_device rd;\n+\t\tunsigned int seed = rd();\n+\n+\t\tstd::cout << \"Random seed is \" << seed << std::endl;\n+\n+\t\tgenerator_.seed(seed);\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\tint run() override\n+\t{\n+\t\tconst unsigned int numBuffers = 8;\n+\n+\t\tStreamConfiguration cfg;\n+\t\tcfg.pixelFormat = V4L2_PIX_FMT_YUYV;\n+\t\tcfg.size = Size(600, 800);\n+\t\tcfg.bufferCount = numBuffers;\n+\n+\t\tBufferSource source;\n+\t\tint ret = source.allocate(cfg);\n+\t\tif (ret != TestPass)\n+\t\t\treturn ret;\n+\n+\t\tconst std::vector<std::unique_ptr<FrameBuffer>> &buffers =\n+\t\t\tsource.buffers();\n+\n+\t\tif (buffers.size() != numBuffers) {\n+\t\t\tstd::cout << \"Got \" << buffers.size()\n+\t\t\t\t  << \" buffers, expected \" << numBuffers\n+\t\t\t\t  << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\t/*\n+\t\t * Test cache of same size as there are buffers, the cache is\n+\t\t * created from a list of buffers and will be pre-populated.\n+\t\t */\n+\t\tV4L2BufferCache cacheFromBuffers(buffers);\n+\n+\t\tif (testSequential(&cacheFromBuffers, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tif (testRandom(&cacheFromBuffers, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tif (testHot(&cacheFromBuffers, buffers, numBuffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\t/*\n+\t\t * Test cache of same size as there are buffers, the cache is\n+\t\t * not pre-populated.\n+\t\t */\n+\t\tV4L2BufferCache cacheFromNumbers(numBuffers);\n+\n+\t\tif (testSequential(&cacheFromNumbers, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tif (testRandom(&cacheFromNumbers, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tif (testHot(&cacheFromNumbers, buffers, numBuffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\t/*\n+\t\t * Test cache half the size of number of buffers used, the cache\n+\t\t * is not pre-populated.\n+\t\t */\n+\t\tV4L2BufferCache cacheHalf(numBuffers / 2);\n+\n+\t\tif (testRandom(&cacheHalf, buffers) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\tif (testHot(&cacheHalf, buffers, numBuffers / 2) != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\treturn TestPass;\n+\t}\n+\n+private:\n+\tstd::mt19937 generator_;\n+};\n+\n+} /* namespace */\n+\n+TEST_REGISTER(BufferCacheTest);\ndiff --git a/test/v4l2_videodevice/meson.build b/test/v4l2_videodevice/meson.build\nindex 5c52da7219c21cc3..685fcf6d16d72182 100644\n--- a/test/v4l2_videodevice/meson.build\n+++ b/test/v4l2_videodevice/meson.build\n@@ -5,6 +5,7 @@ v4l2_videodevice_tests = [\n     [ 'controls',           'controls.cpp' ],\n     [ 'formats',            'formats.cpp' ],\n     [ 'request_buffers',    'request_buffers.cpp' ],\n+    [ 'buffer_cache',       'buffer_cache.cpp' ],\n     [ 'stream_on_off',      'stream_on_off.cpp' ],\n     [ 'capture_async',      'capture_async.cpp' ],\n     [ 'buffer_sharing',     'buffer_sharing.cpp' ],\n",
    "prefixes": [
        "libcamera-devel",
        "v2",
        "3/4"
    ]
}