From 6db6949d4d34c03ada655d7743f00d34ff2e9926 Mon Sep 17 00:00:00 2001 From: egi Date: Fri, 24 Apr 2020 23:52:51 +0200 Subject: [PATCH 1/4] added skytools benchmarks --- TLM/TLM/Benchmark/BenchmarkManager.cs | 87 +++++++++++++++++++++++++++ TLM/TLM/LoadingExtension.cs | 2 + TLM/TLM/TLM.csproj | 13 +++- TLM/TLM/ThreadingExtension.cs | 2 +- TLM/TLM/TrafficManagerMod.cs | 6 ++ TLM/TLM/packages.config | 3 + 6 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 TLM/TLM/Benchmark/BenchmarkManager.cs diff --git a/TLM/TLM/Benchmark/BenchmarkManager.cs b/TLM/TLM/Benchmark/BenchmarkManager.cs new file mode 100644 index 000000000..720a69b60 --- /dev/null +++ b/TLM/TLM/Benchmark/BenchmarkManager.cs @@ -0,0 +1,87 @@ +// +// Copyright (c) dymanoid. All rights reserved. +// + +namespace TrafficManager.Benchmark { +#if BENCHMARK + using ColossalFramework.IO; + using SkyTools.Benchmarks; + using SkyTools.Tools; + + /// + /// A special class that handles the performance benchmarking. + /// + internal static class BenchmarkManager { + /// + /// Initializes the benchmarking. + /// + public static void Setup() { + var benchmark = new BenchmarkSimulationManager(); + SimulationManager.RegisterSimulationManager(benchmark); + LoadingManager.instance.m_levelUnloaded += benchmark.Stop; + } + + private sealed class BenchmarkSimulationManager : ISimulationManager { + private readonly Benchmark benchmark; + private bool isRunning; + + public BenchmarkSimulationManager() { + benchmark = Benchmark.Create(0x1000 * 0x100); + SetupMethods(); + } + + public void Stop() { + benchmark.Stop(); + benchmark.Dump(); + isRunning = false; + Log.Info("Benchmarking stopped."); + } + + public void EarlyUpdateData() { + } + + public void GetData(FastList data) { + } + + public string GetName() => nameof(BenchmarkSimulationManager); + + public ThreadProfiler GetSimulationProfiler() => null; + + public void LateUpdateData(SimulationManager.UpdateMode mode) { + } + + public void SimulationStep(int subStep) { + if (subStep == 1000 || subStep == 0) { + // This is the 'late update data' phase or the simulation is paused + return; + } else if (!isRunning) { + isRunning = true; + + try { + // On failure, don't try to activate benchmark on each step + benchmark.Start(); + Log.Info("Benchmarking started."); + } + catch { + } + } + + if ((SimulationManager.instance.m_currentFrameIndex & 0xFFF) == 0xFFF) { + benchmark.MakeSnapshot(); + } + } + + public void UpdateData(SimulationManager.UpdateMode mode) { + } + + private void SetupMethods() { + try { + benchmark.BenchmarkMethod(typeof(SimulationManager), "SimulationStep"); + } + catch { + } + } + } + } +#endif +} diff --git a/TLM/TLM/LoadingExtension.cs b/TLM/TLM/LoadingExtension.cs index a54c50d25..1d348de5a 100644 --- a/TLM/TLM/LoadingExtension.cs +++ b/TLM/TLM/LoadingExtension.cs @@ -18,6 +18,8 @@ namespace TrafficManager { using TrafficManager.UI; using static TrafficManager.Util.Shortcuts; using UnityEngine; + using SkyTools.Benchmarks; + using TrafficManager.Custom.AI; [UsedImplicitly] public class LoadingExtension : LoadingExtensionBase { diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 254c3930d..f2a3c5696 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -87,8 +87,8 @@ ..\dependencies - - RedirectionFramework\lib\0TMPE.Harmony.dll + + ..\packages\Lib.Harmony.1.2.0.1\lib\net35\0Harmony.dll $(MangedDLLPath)\Assembly-CSharp.dll @@ -102,6 +102,12 @@ $(MangedDLLPath)\ICities.dll False + + ..\packages\SkyTools.Common.2.1.0\lib\net35\SkyTools.Common.dll + + + ..\packages\SkyTools.Patching.2.1.0\lib\net35\SkyTools.Patching.dll + $(MangedDLLPath)\System.Core.dll @@ -125,6 +131,7 @@ Properties\SharedAssemblyInfo.cs + @@ -675,6 +682,8 @@ xcopy /y "$(TargetDir)CSUtil.CameraControl.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)TMPE.RedirectionFramework.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)CSUtil.Commons.dll" "%25DEPLOYDIR%25" xcopy /y "$(TargetDir)0TMPE.Harmony.dll" "%25DEPLOYDIR%25" +xcopy /y "$(TargetDir)SkyTools.Common.dll" "%25DEPLOYDIR%25" +xcopy /y "$(TargetDir)SkyTools.Patching.dll" "%25DEPLOYDIR%25" rem To avoid double hot reload, TrafficManager.dll must be replaced last and fast. rem Once TrafficManager.dll is re-loaded, all other dlls will be reloaded as well diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index 6a9a267a4..db10afcde 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -127,7 +127,7 @@ public override void OnBeforeSimulationFrame() { public override void OnUpdate(float realTimeDelta, float simulationTimeDelta) { base.OnUpdate(realTimeDelta, simulationTimeDelta); - using (var bm = Benchmark.MaybeCreateBenchmark()) { + using (var bm = CSUtil.Commons.Benchmark.Benchmark.MaybeCreateBenchmark()) { if (ToolsModifierControl.toolController == null || ModUI.Instance == null) { return; } diff --git a/TLM/TLM/TrafficManagerMod.cs b/TLM/TLM/TrafficManagerMod.cs index 513e507dc..1b311ad1a 100644 --- a/TLM/TLM/TrafficManagerMod.cs +++ b/TLM/TLM/TrafficManagerMod.cs @@ -14,6 +14,12 @@ namespace TrafficManager { using UnityEngine.SceneManagement; public class TrafficManagerMod : IUserMod { +#if BENCHMARK + public TrafficManagerMod() { + Benchmark.BenchmarkManager.Setup(); + } +#endif + #if LABS public const string BRANCH = "LABS"; #elif DEBUG diff --git a/TLM/TLM/packages.config b/TLM/TLM/packages.config index 5011e1991..4c1362466 100644 --- a/TLM/TLM/packages.config +++ b/TLM/TLM/packages.config @@ -1,5 +1,8 @@  + + + \ No newline at end of file From bdd0aa882db3e84ef650c426d71a53e98bad0185 Mon Sep 17 00:00:00 2001 From: egi Date: Sat, 23 May 2020 20:01:49 +0200 Subject: [PATCH 2/4] remove old benchmark code --- TLM/CSUtil.Commons/Benchmark/Benchmark.cs | 50 -- .../Benchmark/BenchmarkProfile.cs | 32 -- .../Benchmark/BenchmarkProfileProvider.cs | 91 ---- TLM/CSUtil.Commons/CSUtil.Commons.csproj | 3 - TLM/TLM/Custom/AI/CustomCarAI.cs | 16 +- TLM/TLM/Custom/AI/CustomFireTruckAI.cs | 9 +- TLM/TLM/Custom/AI/CustomHumanAI.cs | 79 ++- TLM/TLM/Custom/AI/CustomPassengerCarAI.cs | 35 +- .../Manager/Impl/AdvancedParkingManager.cs | 7 +- .../Manager/Impl/ExtCitizenInstanceManager.cs | 479 +++++++++--------- .../Impl/TrafficLightSimulationManager.cs | 61 +-- .../Manager/Impl/VehicleBehaviorManager.cs | 331 ++++++------ .../_RoadBaseAI/SegmentSimulationStepPatch.cs | 46 +- .../OptionsTabs/OptionsMaintenanceTab.cs | 1 - TLM/TLM/ThreadingExtension.cs | 25 +- .../Impl/TimedTrafficLightsStep.cs | 5 +- TLM/TLM/UI/MainMenu/DebugMenu.cs | 20 - 17 files changed, 516 insertions(+), 774 deletions(-) delete mode 100644 TLM/CSUtil.Commons/Benchmark/Benchmark.cs delete mode 100644 TLM/CSUtil.Commons/Benchmark/BenchmarkProfile.cs delete mode 100644 TLM/CSUtil.Commons/Benchmark/BenchmarkProfileProvider.cs diff --git a/TLM/CSUtil.Commons/Benchmark/Benchmark.cs b/TLM/CSUtil.Commons/Benchmark/Benchmark.cs deleted file mode 100644 index 80c41f25c..000000000 --- a/TLM/CSUtil.Commons/Benchmark/Benchmark.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace CSUtil.Commons.Benchmark { - using System; - using System.Diagnostics; - using System.Reflection; - - public class Benchmark : IDisposable { - private BenchmarkProfile profile; - -#if !BENCHMARK - /// - /// Does nothing when #define BENCHMARK is not set - /// - public class NullBenchmark : IDisposable { - public void Dispose() { } - } - - private static NullBenchmark reusableNullBenchmark_ = new NullBenchmark(); -#endif - - /// - /// Creates Benchmark object if #define BENCHMARK is set, otherwise creates a NullBenchmark - /// - public static IDisposable MaybeCreateBenchmark(string id = null, string postfix = null) { -#if BENCHMARK - return new Benchmark(id, postfix); -#else - return reusableNullBenchmark_; -#endif - } - - private Benchmark(string id = null, string postfix = null) { - if (id == null) { - StackFrame frame = new StackFrame(1); - MethodBase method = frame.GetMethod(); - id = method.DeclaringType.Name + "#" + method.Name; - } - - if (postfix != null) { - id += "#" + postfix; - } - - profile = BenchmarkProfileProvider.Instance.GetProfile(id); - profile.Start(); - } - - public void Dispose() { - profile.Stop(); - } - } -} \ No newline at end of file diff --git a/TLM/CSUtil.Commons/Benchmark/BenchmarkProfile.cs b/TLM/CSUtil.Commons/Benchmark/BenchmarkProfile.cs deleted file mode 100644 index 539cddaf7..000000000 --- a/TLM/CSUtil.Commons/Benchmark/BenchmarkProfile.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace CSUtil.Commons.Benchmark { - using System; - using System.Diagnostics; - - public class BenchmarkProfile { - private string Id { get; } - - private Stopwatch timer; - - public int NumBenchmarks { get; private set; } - - public BenchmarkProfile(string id) { - Id = id; - timer = new Stopwatch(); - } - - public void Start() { - timer.Start(); - } - - public void Stop() { - if (timer.IsRunning) { - timer.Stop(); - ++NumBenchmarks; - } - } - - public TimeSpan GetElapsedTime() { - return timer.Elapsed; - } - } -} \ No newline at end of file diff --git a/TLM/CSUtil.Commons/Benchmark/BenchmarkProfileProvider.cs b/TLM/CSUtil.Commons/Benchmark/BenchmarkProfileProvider.cs deleted file mode 100644 index bed732db8..000000000 --- a/TLM/CSUtil.Commons/Benchmark/BenchmarkProfileProvider.cs +++ /dev/null @@ -1,91 +0,0 @@ -namespace CSUtil.Commons.Benchmark { - using System.Collections.Generic; - using System.Text; - - public class BenchmarkProfileProvider { - public static readonly BenchmarkProfileProvider Instance = new BenchmarkProfileProvider(); - - private IDictionary Profiles = - new Dictionary(); - - public BenchmarkProfile GetProfile(string id) { - BenchmarkProfile profile; - Profiles.TryGetValue(id, out profile); - - if (profile == null) { - profile = new BenchmarkProfile(id); - Profiles.Add(id, profile); - } - - return profile; - } - - public void ClearProfiles() { - Profiles.Clear(); - } - - public string CreateReport() { - var reportSb = new StringBuilder(); - reportSb.Append("=== BENCHMARK REPORT ===\n"); - reportSb.Append("=== ORDERED BY TOTAL TIME ===\n"); - - var orderedKeys = new List(Profiles.Keys); - orderedKeys.Sort( - (x, y) => { - long xTicks = Profiles[x].GetElapsedTime().Ticks; - long yTicks = Profiles[y].GetElapsedTime().Ticks; - return yTicks.CompareTo(xTicks); - }); - - CreateReport(reportSb, orderedKeys); - reportSb.Append("\n=== ORDERED BY AVG. TIME ===\n"); - - orderedKeys = new List(Profiles.Keys); - orderedKeys.Sort( - (x, y) => { - BenchmarkProfile xProfile = Profiles[x]; - BenchmarkProfile yProfile = Profiles[y]; - if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks <= 0) { - return 0; - } - - if (xProfile.NumBenchmarks > 0 && yProfile.NumBenchmarks <= 0) { - return -1; - } - - if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks > 0) { - return 1; - } - - float xAvg = (float)xProfile.GetElapsedTime().TotalMilliseconds / - xProfile.NumBenchmarks; - float yAvg = (float)yProfile.GetElapsedTime().TotalMilliseconds / - yProfile.NumBenchmarks; - return yAvg.CompareTo(xAvg); - }); - - CreateReport(reportSb, orderedKeys); - return reportSb.ToString(); - } - - /// - /// Adds ordered report lines to the stringbuilder - /// - /// StringBuilder where report lines are added - /// The data - private void CreateReport(StringBuilder reportSb, List orderedKeys) { - foreach (string key in orderedKeys) { - BenchmarkProfile profile = Profiles[key]; - reportSb.AppendFormat( - "\t{0}: {1} ({2} benchmarks, avg. {3})\n", - key, - profile.GetElapsedTime(), - profile.NumBenchmarks, - profile.NumBenchmarks <= 0 - ? 0f - : (float)profile.GetElapsedTime().TotalMilliseconds / - (float)profile.NumBenchmarks); - } - } - } -} \ No newline at end of file diff --git a/TLM/CSUtil.Commons/CSUtil.Commons.csproj b/TLM/CSUtil.Commons/CSUtil.Commons.csproj index d791a65b3..a0d1331eb 100644 --- a/TLM/CSUtil.Commons/CSUtil.Commons.csproj +++ b/TLM/CSUtil.Commons/CSUtil.Commons.csproj @@ -75,11 +75,8 @@ - - - diff --git a/TLM/TLM/Custom/AI/CustomCarAI.cs b/TLM/TLM/Custom/AI/CustomCarAI.cs index 195a72ff2..67431aa6b 100644 --- a/TLM/TLM/Custom/AI/CustomCarAI.cs +++ b/TLM/TLM/Custom/AI/CustomCarAI.cs @@ -3,7 +3,6 @@ namespace TrafficManager.Custom.AI { using ColossalFramework.Math; using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using JetBrains.Annotations; using System.Diagnostics.CodeAnalysis; @@ -469,15 +468,12 @@ public bool CustomStartPathFind(ushort vehicleId, ExtVehicleType vehicleType; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "OnStartPathFind")) { - vehicleType = - ExtVehicleManager.Instance.OnStartPathFind(vehicleId, ref vehicleData, null); - if (vehicleType == ExtVehicleType.None) { - Log._DebugOnlyWarning( - $"CustomCarAI.CustomStartPathFind({vehicleId}): Vehicle {vehicleId} " + - "does not have a valid vehicle type!"); - vehicleType = ExtVehicleType.RoadVehicle; - } + vehicleType = ExtVehicleManager.Instance.OnStartPathFind(vehicleId, ref vehicleData, null); + if (vehicleType == ExtVehicleType.None) { + Log._DebugOnlyWarning( + $"CustomCarAI.CustomStartPathFind({vehicleId}): Vehicle {vehicleId} " + + "does not have a valid vehicle type!"); + vehicleType = ExtVehicleType.RoadVehicle; } VehicleInfo info = m_info; diff --git a/TLM/TLM/Custom/AI/CustomFireTruckAI.cs b/TLM/TLM/Custom/AI/CustomFireTruckAI.cs index 9d6a97b16..8b4d05cd0 100644 --- a/TLM/TLM/Custom/AI/CustomFireTruckAI.cs +++ b/TLM/TLM/Custom/AI/CustomFireTruckAI.cs @@ -1,6 +1,5 @@ -namespace TrafficManager.Custom.AI { +namespace TrafficManager.Custom.AI { using ColossalFramework; - using CSUtil.Commons.Benchmark; using JetBrains.Annotations; using TrafficManager.API.Traffic.Data; using TrafficManager.API.Traffic.Enums; @@ -21,16 +20,12 @@ public bool CustomStartPathFind(ushort vehicleId, bool endBothWays, bool undergroundTarget) { - ExtVehicleType vehicleType; - - using (var bm = Benchmark.MaybeCreateBenchmark(null, "OnStartPathFind")) { - vehicleType = ExtVehicleManager.Instance.OnStartPathFind( + ExtVehicleType vehicleType = ExtVehicleManager.Instance.OnStartPathFind( vehicleId, ref vehicleData, (vehicleData.m_flags & Vehicle.Flags.Emergency2) != 0 ? ExtVehicleType.Emergency : ExtVehicleType.Service); - } VehicleInfo info = m_info; bool allowUnderground = (vehicleData.m_flags & (Vehicle.Flags.Underground diff --git a/TLM/TLM/Custom/AI/CustomHumanAI.cs b/TLM/TLM/Custom/AI/CustomHumanAI.cs index 3bc0ec9f7..b310947fa 100644 --- a/TLM/TLM/Custom/AI/CustomHumanAI.cs +++ b/TLM/TLM/Custom/AI/CustomHumanAI.cs @@ -1,6 +1,5 @@ namespace TrafficManager.Custom.AI { using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using JetBrains.Annotations; using System.Runtime.CompilerServices; @@ -68,35 +67,29 @@ public void CustomSimulationStep(ushort instanceId, $"Path: {instanceData.m_path}, mainPathState={mainPathState}"); } - ExtSoftPathState finalPathState; - using (var bm = Benchmark.MaybeCreateBenchmark( - null, - "ConvertPathStateToSoftPathState+UpdateCitizenPathState")) - { - finalPathState = ExtCitizenInstance.ConvertPathStateToSoftPathState(mainPathState); + ExtSoftPathState finalPathState = ExtCitizenInstance.ConvertPathStateToSoftPathState(mainPathState); - if (Options.parkingAI) { - finalPathState = AdvancedParkingManager.Instance.UpdateCitizenPathState( - instanceId, - ref instanceData, - ref ExtCitizenInstanceManager - .Instance.ExtInstances[ - instanceId], - ref ExtCitizenManager - .Instance.ExtCitizens[ - citizenId], - ref citizensBuffer[ - instanceData.m_citizen], - mainPathState); - if (logParkingAi) { - Log._Debug( - $"CustomHumanAI.CustomSimulationStep({instanceId}): " + - $"Applied Parking AI logic. Path: {instanceData.m_path}, " + - $"mainPathState={mainPathState}, finalPathState={finalPathState}, " + - $"extCitizenInstance={ExtCitizenInstanceManager.Instance.ExtInstances[instanceId]}"); - } - } // if Options.parkingAi - } + if (Options.parkingAI) { + finalPathState = AdvancedParkingManager.Instance.UpdateCitizenPathState( + instanceId, + ref instanceData, + ref ExtCitizenInstanceManager + .Instance.ExtInstances[ + instanceId], + ref ExtCitizenManager + .Instance.ExtCitizens[ + citizenId], + ref citizensBuffer[ + instanceData.m_citizen], + mainPathState); + if (logParkingAi) { + Log._Debug( + $"CustomHumanAI.CustomSimulationStep({instanceId}): " + + $"Applied Parking AI logic. Path: {instanceData.m_path}, " + + $"mainPathState={mainPathState}, finalPathState={finalPathState}, " + + $"extCitizenInstance={ExtCitizenInstanceManager.Instance.ExtInstances[instanceId]}"); + } + } // if Options.parkingAi switch (finalPathState) { case ExtSoftPathState.Ready: { @@ -206,15 +199,13 @@ ref citizensBuffer[ } // NON-STOCK CODE START - using (var bm = Benchmark.MaybeCreateBenchmark(null, "ExtSimulationStep")) { - if (Options.parkingAI) { - if (ExtSimulationStep( - instanceId, - ref instanceData, - ref ExtCitizenInstanceManager.Instance.ExtInstances[instanceId], - physicsLodRefPos)) { - return; - } + if (Options.parkingAI) { + if (ExtSimulationStep( + instanceId, + ref instanceData, + ref ExtCitizenInstanceManager.Instance.ExtInstances[instanceId], + physicsLodRefPos)) { + return; } } @@ -430,13 +421,11 @@ public bool CustomCheckTrafficLights(ushort nodeId, ushort segmentId) { bool startNode = segmentsBuffer[segmentId].m_startNode == nodeId; ICustomSegmentLights lights = null; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "GetSegmentLights")) { - if (customSim) { - lights = CustomSegmentLightsManager.Instance.GetSegmentLights( - segmentId, - startNode, - false); - } + if (customSim) { + lights = CustomSegmentLightsManager.Instance.GetSegmentLights( + segmentId, + startNode, + false); } if (lights == null) { diff --git a/TLM/TLM/Custom/AI/CustomPassengerCarAI.cs b/TLM/TLM/Custom/AI/CustomPassengerCarAI.cs index 4eb16142a..cef8f3a26 100644 --- a/TLM/TLM/Custom/AI/CustomPassengerCarAI.cs +++ b/TLM/TLM/Custom/AI/CustomPassengerCarAI.cs @@ -2,7 +2,6 @@ namespace TrafficManager.Custom.AI { using ColossalFramework.Globalization; using ColossalFramework.Math; using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using JetBrains.Annotations; using System.Runtime.CompilerServices; @@ -181,24 +180,22 @@ public bool CustomParkVehicle(ushort vehicleId, } } - using (var bm = Benchmark.MaybeCreateBenchmark(null, "ExtParkVehicle")) { - return Constants.ManagerFactory.VehicleBehaviorManager.ParkPassengerCar( - vehicleId, - ref vehicleData, - vehicleData.Info, - driverCitizenId, - ref citizenManager.m_citizens.m_buffer[driverCitizenId], - driverCitizenInstanceId, - ref citizenManager.m_instances.m_buffer[ - driverCitizenInstanceId], - ref ExtCitizenInstanceManager.Instance.ExtInstances[ - driverCitizenInstanceId], - targetBuildingId, - pathPos, - nextPath, - nextPositionIndex, - out segmentOffset); - } + return Constants.ManagerFactory.VehicleBehaviorManager.ParkPassengerCar( + vehicleId, + ref vehicleData, + vehicleData.Info, + driverCitizenId, + ref citizenManager.m_citizens.m_buffer[driverCitizenId], + driverCitizenInstanceId, + ref citizenManager.m_instances.m_buffer[ + driverCitizenInstanceId], + ref ExtCitizenInstanceManager.Instance.ExtInstances[ + driverCitizenInstanceId], + targetBuildingId, + pathPos, + nextPath, + nextPositionIndex, + out segmentOffset); } [RedirectReverse] diff --git a/TLM/TLM/Manager/Impl/AdvancedParkingManager.cs b/TLM/TLM/Manager/Impl/AdvancedParkingManager.cs index d738359f4..62c8f7145 100644 --- a/TLM/TLM/Manager/Impl/AdvancedParkingManager.cs +++ b/TLM/TLM/Manager/Impl/AdvancedParkingManager.cs @@ -1,8 +1,7 @@ -namespace TrafficManager.Manager.Impl { +namespace TrafficManager.Manager.Impl { using ColossalFramework.Globalization; using ColossalFramework.Math; using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using JetBrains.Annotations; using System; @@ -1817,8 +1816,7 @@ public bool TryMoveParkedVehicle(ushort parkedVehicleId, Vector3 parkPos; Quaternion parkRot; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "FindParkingSpaceInVicinity")) { - found = Instance.FindParkingSpaceInVicinity( + found = Instance.FindParkingSpaceInVicinity( refPos, Vector3.zero, parkedVehicle.Info, @@ -1830,7 +1828,6 @@ public bool TryMoveParkedVehicle(ushort parkedVehicleId, out parkPos, out parkRot, out _); - } if (found) { Singleton.instance.RemoveFromGrid(parkedVehicleId, ref parkedVehicle); diff --git a/TLM/TLM/Manager/Impl/ExtCitizenInstanceManager.cs b/TLM/TLM/Manager/Impl/ExtCitizenInstanceManager.cs index c44e2b0d2..6314001a5 100644 --- a/TLM/TLM/Manager/Impl/ExtCitizenInstanceManager.cs +++ b/TLM/TLM/Manager/Impl/ExtCitizenInstanceManager.cs @@ -1,7 +1,6 @@ namespace TrafficManager.Manager.Impl { using ColossalFramework.Globalization; using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using System.Collections.Generic; using System; @@ -439,13 +438,12 @@ bool citizenDebug var startsAtOutsideConnection = false; ParkingAI parkingAiConf = GlobalConfig.Instance.ParkingAI; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "ParkingAI.Preparation")) { - if (Options.parkingAI) { - switch (extInstance.pathMode) { - case ExtPathMode.RequiresWalkingPathToParkedCar: - case ExtPathMode.CalculatingWalkingPathToParkedCar: - case ExtPathMode.WalkingToParkedCar: - case ExtPathMode.ApproachingParkedCar: { + if (Options.parkingAI) { + switch (extInstance.pathMode) { + case ExtPathMode.RequiresWalkingPathToParkedCar: + case ExtPathMode.CalculatingWalkingPathToParkedCar: + case ExtPathMode.WalkingToParkedCar: + case ExtPathMode.ApproachingParkedCar: { if (parkedVehicleId == 0) { // Parked vehicle not present but citizen wants to reach it // -> Reset path mode @@ -475,9 +473,9 @@ bool citizenDebug break; } - case ExtPathMode.RequiresWalkingPathToTarget: - case ExtPathMode.CalculatingWalkingPathToTarget: - case ExtPathMode.WalkingToTarget: { + case ExtPathMode.RequiresWalkingPathToTarget: + case ExtPathMode.CalculatingWalkingPathToTarget: + case ExtPathMode.WalkingToTarget: { // Citizen walks to target // -> Reset path mode if (extendedLogParkingAi) { @@ -492,14 +490,14 @@ bool citizenDebug break; } - case ExtPathMode.RequiresCarPath: - case ExtPathMode.RequiresMixedCarPathToTarget: - case ExtPathMode.DrivingToTarget: - case ExtPathMode.DrivingToKnownParkPos: - case ExtPathMode.DrivingToAltParkPos: - case ExtPathMode.CalculatingCarPathToAltParkPos: - case ExtPathMode.CalculatingCarPathToKnownParkPos: - case ExtPathMode.CalculatingCarPathToTarget: { + case ExtPathMode.RequiresCarPath: + case ExtPathMode.RequiresMixedCarPathToTarget: + case ExtPathMode.DrivingToTarget: + case ExtPathMode.DrivingToKnownParkPos: + case ExtPathMode.DrivingToAltParkPos: + case ExtPathMode.CalculatingCarPathToAltParkPos: + case ExtPathMode.CalculatingCarPathToKnownParkPos: + case ExtPathMode.CalculatingCarPathToTarget: { if (parkedVehicleId == 0) { // Citizen wants to drive to target but parked vehicle is not present // -> Reset path mode @@ -530,7 +528,7 @@ bool citizenDebug break; } - default: { + default: { if (logParkingAi) { Log.Info( $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen has " + @@ -540,169 +538,168 @@ bool citizenDebug Reset(ref extInstance); break; } - } + } - startsAtOutsideConnection = - Constants.ManagerFactory.ExtCitizenInstanceManager.IsAtOutsideConnection( - instanceID, - ref instanceData, - ref extInstance, - startPos); + startsAtOutsideConnection = + Constants.ManagerFactory.ExtCitizenInstanceManager.IsAtOutsideConnection( + instanceID, + ref instanceData, + ref extInstance, + startPos); - if (extInstance.pathMode == ExtPathMode.None) { - bool isOnTour = (instanceData.m_flags & CitizenInstance.Flags.OnTour) != - CitizenInstance.Flags.None; + if (extInstance.pathMode == ExtPathMode.None) { + bool isOnTour = (instanceData.m_flags & CitizenInstance.Flags.OnTour) != + CitizenInstance.Flags.None; - if (isOnTour || ignoreCost) { - // Citizen is on a walking tour or is a mascot - // -> Prohibit car usage - if (logParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen ignores " + - $"cost ({ignoreCost}) or is on a walking tour ({isOnTour}): Setting " + - "path mode to 'CalculatingWalkingPathToTarget'"); - } + if (isOnTour || ignoreCost) { + // Citizen is on a walking tour or is a mascot + // -> Prohibit car usage + if (logParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen ignores " + + $"cost ({ignoreCost}) or is on a walking tour ({isOnTour}): Setting " + + "path mode to 'CalculatingWalkingPathToTarget'"); + } - carUsageMode = CarUsagePolicy.Forbidden; - extInstance.pathMode = ExtPathMode.CalculatingWalkingPathToTarget; - } else { - // Citizen is not on a walking tour and is not a mascot - // -> Check if citizen is located at an outside connection and make them - // obey Parking AI restrictions - if (instanceData.m_sourceBuilding != 0) { - ItemClass.Service sourceBuildingService - = buildingsBuffer[instanceData.m_sourceBuilding].Info - .m_class - .m_service; - - if (startsAtOutsideConnection) { - if (sourceBuildingService == ItemClass.Service.Road) { - if (vehicleInfo != null) { - // Citizen is located at a road outside connection and can spawn a car - // -> Force car usage - if (logParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): " + - "Citizen is located at a road outside connection: " + - "Setting path mode to 'RequiresCarPath' and carUsageMode " + - "to 'ForcedPocket'"); - } - - extInstance.pathMode = ExtPathMode.RequiresCarPath; - carUsageMode = CarUsagePolicy.ForcedPocket; - } else { - // Citizen is located at a non-road outside connection and - // cannot spawn a car - // -> Path-finding fails - if (logParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): " + - "Citizen is located at a road outside connection but " + - "does not have a car template: ABORTING PATH-FINDING"); - } - - Reset(ref extInstance); - return false; + carUsageMode = CarUsagePolicy.Forbidden; + extInstance.pathMode = ExtPathMode.CalculatingWalkingPathToTarget; + } else { + // Citizen is not on a walking tour and is not a mascot + // -> Check if citizen is located at an outside connection and make them + // obey Parking AI restrictions + if (instanceData.m_sourceBuilding != 0) { + ItemClass.Service sourceBuildingService + = buildingsBuffer[instanceData.m_sourceBuilding].Info + .m_class + .m_service; + + if (startsAtOutsideConnection) { + if (sourceBuildingService == ItemClass.Service.Road) { + if (vehicleInfo != null) { + // Citizen is located at a road outside connection and can spawn a car + // -> Force car usage + if (logParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): " + + "Citizen is located at a road outside connection: " + + "Setting path mode to 'RequiresCarPath' and carUsageMode " + + "to 'ForcedPocket'"); } + + extInstance.pathMode = ExtPathMode.RequiresCarPath; + carUsageMode = CarUsagePolicy.ForcedPocket; } else { - // Citizen is located at a non-road outside connection - // -> Prohibit car usage + // Citizen is located at a non-road outside connection and + // cannot spawn a car + // -> Path-finding fails if (logParkingAi) { Log.Info( $"CustomCitizenAI.ExtStartPathFind({instanceID}): " + - "Citizen is located at a non-road outside connection: " + - "Setting path mode to 'CalculatingWalkingPathToTarget'"); + "Citizen is located at a road outside connection but " + + "does not have a car template: ABORTING PATH-FINDING"); } - extInstance.pathMode = - ExtPathMode.CalculatingWalkingPathToTarget; - carUsageMode = CarUsagePolicy.Forbidden; + Reset(ref extInstance); + return false; + } + } else { + // Citizen is located at a non-road outside connection + // -> Prohibit car usage + if (logParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): " + + "Citizen is located at a non-road outside connection: " + + "Setting path mode to 'CalculatingWalkingPathToTarget'"); } - } // starts at outside connection - } // sourceBuilding != 0 - } // not isOnTour && not ignoreCost - } // if (extInstance.pathMode == ExtPathMode.None) - - if ((carUsageMode == CarUsagePolicy.Allowed || - carUsageMode == CarUsagePolicy.ForcedParked) && parkedVehicleId != 0) { - // Reuse parked vehicle info - vehicleInfo = parkedVehiclesBuffer[parkedVehicleId].Info; - - // Check if the citizen should return their car back home - if (extInstance.pathMode == ExtPathMode.None && // initiating a new path - homeId != 0 && // home building present - instanceData.m_targetBuilding == homeId // current target is home - ) { - // citizen travels back home - // -> check if their car should be returned - if ((extCitizen.lastTransportMode & ExtTransportMode.Car) != - ExtTransportMode.None) { - // citizen travelled by car - // -> return car back home + + extInstance.pathMode = + ExtPathMode.CalculatingWalkingPathToTarget; + carUsageMode = CarUsagePolicy.Forbidden; + } + } // starts at outside connection + } // sourceBuilding != 0 + } // not isOnTour && not ignoreCost + } // if (extInstance.pathMode == ExtPathMode.None) + + if ((carUsageMode == CarUsagePolicy.Allowed || + carUsageMode == CarUsagePolicy.ForcedParked) && parkedVehicleId != 0) { + // Reuse parked vehicle info + vehicleInfo = parkedVehiclesBuffer[parkedVehicleId].Info; + + // Check if the citizen should return their car back home + if (extInstance.pathMode == ExtPathMode.None && // initiating a new path + homeId != 0 && // home building present + instanceData.m_targetBuilding == homeId // current target is home + ) { + // citizen travels back home + // -> check if their car should be returned + if ((extCitizen.lastTransportMode & ExtTransportMode.Car) != + ExtTransportMode.None) { + // citizen travelled by car + // -> return car back home + extInstance.pathMode = + ExtPathMode.CalculatingWalkingPathToParkedCar; + carUsageMode = CarUsagePolicy.Forbidden; + + if (extendedLogParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen used " + + "their car before and is not at home. Forcing to walk to parked car."); + } + } else { + // citizen travelled by other means of transport + // -> check distance between home and parked car. if too far away: + // force to take the car back home + float distHomeToParked = + (parkedVehiclesBuffer[parkedVehicleId].m_position - + buildingsBuffer[homeId].m_position).magnitude; + + if (distHomeToParked > parkingAiConf.MaxParkedCarDistanceToHome) { + // force to take car back home extInstance.pathMode = ExtPathMode.CalculatingWalkingPathToParkedCar; carUsageMode = CarUsagePolicy.Forbidden; if (extendedLogParkingAi) { Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen used " + - "their car before and is not at home. Forcing to walk to parked car."); - } - } else { - // citizen travelled by other means of transport - // -> check distance between home and parked car. if too far away: - // force to take the car back home - float distHomeToParked = - (parkedVehiclesBuffer[parkedVehicleId].m_position - - buildingsBuffer[homeId].m_position).magnitude; - - if (distHomeToParked > parkingAiConf.MaxParkedCarDistanceToHome) { - // force to take car back home - extInstance.pathMode = - ExtPathMode.CalculatingWalkingPathToParkedCar; - carUsageMode = CarUsagePolicy.Forbidden; - - if (extendedLogParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " + - "wants to go home and parked car is too far away " + - $"({distHomeToParked}). Forcing walking to parked car."); - } + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " + + "wants to go home and parked car is too far away " + + $"({distHomeToParked}). Forcing walking to parked car."); } } } } + } - //-------------------------------------------------------------- - // The following holds: - // - pathMode is now either CalculatingWalkingPathToParkedCar, - // CalculatingWalkingPathToTarget, RequiresCarPath or None. - // - if pathMode is CalculatingWalkingPathToParkedCar or RequiresCarPath: parked - // car is present and citizen is not on a walking tour - // - carUsageMode is valid - // - if pathMode is RequiresCarPath: carUsageMode is either ForcedParked or - // ForcedPocket - //-------------------------------------------------------------- - - // modify path-finding constraints (vehicleInfo, endPos) if citizen is forced to walk - if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar || - extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToTarget) { - // vehicle must not be used since we need a walking path to either - // 1. a parked car or - // 2. the target building - if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar) { - // walk to parked car - // -> end position is parked car - endPos = parkedVehiclesBuffer[parkedVehicleId].m_position; - if (extendedLogParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen shall " + - $"go to parked vehicle @ {endPos}"); - } + //-------------------------------------------------------------- + // The following holds: + // - pathMode is now either CalculatingWalkingPathToParkedCar, + // CalculatingWalkingPathToTarget, RequiresCarPath or None. + // - if pathMode is CalculatingWalkingPathToParkedCar or RequiresCarPath: parked + // car is present and citizen is not on a walking tour + // - carUsageMode is valid + // - if pathMode is RequiresCarPath: carUsageMode is either ForcedParked or + // ForcedPocket + //-------------------------------------------------------------- + + // modify path-finding constraints (vehicleInfo, endPos) if citizen is forced to walk + if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar || + extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToTarget) { + // vehicle must not be used since we need a walking path to either + // 1. a parked car or + // 2. the target building + if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar) { + // walk to parked car + // -> end position is parked car + endPos = parkedVehiclesBuffer[parkedVehicleId].m_position; + if (extendedLogParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen shall " + + $"go to parked vehicle @ {endPos}"); } } - } // if Options.ParkingAi - } // end benchmark + } + } // if Options.ParkingAi if (extendedLogParkingAi) { Log.Info( @@ -778,103 +775,101 @@ ItemClass.Service sourceBuildingService bool calculateEndPos = true; bool allowRandomParking = true; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "ParkingAI.Main")) { - if (Options.parkingAI) { - // Parking AI - if (extInstance.pathMode == ExtPathMode.RequiresCarPath) { + if (Options.parkingAI) { + // Parking AI + if (extInstance.pathMode == ExtPathMode.RequiresCarPath) { + if (logParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Setting " + + $"startPos={startPos} for citizen instance {instanceID}. " + + $"CurrentDepartureMode={extInstance.pathMode}"); + } + + if (instanceData.m_targetBuilding == 0 || + (buildingsBuffer[instanceData.m_targetBuilding].m_flags & + Building.Flags.IncomingOutgoing) == Building.Flags.None) { + // the citizen is starting their journey and the target is not an outside + // connection + // -> find a suitable parking space near the target if (logParkingAi) { Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Setting " + - $"startPos={startPos} for citizen instance {instanceID}. " + - $"CurrentDepartureMode={extInstance.pathMode}"); + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding parking " + + $"space at target for citizen instance {instanceID}. " + + $"CurrentDepartureMode={extInstance.pathMode} parkedVehicleId={parkedVehicleId}"); } - if (instanceData.m_targetBuilding == 0 || - (buildingsBuffer[instanceData.m_targetBuilding].m_flags & - Building.Flags.IncomingOutgoing) == Building.Flags.None) { - // the citizen is starting their journey and the target is not an outside - // connection - // -> find a suitable parking space near the target + // find a parking space in the vicinity of the target + if (AdvancedParkingManager.Instance.FindParkingSpaceForCitizen( + endPos, + vehicleInfo, + ref extInstance, + homeId, + instanceData.m_targetBuilding == homeId, + 0, + false, + out Vector3 parkPos, + ref endPosA, + out bool calcEndPos) + && CalculateReturnPath( + ref extInstance, + parkPos, + endPos)) { + // success + extInstance.pathMode = ExtPathMode.CalculatingCarPathToKnownParkPos; + + // if true, the end path position still needs to be calculated + calculateEndPos = calcEndPos; + + // find a direct path to the calculated parking position + allowRandomParking = false; if (logParkingAi) { Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding parking " + - $"space at target for citizen instance {instanceID}. " + - $"CurrentDepartureMode={extInstance.pathMode} parkedVehicleId={parkedVehicleId}"); + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding " + + $"known parking space for citizen instance {instanceID}, parked " + + $"vehicle {parkedVehicleId} succeeded and return path " + + $"{extInstance.returnPathId} ({extInstance.returnPathState}) " + + $"is calculating. PathMode={extInstance.pathMode}"); } - // find a parking space in the vicinity of the target - if (AdvancedParkingManager.Instance.FindParkingSpaceForCitizen( - endPos, - vehicleInfo, - ref extInstance, - homeId, - instanceData.m_targetBuilding == homeId, - 0, - false, - out Vector3 parkPos, - ref endPosA, - out bool calcEndPos) - && CalculateReturnPath( - ref extInstance, - parkPos, - endPos)) { - // success - extInstance.pathMode = ExtPathMode.CalculatingCarPathToKnownParkPos; - - // if true, the end path position still needs to be calculated - calculateEndPos = calcEndPos; - - // find a direct path to the calculated parking position - allowRandomParking = false; - if (logParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding " + - $"known parking space for citizen instance {instanceID}, parked " + - $"vehicle {parkedVehicleId} succeeded and return path " + - $"{extInstance.returnPathId} ({extInstance.returnPathState}) " + - $"is calculating. PathMode={extInstance.pathMode}"); - } - - // if (!extInstance.CalculateReturnPath(parkPos, endPos)) { - // // TODO retry? - // if (debug) - // Log._Debug( - // $"CustomCitizenAI.CustomStartPathFind: [PFFAIL] Could not - // calculate return path for citizen instance {instanceID}, parked vehicle - // {parkedVehicleId}. Calling OnPathFindFailed."); - // CustomHumanAI.OnPathFindFailure(extInstance); - // return false; - // } - } + // if (!extInstance.CalculateReturnPath(parkPos, endPos)) { + // // TODO retry? + // if (debug) + // Log._Debug( + // $"CustomCitizenAI.CustomStartPathFind: [PFFAIL] Could not + // calculate return path for citizen instance {instanceID}, parked vehicle + // {parkedVehicleId}. Calling OnPathFindFailed."); + // CustomHumanAI.OnPathFindFailure(extInstance); + // return false; + // } } + } - if (extInstance.pathMode == ExtPathMode.RequiresCarPath) { - // no known parking space found (pathMode has not been updated in the block above) - // -> calculate direct path to target - if (logParkingAi) { - Log.Info( - $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen instance " + - $"{instanceID} is still at CurrentPathMode={extInstance.pathMode} " + - "(no parking space found?). Setting it to CalculatingCarPath. " + - $"parkedVehicleId={parkedVehicleId}"); - } - - extInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; + if (extInstance.pathMode == ExtPathMode.RequiresCarPath) { + // no known parking space found (pathMode has not been updated in the block above) + // -> calculate direct path to target + if (logParkingAi) { + Log.Info( + $"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen instance " + + $"{instanceID} is still at CurrentPathMode={extInstance.pathMode} " + + "(no parking space found?). Setting it to CalculatingCarPath. " + + $"parkedVehicleId={parkedVehicleId}"); } + + extInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; } + } - // determine path type from path mode - extPathType = extInstance.GetPathType(); - extInstance.atOutsideConnection = startsAtOutsideConnection; - - //------------------------------------------ - // the following holds: - // - pathMode is now either CalculatingWalkingPathToParkedCar, - // CalculatingWalkingPathToTarget, CalculatingCarPathToTarget, - // CalculatingCarPathToKnownParkPos or None. - //------------------------------------------ - } // end if Options.ParkingAi - } // end benchmark + // determine path type from path mode + extPathType = extInstance.GetPathType(); + extInstance.atOutsideConnection = startsAtOutsideConnection; + + //------------------------------------------ + // the following holds: + // - pathMode is now either CalculatingWalkingPathToParkedCar, + // CalculatingWalkingPathToTarget, CalculatingCarPathToTarget, + // CalculatingCarPathToKnownParkPos or None. + //------------------------------------------ + } // end if Options.ParkingAi /* * enable random parking if exact parking space was not calculated yet diff --git a/TLM/TLM/Manager/Impl/TrafficLightSimulationManager.cs b/TLM/TLM/Manager/Impl/TrafficLightSimulationManager.cs index 41904ec68..38e14b7b4 100644 --- a/TLM/TLM/Manager/Impl/TrafficLightSimulationManager.cs +++ b/TLM/TLM/Manager/Impl/TrafficLightSimulationManager.cs @@ -1,5 +1,4 @@ namespace TrafficManager.Manager.Impl { - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using ExtVehicleType = global::TrafficManager.Traffic.ExtVehicleType; using static RoadBaseAI; @@ -69,12 +68,8 @@ public void GetTrafficLightState( out TrafficLightState vehicleLightState, out TrafficLightState pedestrianLightState) { - bool callStockMethod = true; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "callStockMethod")) { - callStockMethod = !Options.timedLightsEnabled - || !Instance - .TrafficLightSimulations[nodeId].IsSimulationRunning(); - } + bool callStockMethod = !Options.timedLightsEnabled + || !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning(); if (callStockMethod) { RoadBaseAI.GetTrafficLightState( @@ -84,20 +79,18 @@ public void GetTrafficLightState( out vehicleLightState, out pedestrianLightState); } else { - using (var bm = Benchmark.MaybeCreateBenchmark(null, "GetCustomTrafficLightState")) { - GetCustomTrafficLightState( + GetCustomTrafficLightState( #if DEBUG - vehicleId, - ref vehicleData, + vehicleId, + ref vehicleData, #endif - nodeId, - fromSegmentId, - fromLaneIndex, - toSegmentId, - out vehicleLightState, - out pedestrianLightState, - ref Instance.TrafficLightSimulations[nodeId]); - } + nodeId, + fromSegmentId, + fromLaneIndex, + toSegmentId, + out vehicleLightState, + out pedestrianLightState, + ref Instance.TrafficLightSimulations[nodeId]); } } @@ -117,12 +110,8 @@ public void GetTrafficLightState( out bool vehicles, out bool pedestrians) { - bool callStockMethod; - - using (var bm = Benchmark.MaybeCreateBenchmark(null, "callStockMethod")) { - callStockMethod = !Options.timedLightsEnabled || - !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning(); - } + bool callStockMethod = !Options.timedLightsEnabled + || !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning(); if (callStockMethod) { RoadBaseAI.GetTrafficLightState( @@ -134,20 +123,18 @@ public void GetTrafficLightState( out vehicles, out pedestrians); } else { - using (var bm = Benchmark.MaybeCreateBenchmark(null, "GetCustomTrafficLightState")) { - GetCustomTrafficLightState( + GetCustomTrafficLightState( #if DEBUG - vehicleId, - ref vehicleData, + vehicleId, + ref vehicleData, #endif - nodeId, - fromSegmentId, - fromLaneIndex, - toSegmentId, - out vehicleLightState, - out pedestrianLightState, - ref Instance.TrafficLightSimulations[nodeId]); - } + nodeId, + fromSegmentId, + fromLaneIndex, + toSegmentId, + out vehicleLightState, + out pedestrianLightState, + ref Instance.TrafficLightSimulations[nodeId]); vehicles = false; pedestrians = false; diff --git a/TLM/TLM/Manager/Impl/VehicleBehaviorManager.cs b/TLM/TLM/Manager/Impl/VehicleBehaviorManager.cs index e7e5ef3f2..bc8e872e6 100644 --- a/TLM/TLM/Manager/Impl/VehicleBehaviorManager.cs +++ b/TLM/TLM/Manager/Impl/VehicleBehaviorManager.cs @@ -1,6 +1,5 @@ namespace TrafficManager.Manager.Impl { using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using JetBrains.Annotations; using System; @@ -601,213 +600,211 @@ bool citizenDebug Building[] buildingsBuffer = Singleton.instance.m_buildings.m_buffer; Citizen[] citizensBuffer = Singleton.instance.m_citizens.m_buffer; - using (var bm = Benchmark.MaybeCreateBenchmark(null, "ParkingAI")) { - if (Options.parkingAI) { - // if (driverExtInstance != null) { + if (Options.parkingAI) { + // if (driverExtInstance != null) { + if (logParkingAi) { + Log.WarningFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode={1} for vehicle {2}, " + + "driver citizen instance {3}!", + vehicleID, + driverExtInstance.pathMode, + vehicleID, + driverExtInstance.instanceId); + } + + if (driverExtInstance.pathMode == ExtPathMode.RequiresMixedCarPathToTarget) { + driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; + startBothWays = false; + if (logParkingAi) { - Log.WarningFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode={1} for vehicle {2}, " + - "driver citizen instance {3}!", - vehicleID, - driverExtInstance.pathMode, + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode was " + + "RequiresDirectCarPathToTarget: Parking spaces will NOT be searched " + + "beforehand. Setting pathMode={1}", vehicleID, - driverExtInstance.instanceId); + driverExtInstance.pathMode); } + } else if (driverExtInstance.pathMode != ExtPathMode.ParkingFailed + && targetBuildingId != 0 + && (buildingsBuffer[targetBuildingId].m_flags & + Building.Flags.IncomingOutgoing) + != Building.Flags.None) { + // target is outside connection + driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; - if (driverExtInstance.pathMode == ExtPathMode.RequiresMixedCarPathToTarget) { - driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; - startBothWays = false; - + if (logParkingAi) { + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode was not ParkingFailed " + + "and target is outside connection: Setting pathMode={1}", + vehicleID, + driverExtInstance.pathMode); + } + } else { + if (driverExtInstance.pathMode == ExtPathMode.DrivingToTarget || + driverExtInstance.pathMode == ExtPathMode.DrivingToKnownParkPos || + driverExtInstance.pathMode == ExtPathMode.ParkingFailed) { if (logParkingAi) { Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode was " + - "RequiresDirectCarPathToTarget: Parking spaces will NOT be searched " + - "beforehand. Setting pathMode={1}", + "CustomPassengerCarAI.ExtStartPathFind({0}): Skipping queue. pathMode={1}", vehicleID, driverExtInstance.pathMode); } - } else if (driverExtInstance.pathMode != ExtPathMode.ParkingFailed - && targetBuildingId != 0 - && (buildingsBuffer[targetBuildingId].m_flags & - Building.Flags.IncomingOutgoing) - != Building.Flags.None) { - // target is outside connection - driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; + + skipQueue = true; + } + + bool allowTourists = false; + bool searchAtCurrentPos = false; + + if (driverExtInstance.pathMode == ExtPathMode.ParkingFailed) { + // previous parking attempt failed + driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToAltParkPos; + allowTourists = true; + searchAtCurrentPos = true; if (logParkingAi) { Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): PathMode was not ParkingFailed " + - "and target is outside connection: Setting pathMode={1}", + "CustomPassengerCarAI.ExtStartPathFind({0}): Vehicle {1} shall move " + + "to an alternative parking position! CurrentPathMode={2} FailedParkingAttempts={3}", vehicleID, - driverExtInstance.pathMode); - } - } else { - if (driverExtInstance.pathMode == ExtPathMode.DrivingToTarget || - driverExtInstance.pathMode == ExtPathMode.DrivingToKnownParkPos || - driverExtInstance.pathMode == ExtPathMode.ParkingFailed) { - if (logParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): Skipping queue. pathMode={1}", - vehicleID, - driverExtInstance.pathMode); - } - - skipQueue = true; + vehicleID, + driverExtInstance.pathMode, + driverExtInstance.failedParkingAttempts); } - bool allowTourists = false; - bool searchAtCurrentPos = false; - - if (driverExtInstance.pathMode == ExtPathMode.ParkingFailed) { - // previous parking attempt failed - driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToAltParkPos; - allowTourists = true; - searchAtCurrentPos = true; + if (driverExtInstance.parkingPathStartPosition != null) { + startPosA = + (PathUnit.Position)driverExtInstance.parkingPathStartPosition; + foundStartingPos = true; if (logParkingAi) { Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): Vehicle {1} shall move " + - "to an alternative parking position! CurrentPathMode={2} FailedParkingAttempts={3}", + "CustomPassengerCarAI.ExtStartPathFind({0}): Setting starting pos " + + "for {1} to segment={2}, laneIndex={3}, offset={4}", vehicleID, vehicleID, - driverExtInstance.pathMode, - driverExtInstance.failedParkingAttempts); - } - - if (driverExtInstance.parkingPathStartPosition != null) { - startPosA = - (PathUnit.Position)driverExtInstance.parkingPathStartPosition; - foundStartingPos = true; - - if (logParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): Setting starting pos " + - "for {1} to segment={2}, laneIndex={3}, offset={4}", - vehicleID, - vehicleID, - startPosA.m_segment, - startPosA.m_lane, - startPosA.m_offset); - } + startPosA.m_segment, + startPosA.m_lane, + startPosA.m_offset); } + } - startBothWays = false; - - if (driverExtInstance.failedParkingAttempts - > GlobalConfig.Instance.ParkingAI.MaxParkingAttempts) { - // maximum number of parking attempts reached - Log._DebugIf( - logParkingAi, - () => - $"CustomPassengerCarAI.ExtStartPathFind({vehicleID}): Reached " + - $"maximum number of parking attempts for vehicle {vehicleID}! GIVING UP."); - - extCitizenInstanceManager.Reset(ref driverExtInstance); + startBothWays = false; - // pocket car fallback - // vehicleData.m_flags |= Vehicle.Flags.Parking; - return false; - } + if (driverExtInstance.failedParkingAttempts + > GlobalConfig.Instance.ParkingAI.MaxParkingAttempts) { + // maximum number of parking attempts reached + Log._DebugIf( + logParkingAi, + () => + $"CustomPassengerCarAI.ExtStartPathFind({vehicleID}): Reached " + + $"maximum number of parking attempts for vehicle {vehicleID}! GIVING UP."); - if (extendedLogParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): Increased number of " + - "parking attempts for vehicle {1}: {2}/{3}", - vehicleID, - vehicleID, - driverExtInstance.failedParkingAttempts, - GlobalConfig.Instance.ParkingAI.MaxParkingAttempts); - } - } else { - driverExtInstance.pathMode = - ExtPathMode.CalculatingCarPathToKnownParkPos; + extCitizenInstanceManager.Reset(ref driverExtInstance); - if (logParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): No parking involved: " + - "Setting pathMode={1}", - vehicleID, - driverExtInstance.pathMode); - } + // pocket car fallback + // vehicleData.m_flags |= Vehicle.Flags.Parking; + return false; } - ushort homeId = citizensBuffer[driverCitizenId].m_homeBuilding; - Vector3 returnPos = - searchAtCurrentPos ? (Vector3)vehicleData.m_targetPos3 : endPos; + if (extendedLogParkingAi) { + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): Increased number of " + + "parking attempts for vehicle {1}: {2}/{3}", + vehicleID, + vehicleID, + driverExtInstance.failedParkingAttempts, + GlobalConfig.Instance.ParkingAI.MaxParkingAttempts); + } + } else { + driverExtInstance.pathMode = + ExtPathMode.CalculatingCarPathToKnownParkPos; - if (AdvancedParkingManager.Instance.FindParkingSpaceForCitizen( - returnPos, - vehicleData.Info, - ref driverExtInstance, - homeId, - targetBuildingId == homeId, - vehicleID, - allowTourists, - out Vector3 parkPos, - ref endPosA, - out bool calcEndPos)) { - calculateEndPos = calcEndPos; - allowRandomParking = false; - movingToParkingPos = true; - - if (!extCitizenInstanceManager.CalculateReturnPath( - ref driverExtInstance, - parkPos, - returnPos)) { - if (logParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): Could not calculate " + - "return path for citizen instance {1}, vehicle {2}. Resetting instance.", - vehicleID, - driverExtInstance.instanceId, - vehicleID); - } + if (logParkingAi) { + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): No parking involved: " + + "Setting pathMode={1}", + vehicleID, + driverExtInstance.pathMode); + } + } - extCitizenInstanceManager.Reset(ref driverExtInstance); - return false; - } - } else if (driverExtInstance.pathMode == - ExtPathMode.CalculatingCarPathToAltParkPos) { - // no alternative parking spot found: abort + ushort homeId = citizensBuffer[driverCitizenId].m_homeBuilding; + Vector3 returnPos = + searchAtCurrentPos ? (Vector3)vehicleData.m_targetPos3 : endPos; + + if (AdvancedParkingManager.Instance.FindParkingSpaceForCitizen( + returnPos, + vehicleData.Info, + ref driverExtInstance, + homeId, + targetBuildingId == homeId, + vehicleID, + allowTourists, + out Vector3 parkPos, + ref endPosA, + out bool calcEndPos)) { + calculateEndPos = calcEndPos; + allowRandomParking = false; + movingToParkingPos = true; + + if (!extCitizenInstanceManager.CalculateReturnPath( + ref driverExtInstance, + parkPos, + returnPos)) { if (logParkingAi) { Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): No alternative parking " + - "spot found for vehicle {1}, citizen instance {2} with CurrentPathMode={3}! " + - "GIVING UP.", - vehicleID, + "CustomPassengerCarAI.ExtStartPathFind({0}): Could not calculate " + + "return path for citizen instance {1}, vehicle {2}. Resetting instance.", vehicleID, driverExtInstance.instanceId, - driverExtInstance.pathMode); + vehicleID); } extCitizenInstanceManager.Reset(ref driverExtInstance); return false; - } else { - // calculate a direct path to target - if (logParkingAi) { - Log._DebugFormat( - "CustomPassengerCarAI.ExtStartPathFind({0}): No alternative parking " + - "spot found for vehicle {1}, citizen instance {2} with CurrentPathMode={3}! " + - "Setting CurrentPathMode to 'CalculatingCarPath'.", - vehicleID, - vehicleID, - driverExtInstance.instanceId, - driverExtInstance.pathMode); - } + } + } else if (driverExtInstance.pathMode == + ExtPathMode.CalculatingCarPathToAltParkPos) { + // no alternative parking spot found: abort + if (logParkingAi) { + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): No alternative parking " + + "spot found for vehicle {1}, citizen instance {2} with CurrentPathMode={3}! " + + "GIVING UP.", + vehicleID, + vehicleID, + driverExtInstance.instanceId, + driverExtInstance.pathMode); + } - driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; + extCitizenInstanceManager.Reset(ref driverExtInstance); + return false; + } else { + // calculate a direct path to target + if (logParkingAi) { + Log._DebugFormat( + "CustomPassengerCarAI.ExtStartPathFind({0}): No alternative parking " + + "spot found for vehicle {1}, citizen instance {2} with CurrentPathMode={3}! " + + "Setting CurrentPathMode to 'CalculatingCarPath'.", + vehicleID, + vehicleID, + driverExtInstance.instanceId, + driverExtInstance.pathMode); } + + driverExtInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget; } + } - extPathType = driverExtInstance.GetPathType(); - driverExtInstance.atOutsideConnection = - Constants.ManagerFactory.ExtCitizenInstanceManager.IsAtOutsideConnection( - driverInstanceId, - ref driverInstance, - ref driverExtInstance, - startPos); - } // end if Options.ParkingAi4 - } // end benchmark + extPathType = driverExtInstance.GetPathType(); + driverExtInstance.atOutsideConnection = + Constants.ManagerFactory.ExtCitizenInstanceManager.IsAtOutsideConnection( + driverInstanceId, + ref driverInstance, + ref driverExtInstance, + startPos); + } // end if Options.ParkingAi4 var laneTypes = NetInfo.LaneType.Vehicle; diff --git a/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs b/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs index aebb6158d..c0a787888 100644 --- a/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs +++ b/TLM/TLM/Patch/_RoadBaseAI/SegmentSimulationStepPatch.cs @@ -1,10 +1,8 @@ -namespace TrafficManager.Patch._RoadBaseAI { +namespace TrafficManager.Patch._RoadBaseAI { using API.Traffic.Enums; using ColossalFramework; using JetBrains.Annotations; using TrafficManager.State; - using CSUtil.Commons.Benchmark; - // [Harmony] Manually patched because struct references are used public class SegmentSimulationStepPatch { @@ -31,34 +29,28 @@ public static void Prefix(RoadBaseAI __instance, ushort segmentID, ref NetSegmen // ↑↑↑↑ // TODO check if this is required *END* -#if BENCHMARK - using (Benchmark.MaybeCreateBenchmark(null, "Traffic-measurement")) { -#endif - if (segmentID < lastSimulatedSegmentId) { - // segment simulation restart - ++trafficMeasurementMod; - if (trafficMeasurementMod >= 4) - trafficMeasurementMod = 0; - } + if (segmentID < lastSimulatedSegmentId) { + // segment simulation restart + ++trafficMeasurementMod; + if (trafficMeasurementMod >= 4) + trafficMeasurementMod = 0; + } - lastSimulatedSegmentId = segmentID; + lastSimulatedSegmentId = segmentID; - bool doTrafficMeasurement = true; - if (Options.simulationAccuracy == SimulationAccuracy.High || - Options.simulationAccuracy == SimulationAccuracy.Medium) { - doTrafficMeasurement = (segmentID & 1) == trafficMeasurementMod; - } else if (Options.simulationAccuracy <= SimulationAccuracy.Low) { - doTrafficMeasurement = (segmentID & 3) == trafficMeasurementMod; - } + bool doTrafficMeasurement = true; + if (Options.simulationAccuracy == SimulationAccuracy.High || + Options.simulationAccuracy == SimulationAccuracy.Medium) { + doTrafficMeasurement = (segmentID & 1) == trafficMeasurementMod; + } else if (Options.simulationAccuracy <= SimulationAccuracy.Low) { + doTrafficMeasurement = (segmentID & 3) == trafficMeasurementMod; + } - if (doTrafficMeasurement) { - Constants.ManagerFactory.TrafficMeasurementManager.OnBeforeSimulationStep( - segmentID, - ref data); - } -#if BENCHMARK + if (doTrafficMeasurement) { + Constants.ManagerFactory.TrafficMeasurementManager.OnBeforeSimulationStep( + segmentID, + ref data); } -#endif } } } \ No newline at end of file diff --git a/TLM/TLM/State/OptionsTabs/OptionsMaintenanceTab.cs b/TLM/TLM/State/OptionsTabs/OptionsMaintenanceTab.cs index e5be75246..d0989438f 100644 --- a/TLM/TLM/State/OptionsTabs/OptionsMaintenanceTab.cs +++ b/TLM/TLM/State/OptionsTabs/OptionsMaintenanceTab.cs @@ -1,6 +1,5 @@ namespace TrafficManager.State { using ColossalFramework.UI; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using ICities; using JetBrains.Annotations; diff --git a/TLM/TLM/ThreadingExtension.cs b/TLM/TLM/ThreadingExtension.cs index 39a451302..9c5cff363 100644 --- a/TLM/TLM/ThreadingExtension.cs +++ b/TLM/TLM/ThreadingExtension.cs @@ -1,7 +1,6 @@ namespace TrafficManager { using ColossalFramework.UI; using ColossalFramework; - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using ICities; using JetBrains.Annotations; @@ -127,21 +126,19 @@ public override void OnBeforeSimulationFrame() { public override void OnUpdate(float realTimeDelta, float simulationTimeDelta) { base.OnUpdate(realTimeDelta, simulationTimeDelta); - using (var bm = CSUtil.Commons.Benchmark.Benchmark.MaybeCreateBenchmark()) { - if (ToolsModifierControl.toolController == null || ModUI.Instance == null) { - return; - } + if (ToolsModifierControl.toolController == null || ModUI.Instance == null) { + return; + } - TrafficManagerTool tmTool = ModUI.GetTrafficManagerTool(false); - if (tmTool != null && ToolsModifierControl.toolController.CurrentTool != tmTool && - ModUI.Instance.IsVisible()) { - ModUI.Instance.CloseMainMenu(); - } + TrafficManagerTool tmTool = ModUI.GetTrafficManagerTool(false); + if (tmTool != null && ToolsModifierControl.toolController.CurrentTool != tmTool && + ModUI.Instance.IsVisible()) { + ModUI.Instance.CloseMainMenu(); + } - if (Input.GetKeyDown(KeyCode.Escape)) { - ModUI.Instance.CloseMainMenu(); - } - } // end benchmark + if (Input.GetKeyDown(KeyCode.Escape)) { + ModUI.Instance.CloseMainMenu(); + } } } // end class } \ No newline at end of file diff --git a/TLM/TLM/TrafficLight/Impl/TimedTrafficLightsStep.cs b/TLM/TLM/TrafficLight/Impl/TimedTrafficLightsStep.cs index 62b118e16..e920a06b7 100644 --- a/TLM/TLM/TrafficLight/Impl/TimedTrafficLightsStep.cs +++ b/TLM/TLM/TrafficLight/Impl/TimedTrafficLightsStep.cs @@ -1,5 +1,4 @@ namespace TrafficManager.TrafficLight.Impl { - using CSUtil.Commons.Benchmark; using CSUtil.Commons; using ExtVehicleType = global::TrafficManager.API.Traffic.Enums.ExtVehicleType; using System.Collections.Generic; @@ -643,9 +642,7 @@ public bool StepDone(bool updateValues) { // Log._Debug($"TTL @ {timedNode.NodeId}: curFrame={curFrame} lastFlowWaitCalc={lastFlowWaitCalc}"); if (lastFlowWaitCalc < curFrame) { // Log._Debug($"TTL @ {timedNode.NodeId}: lastFlowWaitCalc { Log.Info(BenchmarkProfileProvider.Instance.CreateReport()); }); - } - - private void ClickResetBenchmarks(UIComponent component, UIMouseEventParameter eventParam) { - Constants.ServiceFactory.SimulationService.AddAction( - () => { BenchmarkProfileProvider.Instance.ClearProfiles(); }); - } - [UsedImplicitly] private void ClickVehicleToNone(UIComponent component, UIMouseEventParameter eventParam) { foreach (KeyValuePair> e in _customEmergencyLanes) { From 7b32ff40492a7712fc0db3823535458b0bd1bb63 Mon Sep 17 00:00:00 2001 From: egi Date: Sat, 23 May 2020 20:23:29 +0200 Subject: [PATCH 3/4] removed the old benchmark solution configuration and replaced it with a default release configuration just adding the BENCHMARK compilation symbol. --- TLM/CSUtil.Commons/CSUtil.Commons.csproj | 12 +------ TLM/TLM/Benchmark/BenchmarkManager.cs | 20 ++++++------ TLM/TLM/TLM.csproj | 24 +++++++------- TLM/TMPE.API/TMPE.API.csproj | 3 +- .../TMPE.CitiesGameBridge.csproj | 9 ------ .../TMPE.GenericGameBridge.csproj | 9 ------ .../TMPE.TestGameBridge.csproj | 9 ------ TLM/TMPE.UnitTest/TMPE.UnitTest.csproj | 13 ++------ TLM/TMPE.sln | 32 +++++++++---------- 9 files changed, 42 insertions(+), 89 deletions(-) diff --git a/TLM/CSUtil.Commons/CSUtil.Commons.csproj b/TLM/CSUtil.Commons/CSUtil.Commons.csproj index a0d1331eb..dfded453b 100644 --- a/TLM/CSUtil.Commons/CSUtil.Commons.csproj +++ b/TLM/CSUtil.Commons/CSUtil.Commons.csproj @@ -28,21 +28,11 @@ pdbonly true bin\Release\ - - + TRACE prompt 4 ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;TRACE;BENCHMARK - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ diff --git a/TLM/TLM/Benchmark/BenchmarkManager.cs b/TLM/TLM/Benchmark/BenchmarkManager.cs index 720a69b60..cb2334c3c 100644 --- a/TLM/TLM/Benchmark/BenchmarkManager.cs +++ b/TLM/TLM/Benchmark/BenchmarkManager.cs @@ -16,23 +16,23 @@ internal static class BenchmarkManager { /// Initializes the benchmarking. /// public static void Setup() { - var benchmark = new BenchmarkSimulationManager(); - SimulationManager.RegisterSimulationManager(benchmark); - LoadingManager.instance.m_levelUnloaded += benchmark.Stop; + var benchmarkSimulationManager = new BenchmarkSimulationManager(); + SimulationManager.RegisterSimulationManager(benchmarkSimulationManager); + LoadingManager.instance.m_levelUnloaded += benchmarkSimulationManager.Stop; } private sealed class BenchmarkSimulationManager : ISimulationManager { - private readonly Benchmark benchmark; + private readonly Benchmark simulationManagerSimulationStepBenchmark; private bool isRunning; public BenchmarkSimulationManager() { - benchmark = Benchmark.Create(0x1000 * 0x100); + simulationManagerSimulationStepBenchmark = Benchmark.Create(0x1000 * 0x100); SetupMethods(); } public void Stop() { - benchmark.Stop(); - benchmark.Dump(); + simulationManagerSimulationStepBenchmark.Stop(); + simulationManagerSimulationStepBenchmark.Dump(); isRunning = false; Log.Info("Benchmarking stopped."); } @@ -59,7 +59,7 @@ public void SimulationStep(int subStep) { try { // On failure, don't try to activate benchmark on each step - benchmark.Start(); + simulationManagerSimulationStepBenchmark.Start(); Log.Info("Benchmarking started."); } catch { @@ -67,7 +67,7 @@ public void SimulationStep(int subStep) { } if ((SimulationManager.instance.m_currentFrameIndex & 0xFFF) == 0xFFF) { - benchmark.MakeSnapshot(); + simulationManagerSimulationStepBenchmark.MakeSnapshot(); } } @@ -76,7 +76,7 @@ public void UpdateData(SimulationManager.UpdateMode mode) { private void SetupMethods() { try { - benchmark.BenchmarkMethod(typeof(SimulationManager), "SimulationStep"); + simulationManagerSimulationStepBenchmark.BenchmarkMethod(typeof(SimulationManager), "SimulationStep"); } catch { } diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 0744264f1..237cd35af 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -29,7 +29,7 @@ pdbonly true bin\Release\ - PF_DIJKSTRA;QUEUEDSTATS;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS + TRACE;PF_DIJKSTRA;QUEUEDSTATS;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS prompt 4 true @@ -46,17 +46,6 @@ prompt ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;QUEUEDSTATS;DEBUGVSTATE;DEBUGNEWPF;DEBUGCOSTS;DEBUGCONN2;DEBUGHWJUNCTIONROUTING;DEBUGROUTING;BENCHMARK;PF_DIJKSTRA;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS - true - 4 - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ @@ -86,6 +75,17 @@ $(CSPath)\Cities_Data\Managed ..\dependencies + + bin\Benchmark\ + TRACE;PF_DIJKSTRA;QUEUEDSTATS;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS;BENCHMARK + true + true + pdbonly + AnyCPU + latest + prompt + ..\TMPE.ruleset + ..\packages\Lib.Harmony.1.2.0.1\lib\net35\0Harmony.dll diff --git a/TLM/TMPE.API/TMPE.API.csproj b/TLM/TMPE.API/TMPE.API.csproj index 6179001d9..54cffa221 100644 --- a/TLM/TMPE.API/TMPE.API.csproj +++ b/TLM/TMPE.API/TMPE.API.csproj @@ -26,8 +26,7 @@ pdbonly true bin\Release\ - - + TRACE prompt 4 ..\TMPE.ruleset diff --git a/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj b/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj index d3f81479a..27fa9bde1 100644 --- a/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj +++ b/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj @@ -33,15 +33,6 @@ 4 ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;TRACE - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ diff --git a/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj b/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj index 03b9e4073..5ce8f2573 100644 --- a/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj +++ b/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj @@ -33,15 +33,6 @@ 4 ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;TRACE - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ diff --git a/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj b/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj index 999043a42..60f041551 100644 --- a/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj +++ b/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj @@ -33,15 +33,6 @@ 4 ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;TRACE - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ diff --git a/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj b/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj index 645b9b447..0540cd138 100644 --- a/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj +++ b/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj @@ -1,4 +1,4 @@ - + Debug @@ -38,15 +38,6 @@ 4 ..\TMPE.ruleset - - true - bin\Benchmark\ - DEBUG;TRACE - full - AnyCPU - prompt - ..\TMPE.ruleset - true bin\PF2_Debug\ @@ -168,4 +159,4 @@ --> - + \ No newline at end of file diff --git a/TLM/TMPE.sln b/TLM/TMPE.sln index 3a73f1996..04a37c00e 100644 --- a/TLM/TMPE.sln +++ b/TLM/TMPE.sln @@ -57,8 +57,8 @@ Global {7422AE58-8B0A-401C-9404-F4A438EFFE10}.Release LABS|Any CPU.Build.0 = Release LABS|Any CPU {7422AE58-8B0A-401C-9404-F4A438EFFE10}.Release|Any CPU.ActiveCfg = Release|Any CPU {7422AE58-8B0A-401C-9404-F4A438EFFE10}.Release|Any CPU.Build.0 = Release|Any CPU - {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Benchmark|Any CPU.Build.0 = Release|Any CPU {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Debug|Any CPU.Build.0 = Debug|Any CPU {D0D1848A-9BAE-4121-89A0-66757D16BC73}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -69,8 +69,8 @@ Global {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Release LABS|Any CPU.Build.0 = Release|Any CPU {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Release|Any CPU.ActiveCfg = Release|Any CPU {D0D1848A-9BAE-4121-89A0-66757D16BC73}.Release|Any CPU.Build.0 = Release|Any CPU - {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Benchmark|Any CPU.Build.0 = Release|Any CPU {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Debug|Any CPU.Build.0 = Debug|Any CPU {663B991F-32A1-46E1-A4D3-540F8EA7F386}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -81,8 +81,8 @@ Global {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Release LABS|Any CPU.Build.0 = Release|Any CPU {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Release|Any CPU.ActiveCfg = Release|Any CPU {663B991F-32A1-46E1-A4D3-540F8EA7F386}.Release|Any CPU.Build.0 = Release|Any CPU - {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Benchmark|Any CPU.Build.0 = Release|Any CPU {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -93,8 +93,8 @@ Global {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Release LABS|Any CPU.Build.0 = Release|Any CPU {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F2F7926-5D51-4880-A2B7-4594A10D7E54}.Release|Any CPU.Build.0 = Release|Any CPU - {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Benchmark|Any CPU.Build.0 = Release|Any CPU {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Debug|Any CPU.Build.0 = Debug|Any CPU {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -105,8 +105,8 @@ Global {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Release LABS|Any CPU.Build.0 = Release|Any CPU {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Release|Any CPU.ActiveCfg = Release|Any CPU {97DBFE4D-0DB1-43B3-AA35-067C06CF125B}.Release|Any CPU.Build.0 = Release|Any CPU - {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Benchmark|Any CPU.Build.0 = Release|Any CPU {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -117,8 +117,8 @@ Global {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Release LABS|Any CPU.Build.0 = Release|Any CPU {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Release|Any CPU.ActiveCfg = Release|Any CPU {D3ADE06E-F493-4819-865A-3BB44FEEDF01}.Release|Any CPU.Build.0 = Release|Any CPU - {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Benchmark|Any CPU.ActiveCfg = Benchmark|Any CPU - {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Benchmark|Any CPU.Build.0 = Benchmark|Any CPU + {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Benchmark|Any CPU.Build.0 = Release|Any CPU {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Debug|Any CPU.Build.0 = Debug|Any CPU {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -129,8 +129,8 @@ Global {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Release LABS|Any CPU.Build.0 = Release|Any CPU {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Release|Any CPU.ActiveCfg = Release|Any CPU {F4BEABA8-E56B-4201-99C8-5E0115E87D1C}.Release|Any CPU.Build.0 = Release|Any CPU - {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Benchmark|Any CPU.ActiveCfg = Debug|Any CPU - {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Benchmark|Any CPU.Build.0 = Debug|Any CPU + {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Benchmark|Any CPU.Build.0 = Release|Any CPU {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Debug|Any CPU.Build.0 = Debug|Any CPU {F8759084-DF5B-4A54-B73C-824640A8FA3F}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU @@ -141,8 +141,8 @@ Global {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Release LABS|Any CPU.Build.0 = Release|Any CPU {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Release|Any CPU.ActiveCfg = Release|Any CPU {F8759084-DF5B-4A54-B73C-824640A8FA3F}.Release|Any CPU.Build.0 = Release|Any CPU - {C911D31C-C85D-42A8-A839-7B209A715495}.Benchmark|Any CPU.ActiveCfg = Debug|Any CPU - {C911D31C-C85D-42A8-A839-7B209A715495}.Benchmark|Any CPU.Build.0 = Debug|Any CPU + {C911D31C-C85D-42A8-A839-7B209A715495}.Benchmark|Any CPU.ActiveCfg = Release|Any CPU + {C911D31C-C85D-42A8-A839-7B209A715495}.Benchmark|Any CPU.Build.0 = Release|Any CPU {C911D31C-C85D-42A8-A839-7B209A715495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C911D31C-C85D-42A8-A839-7B209A715495}.Debug|Any CPU.Build.0 = Debug|Any CPU {C911D31C-C85D-42A8-A839-7B209A715495}.FullDebug|Any CPU.ActiveCfg = Debug|Any CPU From 2a9839622a5a7f9bdc7a2e6c4c114a2420b69727 Mon Sep 17 00:00:00 2001 From: egi Date: Sat, 23 May 2020 20:29:15 +0200 Subject: [PATCH 4/4] removed TRACE constant from each project for release and benchmark configuration. --- TLM/CSUtil.Commons/CSUtil.Commons.csproj | 3 ++- TLM/TLM/TLM.csproj | 2 +- TLM/TMPE.API/TMPE.API.csproj | 3 ++- TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj | 3 ++- TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj | 3 ++- TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj | 3 ++- TLM/TMPE.UnitTest/TMPE.UnitTest.csproj | 3 ++- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/TLM/CSUtil.Commons/CSUtil.Commons.csproj b/TLM/CSUtil.Commons/CSUtil.Commons.csproj index dfded453b..228b2cea2 100644 --- a/TLM/CSUtil.Commons/CSUtil.Commons.csproj +++ b/TLM/CSUtil.Commons/CSUtil.Commons.csproj @@ -28,7 +28,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset diff --git a/TLM/TLM/TLM.csproj b/TLM/TLM/TLM.csproj index 237cd35af..5efeab1dd 100644 --- a/TLM/TLM/TLM.csproj +++ b/TLM/TLM/TLM.csproj @@ -77,7 +77,7 @@ bin\Benchmark\ - TRACE;PF_DIJKSTRA;QUEUEDSTATS;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS;BENCHMARK + PF_DIJKSTRA;QUEUEDSTATS;PARKINGAI;SPEEDLIMITS;ROUTING;JUNCTIONRESTRICTIONS;VEHICLERESTRICTIONS;ADVANCEDAI;CUSTOMTRAFFICLIGHTS;BENCHMARK true true pdbonly diff --git a/TLM/TMPE.API/TMPE.API.csproj b/TLM/TMPE.API/TMPE.API.csproj index 54cffa221..6179001d9 100644 --- a/TLM/TMPE.API/TMPE.API.csproj +++ b/TLM/TMPE.API/TMPE.API.csproj @@ -26,7 +26,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset diff --git a/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj b/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj index 27fa9bde1..95ad7e528 100644 --- a/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj +++ b/TLM/TMPE.CitiesGameBridge/TMPE.CitiesGameBridge.csproj @@ -28,7 +28,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset diff --git a/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj b/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj index 5ce8f2573..7e476ac4f 100644 --- a/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj +++ b/TLM/TMPE.GenericGameBridge/TMPE.GenericGameBridge.csproj @@ -28,7 +28,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset diff --git a/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj b/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj index 60f041551..3d2343180 100644 --- a/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj +++ b/TLM/TMPE.TestGameBridge/TMPE.TestGameBridge.csproj @@ -28,7 +28,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset diff --git a/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj b/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj index 0540cd138..6b4296e0a 100644 --- a/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj +++ b/TLM/TMPE.UnitTest/TMPE.UnitTest.csproj @@ -33,7 +33,8 @@ pdbonly true bin\Release\ - TRACE + + prompt 4 ..\TMPE.ruleset