{"id":2878,"url":"https://patchwork.libcamera.org/api/1.1/patches/2878/?format=json","web_url":"https://patchwork.libcamera.org/patch/2878/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","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-5-niklas.soderlund@ragnatech.se>","date":"2020-02-24T19:36:01","name":"[libcamera-devel,v2,4/4] libcamera: V4L2BufferCache: Improve cache eviction strategy","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"a081f8a9471833377a5fb892e72775f00a3c3769","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2878/mbox/","series":[{"id":691,"url":"https://patchwork.libcamera.org/api/1.1/series/691/?format=json","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/2878/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2878/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 15CB26265B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Feb 2020 20:36:28 +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 ef92b543-573c-11ea-9f40-0050569116f7;\n\tMon, 24 Feb 2020 20:36:22 +0100 (CET)"],"X-Halon-ID":"ef92b543-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:01 +0100","Message-Id":"<20200224193601.1040770-5-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 4/4] libcamera: V4L2BufferCache:\n\tImprove cache eviction strategy","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:28 -0000"},"content":"The strategy used to find a free cache entry in the first implementation\nwas not the smartest, it picked the first free entry. This lead to\nunwanted performance issues as they cache was not used as good as it\ncould for imported buffers.\n\nImprove this by adding a last usage timestamp to the cache entries and\nchange the eviction strategy to use the oldest free entry instead of the\nfirst one it finds.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n src/libcamera/include/v4l2_videodevice.h | 1 +\n src/libcamera/v4l2_videodevice.cpp       | 9 ++++++---\n 2 files changed, 7 insertions(+), 3 deletions(-)","diff":"diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h\nindex fcf072641420dacf..f04feed87b24f28f 100644\n--- a/src/libcamera/include/v4l2_videodevice.h\n+++ b/src/libcamera/include/v4l2_videodevice.h\n@@ -125,6 +125,7 @@ private:\n \t\tbool operator==(const FrameBuffer &buffer);\n \n \t\tbool free;\n+\t\tutils::time_point lastUsed;\n \n \tprivate:\n \t\tstruct Plane {\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 99470ce11421c77c..9a4d2479b20f5e27 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -205,6 +205,7 @@ int V4L2BufferCache::get(const FrameBuffer &buffer)\n {\n \tbool hit = false;\n \tint use = -1;\n+\tutils::time_point oldest = utils::clock::now();\n \n \tfor (unsigned int index = 0; index < cache_.size(); index++) {\n \t\tconst Entry &entry = cache_[index];\n@@ -212,8 +213,10 @@ int V4L2BufferCache::get(const FrameBuffer &buffer)\n \t\tif (!entry.free)\n \t\t\tcontinue;\n \n-\t\tif (use < 0)\n+\t\tif (cache_[index].lastUsed < oldest) {\n \t\t\tuse = index;\n+\t\t\toldest = cache_[index].lastUsed;\n+\t\t}\n \n \t\t/* Try to find a cache hit by comparing the planes. */\n \t\tif (cache_[index] == buffer) {\n@@ -245,12 +248,12 @@ void V4L2BufferCache::put(unsigned int index)\n }\n \n V4L2BufferCache::Entry::Entry()\n-\t: free(true)\n+\t: free(true), lastUsed(utils::clock::now())\n {\n }\n \n V4L2BufferCache::Entry::Entry(bool free, const FrameBuffer &buffer)\n-\t: free(free)\n+\t: free(free), lastUsed(utils::clock::now())\n {\n \tfor (const FrameBuffer::Plane &plane : buffer.planes())\n \t\tplanes_.emplace_back(plane);\n","prefixes":["libcamera-devel","v2","4/4"]}