Skip to content

Improve direct IO range scan performance with readahead - #3884

Closed
sagar0 wants to merge 10 commits into
meta:masterfrom
sagar0:readahead-iter-directio
Closed

sagar0 wants to merge 10 commits into
meta:masterfrom
sagar0:readahead-iter-directio

Conversation

@sagar0

@sagar0 sagar0 commented May 21, 2018 •

Copy link
Copy Markdown
Contributor

This PR extends the improvements in #3282 to also work when using Direct IO.
We see 4.5X performance improvement in seekrandom benchmark doing long range scans, when using direct reads, on flash.

Description:
This change improves the performance of iterators doing long range scans (e.g. big/full index or table scans in MyRocks) by using readahead and prefetching additional data on each disk IO, and storing in a local buffer. This prefetching is automatically enabled on noticing more than 2 IOs for the same table file during iteration. The readahead size starts with 8KB and is exponentially increased on each additional sequential IO, up to a max of 256 KB. This helps in cutting down the number of IOs needed to complete the range scan.

Implementation Details:

  • Used FilePrefetchBuffer as the underlying buffer to store the readahead data. FilePrefetchBuffer can now take file_reader, readahead_size and max_readahead_size as input to the constructor, and automatically do readahead.
  • FilePrefetchBuffer::TryReadFromCache can now call FilePrefetchBuffer::Prefetch if readahead is enabled.
  • AlignedBuffer (which is the underlying store for FilePrefetchBuffer) now takes a few additional args in AlignedBuffer::AllocateNewBuffer to allow copying data from the old buffer.
  • Made sure not to re-read partial chunks of data that were already available in the buffer, from device again.
  • Fixed a couple of cases where AlignedBuffer::cursize_ was not being properly kept up-to-date.

Constraints:

  • Similar to Improve performance of long range scans with readahead #3282, this gets currently enabled only when ReadOptions.readahead_size = 0 (which is the default value).
  • Since the prefetched data is stored in a temporary buffer allocated on heap, this could increase the memory usage if you have many iterators doing long range scans simultaneously.
  • Enabled only for user reads, and disabled for compactions. Compaction reads are controlled by the options use_direct_io_for_flush_and_compaction and compaction_readahead_size, and the current feature takes precautions not to mess with them.

Benchmarks:
I used the same benchmark as used in #3282.
Data fill:

TEST_TMPDIR=/data/users/$USER/benchmarks/iter ./db_bench -benchmarks=fillrandom -num=1000000000 -compression_type="none" -level_compaction_dynamic_level_bytes

Do a long range scan: Seekrandom with large number of nexts

TEST_TMPDIR=/data/users/$USER/benchmarks/iter ./db_bench -benchmarks=seekrandom -use_direct_reads -duration=60 -num=1000000000 -use_existing_db -seek_nexts=10000 -statistics -histogram
Before:
seekrandom   :   37939.906 micros/op 26 ops/sec;   29.2 MB/s (1636 of 1999 found)
With this change:
seekrandom   :   8527.720 micros/op 117 ops/sec;  129.7 MB/s (6530 of 7999 found)

~4.5X perf improvement. Taken on an average of 3 runs.

@sagar0 sagar0 changed the title Improve range scan perf in direct i/o case, with readahead Improve range scan performance with readahead, when using direct i/o May 21, 2018

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

@sagar0
sagar0 force-pushed the readahead-iter-directio branch from a10bdc5 to e6b7317 Compare June 16, 2018 03:16
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request.

@sagar0 sagar0 changed the title Improve range scan performance with readahead, when using direct i/o Improve direct IO range scan performance with readahead Jun 16, 2018

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@sagar0
sagar0 requested a review from siying June 16, 2018 03:32
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Comment thread table/block_based_table_reader.cc Resolved
Comment thread table/block_based_table_reader.cc Resolved
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

sagar0 added 3 commits June 20, 2018 10:59
1. Don't allocate a new buffer if the current buffer is sufficient for new
data. This reduces one malloc + memcpy sequence.
2. Reduce a memmove by copying data directly from tail to the new
buffer instead of doing a memmove + memcopy as earlier.
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

@siying siying left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Comment thread table/block_based_table_reader.cc Outdated
&data_block_iter_, is_index_,
key_includes_seq_,
/* get_context */ nullptr, s);
/* get_context */ nullptr, s, prefetch_buffer_.get());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel like over 80-char.

buffer_.Alignment(alignment);
buffer_.AllocateNewBuffer(static_cast<size_t>(roundup_len),
copy_data_to_new_buffer, chunk_offset_in_buffer,
chunk_len);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May consider to add a little bit extra buffer to avoid another allocation if we have reached max readahead size.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer this optimization for now; will get back to it.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@sagar0 has updated the pull request. View: changes, changes since last import

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagar0 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

rcane pushed a commit to rcane/rocksdb that referenced this pull request Sep 13, 2018
Summary:
This PR extends the improvements in meta#3282 to also work when using Direct IO.
We see **4.5X performance improvement** in seekrandom benchmark doing long range scans, when using direct reads, on flash.

**Description:**
This change improves the performance of iterators doing long range scans (e.g. big/full index or table scans in MyRocks) by using readahead and prefetching additional data on each disk IO, and storing in a local buffer. This prefetching is automatically enabled on noticing more than 2 IOs for the same table file during iteration. The readahead size starts with 8KB and is exponentially increased on each additional sequential IO, up to a max of 256 KB. This helps in cutting down the number of IOs needed to complete the range scan.

**Implementation Details:**
- Used `FilePrefetchBuffer` as the underlying buffer to store the readahead data. `FilePrefetchBuffer` can now take file_reader, readahead_size and max_readahead_size as input to the constructor, and automatically do readahead.
- `FilePrefetchBuffer::TryReadFromCache` can now call `FilePrefetchBuffer::Prefetch` if readahead is enabled.
- `AlignedBuffer` (which is the underlying store for `FilePrefetchBuffer`) now takes a few additional args in `AlignedBuffer::AllocateNewBuffer` to allow copying data from the old buffer.
- Made sure not to re-read partial chunks of data that were already available in the buffer, from device again.
- Fixed a couple of cases where `AlignedBuffer::cursize_` was not being properly kept up-to-date.

**Constraints:**
- Similar to meta#3282, this gets currently enabled only when ReadOptions.readahead_size = 0 (which is the default value).
- Since the prefetched data is stored in a temporary buffer allocated on heap, this could increase the memory usage if you have many iterators doing long range scans simultaneously.
- Enabled only for user reads, and disabled for compactions. Compaction reads are controlled by the options `use_direct_io_for_flush_and_compaction` and `compaction_readahead_size`, and the current feature takes precautions not to mess with them.

**Benchmarks:**
I used the same benchmark as used in meta#3282.
Data fill:
```
TEST_TMPDIR=/data/users/$USER/benchmarks/iter ./db_bench -benchmarks=fillrandom -num=1000000000 -compression_type="none" -level_compaction_dynamic_level_bytes
```

Do a long range scan: Seekrandom with large number of nexts
```
TEST_TMPDIR=/data/users/$USER/benchmarks/iter ./db_bench -benchmarks=seekrandom -use_direct_reads -duration=60 -num=1000000000 -use_existing_db -seek_nexts=10000 -statistics -histogram
```

```
Before:
seekrandom   :   37939.906 micros/op 26 ops/sec;   29.2 MB/s (1636 of 1999 found)
With this change:
seekrandom   :   8527.720 micros/op 117 ops/sec;  129.7 MB/s (6530 of 7999 found)
```
~4.5X perf improvement. Taken on an average of 3 runs.
Closes meta#3884

Differential Revision: D8082143

Pulled By: sagar0

fbshipit-source-id: 4d7a8561cbac03478663713df4d31ad2620253bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants