private DbGets GiveItWhatYouCan(DbNeeds dbNeeds, ulong memoryHint, ulong remaining) { uint buffers = dbNeeds.PreferredBuffers; // this is fine for now decimal maxPercentage = Math.Min((decimal)remaining / memoryHint, dbNeeds.PreferredMemoryPercentage); ulong availableMemory = remaining; ulong minBufferMem = buffers * dbNeeds.PreferredMinBufferMemory; ulong minCacheMem = dbNeeds.PreferredMinMemory; ulong maxBufferMem = buffers * dbNeeds.PreferredMaxBufferMemory; ulong minMemory = minBufferMem + minCacheMem; if (minMemory > availableMemory) { throw new ArgumentException($"Memory hint of {memoryHint} is not enough to cover DB requirements."); } ulong maxWantedMemory = Math.Max(minMemory, (ulong)(memoryHint * maxPercentage)); ulong availableDynamic = minMemory >= maxWantedMemory ? 0ul : maxWantedMemory - minMemory; ulong availableForBuffer = (ulong)(availableDynamic * 0.05m); ulong bufferDynamic = Math.Min(maxBufferMem, availableForBuffer); ulong bufferMem = minBufferMem + bufferDynamic; ulong cacheDynamic = availableDynamic - bufferDynamic; ulong cacheMem = Math.Min(dbNeeds.PreferredMaxMemory, minCacheMem + cacheDynamic); Debug.Assert(bufferDynamic + cacheDynamic <= availableDynamic, "dynamic exceeded"); Debug.Assert(bufferMem + cacheMem <= maxWantedMemory, "max wanted exceeded"); Debug.Assert(bufferMem + cacheMem <= availableMemory, "available exceeded"); DbGets dbGets = new DbGets(buffers, bufferMem / buffers, cacheMem); return(dbGets); }
private void UpdateDbConfig(uint cpuCount, ISyncConfig syncConfig, IDbConfig dbConfig, IInitConfig initConfig) { if (initConfig.DiagnosticMode == DiagnosticMode.MemDb) { DbMemory = _remainingMemory; return; } DbMemory = _remainingMemory; long remaining = DbMemory; DbNeeds dbNeeds = GetHeaderNeeds(cpuCount, syncConfig); DbGets dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.HeadersDbWriteBufferNumber = dbGets.Buffers; dbConfig.HeadersDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.HeadersDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetBlocksNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.BlocksDbWriteBufferNumber = dbGets.Buffers; dbConfig.BlocksDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.BlocksDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetBlockInfosNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.BlockInfosDbWriteBufferNumber = dbGets.Buffers; dbConfig.BlockInfosDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.BlockInfosDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetReceiptsNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.ReceiptsDbWriteBufferNumber = dbGets.Buffers; dbConfig.ReceiptsDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.ReceiptsDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetCodeNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.CodeDbWriteBufferNumber = dbGets.Buffers; dbConfig.CodeDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.CodeDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetPendingTxNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.PendingTxsDbWriteBufferNumber = dbGets.Buffers; dbConfig.PendingTxsDbWriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.PendingTxsDbBlockCacheSize = (ulong)dbGets.CacheMem; dbNeeds = GetStateNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, DbMemory, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.WriteBufferNumber = dbGets.Buffers; dbConfig.WriteBufferSize = (ulong)dbGets.SingleBufferMem; dbConfig.BlockCacheSize = (ulong)dbGets.CacheMem; }
/// <param name="memoryHint"></param> /// <param name="cpuCount"></param> /// <param name="syncConfig"></param> /// <param name="dbConfig"></param> public void UpdateDbConfig(ulong memoryHint, uint cpuCount, ISyncConfig syncConfig, IDbConfig dbConfig) { ValidateMemoryHint(memoryHint); ValidateCpuCount(cpuCount); ulong remaining = (ulong)(memoryHint * AvailableForDb); DbNeeds dbNeeds = GetHeaderNeeds(cpuCount, syncConfig); DbGets dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.HeadersDbWriteBufferNumber = dbGets.Buffers; dbConfig.HeadersDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.HeadersDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetBlocksNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.BlocksDbWriteBufferNumber = dbGets.Buffers; dbConfig.BlocksDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.BlocksDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetBlockInfosNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.BlockInfosDbWriteBufferNumber = dbGets.Buffers; dbConfig.BlockInfosDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.BlockInfosDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetReceiptsNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.ReceiptsDbWriteBufferNumber = dbGets.Buffers; dbConfig.ReceiptsDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.ReceiptsDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetCodeNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.CodeDbWriteBufferNumber = dbGets.Buffers; dbConfig.CodeDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.CodeDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetPendingTxNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.PendingTxsDbWriteBufferNumber = dbGets.Buffers; dbConfig.PendingTxsDbWriteBufferSize = dbGets.SingleBufferMem; dbConfig.PendingTxsDbBlockCacheSize = dbGets.CacheMem; dbNeeds = GetStateNeeds(cpuCount, syncConfig); dbGets = GiveItWhatYouCan(dbNeeds, memoryHint, remaining); remaining -= dbGets.CacheMem + dbGets.Buffers * dbGets.SingleBufferMem; dbConfig.WriteBufferNumber = dbGets.Buffers; dbConfig.WriteBufferSize = dbGets.SingleBufferMem; dbConfig.BlockCacheSize = dbGets.CacheMem; }