Skip to content

Commit bba89aa

Browse files
committed
Pre-allocate block and undo files in chunks
Introduce a AllocateFileRange() function in util, which wipes or at least allocates a given range of a file. It can be overriden by more efficient OS-dependent versions if necessary. Block and undo files are now allocated in chunks of 16 and 1 MiB, respectively.
1 parent 5382bcf commit bba89aa

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

src/main.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,17 @@ bool FindBlockPos(CTxDB &txdb, CDiskBlockPos &pos, unsigned int nAddSize, unsign
18441844
infoLastBlockFile.nSize += nAddSize;
18451845
infoLastBlockFile.AddBlock(nHeight, nTime);
18461846

1847+
unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
1848+
unsigned int nNewChunks = (infoLastBlockFile.nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
1849+
if (nNewChunks > nOldChunks) {
1850+
FILE *file = OpenBlockFile(pos);
1851+
if (file) {
1852+
printf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
1853+
AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
1854+
}
1855+
fclose(file);
1856+
}
1857+
18471858
if (!txdb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
18481859
return error("FindBlockPos() : cannot write updated block info");
18491860
if (fUpdatedLast)
@@ -1858,21 +1869,33 @@ bool FindUndoPos(CTxDB &txdb, int nFile, CDiskBlockPos &pos, unsigned int nAddSi
18581869

18591870
LOCK(cs_LastBlockFile);
18601871

1872+
unsigned int nNewSize;
18611873
if (nFile == nLastBlockFile) {
18621874
pos.nPos = infoLastBlockFile.nUndoSize;
1863-
infoLastBlockFile.nUndoSize += nAddSize;
1875+
nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
18641876
if (!txdb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
18651877
return error("FindUndoPos() : cannot write updated block info");
1866-
return true;
1878+
} else {
1879+
CBlockFileInfo info;
1880+
if (!txdb.ReadBlockFileInfo(nFile, info))
1881+
return error("FindUndoPos() : cannot read block info");
1882+
pos.nPos = info.nUndoSize;
1883+
nNewSize = (info.nUndoSize += nAddSize);
1884+
if (!txdb.WriteBlockFileInfo(nFile, info))
1885+
return error("FindUndoPos() : cannot write updated block info");
1886+
}
1887+
1888+
unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
1889+
unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
1890+
if (nNewChunks > nOldChunks) {
1891+
FILE *file = OpenUndoFile(pos);
1892+
if (file) {
1893+
printf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
1894+
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
1895+
}
1896+
fclose(file);
18671897
}
18681898

1869-
CBlockFileInfo info;
1870-
if (!txdb.ReadBlockFileInfo(nFile, info))
1871-
return error("FindUndoPos() : cannot read block info");
1872-
pos.nPos = info.nUndoSize;
1873-
info.nUndoSize += nAddSize;
1874-
if (!txdb.WriteBlockFileInfo(nFile, info))
1875-
return error("FindUndoPos() : cannot write updated block info");
18761899
return true;
18771900
}
18781901

src/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
2929
static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
3030
static const unsigned int MAX_INV_SZ = 50000;
3131
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
32+
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
33+
static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
3234
static const int64 MIN_TX_FEE = 50000;
3335
static const int64 MIN_RELAY_TX_FEE = 10000;
3436
static const int64 MAX_MONEY = 21000000 * COIN;

src/util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,20 @@ int GetFilesize(FILE* file)
11371137
return nFilesize;
11381138
}
11391139

1140+
// this function tries to make a particular range of a file allocated (corresponding to disk space)
1141+
// it is advisory, and the range specified in the arguments will never contain live data
1142+
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
1143+
static const char buf[65536] = {};
1144+
fseek(file, offset, SEEK_SET);
1145+
while (length > 0) {
1146+
unsigned int now = 65536;
1147+
if (length < now)
1148+
now = length;
1149+
fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
1150+
length -= now;
1151+
}
1152+
}
1153+
11401154
void ShrinkDebugFile()
11411155
{
11421156
// Scroll debug.log if it's getting too big

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ bool WildcardMatch(const char* psz, const char* mask);
196196
bool WildcardMatch(const std::string& str, const std::string& mask);
197197
void FileCommit(FILE *fileout);
198198
int GetFilesize(FILE* file);
199+
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
199200
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
200201
boost::filesystem::path GetDefaultDataDir();
201202
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);

0 commit comments

Comments
 (0)