-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BDN that includes embedded network stack (#889)
* Initial checkin on BDN that includes network path - handles non-TLS only * cleanup * ensure dispose * cleanup * merge from main * Update ci-bdnbenchmark.yml
- Loading branch information
Showing
21 changed files
with
502 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
benchmark/BDN.benchmark/Embedded/EmbeddedNetworkHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Garnet.common; | ||
using Garnet.networking; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Embedded.server | ||
{ | ||
internal class EmbeddedNetworkHandler : NetworkHandler<GarnetServerEmbedded, EmbeddedNetworkSender> | ||
{ | ||
public EmbeddedNetworkHandler(GarnetServerEmbedded serverHook, EmbeddedNetworkSender networkSender, NetworkBufferSettings networkBufferSettings, LimitedFixedBufferPool networkPool, bool useTLS, IMessageConsumer messageConsumer = null, ILogger logger = null) : base(serverHook, networkSender, networkBufferSettings, networkPool, useTLS, messageConsumer, logger) | ||
{ | ||
} | ||
|
||
public override string RemoteEndpointName => throw new NotImplementedException(); | ||
public override string LocalEndpointName => throw new NotImplementedException(); | ||
public override void Dispose() | ||
{ | ||
DisposeImpl(); | ||
} | ||
|
||
public override bool TryClose() => throw new NotImplementedException(); | ||
|
||
public unsafe void Send(byte[] buffer, byte* bufferPtr, int length) | ||
{ | ||
networkReceiveBuffer = buffer; | ||
networkReceiveBufferPtr = bufferPtr; | ||
OnNetworkReceive(length); | ||
|
||
// We should have consumed the entire buffer | ||
Debug.Assert(networkBytesRead == 0); | ||
Debug.Assert(networkReadHead == 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
benchmark/BDN.benchmark/Embedded/GarnetServerEmbedded.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Net.Security; | ||
using System.Threading; | ||
using Garnet.common; | ||
using Garnet.networking; | ||
using Garnet.server; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Embedded.server | ||
{ | ||
internal class GarnetServerEmbedded : GarnetServerBase, IServerHook | ||
{ | ||
public GarnetServerEmbedded() : base("0.0.0.0", 0, 1 << 10) | ||
{ | ||
} | ||
|
||
public EmbeddedNetworkHandler CreateNetworkHandler(SslClientAuthenticationOptions tlsOptions = null, string remoteEndpointName = null) | ||
{ | ||
var networkSender = new EmbeddedNetworkSender(); | ||
var networkSettings = new NetworkBufferSettings(); | ||
var networkPool = networkSettings.CreateBufferPool(); | ||
EmbeddedNetworkHandler handler = null; | ||
|
||
if (activeHandlerCount >= 0) | ||
{ | ||
var currentActiveHandlerCount = Interlocked.Increment(ref activeHandlerCount); | ||
if (currentActiveHandlerCount > 0) | ||
{ | ||
try | ||
{ | ||
handler = new EmbeddedNetworkHandler(this, networkSender, networkSettings, networkPool, tlsOptions != null); | ||
if (!activeHandlers.TryAdd(handler, default)) | ||
throw new Exception("Unable to add handler to dictionary"); | ||
|
||
handler.Start(tlsOptions, remoteEndpointName); | ||
incr_conn_recv(); | ||
return handler; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.LogError(ex, "Error starting network handler"); | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
handler?.Dispose(); | ||
} | ||
} | ||
else | ||
{ | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
} | ||
} | ||
return handler; | ||
} | ||
|
||
public void DisposeMessageConsumer(INetworkHandler session) | ||
{ | ||
if (activeHandlers.TryRemove(session, out _)) | ||
{ | ||
Interlocked.Decrement(ref activeHandlerCount); | ||
incr_conn_disp(); | ||
try | ||
{ | ||
session.Session?.Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger?.LogError(ex, "Error disposing RespServerSession"); | ||
} | ||
} | ||
} | ||
|
||
public override void Start() | ||
{ | ||
} | ||
|
||
public bool TryCreateMessageConsumer(Span<byte> bytes, INetworkSender networkSender, out IMessageConsumer session) | ||
{ | ||
session = null; | ||
|
||
// We need at least 4 bytes to determine session | ||
if (bytes.Length < 4) | ||
return false; | ||
|
||
WireFormat protocol = WireFormat.ASCII; | ||
|
||
if (!GetSessionProviders().TryGetValue(protocol, out var provider)) | ||
{ | ||
var input = System.Text.Encoding.ASCII.GetString(bytes); | ||
logger?.LogError("Cannot identify wire protocol {bytes}", input); | ||
throw new Exception($"Unsupported incoming wire format {protocol} {input}"); | ||
} | ||
|
||
if (!AddSession(protocol, ref provider, networkSender, out session)) | ||
throw new Exception($"Unable to add session"); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace BDN.benchmark.Network | ||
{ | ||
/// <summary> | ||
/// Benchmark for BasicOperations | ||
/// </summary> | ||
[MemoryDiagnoser] | ||
public unsafe class BasicOperations : NetworkBase | ||
{ | ||
static ReadOnlySpan<byte> INLINE_PING => "PING\r\n"u8; | ||
byte[] pingRequestBuffer; | ||
byte* pingRequestBufferPointer; | ||
|
||
public override void GlobalSetup() | ||
{ | ||
base.GlobalSetup(); | ||
SetupOperation(ref pingRequestBuffer, ref pingRequestBufferPointer, INLINE_PING); | ||
} | ||
|
||
[Benchmark] | ||
public void InlinePing() | ||
{ | ||
Send(pingRequestBuffer, pingRequestBufferPointer, pingRequestBuffer.Length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using BenchmarkDotNet.Attributes; | ||
using Embedded.server; | ||
using Garnet.server; | ||
|
||
namespace BDN.benchmark.Network | ||
{ | ||
/// <summary> | ||
/// Base class for network benchmarks | ||
/// </summary> | ||
public abstract unsafe class NetworkBase | ||
{ | ||
/// <summary> | ||
/// Parameters | ||
/// </summary> | ||
[ParamsSource(nameof(NetworkParamsProvider))] | ||
public NetworkParams Params { get; set; } | ||
|
||
/// <summary> | ||
/// Operation parameters provider | ||
/// </summary> | ||
public IEnumerable<NetworkParams> NetworkParamsProvider() | ||
{ | ||
yield return new(false); | ||
} | ||
|
||
/// <summary> | ||
/// Batch size per method invocation | ||
/// With a batchSize of 100, we have a convenient conversion of latency to throughput: | ||
/// 5 us = 20 Mops/sec | ||
/// 10 us = 10 Mops/sec | ||
/// 20 us = 5 Mops/sec | ||
/// 25 us = 4 Mops/sec | ||
/// 100 us = 1 Mops/sec | ||
/// </summary> | ||
const int batchSize = 100; | ||
EmbeddedRespServer server; | ||
EmbeddedNetworkHandler networkHandler; | ||
|
||
/// <summary> | ||
/// Setup | ||
/// </summary> | ||
[GlobalSetup] | ||
public virtual void GlobalSetup() | ||
{ | ||
var opts = new GarnetServerOptions | ||
{ | ||
QuietMode = true, | ||
DisablePubSub = true, | ||
}; | ||
|
||
server = new EmbeddedRespServer(opts, null, new GarnetServerEmbedded()); | ||
networkHandler = server.GetNetworkHandler(); | ||
|
||
// Send a PING message to warm up the session | ||
SlowConsumeMessage("PING\r\n"u8); | ||
} | ||
|
||
/// <summary> | ||
/// Cleanup | ||
/// </summary> | ||
[GlobalCleanup] | ||
public virtual void GlobalCleanup() | ||
{ | ||
networkHandler.Dispose(); | ||
server.Dispose(); | ||
} | ||
|
||
protected void Send(byte[] requestBuffer, byte* requestBufferPointer, int length) | ||
{ | ||
networkHandler.Send(requestBuffer, requestBufferPointer, length); | ||
} | ||
|
||
protected void SetupOperation(ref byte[] requestBuffer, ref byte* requestBufferPointer, ReadOnlySpan<byte> operation) | ||
{ | ||
requestBuffer = GC.AllocateArray<byte>(operation.Length * batchSize, pinned: true); | ||
requestBufferPointer = (byte*)Unsafe.AsPointer(ref requestBuffer[0]); | ||
for (int i = 0; i < batchSize; i++) | ||
operation.CopyTo(new Span<byte>(requestBuffer).Slice(i * operation.Length)); | ||
} | ||
|
||
protected void SlowConsumeMessage(ReadOnlySpan<byte> message) | ||
{ | ||
var buffer = GC.AllocateArray<byte>(message.Length, pinned: true); | ||
var bufferPointer = (byte*)Unsafe.AsPointer(ref buffer[0]); | ||
message.CopyTo(new Span<byte>(buffer)); | ||
Send(buffer, bufferPointer, buffer.Length); | ||
} | ||
} | ||
} |
Oops, something went wrong.