Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Telecom/RuntimeMetrics.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace σκοπός {
internal class RuntimeMetrics {
public RuntimeMetrics() { }
public int num_fixed_update_iterations_ = 0;
public double fixed_update_runtime_ = 0;
public int num_iterations_ = 0;
public double total_runtime_ = 0;

public double AverageFixedUpdateRuntime => fixed_update_runtime_ / num_fixed_update_iterations_;
public double AverageRefreshRuntime => total_runtime_ / num_iterations_;
}
}
4 changes: 2 additions & 2 deletions Telecom/main_window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ protected override void RenderWindowContents(int window_id) {

using (new UnityEngine.GUILayout.HorizontalScope()) {
UnityEngine.GUILayout.Label($"Contracted connections: {telecom_.network.contracted_connections.Count}");
UnityEngine.GUILayout.Label($"Fixed Updates: {telecom_.runtimeMetrics_.num_fixed_update_iterations_}");
UnityEngine.GUILayout.Label($"Average Runtime: {telecom_.runtimeMetrics_.AverageFixedUpdateRuntime:F2} ms");
UnityEngine.GUILayout.Label($"Total Runs: {telecom_.runtimeMetrics_.num_iterations_}");
UnityEngine.GUILayout.Label($"Average Total Runtime: {telecom_.runtimeMetrics_.AverageRefreshRuntime:F2} ms");
}

var inspected_connections = connection_inspectors_.Keys.ToArray();
Expand Down
27 changes: 15 additions & 12 deletions Telecom/network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,9 @@ public void Refresh() {
var metrics = Telecom.Instance.runtimeMetrics_;
refresh_watch_.Start();
UpdateConnections();
foreach (RealAntennaDigital antenna in routing_.usage.Transmitters()) {
if ((antenna?.ParentNode as RACommNode).ParentVessel is Vessel vessel) {
Kerbalism.ConsumeResource(
vessel,
"ElectricCharge",
// PowerDrawLinear is in mW, ElectricCharge is in kJ.
routing_.usage.TxPowerUsage(antenna) * antenna.PowerDrawLinear * 1e-6 * TimeWarp.fixedDeltaTime,
"Σκοπός telecom");
}
}
refresh_watch_.Stop();
metrics.num_fixed_update_iterations_++;
metrics.fixed_update_runtime_ = refresh_watch_.Elapsed.TotalMilliseconds;
++metrics.num_iterations_;
metrics.total_runtime_ = refresh_watch_.Elapsed.TotalMilliseconds;
UnityEngine.Profiling.Profiler.EndSample();
}

Expand All @@ -198,6 +188,19 @@ private void UpdateConnections() {
}
}

public void ConsumeElectricCharge() {
foreach (RealAntennaDigital antenna in routing_?.usage?.Transmitters()) {
if ((antenna?.ParentNode as RACommNode).ParentVessel is Vessel vessel) {
Kerbalism.ConsumeResource(
vessel,
"ElectricCharge",
// PowerDrawLinear is in mW, ElectricCharge is in kJ.
routing_.usage.TxPowerUsage(antenna) * antenna.PowerDrawLinear * 1e-6 * TimeWarp.fixedDeltaTime,
"Σκοπός telecom");
}
}
}

internal void ReloadContractConnections() {
connections_by_contract.Clear();
contracted_connections.Clear();
Expand Down
23 changes: 22 additions & 1 deletion Telecom/telecom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public void Start() {
Log("Starting");
enabled = false;
GameEvents.CommNet.OnNetworkInitialized.Add(NetworkInitializedNotify);
GameEvents.CommNet.OnNetworkInitialized.Add(AddPostUpdateHandler);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add, you typically need to Remove in OnDestroy

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that explains the memory leak I saw. Fixed.

GameEvents.Contract.onContractsLoaded.Add(NotifyContractsLoaded);
StartCoroutine(CreateNetwork());
}

public void OnDestroy() {
Log("Destroying");
GameEvents.CommNet.OnNetworkInitialized.Remove(NetworkInitializedNotify);
GameEvents.CommNet.OnNetworkInitialized.Remove(AddPostUpdateHandler);
GameEvents.Contract.onAccepted.Remove(ReloadContractConnections);
GameEvents.Contract.onFinished.Remove(ReloadContractConnections);
GameEvents.Contract.onContractsLoaded.Remove(NotifyContractsLoaded);
Expand All @@ -68,6 +70,18 @@ private void NetworkInitializedNotify() {
Log("CommNet Network Initialization fired.");
}

private void AddPostUpdateHandler() {
((RACommNetwork) RACommNetNetwork.Instance.CommNet).NetworkUpdateComplete.Add(PostUpdateHandler);
}

private void PostUpdateHandler() {
double network_last_ut = ((RACommNetwork) RACommNetNetwork.Instance.CommNet).LastUpdateUT;
if (network_last_ut > last_update_ut_) {
do_refresh_ = true;
last_update_ut_ = network_last_ut;
}
}
Copy link
Copy Markdown
Contributor

@DRVeyl DRVeyl Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to assume the duplicate cast and derefrences get JIT-compiled out. But maybe they don't, and it probably makes readability better to do:

var network_last = ((RACommNetwork) RACommNetNetwork.Instance.CommNet).LastUpdateUT;
if (network_last > last_update_ut) {
  do_refresh = true;
  last_update_ut = network_last;
}


private IEnumerator CreateNetwork() {
while (RACommNetScenario.RACN == null || !CommNet.CommNetNetwork.Initialized) {
yield return new UnityEngine.WaitForFixedUpdate();
Expand Down Expand Up @@ -180,7 +194,12 @@ private void FixedUpdate() {
// Time does not advance in the VAB, but after a revert, it is incorrectly stuck in the past.
ut_ = Planetarium.GetUniversalTime();
}
network?.Refresh();

if (do_refresh_) {
do_refresh_ = false; // Unset now so that it can reset if RA updates while we're working.
network?.Refresh();
}
network?.ConsumeElectricCharge();
}

private void LateUpdate() {
Expand Down Expand Up @@ -231,5 +250,7 @@ link.b is RACommNode node_b &&
private KSP.UI.Screens.ApplicationLauncherButton toolbar_button_;

internal RuntimeMetrics runtimeMetrics_ = new RuntimeMetrics();
internal bool do_refresh_ = false;
private double last_update_ut_;
}
}