forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryHintMan.cs
More file actions
290 lines (251 loc) · 13.3 KB
/
MemoryHintMan.cs
File metadata and controls
290 lines (251 loc) · 13.3 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.IO;
using DotNetty.Buffers;
using Nethermind.Api;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core.Extensions;
using Nethermind.Core.Memory;
using Nethermind.Db.Rocks.Config;
using Nethermind.Init.Steps;
using Nethermind.Logging;
using Nethermind.Network.Config;
using Nethermind.Serialization.Rlp;
using Nethermind.TxPool;
namespace Nethermind.Init
{
/// <summary>
/// Applies changes to the NetworkConfig and the DbConfig so to adhere to the max memory limit hint.
/// </summary>
public class MemoryHintMan
{
private readonly ILogger _logger;
private readonly MallocHelper _mallocHelper;
public MemoryHintMan(ILogManager logManager, MallocHelper? mallocHelper = null)
{
_mallocHelper = mallocHelper ?? MallocHelper.Instance;
_logger = logManager?.GetClassLogger<MemoryHintMan>()
?? throw new ArgumentNullException(nameof(logManager));
}
public void SetMemoryAllowances(
IDbConfig dbConfig,
IInitConfig initConfig,
INetworkConfig networkConfig,
ISyncConfig syncConfig,
ITxPoolConfig txPoolConfig,
uint cpuCount)
{
TotalMemory = initConfig.MemoryHint ?? 1.GB();
ValidateCpuCount(cpuCount);
checked
{
SetupMallocOpts(initConfig);
if (_logger.IsInfo) _logger.Info("Setting up memory allowances");
if (_logger.IsInfo) _logger.Info($" Memory hint: {TotalMemory / 1000 / 1000,5} MB");
_remainingMemory = initConfig.MemoryHint ?? 1.GB();
_remainingMemory -= GeneralMemory;
if (_logger.IsInfo) _logger.Info($" General memory: {GeneralMemory / 1000 / 1000,5} MB");
AssignPeersMemory(networkConfig);
_remainingMemory -= PeersMemory;
if (_logger.IsInfo) _logger.Info($" Peers memory: {PeersMemory / 1000 / 1000,5} MB");
AssignNettyMemory(networkConfig, cpuCount);
_remainingMemory -= NettyMemory;
if (_logger.IsInfo) _logger.Info($" Netty memory: {NettyMemory / 1000 / 1000,5} MB");
AssignTxPoolMemory(txPoolConfig);
_remainingMemory -= TxPoolMemory;
if (_logger.IsInfo) _logger.Info($" Mempool memory: {TxPoolMemory / 1000 / 1000,5} MB");
AssignFastBlocksMemory(syncConfig);
_remainingMemory -= FastBlocksMemory;
if (_logger.IsInfo) _logger.Info($" Fast blocks memory: {FastBlocksMemory / 1000 / 1000,5} MB");
AssignTrieCacheMemory(dbConfig);
_remainingMemory -= TrieCacheMemory;
if (_logger.IsInfo) _logger.Info($" Trie memory: {TrieCacheMemory / 1000 / 1000,5} MB");
UpdateDbConfig(dbConfig, initConfig);
_remainingMemory -= DbMemory;
if (_logger.IsInfo) _logger.Info($" DB memory: {DbMemory / 1000 / 1000,5} MB");
}
}
private void SetupMallocOpts(IInitConfig initConfig)
{
if (initConfig.DisableMallocOpts) return;
if (_logger.IsDebug) _logger.Debug("Setting malloc parameters..");
// The MMAP threshold is the minimum size of allocation before glibc uses mmap to allocate the memory
// instead of sbrk. This means the whole allocation can be released on its own without incurring fragmentation
// but its not reusable and incur a system call. It turns out by default this value is dynamically adjusted
// from 128KB up to 32MB in size on 64bit machine, so most of the memory reduction is due to just disabling
// this auto adjustment.
// Setting this essentially reduces the maximum size of a `hole` in the heap, but it causes extra system call.
// On 16C/32T machine, this reduces memory usage by about 7GB.
// There aren't much difference between 16KB to 64KB, but the system cpu time increase slightly as threshold
// lowers. 4k significantly increase cpu system time.
bool success = MallocHelper.Instance.MallOpt(MallocHelper.Option.M_MMAP_THRESHOLD, (int)64.KiB());
if (!success && _logger.IsDebug) _logger.Debug("Unable to set M_MAP_THRESHOLD");
}
private long _remainingMemory;
public long TotalMemory = 1024 * 1024 * 1024;
public long GeneralMemory { get; } = 32.MB();
public long FastBlocksMemory { get; private set; }
public long DbMemory { get; private set; }
public long NettyMemory { get; private set; }
public long TxPoolMemory { get; private set; }
public long PeersMemory { get; private set; }
public long TrieCacheMemory { get; private set; }
private void AssignTrieCacheMemory(IDbConfig dbConfig)
{
TrieCacheMemory = (long)(0.2 * _remainingMemory);
dbConfig.StateDbRowCacheSize = (ulong)TrieCacheMemory;
}
private void AssignPeersMemory(INetworkConfig networkConfig)
{
PeersMemory = networkConfig.MaxActivePeers.MB();
if (PeersMemory > _remainingMemory * 0.75)
{
throw new InvalidDataException(
$"Memory hint is not enough to satisfy the {nameof(NetworkConfig)}.{nameof(INetworkConfig.MaxActivePeers)}. " +
$"Assign at least MaxActivePeers * ~1MB * ~1.25 of memory.");
}
}
private void AssignTxPoolMemory(ITxPoolConfig txPoolConfig)
{
long hashCacheMemory = txPoolConfig.Size / 1024L * 128L;
if ((_remainingMemory * 0.05) < hashCacheMemory)
{
hashCacheMemory = Math.Min((long)(_remainingMemory * 0.05), hashCacheMemory);
}
MemoryAllowance.TxHashCacheSize = (int)(hashCacheMemory / 128);
hashCacheMemory = MemoryAllowance.TxHashCacheSize * 128;
long txPoolMemory = txPoolConfig.Size * 40.KB() + hashCacheMemory;
if (txPoolMemory > _remainingMemory * 0.5)
{
throw new InvalidDataException(
$"Memory hint is not enough to satisfy the {nameof(TxPoolConfig)}.{nameof(TxPoolConfig.Size)}");
}
TxPoolMemory = txPoolMemory;
}
private void AssignFastBlocksMemory(ISyncConfig syncConfig)
{
if (syncConfig.FastSync)
{
if (!syncConfig.DownloadBodiesInFastSync && !syncConfig.DownloadReceiptsInFastSync)
{
FastBlocksMemory = Math.Min(128.MB(), (long)(0.1 * _remainingMemory));
}
else
{
FastBlocksMemory = Math.Min(1.GB(), (long)(0.1 * _remainingMemory));
}
syncConfig.FastHeadersMemoryBudget = (ulong)FastBlocksMemory;
}
}
private void UpdateDbConfig(IDbConfig dbConfig, IInitConfig initConfig)
{
if (initConfig.DiagnosticMode == DiagnosticMode.MemDb)
{
DbMemory = _remainingMemory;
return;
}
if (dbConfig.SkipMemoryHintSetting) return;
DbMemory = _remainingMemory;
dbConfig.SharedBlockCacheSize = (ulong)DbMemory;
}
private struct DbNeeds
{
public DbNeeds(
uint preferredBuffers,
long preferredMinBufferMemory,
long preferredMaxBufferMemory,
long preferredMinMemory,
long preferredMaxMemory,
decimal preferredMemoryPercentage)
{
PreferredBuffers = preferredBuffers;
PreferredMinBufferMemory = preferredMinBufferMemory;
PreferredMaxBufferMemory = preferredMaxBufferMemory;
PreferredMinMemory = preferredMinMemory;
PreferredMaxMemory = preferredMaxMemory;
PreferredMemoryPercentage = preferredMemoryPercentage;
}
public uint PreferredBuffers { get; set; }
public long PreferredMinBufferMemory { get; set; }
public long PreferredMaxBufferMemory { get; set; }
public long PreferredMinMemory { get; set; }
public long PreferredMaxMemory { get; set; }
public decimal PreferredMemoryPercentage { get; set; }
}
private void AssignNettyMemory(INetworkConfig networkConfig, uint cpuCount)
{
ValidateCpuCount(cpuCount);
NettyMemory = Math.Min(256.MB(), (long)(0.2 * _remainingMemory));
uint arenaCount = (uint)Math.Min(cpuCount * 2, networkConfig.MaxNettyArenaCount);
NettyMemoryEstimator.SetPageSize();
long estimate = NettyMemoryEstimator.Estimate(arenaCount, networkConfig.NettyArenaOrder);
/* first of all we assume that the mainnet will be heavier than any other chain on the side */
/* we will leave the arena order as in config if it is set to a non-default value */
if (networkConfig.NettyArenaOrder != INetworkConfig.DefaultNettyArenaOrder)
{
if (_logger.IsInfo)
_logger.Info($"Leaving {nameof(INetworkConfig.NettyArenaOrder)} " +
$"at {networkConfig.NettyArenaOrder} as it is set to non-default.");
}
else
{
int targetNettyArenaOrder = INetworkConfig.MaxNettyArenaOrder;
for (int i = INetworkConfig.MaxNettyArenaOrder; i > 0; i--)
{
estimate = NettyMemoryEstimator.Estimate(arenaCount, i);
long maxAvailableFoNetty = NettyMemory;
if (estimate <= maxAvailableFoNetty)
{
targetNettyArenaOrder = i;
break;
}
}
networkConfig.NettyArenaOrder = Math.Min(11, targetNettyArenaOrder);
}
NettyMemory = estimate;
// Set PooledByteBufferAllocator.Default configuration.
// Set it to a fixed 1 MB, 1 arena. Code should prefer NethermindBuffers.Default instead as it is easier to override.
ConfigureDefaultPooledByteBufferAllocator(8, 1);
NethermindBuffers.Default = NethermindBuffers.CreateAllocator(networkConfig.NettyArenaOrder, arenaCount);
NethermindBuffers.RlpxAllocator = NethermindBuffers.CreateAllocator(networkConfig.NettyArenaOrder, arenaCount);
// 1 MB chunk independent of memory hint as discovery should be fairly small and does not do much.
NethermindBuffers.DiscoveryAllocator = NethermindBuffers.CreateAllocator(8, arenaCount);
}
private void ConfigureDefaultPooledByteBufferAllocator(int arenaOrder, uint arenaCount)
{
// Need to set these early, or otherwise if the allocator is used ahead of these setting, these config
// will not take affect
Environment.SetEnvironmentVariable("io.netty.allocator.maxOrder", arenaOrder.ToString());
// Arena count is capped because if its too high, the memory budget per arena can get too low causing
// a very small chunk size. Any allocation of size higher than a chunk will essentially be unpooled triggering LOH.
// For example, on 16C32T machine, the default arena count is 64. Mainnet with its 383MB budget will cause the chunk size to be 4 MB (lower
// power of two from 5.9 MB).
//
// When a thread first try to allocate from the pooled byte buffer, a threadlocal is created and pick
// one of the many arena, binding the thread to it. So arena count is like sharding.
//
// An arena consist of a list of chunks. Usually only one remain most of the time per arena.
// Multiple allocation will share a chunk as long as there is enough space. If no chunk with enough space
// is available, a new chunk is created, triggering a LOH allocation. There are also a thread level cache,
// so a chunk usually is not immediately freed once buffer allocated to it is released.
//
// Heap arena frees a chunk by just dereferencing, leaving GC to take it later.
// Direct arena holds a pinned `GCHandle` per chunk and calls `GCHandle.Free` to release the chunk.
// We never use any direct arena, but it does not take up memory because of that.
Environment.SetEnvironmentVariable("io.netty.allocator.numHeapArenas", arenaCount.ToString());
Environment.SetEnvironmentVariable("io.netty.allocator.numDirectArenas", arenaCount.ToString());
if (PooledByteBufferAllocator.Default.Metric.HeapArenas().Count != arenaCount)
{
_logger.Warn("unable to set netty pooled byte buffer config");
}
}
private static void ValidateCpuCount(uint cpuCount)
{
if (cpuCount < 1U)
{
throw new ArgumentOutOfRangeException(nameof(cpuCount), "CPU count has to be >= 1.");
}
}
}
}