Fixed: Reduced spurious cpu usage on mono while idle.

pull/3113/head
Taloth Saldono 9 years ago
parent d4bab775df
commit 958153be55

@ -28,9 +28,11 @@ namespace NzbDrone.Core.Instrumentation
public void Register()
{
Rule = new LoggingRule("*", LogLevel.Info, this);
var target = new SlowRunningAsyncTargetWrapper(this) { TimeToSleepBetweenBatches = 500 };
LogManager.Configuration.AddTarget("DbLogger", new AsyncTargetWrapper(this));
Rule = new LoggingRule("*", LogLevel.Info, target);
LogManager.Configuration.AddTarget("DbLogger", target);
LogManager.Configuration.LoggingRules.Add(Rule);
LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded;
LogManager.ReconfigExistingLoggers();

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NLog.Common;
using NLog.Targets;
using NLog.Targets.Wrappers;
namespace NzbDrone.Core.Instrumentation
{
[Target("SlowRunningAsyncTargetWrapper", IsWrapper = true)]
public class SlowRunningAsyncTargetWrapper : AsyncTargetWrapper
{
private int _state; // 0 = idle, 1 = timer active, 2 = timer active + possibly more work
public SlowRunningAsyncTargetWrapper(Target wrappedTarget)
: base(wrappedTarget)
{
}
protected override void StopLazyWriterThread()
{
if (Interlocked.Exchange(ref _state, 0) > 0)
{
base.StopLazyWriterThread();
}
}
protected override void Write(AsyncLogEventInfo logEvent)
{
base.Write(logEvent);
if (Interlocked.Exchange(ref _state, 2) <= 0)
{ // Timer was idle. Starting.
base.StartLazyWriterTimer();
}
}
protected override void StartLazyWriterTimer()
{
// Is executed when the background task has finished processing the queue. (also executed by base.InitializeTarget once)
if (Interlocked.Decrement(ref _state) == 1)
{ // There might be more work. Restart timer.
base.StartLazyWriterTimer();
}
}
}
}

@ -641,6 +641,7 @@
<Compile Include="Instrumentation\LogRepository.cs" />
<Compile Include="Instrumentation\LogService.cs" />
<Compile Include="Instrumentation\ReconfigureLogging.cs" />
<Compile Include="Instrumentation\SlowRunningAsyncTargetWrapper.cs" />
<Compile Include="Jobs\ScheduledTaskRepository.cs" />
<Compile Include="Jobs\ScheduledTask.cs" />
<Compile Include="Jobs\Scheduler.cs" />

Loading…
Cancel
Save