[{"id":3076,"web_url":"https://patchwork.libcamera.org/comment/3076/","msgid":"<20191118200527.GK4888@pendragon.ideasonboard.com>","date":"2019-11-18T20:05:27","subject":"Re: [libcamera-devel] [RFC 09/12] libcamera: v4l2_videodevice: Add\n\ta buffer cache class","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Oct 28, 2019 at 03:25:22AM +0100, Niklas Söderlund wrote:\n> In preparation for the new buffer handling add a class which will deal\n> with keeping the cache between dmafds and V4L2 video device buffer\n> indexes. Previously this responsibility have been split between multiple\n\ns/have been/was/\n\n> classes.\n> \n> This initial implement ensures that no hot association is lost while its\n> eviction strategy could be improved in the future.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/libcamera/include/v4l2_videodevice.h | 19 ++++++++++\n>  src/libcamera/v4l2_videodevice.cpp       | 47 ++++++++++++++++++++++++\n>  2 files changed, 66 insertions(+)\n> \n> diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h\n> index 4b8cf9394eb9516f..5b178339d0ce7e2c 100644\n> --- a/src/libcamera/include/v4l2_videodevice.h\n> +++ b/src/libcamera/include/v4l2_videodevice.h\n> @@ -104,6 +104,25 @@ struct V4L2Capability final : v4l2_capability {\n>  \t}\n>  };\n>  \n> +class V4L2BufferCache\n> +{\n> +public:\n> +\tV4L2BufferCache(unsigned int size);\n> +\n> +\tvoid populate(unsigned int index, const std::array<int, 3> &fds);\n> +\n> +\tint pop(const std::array<int, 3> &fds);\n> +\tvoid push(unsigned int index);\n\nI understand why there's no doc at this stage, but you then need to name\nmethods in a self-documenting way :-) Jokes aside, I think better names\nare needed here. I also fail to see why you need both populate() and\npop(), but maybe I'll figure it out in a later patch.\n\n> +\n> +private:\n> +\tstruct CacheInfo {\n> +\t\tbool free;\n> +\t\tstd::array<int, 3> last;\n> +\t};\n> +\n> +\tstd::map<unsigned int, CacheInfo> cache_;\n\nAs indices start at zero and are consecutive, how about using an\nstd::vector<CacheInfo> instead ?\n\n> +};\n> +\n>  class V4L2DeviceFormat\n>  {\n>  public:\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index a2a9eab2bcc0d7e8..8bc2e439e4faeb68 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -132,6 +132,53 @@ LOG_DECLARE_CATEGORY(V4L2)\n>   * \\return True if the video device provides Streaming I/O IOCTLs\n>   */\n>  \n> +V4L2BufferCache::V4L2BufferCache(unsigned int size)\n> +{\n> +\tfor (unsigned int i = 0; i < size; i++)\n> +\t\tcache_[i] = { .free = true, .last = { -1, -1, -1 } };\n\nIf you turn cache_ into a vector, this could just become\n\n\tcache_.resize(size, { .free = true, .last = { -1, -1, -1 } });\n\n> +}\n> +\n> +void V4L2BufferCache::populate(unsigned int index, const std::array<int, 3> &fds)\n> +{\n> +\tASSERT(index < cache_.size());\n> +\tcache_[index].last = fds;\n> +}\n> +\n> +int V4L2BufferCache::pop(const std::array<int, 3> &fds)\n> +{\n> +\tint use = -1;\n> +\n> +\tfor (auto &it : cache_) {\n> +\t\tint index = it.first;\n> +\t\tCacheInfo &info = it.second;\n> +\n> +\t\tif (!info.free)\n> +\t\t\tcontinue;\n> +\n> +\t\tif (use < 0)\n> +\t\t\tuse = index;\n> +\n> +\t\tif (info.last == fds) {\n> +\t\t\tuse = index;\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n\nOr maybe cache_ should be a map from fds to { index, free } to optimize\nthis ?\n\n> +\n> +\tif (use < 0)\n> +\t\treturn -ENOENT;\n> +\n> +\tcache_[use].free = false;\n> +\tcache_[use].last = fds;\n> +\n> +\treturn use;\n> +}\n> +\n> +void V4L2BufferCache::push(unsigned int index)\n> +{\n> +\tASSERT(index < cache_.size());\n> +\tcache_[index].free = true;\n> +}\n> +\n>  /**\n>   * \\class V4L2DeviceFormat\n>   * \\brief The V4L2 video device image format and sizes","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4AE4060F1C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2019 21:05:43 +0100 (CET)","from pendragon.ideasonboard.com (unknown [38.98.37.142])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 90F3C563;\n\tMon, 18 Nov 2019 21:05:40 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1574107543;\n\tbh=3mrvYI43QSmShdaD09ry37coWypyzGQWnMKE0EpMOfs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=pBrEvMJaOD8dApuJ9Vvwt5j6zizNfSsgFM/YBd0rj1kDjRuH+oFiwZZWJCYqlqZ5a\n\tboPTqJAURxMYL46inPux0KvrULnLbGUxlN85EhIWKyvRNYAtQEEI7Qi1So0mBjfTiR\n\tyeyPvBQ99APveabtvtrVCVcOJwiIxf/WpgfG+wP8=","Date":"Mon, 18 Nov 2019 22:05:27 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191118200527.GK4888@pendragon.ideasonboard.com>","References":"<20191028022525.796995-1-niklas.soderlund@ragnatech.se>\n\t<20191028022525.796995-10-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191028022525.796995-10-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [RFC 09/12] libcamera: v4l2_videodevice: Add\n\ta buffer cache 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>","X-List-Received-Date":"Mon, 18 Nov 2019 20:05:43 -0000"}}]