From patchwork Mon Feb 24 19:36:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2878 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 15CB26265B for ; Mon, 24 Feb 2020 20:36:28 +0100 (CET) X-Halon-ID: ef92b543-573c-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id ef92b543-573c-11ea-9f40-0050569116f7; Mon, 24 Feb 2020 20:36:22 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= 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 Subject: [libcamera-devel] [PATCH v2 4/4] libcamera: V4L2BufferCache: Improve cache eviction strategy 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-List-Received-Date: Mon, 24 Feb 2020 19:36:28 -0000 The strategy used to find a free cache entry in the first implementation was not the smartest, it picked the first free entry. This lead to unwanted performance issues as they cache was not used as good as it could for imported buffers. Improve this by adding a last usage timestamp to the cache entries and change the eviction strategy to use the oldest free entry instead of the first one it finds. Signed-off-by: Niklas Söderlund Reviewed-by: Naushir Patuck Reviewed-by: Jacopo Mondi --- src/libcamera/include/v4l2_videodevice.h | 1 + src/libcamera/v4l2_videodevice.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index fcf072641420dacf..f04feed87b24f28f 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -125,6 +125,7 @@ private: bool operator==(const FrameBuffer &buffer); bool free; + utils::time_point lastUsed; private: struct Plane { diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 99470ce11421c77c..9a4d2479b20f5e27 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -205,6 +205,7 @@ int V4L2BufferCache::get(const FrameBuffer &buffer) { bool hit = false; int use = -1; + utils::time_point oldest = utils::clock::now(); for (unsigned int index = 0; index < cache_.size(); index++) { const Entry &entry = cache_[index]; @@ -212,8 +213,10 @@ int V4L2BufferCache::get(const FrameBuffer &buffer) if (!entry.free) continue; - if (use < 0) + if (cache_[index].lastUsed < oldest) { use = index; + oldest = cache_[index].lastUsed; + } /* Try to find a cache hit by comparing the planes. */ if (cache_[index] == buffer) { @@ -245,12 +248,12 @@ void V4L2BufferCache::put(unsigned int index) } V4L2BufferCache::Entry::Entry() - : free(true) + : free(true), lastUsed(utils::clock::now()) { } V4L2BufferCache::Entry::Entry(bool free, const FrameBuffer &buffer) - : free(free) + : free(free), lastUsed(utils::clock::now()) { for (const FrameBuffer::Plane &plane : buffer.planes()) planes_.emplace_back(plane);