19 #if STXXL_HAVE_WBTL_FILE
27 #ifndef STXXL_VERBOSE_WBTL
28 #define STXXL_VERBOSE_WBTL STXXL_VERBOSE2
35 size_type write_buffer_size,
37 int queue_id,
int allocator_id)
38 : disk_queued_file(queue_id, allocator_id), storage(backend_file),
39 sz(0), write_block_size(write_buffer_size),
40 free_bytes(0), curbuf(1), curpos(write_block_size)
43 assert(write_buffers == 2);
44 write_buffer[0] =
static_cast<char*
>(stxxl::aligned_alloc<STXXL_BLOCK_ALIGN>(write_block_size));
45 write_buffer[1] =
static_cast<char*
>(stxxl::aligned_alloc<STXXL_BLOCK_ALIGN>(write_block_size));
46 buffer_address[0] = offset_type(-1);
47 buffer_address[1] = offset_type(-1);
50 wbtl_file::~wbtl_file()
52 stxxl::aligned_dealloc<STXXL_BLOCK_ALIGN>(write_buffer[1]);
53 stxxl::aligned_dealloc<STXXL_BLOCK_ALIGN>(write_buffer[0]);
58 void wbtl_file::serve(
void* buffer, offset_type offset, size_type
bytes,
59 request::request_type type)
61 if (type == request::READ)
64 sread(buffer, offset, bytes);
69 swrite(buffer, offset, bytes);
73 void wbtl_file::lock()
78 wbtl_file::offset_type wbtl_file::size()
83 void wbtl_file::set_size(offset_type newsize)
85 scoped_mutex_lock mapping_lock(mapping_mutex);
86 assert(sz <= newsize);
88 _add_free_region(sz, newsize - sz);
89 storage->set_size(newsize);
91 assert(sz == storage->size());
95 #define FMT_A_S(_addr_, _size_) "0x" << std::hex << std::setfill('0') << std::setw(8) << (_addr_) << "/0x" << std::setw(8) << (_size_)
96 #define FMT_A_C(_addr_, _size_) "0x" << std::setw(8) << (_addr_) << "(" << std::dec << (_size_) << ")"
97 #define FMT_A(_addr_) "0x" << std::setw(8) << (_addr_)
102 scoped_mutex_lock mapping_lock(mapping_mutex);
103 sortseq::iterator physical = address_mapping.find(offset);
104 STXXL_VERBOSE_WBTL(
"wbtl:discard l" << FMT_A_S(offset, size) <<
" @ p" << FMT_A(physical != address_mapping.end() ? physical->second : 0xffffffff));
105 if (physical == address_mapping.end()) {
109 offset_type physical_offset = physical->second;
110 address_mapping.erase(physical);
111 _add_free_region(physical_offset, size);
112 place_map::iterator reverse = reverse_mapping.find(physical_offset);
113 if (reverse == reverse_mapping.end()) {
114 STXXL_ERRMSG(
"discard: reverse mapping not found: " << FMT_A_S(offset, size) <<
" ==> " <<
"???");
116 assert(offset == (reverse->second).first);
117 reverse_mapping.erase(reverse);
119 storage->discard(physical_offset, size);
124 void wbtl_file::_add_free_region(offset_type offset, offset_type size)
127 STXXL_VERBOSE_WBTL(
"wbtl:addfre p" << FMT_A_S(offset, size) <<
" F <= f" << FMT_A_C(free_bytes, free_space.size()));
128 offset_type region_pos = offset;
129 offset_type region_size = size;
130 if (!free_space.empty())
132 sortseq::iterator succ = free_space.upper_bound(region_pos);
133 sortseq::iterator pred = succ;
135 check_corruption(region_pos, region_size, pred, succ);
136 if (succ == free_space.end())
138 if (pred == free_space.end())
141 assert(pred != free_space.end());
143 if ((*pred).first + (*pred).second == region_pos)
146 region_size += (*pred).second;
147 region_pos = (*pred).first;
148 free_space.erase(pred);
153 if (free_space.size() > 1)
155 bool succ_is_not_the_first = (succ != free_space.begin());
156 if ((*succ).first == region_pos + region_size)
159 region_size += (*succ).second;
160 free_space.erase(succ);
162 if (succ_is_not_the_first)
164 if (pred == free_space.end())
167 assert(pred != free_space.end());
169 if ((*pred).first + (*pred).second == region_pos)
172 region_size += (*pred).second;
173 region_pos = (*pred).first;
174 free_space.erase(pred);
180 if ((*succ).first == region_pos + region_size)
183 region_size += (*succ).second;
184 free_space.erase(succ);
190 free_space[region_pos] = region_size;
192 STXXL_VERBOSE_WBTL(
"wbtl:free p" << FMT_A_S(region_pos, region_size) <<
" F => f" << FMT_A_C(free_bytes, free_space.size()));
195 void wbtl_file::sread(
void* buffer, offset_type offset, size_type bytes)
197 scoped_mutex_lock buffer_lock(buffer_mutex);
199 offset_type physical_offset;
202 scoped_mutex_lock mapping_lock(mapping_mutex);
203 sortseq::iterator physical = address_mapping.find(offset);
204 if (physical == address_mapping.end()) {
205 STXXL_ERRMSG(
"wbtl_read: mapping not found: " << FMT_A_S(offset, bytes) <<
" ==> " <<
"???");
207 physical_offset = 0xffffffff;
209 physical_offset = physical->second;
213 if (buffer_address[curbuf] <= physical_offset &&
214 physical_offset < buffer_address[curbuf] + write_block_size)
217 assert(physical_offset + bytes <= buffer_address[curbuf] + write_block_size);
218 memcpy(buffer, write_buffer[curbuf] + (physical_offset - buffer_address[curbuf]), bytes);
219 stats::get_instance()->read_cached(bytes);
222 else if (buffer_address[1 - curbuf] <= physical_offset &&
223 physical_offset < buffer_address[1 - curbuf] + write_block_size)
226 assert(physical_offset + bytes <= buffer_address[1 - curbuf] + write_block_size);
227 memcpy(buffer, write_buffer[1 - curbuf] + (physical_offset - buffer_address[1 - curbuf]), bytes);
228 stats::get_instance()->read_cached(bytes);
231 else if (physical_offset == 0xffffffff) {
233 char* uninitialized = (
char*)malloc(
sizeof(
char));
234 memset(buffer, *uninitialized, bytes);
240 request_ptr req = storage->aread(buffer, physical_offset, bytes);
243 STXXL_VERBOSE_WBTL(
"wbtl:sread l" << FMT_A_S(offset, bytes) <<
" @ p" << FMT_A(physical_offset) <<
" " << std::dec << cached);
247 void wbtl_file::swrite(
void* buffer, offset_type offset, size_type bytes)
249 scoped_mutex_lock buffer_lock(buffer_mutex);
252 scoped_mutex_lock mapping_lock(mapping_mutex);
253 sortseq::iterator physical = address_mapping.find(offset);
254 STXXL_VERBOSE_WBTL(
"wbtl:swrite l" << FMT_A_S(offset, bytes) <<
" @ <= p" <<
255 FMT_A_C(physical != address_mapping.end() ? physical->second : 0xffffffff, address_mapping.size()));
256 if (physical != address_mapping.end()) {
257 mapping_lock.unlock();
263 if (bytes > write_block_size - curpos)
267 if (buffer_address[curbuf] != offset_type(-1)) {
268 STXXL_VERBOSE_WBTL(
"wbtl:w2disk p" << FMT_A_S(buffer_address[curbuf], write_block_size));
271 if (curpos < write_block_size)
272 _add_free_region(buffer_address[curbuf] + curpos, write_block_size - curpos);
274 if (backend_request.get()) {
275 backend_request->wait(
false);
278 backend_request = storage->awrite(write_buffer[curbuf], buffer_address[curbuf], write_block_size);
283 buffer_address[curbuf] = get_next_write_block();
286 assert(bytes <= write_block_size - curpos);
289 memcpy(write_buffer[curbuf] + curpos, buffer, bytes);
290 stats::get_instance()->write_cached(bytes);
292 scoped_mutex_lock mapping_lock(mapping_mutex);
293 address_mapping[offset] = buffer_address[curbuf] + curpos;
294 reverse_mapping[buffer_address[curbuf] + curpos] = place(offset, bytes);
295 STXXL_VERBOSE_WBTL(
"wbtl:swrite l" << FMT_A_S(offset, bytes) <<
" @ => p" << FMT_A_C(buffer_address[curbuf] + curpos, address_mapping.size()));
299 wbtl_file::offset_type wbtl_file::get_next_write_block()
302 sortseq::iterator space =
303 std::find_if(free_space.begin(), free_space.end(),
306 if (space != free_space.end())
308 offset_type region_pos = (*space).first;
309 offset_type region_size = (*space).second;
310 free_space.erase(space);
311 if (region_size > write_block_size)
312 free_space[region_pos + write_block_size] = region_size - write_block_size;
314 free_bytes -= write_block_size;
316 STXXL_VERBOSE_WBTL(
"wbtl:nextwb p" << FMT_A_S(region_pos, write_block_size) <<
" F f" << FMT_A_C(free_bytes, free_space.size()));
323 void wbtl_file::check_corruption(offset_type region_pos, offset_type region_size,
324 sortseq::iterator pred, sortseq::iterator succ)
326 if (pred != free_space.end())
328 if (pred->first <= region_pos && pred->first + pred->second > region_pos)
330 STXXL_THROW(bad_ext_alloc,
"Error: double deallocation of external memory " <<
331 "System info: P " << pred->first <<
" " << pred->second <<
" " << region_pos);
334 if (succ != free_space.end())
336 if (region_pos <= succ->first && region_pos + region_size > succ->first)
338 STXXL_THROW(bad_ext_alloc,
"Error: double deallocation of external memory "
339 <<
"System info: S " << region_pos <<
" " << region_size <<
" " << succ->first);
344 const char* wbtl_file::io_type()
const
351 #endif // #if STXXL_HAVE_WBTL_FILE
#define STXXL_THROW(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message]".
counting_ptr< request > request_ptr
A reference counting pointer for request.
virtual void wait(bool measure_time=true)=0
Suspends calling thread until completion of the request.
#define STXXL_BEGIN_NAMESPACE
void STXXL_UNUSED(const U &)
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message] : [errno message]".
static const size_t bytes
number of bytes in uint_pair
#define _STXXL_FORCE_SEQUENTIAL
void discard(StreamAlgorithm &in)
Reads stream content and discards it. Useful where you do not need the processed stream anymore...
#define STXXL_END_NAMESPACE