Add profiler, improve event scheduler performance

This commit is contained in:
Drew DeVault 2015-09-07 16:07:39 -04:00
parent 1429fce68d
commit 14661c7110
18 changed files with 274 additions and 39 deletions

View File

@ -10,7 +10,7 @@ namespace TrueCraft.API.Server
/// <param name="subject">The subject of the event. If the subject is disposed, the event is cancelled.</param> /// <param name="subject">The subject of the event. If the subject is disposed, the event is cancelled.</param>
/// <param name="when">When to trigger the event.</param> /// <param name="when">When to trigger the event.</param>
/// <param name="action">The event to trigger.</param> /// <param name="action">The event to trigger.</param>
void ScheduleEvent(IEventSubject subject, DateTime when, Action<IMultiplayerServer> action); void ScheduleEvent(IEventSubject subject, TimeSpan when, Action<IMultiplayerServer> action);
/// <summary> /// <summary>
/// Triggers all pending scheduled events whose scheduled time has transpired. /// Triggers all pending scheduled events whose scheduled time has transpired.
/// </summary> /// </summary>

View File

@ -50,7 +50,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent( server.Scheduler.ScheduleEvent(
chunk, DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(30, 60)), chunk, TimeSpan.FromSeconds(MathHelper.Random.Next(30, 60)),
(_server) => GrowBlock(_server, world, coords)); (_server) => GrowBlock(_server, world, coords));
} }
} }
@ -67,14 +67,14 @@ namespace TrueCraft.Core.Logic.Blocks
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{ {
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(30, 60)), user.Server.Scheduler.ScheduleEvent(chunk, TimeSpan.FromSeconds(MathHelper.Random.Next(30, 60)),
(server) => GrowBlock(server, world, descriptor.Coordinates + MathHelper.BlockFaceToCoordinates(face))); (server) => GrowBlock(server, world, descriptor.Coordinates + MathHelper.BlockFaceToCoordinates(face)));
} }
public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world) public override void BlockLoadedFromChunk(Coordinates3D coords, IMultiplayerServer server, IWorld world)
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(30, 60)), server.Scheduler.ScheduleEvent(chunk, TimeSpan.FromSeconds(MathHelper.Random.Next(30, 60)),
(s) => GrowBlock(s, world, coords)); (s) => GrowBlock(s, world, coords));
} }
} }

View File

@ -87,7 +87,7 @@ namespace TrueCraft.Core.Logic.Blocks
} }
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(UpdateIntervalSeconds), TimeSpan.FromSeconds(UpdateIntervalSeconds),
_server => HydrationCheckEvent(_server, coords, world)); _server => HydrationCheckEvent(_server, coords, world));
} }
@ -99,7 +99,7 @@ namespace TrueCraft.Core.Logic.Blocks
} }
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, user.Server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(UpdateIntervalSeconds), TimeSpan.FromSeconds(UpdateIntervalSeconds),
server => HydrationCheckEvent(server, descriptor.Coordinates, world)); server => HydrationCheckEvent(server, descriptor.Coordinates, world));
} }
@ -107,7 +107,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(UpdateIntervalSeconds), TimeSpan.FromSeconds(UpdateIntervalSeconds),
s => HydrationCheckEvent(s, coords, world)); s => HydrationCheckEvent(s, coords, world));
} }
} }

View File

@ -71,7 +71,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates), (_server) => TimeSpan.FromSeconds(SecondsBetweenUpdates), (_server) =>
AutomataUpdate(_server, world, coords)); AutomataUpdate(_server, world, coords));
} }
@ -111,7 +111,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates), (_server) => TimeSpan.FromSeconds(SecondsBetweenUpdates), (_server) =>
AutomataUpdate(_server, world, coords)); AutomataUpdate(_server, world, coords));
} }
} }
@ -166,7 +166,7 @@ namespace TrueCraft.Core.Logic.Blocks
world.SetMetadata(target.TargetBlock, target.Level); world.SetMetadata(target.TargetBlock, target.Level);
var chunk = world.FindChunk(target.TargetBlock); var chunk = world.FindChunk(target.TargetBlock);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates), TimeSpan.FromSeconds(SecondsBetweenUpdates),
s => AutomataUpdate(s, world, target.TargetBlock)); s => AutomataUpdate(s, world, target.TargetBlock));
} }

View File

@ -76,7 +76,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(descriptor.Coordinates, generate: false); var chunk = world.FindChunk(descriptor.Coordinates, generate: false);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinDecayTime, MaxDecayTime)), s => TimeSpan.FromSeconds(MathHelper.Random.Next(MinDecayTime, MaxDecayTime)), s =>
{ {
ScheduledUpdate(world, descriptor.Coordinates); ScheduledUpdate(world, descriptor.Coordinates);
}); });
@ -118,7 +118,7 @@ namespace TrueCraft.Core.Logic.Blocks
} }
world.SetBlockID(candidate, GrassBlock.BlockID); world.SetBlockID(candidate, GrassBlock.BlockID);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)),
s => TrySpread(candidate, world, server)); s => TrySpread(candidate, world, server));
break; break;
} }
@ -129,7 +129,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, user.Server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)),
s => TrySpread(descriptor.Coordinates, world, user.Server)); s => TrySpread(descriptor.Coordinates, world, user.Server));
} }
@ -137,7 +137,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthTime, MaxGrowthTime)),
s => TrySpread(coords, world, server)); s => TrySpread(coords, world, server));
} }
} }

View File

@ -12,6 +12,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
public static readonly int MinGrowthSeconds = 30; public static readonly int MinGrowthSeconds = 30;
public static readonly int MaxGrowthSeconds = 120; public static readonly int MaxGrowthSeconds = 120;
public static readonly int MaxGrowHeight = 3;
public static readonly byte BlockID = 0x53; public static readonly byte BlockID = 0x53;
@ -90,12 +91,12 @@ namespace TrueCraft.Core.Logic.Blocks
return; return;
// Find current height of stalk // Find current height of stalk
int height = 0; int height = 0;
for (int y = -3; y <= 3; y++) for (int y = -MaxGrowHeight; y <= MaxGrowHeight; y++)
{ {
if (world.GetBlockID(coords + (Coordinates3D.Down * y)) == BlockID) if (world.GetBlockID(coords + (Coordinates3D.Down * y)) == BlockID)
height++; height++;
} }
if (height < 3) if (height < MaxGrowHeight)
{ {
var meta = world.GetMetadata(coords); var meta = world.GetMetadata(coords);
meta++; meta++;
@ -103,15 +104,18 @@ namespace TrueCraft.Core.Logic.Blocks
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
if (meta == 15) if (meta == 15)
{ {
world.SetBlockID(coords + Coordinates3D.Up, BlockID); if (world.GetBlockID(coords + Coordinates3D.Up) == 0)
server.Scheduler.ScheduleEvent(chunk, {
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), world.SetBlockID(coords + Coordinates3D.Up, BlockID);
(_server) => TryGrowth(_server, coords + Coordinates3D.Up, world)); server.Scheduler.ScheduleEvent(chunk,
TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(_server) => TryGrowth(_server, coords + Coordinates3D.Up, world));
}
} }
else else
{ {
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(_server) => TryGrowth(_server, coords, world)); (_server) => TryGrowth(_server, coords, world));
} }
} }
@ -121,7 +125,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, user.Server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(server) => TryGrowth(server, descriptor.Coordinates, world)); (server) => TryGrowth(server, descriptor.Coordinates, world));
} }
@ -129,7 +133,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), TimeSpan.FromSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
s => TryGrowth(s, coords, world)); s => TryGrowth(s, coords, world));
} }
} }

View File

@ -0,0 +1,99 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
namespace TrueCraft.Profiling
{
public static class Profiler
{
static Profiler()
{
Stopwatch = new Stopwatch();
EnabledBuckets = new List<string>();
ActiveTimers = new Stack<ActiveTimer>();
Stopwatch.Start();
}
private static Stopwatch Stopwatch { get; set; }
private static List<string> EnabledBuckets { get; set; }
private static Stack<ActiveTimer> ActiveTimers { get; set; }
private struct ActiveTimer
{
public long Started, Finished;
public string Bucket;
}
[Conditional("DEBUG")]
public static void EnableBucket(string bucket)
{
EnabledBuckets.Add(bucket);
}
[Conditional("DEBUG")]
public static void DisableBucket(string bucket)
{
EnabledBuckets.Remove(bucket);
}
[Conditional("DEBUG")]
public static void Start(string bucket)
{
ActiveTimers.Push(new ActiveTimer
{
Started = Stopwatch.ElapsedMilliseconds,
Finished = -1,
Bucket = bucket
});
}
[Conditional("DEBUG")]
public static void Done()
{
if (ActiveTimers.Count > 0)
{
var timer = ActiveTimers.Pop();
timer.Finished = Stopwatch.ElapsedMilliseconds;
for (int i = 0; i < EnabledBuckets.Count; i++)
{
if (Match(EnabledBuckets[i], timer.Bucket))
{
Console.WriteLine("{0} took {1}ms", timer.Bucket, timer.Finished - timer.Started);
break;
}
}
}
}
private static bool Match(string mask, string value)
{
if (value == null)
value = string.Empty;
int i = 0;
int j = 0;
for (; j < value.Length && i < mask.Length; j++)
{
if (mask[i] == '?')
i++;
else if (mask[i] == '*')
{
i++;
if (i >= mask.Length)
return true;
while (++j < value.Length && value[j] != mask[i]) ;
if (j-- == value.Length)
return false;
}
else
{
if (char.ToUpper(mask[i]) != char.ToUpper(value[j]))
return false;
i++;
}
}
return i == mask.Length && j == value.Length;
}
}
}

View File

@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("TrueCraft.Profiling")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("sircmpwn")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>TrueCraft.Profiling</RootNamespace>
<AssemblyName>TrueCraft.Profiling</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Profiler.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{6756B61E-5
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrueCraft.Core.Test", "TrueCraft.Core.Test\TrueCraft.Core.Test.csproj", "{BCFDCD93-C23E-49E6-9767-A887B3C2A709}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrueCraft.Core.Test", "TrueCraft.Core.Test\TrueCraft.Core.Test.csproj", "{BCFDCD93-C23E-49E6-9767-A887B3C2A709}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrueCraft.Profiling", "TrueCraft.Profiling\TrueCraft.Profiling.csproj", "{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -35,6 +37,10 @@ Global
{A6516869-A2FB-4E31-85C8-2285490CB32C}.Debug|Any CPU.Build.0 = Debug|Any CPU {A6516869-A2FB-4E31-85C8-2285490CB32C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6516869-A2FB-4E31-85C8-2285490CB32C}.Release|Any CPU.ActiveCfg = Release|Any CPU {A6516869-A2FB-4E31-85C8-2285490CB32C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6516869-A2FB-4E31-85C8-2285490CB32C}.Release|Any CPU.Build.0 = Release|Any CPU {A6516869-A2FB-4E31-85C8-2285490CB32C}.Release|Any CPU.Build.0 = Release|Any CPU
{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}.Release|Any CPU.Build.0 = Release|Any CPU
{BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Debug|Any CPU.Build.0 = Debug|Any CPU {BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Release|Any CPU.ActiveCfg = Release|Any CPU {BCFDCD93-C23E-49E6-9767-A887B3C2A709}.Release|Any CPU.ActiveCfg = Release|Any CPU

View File

@ -2,6 +2,8 @@
using TrueCraft.API.Server; using TrueCraft.API.Server;
using System.Collections.Generic; using System.Collections.Generic;
using TrueCraft.API; using TrueCraft.API;
using System.Diagnostics;
using TrueCraft.Profiling;
namespace TrueCraft namespace TrueCraft
{ {
@ -11,18 +13,22 @@ namespace TrueCraft
private readonly object EventLock = new object(); private readonly object EventLock = new object();
private IMultiplayerServer Server { get; set; } private IMultiplayerServer Server { get; set; }
private HashSet<IEventSubject> Subjects { get; set; } private HashSet<IEventSubject> Subjects { get; set; }
private Stopwatch Stopwatch { get; set; }
public EventScheduler(IMultiplayerServer server) public EventScheduler(IMultiplayerServer server)
{ {
Events = new List<ScheduledEvent>(); Events = new List<ScheduledEvent>();
Server = server; Server = server;
Subjects = new HashSet<IEventSubject>(); Subjects = new HashSet<IEventSubject>();
Stopwatch = new Stopwatch();
Stopwatch.Start();
} }
public void ScheduleEvent(IEventSubject subject, DateTime when, Action<IMultiplayerServer> action) public void ScheduleEvent(IEventSubject subject, TimeSpan when, Action<IMultiplayerServer> action)
{ {
lock (EventLock) lock (EventLock)
{ {
long _when = Stopwatch.ElapsedTicks + when.Ticks;
if (!Subjects.Contains(subject)) if (!Subjects.Contains(subject))
{ {
Subjects.Add(subject); Subjects.Add(subject);
@ -31,10 +37,15 @@ namespace TrueCraft
int i; int i;
for (i = 0; i < Events.Count; i++) for (i = 0; i < Events.Count; i++)
{ {
if (Events[i].When > when) if (Events[i].When > _when)
break; break;
} }
Events.Insert(i, new ScheduledEvent { Subject = subject, When = when, Action = action }); Events.Insert(i, new ScheduledEvent
{
Subject = subject,
When = _when,
Action = action
});
} }
} }
@ -57,9 +68,10 @@ namespace TrueCraft
public void Update() public void Update()
{ {
Profiler.Start("scheduler");
lock (EventLock) lock (EventLock)
{ {
var start = DateTime.UtcNow; var start = Stopwatch.ElapsedTicks;
for (int i = 0; i < Events.Count; i++) for (int i = 0; i < Events.Count; i++)
{ {
var e = Events[i]; var e = Events[i];
@ -73,11 +85,12 @@ namespace TrueCraft
break; // List is sorted, we can exit early break; // List is sorted, we can exit early
} }
} }
Profiler.Done();
} }
private struct ScheduledEvent private struct ScheduledEvent
{ {
public DateTime When; public long When;
public Action<IMultiplayerServer> Action; public Action<IMultiplayerServer> Action;
public IEventSubject Subject; public IEventSubject Subject;
} }

View File

@ -70,8 +70,8 @@ namespace TrueCraft.Handlers
var entityManager = server.GetEntityManagerForWorld(remoteClient.World); var entityManager = server.GetEntityManagerForWorld(remoteClient.World);
entityManager.SpawnEntity(remoteClient.Entity); entityManager.SpawnEntity(remoteClient.Entity);
entityManager.SendEntitiesToClient(remoteClient); entityManager.SendEntitiesToClient(remoteClient);
server.Scheduler.ScheduleEvent(remoteClient, DateTime.UtcNow.AddSeconds(10), remoteClient.SendKeepAlive); server.Scheduler.ScheduleEvent(remoteClient, TimeSpan.FromSeconds(10), remoteClient.SendKeepAlive);
server.Scheduler.ScheduleEvent(remoteClient, DateTime.UtcNow.AddSeconds(1), remoteClient.ExpandChunkRadius); server.Scheduler.ScheduleEvent(remoteClient, TimeSpan.FromSeconds(1), remoteClient.ExpandChunkRadius);
if (!string.IsNullOrEmpty(Program.ServerConfiguration.MOTD)) if (!string.IsNullOrEmpty(Program.ServerConfiguration.MOTD))
remoteClient.SendMessage(Program.ServerConfiguration.MOTD); remoteClient.SendMessage(Program.ServerConfiguration.MOTD);

View File

@ -20,6 +20,7 @@ using TrueCraft.Core.World;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Diagnostics; using System.Diagnostics;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using TrueCraft.Profiling;
namespace TrueCraft namespace TrueCraft
{ {
@ -43,6 +44,8 @@ namespace TrueCraft
public bool EnableClientLogging { get; set; } public bool EnableClientLogging { get; set; }
public IPEndPoint EndPoint { get; private set; } public IPEndPoint EndPoint { get; private set; }
private static readonly int MillisecondsPerTick = 1000 / 20;
private bool _BlockUpdatesEnabled = true; private bool _BlockUpdatesEnabled = true;
private struct BlockUpdate private struct BlockUpdate
@ -131,7 +134,7 @@ namespace TrueCraft
AcceptClient(this, args); AcceptClient(this, args);
Log(LogCategory.Notice, "Running TrueCraft server on {0}", EndPoint); Log(LogCategory.Notice, "Running TrueCraft server on {0}", EndPoint);
EnvironmentWorker.Change(1000 / 20, 0); EnvironmentWorker.Change(MillisecondsPerTick, 0);
if(Program.ServerConfiguration.Query) if(Program.ServerConfiguration.Query)
QueryProtocol.Start(); QueryProtocol.Start();
} }
@ -367,11 +370,19 @@ namespace TrueCraft
{ {
if (ShuttingDown) if (ShuttingDown)
return; return;
Profiler.Start("environment");
Scheduler.Update(); Scheduler.Update();
Profiler.Start("environment.entities");
foreach (var manager in EntityManagers) foreach (var manager in EntityManagers)
{ {
manager.Update(); manager.Update();
} }
Profiler.Done();
Profiler.Start("environment.lighting");
foreach (var lighter in WorldLighters) foreach (var lighter in WorldLighters)
{ {
int attempts = 500; int attempts = 500;
@ -379,10 +390,17 @@ namespace TrueCraft
{ {
} }
} }
Profiler.Done();
Profiler.Start("environment.chunks");
Tuple<IWorld, IChunk> t; Tuple<IWorld, IChunk> t;
if (ChunksToSchedule.TryTake(out t)) if (ChunksToSchedule.TryTake(out t))
ScheduleUpdatesForChunk(t.Item1, t.Item2); ScheduleUpdatesForChunk(t.Item1, t.Item2);
EnvironmentWorker.Change(1000 / 20, 0); Profiler.Done();
Profiler.Done();
EnvironmentWorker.Change(MillisecondsPerTick, 0);
} }
public bool PlayerIsWhitelisted(string client) public bool PlayerIsWhitelisted(string client)

View File

@ -11,6 +11,7 @@ using TrueCraft.API.World;
using System; using System;
using TrueCraft.API; using TrueCraft.API;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
using TrueCraft.Profiling;
namespace TrueCraft namespace TrueCraft
{ {
@ -33,6 +34,15 @@ namespace TrueCraft
ServerConfiguration = Configuration.LoadConfiguration<ServerConfiguration>("config.yaml"); ServerConfiguration = Configuration.LoadConfiguration<ServerConfiguration>("config.yaml");
var buckets = ServerConfiguration.Debug?.Profiler?.Buckets?.Split(',');
if (buckets != null)
{
foreach (var bucket in buckets)
{
Profiler.EnableBucket(bucket.Trim());
}
}
if (ServerConfiguration.Debug.DeleteWorldOnStartup) if (ServerConfiguration.Debug.DeleteWorldOnStartup)
{ {
if (Directory.Exists("world")) if (Directory.Exists("world"))

View File

@ -392,7 +392,7 @@ namespace TrueCraft
{ {
ChunkRadius++; ChunkRadius++;
UpdateChunks(); UpdateChunks();
server.Scheduler.ScheduleEvent(this, DateTime.UtcNow.AddSeconds(1), ExpandChunkRadius); server.Scheduler.ScheduleEvent(this, TimeSpan.FromSeconds(1), ExpandChunkRadius);
} }
}); });
} }
@ -400,7 +400,7 @@ namespace TrueCraft
internal void SendKeepAlive(IMultiplayerServer server) internal void SendKeepAlive(IMultiplayerServer server)
{ {
QueuePacket(new KeepAlivePacket()); QueuePacket(new KeepAlivePacket());
server.Scheduler.ScheduleEvent(this, DateTime.UtcNow.AddSeconds(1), SendKeepAlive); server.Scheduler.ScheduleEvent(this, TimeSpan.FromSeconds(1), SendKeepAlive);
} }
internal void UpdateChunks() internal void UpdateChunks()

View File

@ -7,6 +7,17 @@ namespace TrueCraft
{ {
public class DebugConfiguration public class DebugConfiguration
{ {
public class ProfilerConfiguration
{
public ProfilerConfiguration()
{
Buckets = "";
}
[YamlMember(Alias = "buckets")]
public string Buckets { get; set; }
}
public DebugConfiguration() public DebugConfiguration()
{ {
DeleteWorldOnStartup = false; DeleteWorldOnStartup = false;
@ -18,6 +29,9 @@ namespace TrueCraft
[YamlMember(Alias = "deletePlayersOnStartup")] [YamlMember(Alias = "deletePlayersOnStartup")]
public bool DeletePlayersOnStartup { get; set; } public bool DeletePlayersOnStartup { get; set; }
[YamlMember(Alias = "profiler")]
public ProfilerConfiguration Profiler { get; set; }
} }
public ServerConfiguration() public ServerConfiguration()

View File

@ -17,6 +17,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<ConsolePause>false</ConsolePause> <ConsolePause>false</ConsolePause>
<DefineConstants>DEBUG;</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize> <Optimize>true</Optimize>
@ -29,7 +30,7 @@
<HintPath>..\lib\Ionic.Zip.Reduced.dll</HintPath> <HintPath>..\lib\Ionic.Zip.Reduced.dll</HintPath>
</Reference> </Reference>
<Reference Include="YamlDotNet"> <Reference Include="YamlDotNet">
<HintPath>..\packages\YamlDotNet.3.6.1\lib\net35\YamlDotNet.dll</HintPath> <HintPath>..\packages\YamlDotNet.3.7.0\lib\net35\YamlDotNet.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -74,11 +75,15 @@
<Project>{4488498D-976D-4DA3-BF72-109531AF0488}</Project> <Project>{4488498D-976D-4DA3-BF72-109531AF0488}</Project>
<Name>fNbt</Name> <Name>fNbt</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> <ProjectReference Include="..\TrueCraft.Profiling\TrueCraft.Profiling.csproj">
<ItemGroup> <Project>{BCA0E139-CF47-43B3-9DC9-D4611C0A2AAD}</Project>
<None Include="packages.config" /> <Name>TrueCraft.Profiling</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Rules\" /> <Folder Include="Rules\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="YamlDotNet" version="3.6.1" targetFramework="net45" /> <package id="YamlDotNet" version="3.7.0" targetFramework="net45" />
</packages> </packages>