Seastar
High performance C++ framework for concurrent servers
fstream.hh
Go to the documentation of this file.
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2015 Cloudius Systems, Ltd.
20  */
21 
22 #pragma once
23 
25 
26 // File <-> streams adapters
27 //
28 // Seastar files are block-based due to the reliance on DMA - you must read
29 // on sector boundaries. The adapters in this file provide a byte stream
30 // interface to files, while retaining the zero-copy characteristics of
31 // seastar files.
32 #include <seastar/core/file.hh>
33 #include <seastar/core/iostream.hh>
34 #include <seastar/core/shared_ptr.hh>
35 #include <seastar/core/internal/api-level.hh>
36 #include <seastar/util/modules.hh>
37 
38 #ifndef SEASTAR_MODULE
39 #include <cstdint>
40 #endif
41 
42 namespace seastar {
43 
44 SEASTAR_MODULE_EXPORT_BEGIN
45 
47  static constexpr uint64_t window_size = 4 * 1024 * 1024;
48  struct window {
49  uint64_t total_read = 0;
50  uint64_t unused_read = 0;
51  };
52  window current_window;
53  window previous_window;
54  unsigned read_ahead = 1;
55 
56  friend class file_data_source_impl;
57 };
58 
61  size_t buffer_size = 8192;
62  unsigned read_ahead = 0;
63 #if SEASTAR_API_LEVEL < 7
64  ::seastar::io_priority_class io_priority_class = default_priority_class();
65 #endif
67 };
68 
79  file file, uint64_t offset, uint64_t len, file_input_stream_options options = {});
80 
81 // Create an input_stream for a given file, with the specified options.
82 // Multiple fibers of execution (continuations) may safely open
83 // multiple input streams concurrently for the same file.
84 input_stream<char> make_file_input_stream(
85  file file, uint64_t offset, file_input_stream_options = {});
86 
87 // Create an input_stream for reading starting at a given position of the
88 // given file. Multiple fibers of execution (continuations) may safely open
89 // multiple input streams concurrently for the same file.
90 input_stream<char> make_file_input_stream(
91  file file, file_input_stream_options = {});
92 
94  // For small files, setting preallocation_size can make it impossible for XFS to find
95  // an aligned extent. On the other hand, without it, XFS will divide the file into
96  // file_size/buffer_size extents. To avoid fragmentation, we set the default buffer_size
97  // to 64k (so each extent will be a minimum of 64k) and preallocation_size to 0 (to avoid
98  // extent allocation problems).
99  //
100  // Large files should increase both buffer_size and preallocation_size.
101  unsigned buffer_size = 65536;
102  unsigned preallocation_size = 0;
103  unsigned write_behind = 1;
104 #if SEASTAR_API_LEVEL < 7
105  ::seastar::io_priority_class io_priority_class = default_priority_class();
106 #endif
107 };
108 
114  file file,
115  uint64_t buffer_size = 8192) noexcept;
116 
122  file file,
123  file_output_stream_options options) noexcept;
124 
129 
130 SEASTAR_MODULE_EXPORT_END
131 
132 }
Definition: iostream.hh:148
Definition: fstream.hh:46
Definition: file.hh:194
A representation of a possibly not-yet-computed value.
Definition: future.hh:1238
Definition: shared_ptr.hh:270
Definition: iostream.hh:379
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
unsigned write_behind
Number of buffers to write in parallel.
Definition: fstream.hh:103
size_t buffer_size
I/O buffer size.
Definition: fstream.hh:61
unsigned preallocation_size
Preallocate extents. For large files, set to a large number (a few megabytes) to reduce fragmentation...
Definition: fstream.hh:102
input_stream< char > make_file_input_stream(file file, uint64_t offset, uint64_t len, file_input_stream_options options={})
Creates an input_stream to read a portion of a file.
unsigned read_ahead
Maximum number of extra read-ahead operations.
Definition: fstream.hh:62
future< output_stream< char > > make_file_output_stream(file file, uint64_t buffer_size=8192) noexcept
future< data_sink > make_file_data_sink(file, file_output_stream_options) noexcept
lw_shared_ptr< file_input_stream_history > dynamic_adjustments
Input stream history, if null dynamic adjustments are disabled.
Definition: fstream.hh:66
Data structure describing options for opening a file input stream.
Definition: fstream.hh:60
Definition: fstream.hh:93