Skip to content

Commit

Permalink
introduce info bpstats metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
vazois committed Sep 12, 2024
1 parent a9a7f72 commit 9a6bca1
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 27 deletions.
8 changes: 8 additions & 0 deletions libs/cluster/Server/ClusterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ public MetricsItem[] GetGossipStats(bool metricsDisabled)
];
}

public MetricsItem[] GetBufferPoolStats()
{
return [
new("migration_manager", migrationManager.GetBufferPoolStats()),
new("replication_manager", replicationManager.GetBufferPoolStats())
];
}

internal ReplicationLogCheckpointManager GetReplicationLogCheckpointManager(StoreType storeType)
{
Debug.Assert(serverOptions.EnableCluster);
Expand Down
2 changes: 2 additions & 0 deletions libs/cluster/Server/Migration/MigrationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public bool Purge()
return true;
}

public string GetBufferPoolStats() => networkBuffers.GetStats();

/// <summary>
/// Get number of active migrate sessions
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions libs/cluster/Server/Replication/ReplicationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public bool Purge()
return true;
}

public string GetBufferPoolStats() => networkBuffers.GetStats();

void CheckpointVersionShift(bool isMainStore, long oldVersion, long newVersion)
{
if (clusterProvider.clusterManager.CurrentConfig.LocalNodeRole == NodeRole.REPLICA)
Expand Down
4 changes: 4 additions & 0 deletions libs/common/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public static string GetHostName(ILogger? logger = null)

return "";
}

public static string MegaBytes(long size) => (((size - 1) >> 20) + 1).ToString();

public static string KiloBytes(long size) =>(((size - 1) >> 10) + 1).ToString();
}
#pragma warning restore format
}
47 changes: 35 additions & 12 deletions libs/common/Memory/LimitedFixedBufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,57 @@ public void Dispose()
}

/// <summary>
/// Print pool contents
/// Get statistics for this buffer pool
/// </summary>
public void Print()
/// <returns></returns>
public string GetStats()
{
for (int i = 0; i < numLevels; i++)
var stats = $"totalAllocations = {totalAllocations}, " +
$"numLevels = {numLevels}, " +
$"maxEntriesPerLevel = {maxEntriesPerLevel}";

var bufferStats = "";
var totalBufferCount = 0;
for (var i = 0; i < numLevels; i++)
{
if (pool[i] == null) continue;
foreach (var item in pool[i].items)

var count = pool[i].items.Count;

if (count == 0) continue;

totalBufferCount += count;
bufferStats += $"<{count},{Format.KiloBytes(minAllocationSize * (i + 1))}KB>";

// Keep trying Dequeuing until no items left to free
while (pool[i].items.TryDequeue(out var entry))
{
Console.WriteLine(" " + item.entry.Length.ToString());
entry = null;
Interlocked.Decrement(ref pool[i].size);
}
}

if (totalBufferCount > 0)
stats += $", totalBufferCount:{totalBufferCount}[" + bufferStats + "]";

return stats;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
int Position(int v)
{
if (v < minAllocationSize || !BitOperations.IsPow2(v))
return -1;
return GetLevel(minAllocationSize, v);
}

v /= minAllocationSize;

if (v == 1) return 0;
public static int GetLevel(int minAllocationSize, int requestedSize)
{
Debug.Assert(BitOperations.IsPow2(minAllocationSize));
Debug.Assert(BitOperations.IsPow2(requestedSize));
requestedSize /= minAllocationSize;

int level = BitOperations.Log2((uint)v - 1) + 1;
if (level >= numLevels)
return -1;
return level;
return requestedSize == 1 ? 0 : BitOperations.Log2((uint)requestedSize - 1) + 1;
}
}
}
4 changes: 4 additions & 0 deletions libs/common/Metrics/InfoMetricsType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public enum InfoMetricsType : byte
/// Modules info
/// </summary>
MODULES,
/// <summary>
/// Shared buffer pool stats
/// </summary>
BPSTATS,
}

/// <summary>
Expand Down
15 changes: 12 additions & 3 deletions libs/common/NetworkBuffers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public NetworkBuffers Allocate(int maxEntriesPerLevel = 16, ILogger logger = nul
var minSize = Math.Min(sendMinAllocationSize, recvMinAllocationSize);
var maxSize = Math.Max(sendMinAllocationSize, recvMinAllocationSize);

var levels = (maxSize / minSize) + 1;
Debug.Assert(levels > 0);
var levels = LimitedFixedBufferPool.GetLevel(recvMinAllocationSize, sendMinAllocationSize) + 1;
Debug.Assert(levels >= 0);
levels = Math.Max(4, levels);
bufferPool = new LimitedFixedBufferPool(sendMinAllocationSize, maxEntriesPerLevel: maxEntriesPerLevel, numLevels: levels, logger: logger);
bufferPool = new LimitedFixedBufferPool(recvMinAllocationSize, maxEntriesPerLevel: maxEntriesPerLevel, numLevels: levels, logger: logger);
return this;
}

Expand All @@ -77,6 +77,15 @@ public void Purge()
bufferPool?.Purge();
}

/// <summary>
/// Get buffer pool statistics
/// </summary>
/// <returns></returns>
public string GetStats()
{
return bufferPool.GetStats();
}

/// <summary>
/// Dispose associated network buffer pool
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions libs/server/Cluster/IClusterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public interface IClusterProvider : IDisposable
/// </summary>
MetricsItem[] GetReplicationInfo();

/// <summary>
/// Get buffer poolt stats
/// </summary>
/// <returns></returns>
MetricsItem[] GetBufferPoolStats();

/// <summary>
/// Is replica
/// </summary>
Expand Down
27 changes: 21 additions & 6 deletions libs/server/Metrics/Info/GarnetInfoMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GarnetInfoMetrics
MetricsItem[] persistenceInfo = null;
MetricsItem[] clientsInfo = null;
MetricsItem[] keyspaceInfo = null;
MetricsItem[] bufferPoolStats = null;

public GarnetInfoMetrics() { }

Expand Down Expand Up @@ -224,15 +225,15 @@ private void PopulateObjectStoreStats(StoreWrapper storeWrapper)
];
}

public void PopulateStoreHashDistribution(StoreWrapper storeWrapper) => storeHashDistrInfo = [new("", storeWrapper.store.DumpDistribution())];
private void PopulateStoreHashDistribution(StoreWrapper storeWrapper) => storeHashDistrInfo = [new("", storeWrapper.store.DumpDistribution())];

public void PopulateObjectStoreHashDistribution(StoreWrapper storeWrapper) => objectStoreHashDistrInfo = [new("", storeWrapper.objectStore.DumpDistribution())];
private void PopulateObjectStoreHashDistribution(StoreWrapper storeWrapper) => objectStoreHashDistrInfo = [new("", storeWrapper.objectStore.DumpDistribution())];

public void PopulateStoreRevivInfo(StoreWrapper storeWrapper) => storeRevivInfo = [new("", storeWrapper.store.DumpRevivificationStats())];
private void PopulateStoreRevivInfo(StoreWrapper storeWrapper) => storeRevivInfo = [new("", storeWrapper.store.DumpRevivificationStats())];

public void PopulateObjectStoreRevivInfo(StoreWrapper storeWrapper) => objectStoreRevivInfo = [new("", storeWrapper.objectStore.DumpRevivificationStats())];
private void PopulateObjectStoreRevivInfo(StoreWrapper storeWrapper) => objectStoreRevivInfo = [new("", storeWrapper.objectStore.DumpRevivificationStats())];

public void PopulatePersistenceInfo(StoreWrapper storeWrapper)
private void PopulatePersistenceInfo(StoreWrapper storeWrapper)
{
bool aofEnabled = storeWrapper.serverOptions.EnableAOF;
persistenceInfo =
Expand All @@ -258,6 +259,14 @@ private void PopulateKeyspaceInfo(StoreWrapper storeWrapper)
keyspaceInfo = null;
}

private void PopulateClusterBufferPoolStats(StoreWrapper storeWrapper)
{
if (storeWrapper.clusterProvider != null)
{
bufferPoolStats = storeWrapper.clusterProvider.GetBufferPoolStats();
}
}

public static string GetSectionHeader(InfoMetricsType infoType)
{
return infoType switch
Expand All @@ -277,6 +286,7 @@ public static string GetSectionHeader(InfoMetricsType infoType)
InfoMetricsType.CLIENTS => "Clients",
InfoMetricsType.KEYSPACE => "Keyspace",
InfoMetricsType.MODULES => "Modules",
InfoMetricsType.BPSTATS => "BufferPool Stats",
_ => "Default",
};
}
Expand Down Expand Up @@ -353,6 +363,9 @@ public string GetRespInfo(InfoMetricsType section, StoreWrapper storeWrapper)
return GetSectionRespInfo(InfoMetricsType.KEYSPACE, keyspaceInfo);
case InfoMetricsType.MODULES:
return GetSectionRespInfo(section, null);
case InfoMetricsType.BPSTATS:
PopulateClusterBufferPoolStats(storeWrapper);
return GetSectionRespInfo(InfoMetricsType.BPSTATS, bufferPoolStats);
default:
return "";
}
Expand All @@ -364,7 +377,9 @@ public string GetRespInfo(InfoMetricsType[] sections, StoreWrapper storeWrapper)
for (var i = 0; i < sections.Length; i++)
{
var section = sections[i];
response += GetRespInfo(section, storeWrapper);
var resp = GetRespInfo(section, storeWrapper);
if (string.IsNullOrEmpty(resp)) continue;
response += resp;
response += sections.Length - 1 == i ? "" : "\r\n";
}
return response;
Expand Down
19 changes: 13 additions & 6 deletions libs/server/Metrics/Info/InfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Garnet.common;

namespace Garnet.server
Expand Down Expand Up @@ -70,11 +69,19 @@ private bool NetworkINFO()
}
else
{
InfoMetricsType[] sectionsArr = sections == null ? GarnetInfoMetrics.defaultInfo : [.. sections];
GarnetInfoMetrics garnetInfo = new();
string info = garnetInfo.GetRespInfo(sectionsArr, storeWrapper);
while (!RespWriteUtils.WriteAsciiBulkString(info, ref dcurr, dend))
SendAndReset();
var sectionsArr = sections == null ? GarnetInfoMetrics.defaultInfo : [.. sections];
var garnetInfo = new GarnetInfoMetrics();
var info = garnetInfo.GetRespInfo(sectionsArr, storeWrapper);
if (!string.IsNullOrEmpty(info))
{
while (!RespWriteUtils.WriteAsciiBulkString(info, ref dcurr, dend))
SendAndReset();
}
else
{
while (!RespWriteUtils.WriteDirect(CmdStrings.RESP_ERRNOTFOUND, ref dcurr, dend))
SendAndReset();
}
}
return true;

Expand Down

0 comments on commit 9a6bca1

Please sign in to comment.