public AllocationContexts IncreaseWeakness(AllocationContexts allocationContexts) { AllocationContexts sleeps = AllocationContexts.None; if ((allocationContexts & AllocationContexts.Headers) == AllocationContexts.Headers) { ResolveWeaknessChecks(ref _headersWeakness, AllocationContexts.Headers, ref sleeps); } if ((allocationContexts & AllocationContexts.Bodies) == AllocationContexts.Bodies) { ResolveWeaknessChecks(ref _bodiesWeakness, AllocationContexts.Bodies, ref sleeps); } if ((allocationContexts & AllocationContexts.Receipts) == AllocationContexts.Receipts) { ResolveWeaknessChecks(ref _receiptsWeakness, AllocationContexts.Receipts, ref sleeps); } if ((allocationContexts & AllocationContexts.State) == AllocationContexts.State) { ResolveWeaknessChecks(ref _stateWeakness, AllocationContexts.State, ref sleeps); } return(sleeps); }
private void WakeUp(AllocationContexts allocationContexts) { SleepingContexts ^= allocationContexts; if ((allocationContexts & AllocationContexts.Headers) == AllocationContexts.Headers) { _headersWeakness = 0; } if ((allocationContexts & AllocationContexts.Bodies) == AllocationContexts.Bodies) { _bodiesWeakness = 0; } if ((allocationContexts & AllocationContexts.Receipts) == AllocationContexts.Receipts) { _receiptsWeakness = 0; } if ((allocationContexts & AllocationContexts.State) == AllocationContexts.State) { _stateWeakness = 0; } SleepingSince.TryRemove(allocationContexts, out _); }
public ulong SkipAllocationContext(ClrSegment seg, ulong address) { if (seg is null) { throw new ArgumentNullException(nameof(seg)); } if (seg.IsLargeObjectSegment) { return(address); } uint minObjSize = (uint)IntPtr.Size * 3; while (AllocationContexts.TryGetValue(address, out ulong nextObj)) { nextObj += Align(minObjSize, seg.IsLargeObjectSegment); if (address >= nextObj || address >= seg.End) { return(0); } // Only if there's data corruption: if (address >= nextObj || address >= seg.End) { return(0); } address = nextObj; } return(address); }
private void ResolveWeaknessChecks(ref int weakness, AllocationContexts singleContext, ref AllocationContexts sleeps) { int level = Interlocked.Increment(ref weakness); if (level >= SleepThreshold) { sleeps |= singleContext; } }
public bool TryAllocate(AllocationContexts contexts) { if (CanBeAllocated(contexts)) { AllocatedContexts |= contexts; return(true); } return(false); }
public AllocationContexts IncreaseWeakness(AllocationContexts allocationContexts) { AllocationContexts sleeps = AllocationContexts.None; foreach (KeyValuePair <AllocationContexts, int> allocationIndex in AllocationIndexes) { if ((allocationContexts & allocationIndex.Key) == allocationIndex.Key) { ResolveWeaknessChecks(ref _weaknesses[allocationIndex.Value], allocationIndex.Key, ref sleeps); } } return(sleeps); }
public void Can_put_to_sleep_by_contexts() { PeerInfo peerInfo = new PeerInfo(Substitute.For <ISyncPeer>()); for (int i = 0; i < PeerInfo.SleepThreshold - 1; i++) { AllocationContexts sleeps = peerInfo.IncreaseWeakness(_contexts); sleeps.Should().Be(AllocationContexts.None); } AllocationContexts sleeps2 = peerInfo.IncreaseWeakness(_contexts); sleeps2.Should().Be(_contexts); }
private void WakeUp(AllocationContexts allocationContexts) { SleepingContexts ^= allocationContexts; foreach (KeyValuePair <AllocationContexts, int> allocationIndex in AllocationIndexes) { if ((allocationContexts & allocationIndex.Key) == allocationIndex.Key) { _weaknesses[allocationIndex.Value] = 0; } } SleepingSince.TryRemove(allocationContexts, out _); }
public void Can_put_to_sleep() { PeerInfo peerInfo = new PeerInfo(Substitute.For <ISyncPeer>()); for (int i = 0; i < PeerInfo.SleepThreshold - 1; i++) { AllocationContexts sleeps = peerInfo.IncreaseWeakness(_contexts); sleeps.Should().Be(AllocationContexts.None); } AllocationContexts sleeps2 = peerInfo.IncreaseWeakness(_contexts); sleeps2.Should().Be(_contexts); peerInfo.PutToSleep(sleeps2, DateTime.MinValue); peerInfo.IsAsleep(_contexts).Should().BeTrue(); }
public void ReportWeakPeer(PeerInfo weakPeer, AllocationContexts allocationContexts) { if (weakPeer == null) { /* it may have just got disconnected and in such case the allocation would be nullified * in such case there is no need to talk about whether the peer is good or bad */ return; } AllocationContexts sleeps = weakPeer.IncreaseWeakness(allocationContexts); if (sleeps != AllocationContexts.None) { weakPeer.PutToSleep(sleeps, DateTime.UtcNow); } }
public void Can_wake_up() { PeerInfo peerInfo = new(Substitute.For <ISyncPeer>()); for (int i = 0; i < PeerInfo.SleepThreshold - 1; i++) { AllocationContexts sleeps = peerInfo.IncreaseWeakness(_contexts); sleeps.Should().Be(AllocationContexts.None); } AllocationContexts sleeps2 = peerInfo.IncreaseWeakness(_contexts); sleeps2.Should().Be(_contexts); peerInfo.PutToSleep(sleeps2, DateTime.MinValue); peerInfo.IsAsleep(_contexts).Should().BeTrue(); peerInfo.TryToWakeUp(DateTime.MinValue, TimeSpan.Zero); peerInfo.IsAsleep(_contexts).Should().BeFalse(); }
public bool SupportsAllocation(string versionString, AllocationContexts contexts) { PeerInfo peerInfo = new(SetupSyncPeer(versionString)); return(peerInfo.CanBeAllocated(contexts)); }
public void ReportWeakPeer(PeerInfo peerInfo, AllocationContexts contexts) { }
public void ReportNoSyncProgress(PeerInfo peerInfo, AllocationContexts contexts) { }
public Task <SyncPeerAllocation> Allocate(IPeerAllocationStrategy peerAllocationStrategy, AllocationContexts contexts, int timeoutMilliseconds = 0) { ISyncPeer syncPeer = Substitute.For <ISyncPeer>(); syncPeer.ClientId.Returns("Nethermind"); syncPeer.TotalDifficulty.Returns(UInt256.One); SyncPeerAllocation allocation = new SyncPeerAllocation(new PeerInfo(syncPeer), contexts); allocation.AllocateBestPeer( Substitute.For <IEnumerable <PeerInfo> >(), Substitute.For <INodeStatsManager>(), Substitute.For <IBlockTree>()); return(Task.FromResult(allocation)); }
public void Free(AllocationContexts contexts) { AllocatedContexts ^= contexts; }
public bool IsAsleep(AllocationContexts contexts) { return((contexts & SleepingContexts) != AllocationContexts.None); }
public bool IsAllocated(AllocationContexts contexts) { return((contexts & AllocatedContexts) != AllocationContexts.None); }
private static string BuildContextString(AllocationContexts contexts) { return($"{((contexts & AllocationContexts.Headers) == AllocationContexts.Headers ? "H" : " ")}{((contexts & AllocationContexts.Bodies) == AllocationContexts.Bodies ? "B" : " ")}{((contexts & AllocationContexts.Receipts) == AllocationContexts.Receipts ? "R" : " ")}{((contexts & AllocationContexts.State) == AllocationContexts.State ? "S" : " ")}"); }
public bool CanBeAllocated(AllocationContexts contexts) { return(!IsAsleep(contexts) && !IsAllocated(contexts)); }
public SyncPeerAllocation(PeerInfo peerInfo, AllocationContexts contexts) : this(new StaticStrategy(peerInfo), contexts) { }
public SyncPeerAllocation(IPeerAllocationStrategy peerAllocationStrategy, AllocationContexts contexts) { _peerAllocationStrategy = peerAllocationStrategy; Contexts = contexts; }
public async Task <SyncPeerAllocation> Allocate(IPeerAllocationStrategy peerAllocationStrategy, AllocationContexts allocationContexts = AllocationContexts.All, int timeoutMilliseconds = 0) { int tryCount = 1; DateTime startTime = DateTime.UtcNow; SyncPeerAllocation allocation = new SyncPeerAllocation(peerAllocationStrategy, allocationContexts); while (true) { lock (_isAllocatedChecks) { allocation.AllocateBestPeer(InitializedPeers.Where(p => p.CanBeAllocated(allocationContexts)), _stats, _blockTree); if (allocation.HasPeer) { if (peerAllocationStrategy.CanBeReplaced) { _replaceableAllocations.TryAdd(allocation, null); } return(allocation); } } bool timeoutReached = timeoutMilliseconds == 0 || (DateTime.UtcNow - startTime).TotalMilliseconds > timeoutMilliseconds; if (timeoutReached) { return(SyncPeerAllocation.FailedAllocation); } int waitTime = 10 * tryCount++; if (!_signals.SafeWaitHandle.IsClosed) { await _signals.WaitOneAsync(waitTime, _refreshLoopCancellation.Token); if (!_signals.SafeWaitHandle.IsClosed) { _signals.Reset(); // without this we have no delay } } } }
public override IEnumerable <MemoryRange> EnumerateAllocationContexts() => AllocationContexts.Select(item => new MemoryRange(item.Key, item.Value));
public PeerInfoTests(AllocationContexts contexts) { _contexts = contexts; }
public void ReportNoSyncProgress(PeerInfo peerInfo, AllocationContexts allocationContexts) { ReportWeakPeer(peerInfo, allocationContexts); }
public void PutToSleep(AllocationContexts contexts, DateTime dateTime) { SleepingContexts |= contexts; SleepingSince[contexts] = dateTime; }