Fixed: Reduced spurious cpu usage on mono while idle.

This commit is contained in:
Taloth Saldono 2016-06-11 00:25:59 +02:00
parent d4bab775df
commit 958153be55
3 changed files with 56 additions and 2 deletions

View File

@ -28,9 +28,11 @@ namespace NzbDrone.Core.Instrumentation
public void Register() 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.Configuration.LoggingRules.Add(Rule);
LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded; LogManager.ConfigurationReloaded += OnLogManagerOnConfigurationReloaded;
LogManager.ReconfigExistingLoggers(); LogManager.ReconfigExistingLoggers();

View File

@ -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();
}
}
}
}

View File

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