MISC: Improve several things relating to PID lookups (#1594)

findRunningScriptByPid needlessly took a "server" argument. This caused
there to be a "getRunningScriptByPid" version that did *not*, and it was
looping through all servers in order to function, which is needlessly
inefficient.

By removing the parameter and the needless inefficient helper method,
the following changes:
- Many Netscript functions such as isRunning() and getScript() become faster.
- The terminal "tail" command now works by pid regardless of the current
  server. Note that "kill" already worked this way.

I also improved the docs around "tail", since the pid argument wasn't
in the help.
This commit is contained in:
David Walker 2024-08-17 14:03:00 -07:00 committed by GitHub
parent 34db6e8b26
commit 5b2a4eafcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 21 deletions

@ -10,7 +10,7 @@ import type { WorkerScript } from "./WorkerScript";
import React from "react"; import React from "react";
import { killWorkerScript } from "./killWorkerScript"; import { killWorkerScript } from "./killWorkerScript";
import { GetAllServers, GetServer } from "../Server/AllServers"; import { GetServer } from "../Server/AllServers";
import { Player } from "@player"; import { Player } from "@player";
import { ScriptDeath } from "./ScriptDeath"; import { ScriptDeath } from "./ScriptDeath";
import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber"; import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber";
@ -649,17 +649,9 @@ export function getRunningScriptsByArgs(
return findRunningScripts(path, scriptArgs, server); return findRunningScripts(path, scriptArgs, server);
} }
function getRunningScriptByPid(pid: number): RunningScript | null {
for (const server of GetAllServers()) {
const runningScript = findRunningScriptByPid(pid, server);
if (runningScript) return runningScript;
}
return null;
}
function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): RunningScript | null { function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): RunningScript | null {
if (typeof ident === "number") { if (typeof ident === "number") {
return getRunningScriptByPid(ident); return findRunningScriptByPid(ident);
} else { } else {
const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args); const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
if (scripts === null) return null; if (scripts === null) return null;

@ -98,12 +98,9 @@ export function findRunningScripts(
return server.runningScriptMap.get(scriptKey(path, args)) ?? null; return server.runningScriptMap.get(scriptKey(path, args)) ?? null;
} }
//Returns a RunningScript object matching the pid on the //Returns a RunningScript object with the given pid, or null
//designated server, and false otherwise export function findRunningScriptByPid(pid: number): RunningScript | null {
export function findRunningScriptByPid(pid: number, server: BaseServer): RunningScript | null {
const ws = workerScripts.get(pid); const ws = workerScripts.get(pid);
// Return null if no ws found or if it's on a different server.
if (!ws) return null; if (!ws) return null;
if (ws.scriptRef.server !== server.hostname) return null;
return ws.scriptRef; return ws.scriptRef;
} }

@ -32,14 +32,14 @@ export const TerminalHelpText: string[] = [
" mv [src] [dest] Move/rename a text or script file", " mv [src] [dest] Move/rename a text or script file",
" nano [files...] Text editor - Open up and edit one or more scripts or text files", " nano [files...] Text editor - Open up and edit one or more scripts or text files",
" ps Display all scripts that are currently running", " ps Display all scripts that are currently running",
" rm [OPTIONS]... [FILE]... Delete a file from the server", " rm [OPTIONS]... [FILE]... Delete a file from the server",
" run [script] [-t n] [--tail] Execute a program or script", " run [script] [-t n] [--tail] Execute a program or script",
" [--ram-override n] [args...]", " [--ram-override n] [args...]",
" scan Prints all immediately-available network connections", " scan Prints all immediately-available network connections",
" scan-analyze [d] [-a] Prints info for all servers up to d nodes away", " scan-analyze [d] [-a] Prints info for all servers up to d nodes away",
" scp [files...] [server] Copies a file to a destination server", " scp [files...] [server] Copies a file to a destination server",
" sudov Shows whether you have root access on this computer", " sudov Shows whether you have root access on this computer",
" tail [script] [args...] Displays dynamic logs for the specified script", " tail [script/pid] [args...] Displays dynamic logs for the specified script",
" top Displays all running scripts and their RAM usage", " top Displays all running scripts and their RAM usage",
" unalias [alias name] Deletes the specified alias", " unalias [alias name] Deletes the specified alias",
" vim [files...] Text editor - Open up and edit one or more scripts or text files in vim mode", " vim [files...] Text editor - Open up and edit one or more scripts or text files in vim mode",

@ -7,7 +7,7 @@ import { hasScriptExtension } from "../../Paths/ScriptFilePath";
export function tail(commandArray: (string | number | boolean)[], server: BaseServer): void { export function tail(commandArray: (string | number | boolean)[], server: BaseServer): void {
try { try {
if (commandArray.length < 1) { if (commandArray.length < 1) {
Terminal.error("Incorrect number of arguments. Usage: tail [script] [arg1] [arg2]..."); Terminal.error("Incorrect number of arguments. Usage: tail [pid] or tail [scriptname] [arg1] [arg2]...");
} else if (typeof commandArray[0] === "string") { } else if (typeof commandArray[0] === "string") {
const [rawName, ...args] = commandArray; const [rawName, ...args] = commandArray;
const path = Terminal.getFilepath(rawName); const path = Terminal.getFilepath(rawName);
@ -25,9 +25,9 @@ export function tail(commandArray: (string | number | boolean)[], server: BaseSe
// arguments, they can't be distinguished except by pid). // arguments, they can't be distinguished except by pid).
LogBoxEvents.emit(candidates.values().next().value); LogBoxEvents.emit(candidates.values().next().value);
} else if (typeof commandArray[0] === "number") { } else if (typeof commandArray[0] === "number") {
const runningScript = findRunningScriptByPid(commandArray[0], server); const runningScript = findRunningScriptByPid(commandArray[0]);
if (runningScript == null) { if (runningScript == null) {
Terminal.error(`No script with PID ${commandArray[0]} is running on the server`); Terminal.error(`No script with PID ${commandArray[0]} is running`);
return; return;
} }
LogBoxEvents.emit(runningScript); LogBoxEvents.emit(runningScript);

@ -212,7 +212,7 @@ function LogWindow({ hidden, script, onClose }: LogWindowProps): React.ReactElem
function run(): void { function run(): void {
const server = GetServer(script.server); const server = GetServer(script.server);
if (server === null) return; if (server === null) return;
const s = findRunningScriptByPid(script.pid, server); const s = findRunningScriptByPid(script.pid);
if (s === null) { if (s === null) {
const baseScript = server.scripts.get(script.filename); const baseScript = server.scripts.get(script.filename);
if (!baseScript) { if (!baseScript) {