NETSCRIPT: Rework script ram updates (#408)

This commit is contained in:
Snarling
2023-03-05 22:39:42 -05:00
committed by GitHub
parent 14aafbe0a3
commit 759f86d6e5
24 changed files with 171 additions and 177 deletions
+9 -8
View File
@@ -10,7 +10,9 @@ import { Terminal } from "../Terminal";
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
import { formatTime } from "../utils/helpers/formatTime";
import { ScriptArg } from "../Netscript/ScriptArg";
import { ScriptArg } from "@nsdefs";
import { RamCostConstants } from "../Netscript/RamCostGenerator";
import { PositiveInteger } from "../types";
export class RunningScript {
// Script arguments
@@ -52,25 +54,24 @@ export class RunningScript {
pid = -1;
// How much RAM this script uses for ONE thread
ramUsage = 0;
ramUsage = RamCostConstants.Base;
// hostname of the server on which this script is running
server = "";
// Number of threads that this script is running with
threads = 1;
threads = 1 as PositiveInteger;
// Script urls for the current running script for translating urls back to file names in errors
dependencies: ScriptUrl[] = [];
constructor(script: Script | null = null, args: ScriptArg[] = []) {
if (script == null) {
return;
}
constructor(script?: Script, ramUsage?: number, args: ScriptArg[] = []) {
if (!script) return;
if (!ramUsage) throw new Error("Must provide a ramUsage for RunningScript initialization.");
this.filename = script.filename;
this.args = args;
this.server = script.server;
this.ramUsage = script.ramUsage;
this.ramUsage = ramUsage;
this.dependencies = script.dependencies;
}
-22
View File
@@ -1,22 +0,0 @@
import { GetServer } from "../Server/AllServers";
import { RunningScript } from "./RunningScript";
export function getRamUsageFromRunningScript(script: RunningScript): number {
if (script.ramUsage != null && script.ramUsage > 0) {
return script.ramUsage; // Use cached value
}
const server = GetServer(script.server);
if (server == null) {
return 0;
}
for (let i = 0; i < server.scripts.length; ++i) {
if (server.scripts[i].filename === script.filename) {
// Cache the ram usage for the next call
script.ramUsage = server.scripts[i].ramUsage;
return script.ramUsage;
}
}
return 0;
}
+25 -17
View File
@@ -10,6 +10,7 @@ import { ScriptUrl } from "./ScriptUrl";
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
import { roundToTwo } from "../utils/helpers/roundToTwo";
import { ScriptModule } from "./ScriptModule";
import { RamCostConstants } from "../Netscript/RamCostGenerator";
let globalModuleSequenceNumber = 0;
@@ -41,8 +42,8 @@ export class Script {
dependencies: ScriptUrl[] = [];
dependents: ScriptReference[] = [];
// Amount of RAM this Script requires to run
ramUsage = 0;
// Amount of RAM this Script requires to run. null indicates an error in calculating ram.
ramUsage: number | null = null;
ramUsageEntries?: RamUsageEntry[];
// Used to deconflict multiple simultaneous compilations.
@@ -51,14 +52,11 @@ export class Script {
// hostname of server that this script is on.
server = "";
constructor(fn = "", code = "", server = "", otherScripts: Script[] = []) {
constructor(fn = "", code = "", server = "") {
this.filename = fn;
this.code = code;
this.server = server; // hostname of server this script is on
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
if (this.code !== "") {
this.updateRamUsage(otherScripts);
}
}
/** Download the script as a file */
@@ -97,14 +95,24 @@ export class Script {
this.filename = filename;
this.server = hostname;
this.updateRamUsage(otherScripts);
// Null ramUsage forces a recalc next time ramUsage is needed
this.ramUsage = null;
this.markUpdated();
for (const dependent of this.dependents) {
const [dependentScript] = otherScripts.filter(
(s) => s.filename === dependent.filename && s.server == dependent.server,
this.dependents.forEach((dependent) => {
const scriptToUpdate = otherScripts.find(
(otherScript) => otherScript.filename === dependent.filename && otherScript.server === dependent.server,
);
dependentScript?.markUpdated();
}
if (!scriptToUpdate) return;
scriptToUpdate.ramUsage = null;
scriptToUpdate.markUpdated();
});
}
/** Gets the ram usage, while also attempting to update it if it's currently null */
getRamUsage(otherScripts: Script[]): number | null {
if (this.ramUsage) return this.ramUsage;
this.updateRamUsage(otherScripts);
return this.ramUsage;
}
/**
@@ -112,11 +120,11 @@ export class Script {
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
*/
updateRamUsage(otherScripts: Script[]): void {
const res = calculateRamUsage(this.code, otherScripts);
if (res.cost > 0) {
this.ramUsage = roundToTwo(res.cost);
this.ramUsageEntries = res.entries;
}
const ramCalc = calculateRamUsage(this.code, otherScripts);
if (ramCalc.cost >= RamCostConstants.Base) {
this.ramUsage = roundToTwo(ramCalc.cost);
this.ramUsageEntries = ramCalc.entries;
} else this.ramUsage = null;
this.markUpdated();
}