Skip to content

Commit

Permalink
Pass queue size via message object.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Jan 4, 2025
1 parent b86c619 commit 46e3f47
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
8 changes: 3 additions & 5 deletions ModTek/Features/Logging/AppenderFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ private void Write(string text)
internal static readonly MTStopwatch FiltersStopWatch = new();
internal static readonly MTStopwatch FormatterStopWatch = new();
internal static readonly MTStopwatch WriteStopwatch = new() { SkipFirstNumberOfMeasurements = 0 };
internal void Append(ref MTLoggerMessageDto messageDto, int queueSize)
internal void Append(ref MTLoggerMessageDto messageDto)
{
var hasMore = queueSize > 1;

if (messageDto.FlushToDisk)
{
if (_buffer._length > 0)
Expand All @@ -71,7 +69,7 @@ internal void Append(ref MTLoggerMessageDto messageDto, int queueSize)
measurement.Stop();
if (!included)
{
if (!hasMore && _buffer._length > 0)
if (!messageDto.HasMore && _buffer._length > 0)
{
FlushToOS();
}
Expand All @@ -84,7 +82,7 @@ internal void Append(ref MTLoggerMessageDto messageDto, int queueSize)
_formatter.SerializeMessage(ref messageDto, _buffer);
measurement.Stop();

if (!hasMore || _buffer._length >= _bufferFlushThreshold)
if (!messageDto.HasMore || _buffer._length >= _bufferFlushThreshold)
{
FlushToOS();
}
Expand Down
5 changes: 3 additions & 2 deletions ModTek/Features/Logging/LightWeightBlockingQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ private static int Size(int startIndex, int endIndex)
return (endIndex - startIndex) & FastModuloMaskForBitwiseAnd;
}

internal ref MTLoggerMessageDto AcquireCommittedOrWait(out int queueSize)
internal ref MTLoggerMessageDto AcquireCommittedOrWait()
{
var spinWait = new SpinWait();
while (true)
{
var index = _nextReadIndex;
queueSize = Size(index, _nextWriteIndex);
var queueSize = Size(index, _nextWriteIndex);
if (queueSize > 0)
{
ref var item = ref _ringBuffer[index];
Expand All @@ -56,6 +56,7 @@ internal ref MTLoggerMessageDto AcquireCommittedOrWait(out int queueSize)
{
if (Interlocked.CompareExchange(ref _nextReadIndex, Next(index), index) == index)
{
item.QueueSizeAtTimeOfDequeue = queueSize;
return ref item;
}
else
Expand Down
6 changes: 3 additions & 3 deletions ModTek/Features/Logging/LoggingFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,16 @@ private static DiagnosticsStackTrace GrabStackTrace()
return new DiagnosticsStackTrace(6, false);
}

internal static void LogMessage(ref MTLoggerMessageDto messageDto, int queueSize = 0)
internal static void LogMessage(ref MTLoggerMessageDto messageDto)
{
try
{
_consoleLog?.Append(ref messageDto);

_mainLog.Append(ref messageDto, queueSize);
_mainLog.Append(ref messageDto);
foreach (var logAppender in _logsAppenders)
{
logAppender.Append(ref messageDto, queueSize);
logAppender.Append(ref messageDto);
}
}
finally
Expand Down
4 changes: 2 additions & 2 deletions ModTek/Features/Logging/MTLoggerAsyncQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ private void LoggingLoop()
{
while (true)
{
ref var message = ref _queue.AcquireCommittedOrWait(out var queueSize);
ref var message = ref _queue.AcquireCommittedOrWait();

var measurement = s_loggingStopwatch.StartMeasurement();
try
{
LoggingFeature.LogMessage(ref message, queueSize);
LoggingFeature.LogMessage(ref message);
}
catch (Exception e)
{
Expand Down
2 changes: 2 additions & 0 deletions ModTek/Features/Logging/MTLoggerMessageDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal static void GetTimings(out long stopwatchTimestamp, out DateTime dateTi
}

internal volatile bool CommittedToQueue;
internal int QueueSizeAtTimeOfDequeue;
internal bool HasMore => QueueSizeAtTimeOfDequeue > 1;

// either these are set
internal long Timestamp;
Expand Down

0 comments on commit 46e3f47

Please sign in to comment.