Initial commit for converting workerScripts pool to Map data structure

This commit is contained in:
danielyxie 2019-06-19 01:03:08 -07:00
parent 931de230ae
commit 821725cf4d
4 changed files with 64 additions and 3 deletions

@ -80,6 +80,12 @@ export class WorkerScript {
*/ */
output: string = ""; output: string = "";
/**
* Process ID. Must be an integer. Used for efficient script
* killing and removal.
*/
pid: number;
/** /**
* Script's Static RAM usage. Equivalent to underlying script's RAM usage * Script's Static RAM usage. Equivalent to underlying script's RAM usage
*/ */
@ -100,10 +106,17 @@ export class WorkerScript {
*/ */
serverIp: string; serverIp: string;
constructor(runningScriptObj: RunningScript, nsFuncsGenerator?: (ws: WorkerScript) => object) { constructor(runningScriptObj: RunningScript, pid: number, nsFuncsGenerator?: (ws: WorkerScript) => object) {
this.name = runningScriptObj.filename; this.name = runningScriptObj.filename;
this.serverIp = runningScriptObj.server; this.serverIp = runningScriptObj.server;
const sanitizedPid = Math.round(pid);
if (typeof sanitizedPid !== "number" || isNaN(sanitizedPid)) {
throw new Error(`Invalid PID when constructing WorkerScript: ${pid}`);
}
this.pid = sanitizedPid;
runningScriptObj.pid = sanitizedPid;
// Get the underlying script's code // Get the underlying script's code
const server = AllServers[this.serverIp]; const server = AllServers[this.serverIp];
if (server == null) { if (server == null) {

@ -3,4 +3,4 @@
*/ */
import { WorkerScript } from "./WorkerScript"; import { WorkerScript } from "./WorkerScript";
export const workerScripts: WorkerScript[] = []; export const workerScripts: Map<number, WorkerScript> = new Map();

@ -407,6 +407,42 @@ function processNetscript1Imports(code, workerScript) {
return res; return res;
} }
/**
*
*/
let pidCounter = 1;
function generateNextPid() {
let tempCounter = pidCounter;
// Cap the number of search iterations at some arbitrary value to avoid
// infinite loops. We'll assume that players wont have 1mil+ running scripts
let found = false;
for (let i = 0; i < 1e6;) {
if (!workerScripts.has(tempCounter + i)) {
found = true;
tempCounter = tempCounter + i;
break;
}
if (i === Number.MAX_SAFE_INTEGER - 1) {
i = 1;
} else {
++i;
}
}
if (found) {
pidCounter = tempCounter + 1;
if (pidCounter >= Number.MAX_SAFE_INTEGER) {
pidCounter = 1;
}
return tempCounter;
} else {
return -1;
}
}
/** /**
* Start a script * Start a script
* *
@ -438,8 +474,17 @@ export function addWorkerScript(runningScriptObj, server) {
} }
server.ramUsed = roundToTwo(server.ramUsed + ramUsage); server.ramUsed = roundToTwo(server.ramUsed + ramUsage);
// Get the pid
const pid = getNextPid();
if (pid === -1) {
throw new Error(
`Failed to start script because could not find available PID. This is most ` +
`because you have too many scripts running.`
);
}
// Create the WorkerScript // Create the WorkerScript
const s = new WorkerScript(runningScriptObj, NetscriptFunctions); const s = new WorkerScript(runningScriptObj, pid, NetscriptFunctions);
s.ramUsage = ramUsage; s.ramUsage = ramUsage;
// Start the script's execution // Start the script's execution

@ -56,6 +56,9 @@ export class RunningScript {
// Number of seconds that this script has been running online // Number of seconds that this script has been running online
onlineRunningTime: number = 0.01; onlineRunningTime: number = 0.01;
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
pid: number = -1;
// How much RAM this script uses for ONE thread // How much RAM this script uses for ONE thread
ramUsage: number = 0; ramUsage: number = 0;