diff mbox series

[V6,i-g-t,3/6] lib: Add function to hash a framebuffer

Message ID 539f9b55a8269d3eb20d7d211f701d1a301d5b9d.1560374714.git.rodrigosiqueiramelo@gmail.com (mailing list archive)
State New, archived
Headers show
Series igt: Add support for testing writeback connectors | expand

Commit Message

Rodrigo Siqueira June 13, 2019, 2:17 a.m. UTC
To use writeback buffers as a CRC source, we need to be able to hash
them. Implement a simple FVA-1a hashing routine for this purpose.

Doing a bytewise hash on the framebuffer directly can be very slow if
the memory is noncached. By making a copy of each line in the FB first
(which can take advantage of word-access speedup), we can do the hash
on a cached copy, which is much faster (10x speedup on my platform).

v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
    Chris Wilson

Signed-off-by: Brian Starkey <brian.starkey@arm.com>
[rebased and updated to the most recent API]
Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
---
 lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_fb.h |  3 +++
 2 files changed, 69 insertions(+)

Comments

Ser, Simon July 10, 2019, 3:30 p.m. UTC | #1
Mostly LGTM, here are a few nits.

On Wed, 2019-06-12 at 23:17 -0300, Brian Starkey wrote:
> To use writeback buffers as a CRC source, we need to be able to hash
> them. Implement a simple FVA-1a hashing routine for this purpose.
> 
> Doing a bytewise hash on the framebuffer directly can be very slow if
> the memory is noncached. By making a copy of each line in the FB first
> (which can take advantage of word-access speedup), we can do the hash
> on a cached copy, which is much faster (10x speedup on my platform).
> 
> v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
>     Chris Wilson
> 
> Signed-off-by: Brian Starkey <brian.starkey@arm.com>
> [rebased and updated to the most recent API]
> Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
> ---
>  lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/igt_fb.h |  3 +++
>  2 files changed, 69 insertions(+)
> 
> diff --git a/lib/igt_fb.c b/lib/igt_fb.c
> index 9d4f905e..d07dae39 100644
> --- a/lib/igt_fb.c
> +++ b/lib/igt_fb.c
> @@ -3256,6 +3256,72 @@ bool igt_fb_supported_format(uint32_t drm_format)
>  	return false;
>  }
>  
> +/*
> + * This implements the FNV-1a hashing algorithm instead of CRC, for
> + * simplicity
> + * http://www.isthe.com/chongo/tech/comp/fnv/index.html
> + *
> + * hash = offset_basis
> + * for each octet_of_data to be hashed
> + *         hash = hash xor octet_of_data
> + *         hash = hash * FNV_prime
> + * return hash
> + *
> + * 32 bit offset_basis = 2166136261
> + * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
> + */
> +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
> +{
> +#define FNV1a_OFFSET_BIAS 2166136261
> +#define FNV1a_PRIME 16777619

I'd just use plain uint32_t variables for those, but no big deal.

> +	uint32_t hash;
> +	void *map;
> +	char *ptr, *line = NULL;
> +	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
> +	uint32_t stride = calc_plane_stride(fb, 0);

We could return -EINVAL in case fb->num_planes != 1.

> +	if (fb->is_dumb)
> +		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
> +					      PROT_READ);
> +	else
> +		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
> +				    PROT_READ);
> +	ptr = map;

Nit: no need for this, can assign the result of mmap directly to ptr.

> +
> +	/*
> +	 * Framebuffers are often uncached, which can make byte-wise accesses
> +	 * very slow. We copy each line of the FB into a local buffer to speed
> +	 * up the hashing.
> +	 */
> +	line = malloc(stride);
> +	if (!line) {
> +		munmap(map, fb->size);
> +		return -ENOMEM;
> +	}
> +
> +	hash = FNV1a_OFFSET_BIAS;
> +
> +	for (y = 0; y < fb->height; y++, ptr += stride) {
> +
> +		igt_memcpy_from_wc(line, ptr, stride);

Nit: no need to copy the whole stride actually, we can just copy
fb->width * cpp since we're only going to read that.

> +
> +		for (x = 0; x < fb->width * cpp; x++) {
> +			hash ^= line[x];
> +			hash *= FNV1a_PRIME;
> +		}
> +	}
> +
> +	crc->n_words = 1;
> +	crc->crc[0] = hash;
> +
> +	free(line);
> +	munmap(map, fb->size);
> +
> +	return 0;
> +#undef FNV1a_OFFSET_BIAS
> +#undef FNV1a_PRIME
> +}
> +
>  /**
>   * igt_format_is_yuv:
>   * @drm_format: drm fourcc
> diff --git a/lib/igt_fb.h b/lib/igt_fb.h
> index adefebe1..a2741c05 100644
> --- a/lib/igt_fb.h
> +++ b/lib/igt_fb.h
> @@ -37,6 +37,7 @@
>  #include <i915_drm.h>
>  
>  #include "igt_color_encoding.h"
> +#include "igt_debugfs.h"
>  
>  /*
>   * Internal format to denote a buffer compatible with pixman's
> @@ -194,5 +195,7 @@ int igt_format_plane_bpp(uint32_t drm_format, int plane);
>  void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
>  			   bool allow_yuv);
>  
> +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
> +
>  #endif /* __IGT_FB_H__ */
>  
> _______________________________________________
> igt-dev mailing list
> igt-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/igt-dev
Ser, Simon July 10, 2019, 3:34 p.m. UTC | #2
On Wed, 2019-07-10 at 15:30 +0000, Ser, Simon wrote:
> Mostly LGTM, here are a few nits.
> 
> On Wed, 2019-06-12 at 23:17 -0300, Brian Starkey wrote:
> > To use writeback buffers as a CRC source, we need to be able to hash
> > them. Implement a simple FVA-1a hashing routine for this purpose.
> > 
> > Doing a bytewise hash on the framebuffer directly can be very slow if
> > the memory is noncached. By making a copy of each line in the FB first
> > (which can take advantage of word-access speedup), we can do the hash
> > on a cached copy, which is much faster (10x speedup on my platform).
> > 
> > v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
> >     Chris Wilson
> > 
> > Signed-off-by: Brian Starkey <brian.starkey@arm.com>
> > [rebased and updated to the most recent API]
> > Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
> > ---
> >  lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  lib/igt_fb.h |  3 +++
> >  2 files changed, 69 insertions(+)
> > 
> > diff --git a/lib/igt_fb.c b/lib/igt_fb.c
> > index 9d4f905e..d07dae39 100644
> > --- a/lib/igt_fb.c
> > +++ b/lib/igt_fb.c
> > @@ -3256,6 +3256,72 @@ bool igt_fb_supported_format(uint32_t drm_format)
> >  	return false;
> >  }
> >  
> > +/*
> > + * This implements the FNV-1a hashing algorithm instead of CRC, for
> > + * simplicity
> > + * http://www.isthe.com/chongo/tech/comp/fnv/index.html
> > + *
> > + * hash = offset_basis
> > + * for each octet_of_data to be hashed
> > + *         hash = hash xor octet_of_data
> > + *         hash = hash * FNV_prime
> > + * return hash
> > + *
> > + * 32 bit offset_basis = 2166136261
> > + * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
> > + */
> > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
> > +{
> > +#define FNV1a_OFFSET_BIAS 2166136261
> > +#define FNV1a_PRIME 16777619
> 
> I'd just use plain uint32_t variables for those, but no big deal.
> 
> > +	uint32_t hash;
> > +	void *map;
> > +	char *ptr, *line = NULL;
> > +	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
> > +	uint32_t stride = calc_plane_stride(fb, 0);
> 
> We could return -EINVAL in case fb->num_planes != 1.

Let's not waste cycles. With this ^ fixed, this patch is:

Reviewed-by: Simon Ser <simon.ser@intel.com>

Other nits are optional.

> > +	if (fb->is_dumb)
> > +		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
> > +					      PROT_READ);
> > +	else
> > +		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
> > +				    PROT_READ);
> > +	ptr = map;
> 
> Nit: no need for this, can assign the result of mmap directly to ptr.
> 
> > +
> > +	/*
> > +	 * Framebuffers are often uncached, which can make byte-wise accesses
> > +	 * very slow. We copy each line of the FB into a local buffer to speed
> > +	 * up the hashing.
> > +	 */
> > +	line = malloc(stride);
> > +	if (!line) {
> > +		munmap(map, fb->size);
> > +		return -ENOMEM;
> > +	}
> > +
> > +	hash = FNV1a_OFFSET_BIAS;
> > +
> > +	for (y = 0; y < fb->height; y++, ptr += stride) {
> > +
> > +		igt_memcpy_from_wc(line, ptr, stride);
> 
> Nit: no need to copy the whole stride actually, we can just copy
> fb->width * cpp since we're only going to read that.
> 
> > +
> > +		for (x = 0; x < fb->width * cpp; x++) {
> > +			hash ^= line[x];
> > +			hash *= FNV1a_PRIME;
> > +		}
> > +	}
> > +
> > +	crc->n_words = 1;
> > +	crc->crc[0] = hash;
> > +
> > +	free(line);
> > +	munmap(map, fb->size);
> > +
> > +	return 0;
> > +#undef FNV1a_OFFSET_BIAS
> > +#undef FNV1a_PRIME
> > +}
> > +
> >  /**
> >   * igt_format_is_yuv:
> >   * @drm_format: drm fourcc
> > diff --git a/lib/igt_fb.h b/lib/igt_fb.h
> > index adefebe1..a2741c05 100644
> > --- a/lib/igt_fb.h
> > +++ b/lib/igt_fb.h
> > @@ -37,6 +37,7 @@
> >  #include <i915_drm.h>
> >  
> >  #include "igt_color_encoding.h"
> > +#include "igt_debugfs.h"
> >  
> >  /*
> >   * Internal format to denote a buffer compatible with pixman's
> > @@ -194,5 +195,7 @@ int igt_format_plane_bpp(uint32_t drm_format, int plane);
> >  void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
> >  			   bool allow_yuv);
> >  
> > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
> > +
> >  #endif /* __IGT_FB_H__ */
> >  
> > _______________________________________________
> > igt-dev mailing list
> > igt-dev@lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/igt-dev
> _______________________________________________
> igt-dev mailing list
> igt-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/igt-dev
Rodrigo Siqueira July 12, 2019, 2:49 a.m. UTC | #3
On 07/10, Ser, Simon wrote:
> On Wed, 2019-07-10 at 15:30 +0000, Ser, Simon wrote:
> > Mostly LGTM, here are a few nits.
> > 
> > On Wed, 2019-06-12 at 23:17 -0300, Brian Starkey wrote:
> > > To use writeback buffers as a CRC source, we need to be able to hash
> > > them. Implement a simple FVA-1a hashing routine for this purpose.
> > > 
> > > Doing a bytewise hash on the framebuffer directly can be very slow if
> > > the memory is noncached. By making a copy of each line in the FB first
> > > (which can take advantage of word-access speedup), we can do the hash
> > > on a cached copy, which is much faster (10x speedup on my platform).
> > > 
> > > v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
> > >     Chris Wilson
> > > 
> > > Signed-off-by: Brian Starkey <brian.starkey@arm.com>
> > > [rebased and updated to the most recent API]
> > > Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
> > > ---
> > >  lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> > >  lib/igt_fb.h |  3 +++
> > >  2 files changed, 69 insertions(+)
> > > 
> > > diff --git a/lib/igt_fb.c b/lib/igt_fb.c
> > > index 9d4f905e..d07dae39 100644
> > > --- a/lib/igt_fb.c
> > > +++ b/lib/igt_fb.c
> > > @@ -3256,6 +3256,72 @@ bool igt_fb_supported_format(uint32_t drm_format)
> > >  	return false;
> > >  }
> > >  
> > > +/*
> > > + * This implements the FNV-1a hashing algorithm instead of CRC, for
> > > + * simplicity
> > > + * http://www.isthe.com/chongo/tech/comp/fnv/index.html
> > > + *
> > > + * hash = offset_basis
> > > + * for each octet_of_data to be hashed
> > > + *         hash = hash xor octet_of_data
> > > + *         hash = hash * FNV_prime
> > > + * return hash
> > > + *
> > > + * 32 bit offset_basis = 2166136261
> > > + * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
> > > + */
> > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
> > > +{
> > > +#define FNV1a_OFFSET_BIAS 2166136261
> > > +#define FNV1a_PRIME 16777619
> > 
> > I'd just use plain uint32_t variables for those, but no big deal.
> > 
> > > +	uint32_t hash;
> > > +	void *map;
> > > +	char *ptr, *line = NULL;
> > > +	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
> > > +	uint32_t stride = calc_plane_stride(fb, 0);
> > 
> > We could return -EINVAL in case fb->num_planes != 1.
> 
> Let's not waste cycles. With this ^ fixed, this patch is:
> 
> Reviewed-by: Simon Ser <simon.ser@intel.com>
> 
> Other nits are optional.

I agreed with all your suggestions, and I already applied all of them.
Should I wait for the other patches review, or should I resend the new
version?

Thanks for all the feedback
Best Regards
 
> > > +	if (fb->is_dumb)
> > > +		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
> > > +					      PROT_READ);
> > > +	else
> > > +		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
> > > +				    PROT_READ);
> > > +	ptr = map;
> > 
> > Nit: no need for this, can assign the result of mmap directly to ptr.
> > 
> > > +
> > > +	/*
> > > +	 * Framebuffers are often uncached, which can make byte-wise accesses
> > > +	 * very slow. We copy each line of the FB into a local buffer to speed
> > > +	 * up the hashing.
> > > +	 */
> > > +	line = malloc(stride);
> > > +	if (!line) {
> > > +		munmap(map, fb->size);
> > > +		return -ENOMEM;
> > > +	}
> > > +
> > > +	hash = FNV1a_OFFSET_BIAS;
> > > +
> > > +	for (y = 0; y < fb->height; y++, ptr += stride) {
> > > +
> > > +		igt_memcpy_from_wc(line, ptr, stride);
> > 
> > Nit: no need to copy the whole stride actually, we can just copy
> > fb->width * cpp since we're only going to read that.
> > 
> > > +
> > > +		for (x = 0; x < fb->width * cpp; x++) {
> > > +			hash ^= line[x];
> > > +			hash *= FNV1a_PRIME;
> > > +		}
> > > +	}
> > > +
> > > +	crc->n_words = 1;
> > > +	crc->crc[0] = hash;
> > > +
> > > +	free(line);
> > > +	munmap(map, fb->size);
> > > +
> > > +	return 0;
> > > +#undef FNV1a_OFFSET_BIAS
> > > +#undef FNV1a_PRIME
> > > +}
> > > +
> > >  /**
> > >   * igt_format_is_yuv:
> > >   * @drm_format: drm fourcc
> > > diff --git a/lib/igt_fb.h b/lib/igt_fb.h
> > > index adefebe1..a2741c05 100644
> > > --- a/lib/igt_fb.h
> > > +++ b/lib/igt_fb.h
> > > @@ -37,6 +37,7 @@
> > >  #include <i915_drm.h>
> > >  
> > >  #include "igt_color_encoding.h"
> > > +#include "igt_debugfs.h"
> > >  
> > >  /*
> > >   * Internal format to denote a buffer compatible with pixman's
> > > @@ -194,5 +195,7 @@ int igt_format_plane_bpp(uint32_t drm_format, int plane);
> > >  void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
> > >  			   bool allow_yuv);
> > >  
> > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
> > > +
> > >  #endif /* __IGT_FB_H__ */
> > >  
> > > _______________________________________________
> > > igt-dev mailing list
> > > igt-dev@lists.freedesktop.org
> > > https://lists.freedesktop.org/mailman/listinfo/igt-dev
> > _______________________________________________
> > igt-dev mailing list
> > igt-dev@lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/igt-dev
Ser, Simon July 12, 2019, 12:17 p.m. UTC | #4
On Thu, 2019-07-11 at 23:49 -0300, Rodrigo Siqueira wrote:
> On 07/10, Ser, Simon wrote:
> > On Wed, 2019-07-10 at 15:30 +0000, Ser, Simon wrote:
> > > Mostly LGTM, here are a few nits.
> > > 
> > > On Wed, 2019-06-12 at 23:17 -0300, Brian Starkey wrote:
> > > > To use writeback buffers as a CRC source, we need to be able to hash
> > > > them. Implement a simple FVA-1a hashing routine for this purpose.
> > > > 
> > > > Doing a bytewise hash on the framebuffer directly can be very slow if
> > > > the memory is noncached. By making a copy of each line in the FB first
> > > > (which can take advantage of word-access speedup), we can do the hash
> > > > on a cached copy, which is much faster (10x speedup on my platform).
> > > > 
> > > > v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
> > > >     Chris Wilson
> > > > 
> > > > Signed-off-by: Brian Starkey <brian.starkey@arm.com>
> > > > [rebased and updated to the most recent API]
> > > > Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
> > > > ---
> > > >  lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> > > >  lib/igt_fb.h |  3 +++
> > > >  2 files changed, 69 insertions(+)
> > > > 
> > > > diff --git a/lib/igt_fb.c b/lib/igt_fb.c
> > > > index 9d4f905e..d07dae39 100644
> > > > --- a/lib/igt_fb.c
> > > > +++ b/lib/igt_fb.c
> > > > @@ -3256,6 +3256,72 @@ bool igt_fb_supported_format(uint32_t drm_format)
> > > >  	return false;
> > > >  }
> > > >  
> > > > +/*
> > > > + * This implements the FNV-1a hashing algorithm instead of CRC, for
> > > > + * simplicity
> > > > + * http://www.isthe.com/chongo/tech/comp/fnv/index.html
> > > > + *
> > > > + * hash = offset_basis
> > > > + * for each octet_of_data to be hashed
> > > > + *         hash = hash xor octet_of_data
> > > > + *         hash = hash * FNV_prime
> > > > + * return hash
> > > > + *
> > > > + * 32 bit offset_basis = 2166136261
> > > > + * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
> > > > + */
> > > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
> > > > +{
> > > > +#define FNV1a_OFFSET_BIAS 2166136261
> > > > +#define FNV1a_PRIME 16777619
> > > 
> > > I'd just use plain uint32_t variables for those, but no big deal.
> > > 
> > > > +	uint32_t hash;
> > > > +	void *map;
> > > > +	char *ptr, *line = NULL;
> > > > +	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
> > > > +	uint32_t stride = calc_plane_stride(fb, 0);
> > > 
> > > We could return -EINVAL in case fb->num_planes != 1.
> > 
> > Let's not waste cycles. With this ^ fixed, this patch is:
> > 
> > Reviewed-by: Simon Ser <simon.ser@intel.com>
> > 
> > Other nits are optional.
> 
> I agreed with all your suggestions, and I already applied all of them.
> Should I wait for the other patches review, or should I resend the new
> version?

I'm fine with waiting for the full review before a new version of the
whole patchset, but you can also send an updated version of a single
patch with:

    git send-email --in-reply-to="<cover.1560374714.git.rodrigosiqueiramelo@gmail.com>" -1 <commit hash>

where In-Reply-To is the Message-Id of the patch you want to update. I
agree it's a little tedious since you need to extract the Message-Id
from the message header.

> Thanks for all the feedback

:)

> Best Regards
>  
> > > > +	if (fb->is_dumb)
> > > > +		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
> > > > +					      PROT_READ);
> > > > +	else
> > > > +		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
> > > > +				    PROT_READ);
> > > > +	ptr = map;
> > > 
> > > Nit: no need for this, can assign the result of mmap directly to ptr.
> > > 
> > > > +
> > > > +	/*
> > > > +	 * Framebuffers are often uncached, which can make byte-wise accesses
> > > > +	 * very slow. We copy each line of the FB into a local buffer to speed
> > > > +	 * up the hashing.
> > > > +	 */
> > > > +	line = malloc(stride);
> > > > +	if (!line) {
> > > > +		munmap(map, fb->size);
> > > > +		return -ENOMEM;
> > > > +	}
> > > > +
> > > > +	hash = FNV1a_OFFSET_BIAS;
> > > > +
> > > > +	for (y = 0; y < fb->height; y++, ptr += stride) {
> > > > +
> > > > +		igt_memcpy_from_wc(line, ptr, stride);
> > > 
> > > Nit: no need to copy the whole stride actually, we can just copy
> > > fb->width * cpp since we're only going to read that.
> > > 
> > > > +
> > > > +		for (x = 0; x < fb->width * cpp; x++) {
> > > > +			hash ^= line[x];
> > > > +			hash *= FNV1a_PRIME;
> > > > +		}
> > > > +	}
> > > > +
> > > > +	crc->n_words = 1;
> > > > +	crc->crc[0] = hash;
> > > > +
> > > > +	free(line);
> > > > +	munmap(map, fb->size);
> > > > +
> > > > +	return 0;
> > > > +#undef FNV1a_OFFSET_BIAS
> > > > +#undef FNV1a_PRIME
> > > > +}
> > > > +
> > > >  /**
> > > >   * igt_format_is_yuv:
> > > >   * @drm_format: drm fourcc
> > > > diff --git a/lib/igt_fb.h b/lib/igt_fb.h
> > > > index adefebe1..a2741c05 100644
> > > > --- a/lib/igt_fb.h
> > > > +++ b/lib/igt_fb.h
> > > > @@ -37,6 +37,7 @@
> > > >  #include <i915_drm.h>
> > > >  
> > > >  #include "igt_color_encoding.h"
> > > > +#include "igt_debugfs.h"
> > > >  
> > > >  /*
> > > >   * Internal format to denote a buffer compatible with pixman's
> > > > @@ -194,5 +195,7 @@ int igt_format_plane_bpp(uint32_t drm_format, int plane);
> > > >  void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
> > > >  			   bool allow_yuv);
> > > >  
> > > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
> > > > +
> > > >  #endif /* __IGT_FB_H__ */
> > > >  
> > > > _______________________________________________
> > > > igt-dev mailing list
> > > > igt-dev@lists.freedesktop.org
> > > > https://lists.freedesktop.org/mailman/listinfo/igt-dev
> > > _______________________________________________
> > > igt-dev mailing list
> > > igt-dev@lists.freedesktop.org
> > > https://lists.freedesktop.org/mailman/listinfo/igt-dev
Rodrigo Siqueira July 17, 2019, 1:25 a.m. UTC | #5
On 07/12, Ser, Simon wrote:
> On Thu, 2019-07-11 at 23:49 -0300, Rodrigo Siqueira wrote:
> > On 07/10, Ser, Simon wrote:
> > > On Wed, 2019-07-10 at 15:30 +0000, Ser, Simon wrote:
> > > > Mostly LGTM, here are a few nits.
> > > > 
> > > > On Wed, 2019-06-12 at 23:17 -0300, Brian Starkey wrote:
> > > > > To use writeback buffers as a CRC source, we need to be able to hash
> > > > > them. Implement a simple FVA-1a hashing routine for this purpose.
> > > > > 
> > > > > Doing a bytewise hash on the framebuffer directly can be very slow if
> > > > > the memory is noncached. By making a copy of each line in the FB first
> > > > > (which can take advantage of word-access speedup), we can do the hash
> > > > > on a cached copy, which is much faster (10x speedup on my platform).
> > > > > 
> > > > > v6: use igt_memcpy_from_wc() instead of plain memcpy, as suggested by
> > > > >     Chris Wilson
> > > > > 
> > > > > Signed-off-by: Brian Starkey <brian.starkey@arm.com>
> > > > > [rebased and updated to the most recent API]
> > > > > Signed-off-by: Liviu Dudau <liviu.dudau@arm.com>
> > > > > ---
> > > > >  lib/igt_fb.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> > > > >  lib/igt_fb.h |  3 +++
> > > > >  2 files changed, 69 insertions(+)
> > > > > 
> > > > > diff --git a/lib/igt_fb.c b/lib/igt_fb.c
> > > > > index 9d4f905e..d07dae39 100644
> > > > > --- a/lib/igt_fb.c
> > > > > +++ b/lib/igt_fb.c
> > > > > @@ -3256,6 +3256,72 @@ bool igt_fb_supported_format(uint32_t drm_format)
> > > > >  	return false;
> > > > >  }
> > > > >  
> > > > > +/*
> > > > > + * This implements the FNV-1a hashing algorithm instead of CRC, for
> > > > > + * simplicity
> > > > > + * http://www.isthe.com/chongo/tech/comp/fnv/index.html
> > > > > + *
> > > > > + * hash = offset_basis
> > > > > + * for each octet_of_data to be hashed
> > > > > + *         hash = hash xor octet_of_data
> > > > > + *         hash = hash * FNV_prime
> > > > > + * return hash
> > > > > + *
> > > > > + * 32 bit offset_basis = 2166136261
> > > > > + * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
> > > > > + */
> > > > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
> > > > > +{
> > > > > +#define FNV1a_OFFSET_BIAS 2166136261
> > > > > +#define FNV1a_PRIME 16777619
> > > > 
> > > > I'd just use plain uint32_t variables for those, but no big deal.
> > > > 
> > > > > +	uint32_t hash;
> > > > > +	void *map;
> > > > > +	char *ptr, *line = NULL;
> > > > > +	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
> > > > > +	uint32_t stride = calc_plane_stride(fb, 0);
> > > > 
> > > > We could return -EINVAL in case fb->num_planes != 1.
> > > 
> > > Let's not waste cycles. With this ^ fixed, this patch is:
> > > 
> > > Reviewed-by: Simon Ser <simon.ser@intel.com>
> > > 
> > > Other nits are optional.
> > 
> > I agreed with all your suggestions, and I already applied all of them.
> > Should I wait for the other patches review, or should I resend the new
> > version?
> 
> I'm fine with waiting for the full review before a new version of the
> whole patchset, but you can also send an updated version of a single
> patch with:
> 
>     git send-email --in-reply-to="<cover.1560374714.git.rodrigosiqueiramelo@gmail.com>" -1 <commit hash>
> 
> where In-Reply-To is the Message-Id of the patch you want to update. I
> agree it's a little tedious since you need to extract the Message-Id
> from the message header.

Thanks for the tip with git-send-mail. Since I already applied most of
your suggestions, I'll send a full version soon.
 
> > Thanks for all the feedback
> 
> :)
> 
> > Best Regards
> >  
> > > > > +	if (fb->is_dumb)
> > > > > +		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
> > > > > +					      PROT_READ);
> > > > > +	else
> > > > > +		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
> > > > > +				    PROT_READ);
> > > > > +	ptr = map;
> > > > 
> > > > Nit: no need for this, can assign the result of mmap directly to ptr.
> > > > 
> > > > > +
> > > > > +	/*
> > > > > +	 * Framebuffers are often uncached, which can make byte-wise accesses
> > > > > +	 * very slow. We copy each line of the FB into a local buffer to speed
> > > > > +	 * up the hashing.
> > > > > +	 */
> > > > > +	line = malloc(stride);
> > > > > +	if (!line) {
> > > > > +		munmap(map, fb->size);
> > > > > +		return -ENOMEM;
> > > > > +	}
> > > > > +
> > > > > +	hash = FNV1a_OFFSET_BIAS;
> > > > > +
> > > > > +	for (y = 0; y < fb->height; y++, ptr += stride) {
> > > > > +
> > > > > +		igt_memcpy_from_wc(line, ptr, stride);
> > > > 
> > > > Nit: no need to copy the whole stride actually, we can just copy
> > > > fb->width * cpp since we're only going to read that.
> > > > 
> > > > > +
> > > > > +		for (x = 0; x < fb->width * cpp; x++) {
> > > > > +			hash ^= line[x];
> > > > > +			hash *= FNV1a_PRIME;
> > > > > +		}
> > > > > +	}
> > > > > +
> > > > > +	crc->n_words = 1;
> > > > > +	crc->crc[0] = hash;
> > > > > +
> > > > > +	free(line);
> > > > > +	munmap(map, fb->size);
> > > > > +
> > > > > +	return 0;
> > > > > +#undef FNV1a_OFFSET_BIAS
> > > > > +#undef FNV1a_PRIME
> > > > > +}
> > > > > +
> > > > >  /**
> > > > >   * igt_format_is_yuv:
> > > > >   * @drm_format: drm fourcc
> > > > > diff --git a/lib/igt_fb.h b/lib/igt_fb.h
> > > > > index adefebe1..a2741c05 100644
> > > > > --- a/lib/igt_fb.h
> > > > > +++ b/lib/igt_fb.h
> > > > > @@ -37,6 +37,7 @@
> > > > >  #include <i915_drm.h>
> > > > >  
> > > > >  #include "igt_color_encoding.h"
> > > > > +#include "igt_debugfs.h"
> > > > >  
> > > > >  /*
> > > > >   * Internal format to denote a buffer compatible with pixman's
> > > > > @@ -194,5 +195,7 @@ int igt_format_plane_bpp(uint32_t drm_format, int plane);
> > > > >  void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
> > > > >  			   bool allow_yuv);
> > > > >  
> > > > > +int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
> > > > > +
> > > > >  #endif /* __IGT_FB_H__ */
> > > > >  
> > > > > _______________________________________________
> > > > > igt-dev mailing list
> > > > > igt-dev@lists.freedesktop.org
> > > > > https://lists.freedesktop.org/mailman/listinfo/igt-dev
> > > > _______________________________________________
> > > > igt-dev mailing list
> > > > igt-dev@lists.freedesktop.org
> > > > https://lists.freedesktop.org/mailman/listinfo/igt-dev
diff mbox series

Patch

diff --git a/lib/igt_fb.c b/lib/igt_fb.c
index 9d4f905e..d07dae39 100644
--- a/lib/igt_fb.c
+++ b/lib/igt_fb.c
@@ -3256,6 +3256,72 @@  bool igt_fb_supported_format(uint32_t drm_format)
 	return false;
 }
 
+/*
+ * This implements the FNV-1a hashing algorithm instead of CRC, for
+ * simplicity
+ * http://www.isthe.com/chongo/tech/comp/fnv/index.html
+ *
+ * hash = offset_basis
+ * for each octet_of_data to be hashed
+ *         hash = hash xor octet_of_data
+ *         hash = hash * FNV_prime
+ * return hash
+ *
+ * 32 bit offset_basis = 2166136261
+ * 32 bit FNV_prime = 224 + 28 + 0x93 = 16777619
+ */
+int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc)
+{
+#define FNV1a_OFFSET_BIAS 2166136261
+#define FNV1a_PRIME 16777619
+	uint32_t hash;
+	void *map;
+	char *ptr, *line = NULL;
+	int x, y, cpp = igt_drm_format_to_bpp(fb->drm_format) / 8;
+	uint32_t stride = calc_plane_stride(fb, 0);
+
+	if (fb->is_dumb)
+		map = kmstest_dumb_map_buffer(fb->fd, fb->gem_handle, fb->size,
+					      PROT_READ);
+	else
+		map = gem_mmap__gtt(fb->fd, fb->gem_handle, fb->size,
+				    PROT_READ);
+	ptr = map;
+
+	/*
+	 * Framebuffers are often uncached, which can make byte-wise accesses
+	 * very slow. We copy each line of the FB into a local buffer to speed
+	 * up the hashing.
+	 */
+	line = malloc(stride);
+	if (!line) {
+		munmap(map, fb->size);
+		return -ENOMEM;
+	}
+
+	hash = FNV1a_OFFSET_BIAS;
+
+	for (y = 0; y < fb->height; y++, ptr += stride) {
+
+		igt_memcpy_from_wc(line, ptr, stride);
+
+		for (x = 0; x < fb->width * cpp; x++) {
+			hash ^= line[x];
+			hash *= FNV1a_PRIME;
+		}
+	}
+
+	crc->n_words = 1;
+	crc->crc[0] = hash;
+
+	free(line);
+	munmap(map, fb->size);
+
+	return 0;
+#undef FNV1a_OFFSET_BIAS
+#undef FNV1a_PRIME
+}
+
 /**
  * igt_format_is_yuv:
  * @drm_format: drm fourcc
diff --git a/lib/igt_fb.h b/lib/igt_fb.h
index adefebe1..a2741c05 100644
--- a/lib/igt_fb.h
+++ b/lib/igt_fb.h
@@ -37,6 +37,7 @@ 
 #include <i915_drm.h>
 
 #include "igt_color_encoding.h"
+#include "igt_debugfs.h"
 
 /*
  * Internal format to denote a buffer compatible with pixman's
@@ -194,5 +195,7 @@  int igt_format_plane_bpp(uint32_t drm_format, int plane);
 void igt_format_array_fill(uint32_t **formats_array, unsigned int *count,
 			   bool allow_yuv);
 
+int igt_fb_get_crc(struct igt_fb *fb, igt_crc_t *crc);
+
 #endif /* __IGT_FB_H__ */