Skip to content

local: Add support for setting watermark#781

Merged
pcercuei merged 1 commit intomasterfrom
pcercuei/watermark
Jan 28, 2022
Merged

local: Add support for setting watermark#781
pcercuei merged 1 commit intomasterfrom
pcercuei/watermark

Conversation

@pcercuei
Copy link
Copy Markdown
Contributor

Add support for setting the watermark value, if the driver supports it.
The watermark value represents the number of samples that will be read
in one burst from the hardware. Since we read data at the granularity of
iio_buffer blocks, it makes sense to use the highest watermark value up
to the buffer's size. If the value is too high, the driver will
automatically adjust it to the maximum watermark value possible.

Fixes #780.

Signed-off-by: Paul Cercueil paul@crapouillou.net

@HiFiPhile
Copy link
Copy Markdown

Hi,

Thanks for your PR.

I didn't see in kernel's iio_enable_buffers() watermark setting error is ignored, so even if the driver returns error it will still works.

	if (indio_dev->info->hwfifo_set_watermark)
		indio_dev->info->hwfifo_set_watermark(indio_dev,
			config->watermark);

Because the actual value of watermark and length will decide if otherone's value can be successfully written:

  • length must >= watermark
  • watermark must <= legnth
    Otherwise a Invalid argument will happen when you write the value (you can test simplyly with echo)

It's not sufficient to only write watermark once.

For example:

  1. length=100, watermark=100 from previous run
  2. New run want length=50
  3. local_write_dev_attr(dev, "buffer/length", 50) will fail will Invalid argument since it's smaller than watermark.

I think you need to reset `watermark to 1 before setting length.

@pcercuei
Copy link
Copy Markdown
Contributor Author

This is incorrect. If the watermark is higher than the new buffer length, the kernel will reduce it to the buffer length. See: https://github.com/torvalds/linux/blob/master/drivers/iio/industrialio-buffer.c#L667-L668

@HiFiPhile
Copy link
Copy Markdown

This is incorrect. If the watermark is higher than the new buffer length, the kernel will reduce it to the buffer length.

Hum... Maybe I've some memory corruption... will try it later.

@HiFiPhile
Copy link
Copy Markdown

I've tested creating buffer with size up & down, it's working :)

@pcercuei
Copy link
Copy Markdown
Contributor Author

@HiFiPhile And it also works if the buffer size isn't a multiple of the watermark?

@HiFiPhile
Copy link
Copy Markdown

With a size of 110 and watermark of 20 it seems working. I don't have a high speed device to test if last conversion will be cut off, in low speed device since kernel buffer has a 4*length it should be ok.

@pcercuei
Copy link
Copy Markdown
Contributor Author

But 110*4 is divisible by 20 :)
Could you try e.g. a buffer size of 22 samples? I just want to make sure it doesn't lock up.

@HiFiPhile
Copy link
Copy Markdown

With a length of 123 and watermark of 20 it still works :)

@pcercuei
Copy link
Copy Markdown
Contributor Author

Great, thanks. I sent patches upstream to fix the two drivers that errored with too high watermarks.

Add support for setting the watermark value, if the driver supports it.
The watermark value represents the number of samples that will be read
in one burst from the hardware. Since we read data at the granularity of
iio_buffer blocks, it makes sense to use the highest watermark value up
to the buffer's size. If the value is too high, the driver will
automatically adjust it to the maximum watermark value possible.

Fixes #780.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
@pcercuei pcercuei requested a review from nunojsa January 18, 2022 15:57
@pcercuei pcercuei merged commit 5b4cd21 into master Jan 28, 2022
@pcercuei pcercuei deleted the pcercuei/watermark branch January 28, 2022 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to set buffer watermark correctly

3 participants