- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathTempSpace.cpp
710 lines (600 loc) · 15.5 KB
/
TempSpace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Dmitry Yemanov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2006 Dmitry Yemanov <dimitr@users.sf.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include"iberror.h"
#include"../common/classes/TempFile.h"
#include"../common/config/config.h"
#include"../common/config/dir_list.h"
#include"../common/gdsassert.h"
#include"../common/isc_proto.h"
#include"../common/os/path_utils.h"
#include"../jrd/jrd.h"
#include"../jrd/TempSpace.h"
usingnamespaceFirebird;
usingnamespaceJrd;
// Static definitions/initializations
GlobalPtr<Mutex> TempSpace::initMutex;
TempDirectoryList* TempSpace::tempDirs = NULL;
FB_SIZE_T TempSpace::minBlockSize = 0;
namespace
{
constsize_t MIN_TEMP_BLOCK_SIZE = 64 * 1024;
classTempCacheLimitGuard
{
public:
explicitTempCacheLimitGuard(Database* dbb) :
m_dbb(dbb),
m_size(0)
{}
~TempCacheLimitGuard()
{
if (m_size)
m_dbb->decTempCacheUsage(m_size);
}
boolreserve(FB_SIZE_T size)
{
if (m_dbb->incTempCacheUsage(size))
{
m_size = size;
returntrue;
}
returnfalse;
}
voidcommit()
{
m_size = 0;
}
private:
Database* const m_dbb;
FB_SIZE_T m_size;
};
}
//
// In-memory block class
//
FB_SIZE_T TempSpace::MemoryBlock::read(offset_t offset, void* buffer, FB_SIZE_T length)
{
if (offset + length > size)
{
length = size - offset;
}
memcpy(buffer, ptr + offset, length);
return length;
}
FB_SIZE_T TempSpace::MemoryBlock::write(offset_t offset, constvoid* buffer, FB_SIZE_T length)
{
if (offset + length > size)
{
length = size - offset;
}
memcpy(ptr + offset, buffer, length);
return length;
}
//
// On-disk block class
//
FB_SIZE_T TempSpace::FileBlock::read(offset_t offset, void* buffer, FB_SIZE_T length)
{
if (offset + length > size)
{
length = size - offset;
}
offset += seek;
return file->read(offset, buffer, length);
}
FB_SIZE_T TempSpace::FileBlock::write(offset_t offset, constvoid* buffer, FB_SIZE_T length)
{
if (offset + length > size)
{
length = size - offset;
}
offset += seek;
return file->write(offset, buffer, length);
}
//
// TempSpace::TempSpace
//
// Constructor
//
TempSpace::TempSpace(MemoryPool& p, const PathName& prefix, bool dynamic)
: pool(p), filePrefix(p, prefix),
logicalSize(0), physicalSize(0), localCacheUsage(0),
head(NULL), tail(NULL), tempFiles(p),
initialBuffer(p), initiallyDynamic(dynamic),
freeSegments(p)
{
if (!tempDirs)
{
MutexLockGuard guard(initMutex, FB_FUNCTION);
if (!tempDirs)
{
MemoryPool& def_pool = *getDefaultMemoryPool();
tempDirs = FB_NEW_POOL(def_pool) TempDirectoryList(def_pool);
minBlockSize = Config::getTempBlockSize();
if (minBlockSize < MIN_TEMP_BLOCK_SIZE)
minBlockSize = MIN_TEMP_BLOCK_SIZE;
else
minBlockSize = FB_ALIGN(minBlockSize, MIN_TEMP_BLOCK_SIZE);
}
}
}
//
// TempSpace::~TempSpace
//
// Destructor
//
TempSpace::~TempSpace()
{
while (head)
{
Block* temp = head->next;
delete head;
head = temp;
}
if (localCacheUsage)
{
Database* const dbb = GET_DBB();
dbb->decTempCacheUsage(localCacheUsage);
}
while (tempFiles.getCount())
delete tempFiles.pop();
}
//
// TempSpace::read
//
// Reads bytes from the temporary space
//
FB_SIZE_T TempSpace::read(offset_t offset, void* buffer, FB_SIZE_T length)
{
fb_assert(offset + length <= logicalSize);
if (length)
{
// search for the first needed block
Block* block = findBlock(offset);
UCHAR* p = static_cast<UCHAR*>(buffer);
FB_SIZE_T l = length;
// read data from the block chain
for (Block* itr = block; itr && l; itr = itr->next, offset = 0)
{
const FB_SIZE_T n = itr->read(offset, p, l);
p += n;
l -= n;
}
fb_assert(!l);
}
return length;
}
//
// TempSpace::write
//
// Writes bytes to the temporary space
//
FB_SIZE_T TempSpace::write(offset_t offset, constvoid* buffer, FB_SIZE_T length)
{
fb_assert(offset <= logicalSize);
if (offset + length > logicalSize)
{
// not enough space, allocate one more block
extend(offset + length - logicalSize);
}
if (length)
{
// search for the first needed block
Block* const block = findBlock(offset);
const UCHAR* p = static_cast<const UCHAR*>(buffer);
FB_SIZE_T l = length;
// write data to as many blocks as necessary
for (Block* itr = block; itr && l; itr = itr->next, offset = 0)
{
const FB_SIZE_T n = itr->write(offset, p, l);
p += n;
l -= n;
}
fb_assert(!l);
}
return length;
}
//
// TempSpace::extend
//
// Increases size of the temporary space
//
voidTempSpace::extend(FB_SIZE_T size)
{
logicalSize += size;
if (logicalSize > physicalSize)
{
const FB_SIZE_T initialSize = initialBuffer.getCount();
// If the dynamic mode is specified, then we allocate new blocks
// by growing the same initial memory block in the specified chunks.
// Once the limit (64KB) is reached, we switch to the generic algorithm
// (1MB blocks), copy the existing data there and free the initial buffer.
//
// This mode should not be used if the caller never works with small blocks.
// Also, it MUST NOT be used if the caller deals with inMemory() or allocateBatch()
// routines and caches the pointers to use them later. These pointers may become
// invalid after resizing the initial block or after switching to large blocks.
if (initiallyDynamic && logicalSize < MIN_TEMP_BLOCK_SIZE)
{
// allocate or extend the initial dynamic block, it will grow up to 64KB
if (!initialSize)
{
fb_assert(!head && !tail);
head = tail = FB_NEW_POOL(pool) InitialBlock(initialBuffer.getBuffer(size), size);
}
else
{
fb_assert(head == tail);
size += initialSize;
initialBuffer.resize(size);
new(head) InitialBlock(initialBuffer.begin(), size);
}
physicalSize = size;
return;
}
if (initialSize)
{
fb_assert(head == tail);
delete head;
head = tail = NULL;
size = static_cast<FB_SIZE_T>(FB_ALIGN(logicalSize, minBlockSize));
physicalSize = size;
}
else
{
size = static_cast<FB_SIZE_T>(FB_ALIGN(logicalSize - physicalSize, minBlockSize));
physicalSize += size;
}
Block* block = NULL;
{ // scope
TempCacheLimitGuard guard(GET_DBB());
if (guard.reserve(size))
{
try
{
// allocate block in virtual memory
block = FB_NEW_POOL(pool) MemoryBlock(FB_NEW_POOL(pool) UCHAR[size], tail, size);
localCacheUsage += size;
guard.commit();
}
catch (const BadAlloc&)
{
// not enough memory
}
}
}
// NS 2014-07-31: FIXME: missing exception handling.
// error thrown in block of code below will leave TempSpace in inconsistent state:
// logical/physical size already increased while allocation has in fact failed.
if (!block)
{
// allocate block in the temp file
TempFile* const file = setupFile(size);
fb_assert(file);
if (tail && tail->sameFile(file))
{
fb_assert(!initialSize);
tail->size += size;
return;
}
block = FB_NEW_POOL(pool) FileBlock(file, tail, size);
}
// preserve the initial contents, if any
if (initialSize)
{
block->write(0, initialBuffer.begin(), initialSize);
initialBuffer.free();
}
// append new block to the chain
if (!head)
{
head = block;
}
tail = block;
}
}
//
// TempSpace::findBlock
//
// Locates the space block corresponding to the given global offset
//
TempSpace::Block* TempSpace::findBlock(offset_t& offset) const
{
fb_assert(offset <= logicalSize);
Block* block = NULL;
if (offset < physicalSize / 2)
{
// walk forward
block = head;
while (block && offset >= block->size)
{
offset -= block->size;
block = block->next;
}
fb_assert(block);
}
else
{
// walk backward
block = tail;
while (block && physicalSize - offset > block->size)
{
offset += block->size;
block = block->prev;
}
fb_assert(block);
offset -= physicalSize - block->size;
}
fb_assert(offset <= block->size);
return block;
}
//
// TempSpace::setupFile
//
// Allocates the required space in some temporary file
//
TempFile* TempSpace::setupFile(FB_SIZE_T size)
{
StaticStatusVector status_vector;
for (FB_SIZE_T i = 0; i < tempDirs->getCount(); i++)
{
TempFile* file = NULL;
PathName directory = (*tempDirs)[i];
PathUtils::ensureSeparator(directory);
for (FB_SIZE_T j = 0; j < tempFiles.getCount(); j++)
{
PathName dirname, filename;
PathUtils::splitLastComponent(dirname, filename, tempFiles[j]->getName());
PathUtils::ensureSeparator(dirname);
if (!directory.compare(dirname))
{
file = tempFiles[j];
break;
}
}
try
{
if (!file)
{
file = FB_NEW_POOL(pool) TempFile(pool, filePrefix, directory);
tempFiles.add(file);
}
file->extend(size);
}
catch (const system_error& ex)
{
ex.stuffException(status_vector);
continue;
}
return file;
}
// no room in all directories
Arg::Gds status(isc_out_of_temp_space);
status.append(Arg::StatusVector(status_vector.begin()));
iscLogStatus(NULL, status.value());
status.raise();
returnNULL; // compiler silencer
}
//
// TempSpace::allocateSpace
//
// Allocate available space in free segments. Extend file if necessary
//
offset_tTempSpace::allocateSpace(FB_SIZE_T size)
{
// Find the best available space. This is defined as the smallest free space
// that is big enough. This preserves large blocks.
Segment* best = NULL;
// Search through the available space in the not used segments list
for (bool found = freeSegments.getFirst(); found; found = freeSegments.getNext())
{
Segment* const space = &freeSegments.current();
// If this is smaller than our previous best, use it
if (space->size >= size && (!best || (space->size < best->size))) {
best = space;
}
}
// If we didn't find any space, allocate it at the end of the file
if (!best)
{
extend(size);
returngetSize() - size;
}
// Set up the return parameters
constoffset_t position = best->position;
best->size -= size;
best->position += size;
// If the hunk was an exact fit, remove the segment from the list
if (!best->size)
{
if (!freeSegments.locate(best->position))
fb_assert(false);
freeSegments.fastRemove();
}
return position;
}
//
// TempSpace::releaseSpace
//
// Return previously allocated segment back into not used segments list and
// join it with adjacent segments if found
//
voidTempSpace::releaseSpace(offset_t position, FB_SIZE_T size)
{
fb_assert(size > 0);
fb_assert(position < getSize()); // Block starts in file
constoffset_t end = position + size;
fb_assert(end <= getSize()); // Block ends in file
if (freeSegments.locate(locEqual, end))
{
// The next segment is found to be adjacent
Segment* const next_seg = &freeSegments.current();
next_seg->position -= size;
next_seg->size += size;
if (freeSegments.getPrev())
{
// Check the prior segment for being adjacent
Segment* const prior_seg = &freeSegments.current();
if (position == prior_seg->position + prior_seg->size)
{
next_seg->position -= prior_seg->size;
next_seg->size += prior_seg->size;
freeSegments.fastRemove();
}
}
return;
}
if (freeSegments.locate(locLess, position))
{
// Check the prior segment for being adjacent
Segment* const prior_seg = &freeSegments.current();
if (position == prior_seg->position + prior_seg->size)
{
prior_seg->size += size;
return;
}
}
freeSegments.add(Segment(position, size));
}
//
// TempSpace::inMemory
//
// Return contiguous chunk of memory if present at given location
//
UCHAR* TempSpace::inMemory(offset_t begin, size_t size) const
{
const Block* block = findBlock(begin);
return block ? block->inMemory(begin, size) : NULL;
}
//
// TempSpace::findMemory
//
// Return contiguous chunk of memory and adjust starting offset
// of search range if found
//
UCHAR* TempSpace::findMemory(offset_t& begin, offset_t end, size_t size) const
{
offset_t local_offset = begin;
constoffset_t save_begin = begin;
const Block* block = findBlock(local_offset);
while (block && (begin + size <= end))
{
UCHAR* const mem = block->inMemory(local_offset, size);
if (mem)
{
return mem;
}
begin += block->size - local_offset;
local_offset = 0;
block = block->next;
}
begin = save_begin;
returnNULL;
}
//
// TempSpace::validate
//
// Validate internal lists for consistency and return back to caller
// amount of available free space
//
boolTempSpace::validate(offset_t& free) const
{
free = 0;
FreeSegmentTree::ConstAccessor accessor(&freeSegments);
for (bool found = accessor.getFirst(); found; found = accessor.getNext())
{
constoffset_t size = accessor.current().size;
fb_assert(size != 0);
free += size;
}
offset_t disk = 0;
for (FB_SIZE_T i = 0; i < tempFiles.getCount(); i++)
disk += tempFiles[i]->getSize();
return ((initialBuffer.getCount() + localCacheUsage + disk) == physicalSize);
}
//
// TempSpace::allocateBatch
//
// Allocate up to 'count' contiguous chunks of memory available in free
// segments if any. Adjust size of chunks between minSize and maxSize
// accordingly to available free space (assuming all of the free space
// is in memory blocks). Algorithm is very simple and can be improved in future
//
ULONG TempSpace::allocateBatch(ULONG count, FB_SIZE_T minSize, FB_SIZE_T maxSize, Segments& segments)
{
// adjust passed chunk size to amount of free memory we have and number
// of runs still not allocated.
offset_t freeMem = 0;
for (bool found = freeSegments.getFirst(); found; found = freeSegments.getNext())
freeMem += freeSegments.current().size;
freeMem = MIN(freeMem / count, maxSize);
freeMem = MAX(freeMem, minSize);
freeMem = MIN(freeMem, minBlockSize);
freeMem &= ~(FB_ALIGNMENT - 1);
bool is_positioned = freeSegments.getFirst();
while (segments.getCount() < count && is_positioned)
{
Segment* freeSpace = &freeSegments.current();
offset_t freeSeek = freeSpace->position;
constoffset_t freeEnd = freeSpace->position + freeSpace->size;
UCHAR* const mem = findMemory(freeSeek, freeEnd, freeMem);
if (mem)
{
fb_assert(freeSeek + freeMem <= freeEnd);
#ifdef DEV_BUILD
offset_t seek1 = freeSeek;
UCHAR* const p = findMemory(seek1, freeEnd, freeMem);
fb_assert(p == mem);
fb_assert(seek1 == freeSeek);
#endif
if (freeSeek != freeSpace->position)
{
constoffset_t skip_size = freeSeek - freeSpace->position;
const Segment skip_space(freeSpace->position, skip_size);
freeSpace->position += skip_size;
freeSpace->size -= skip_size;
fb_assert(freeSpace->size != 0);
if (!freeSegments.add(skip_space))
fb_assert(false);
if (!freeSegments.locate(skip_space.position + skip_size))
fb_assert(false);
freeSpace = &freeSegments.current();
}
SegmentInMemory seg;
seg.memory = mem;
seg.position = freeSeek;
seg.size = freeMem;
segments.add(seg);
freeSpace->position += freeMem;
freeSpace->size -= freeMem;
if (!freeSpace->size)
{
is_positioned = freeSegments.fastRemove();
}
}
else
{
is_positioned = freeSegments.getNext();
}
}
return segments.getCount();
}