From 5b2a4eafcbc8c73370be87702af2bb9c4431c07d Mon Sep 17 00:00:00 2001 From: David Walker Date: Sat, 17 Aug 2024 14:03:00 -0700 Subject: [PATCH] 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. --- src/Netscript/NetscriptHelpers.tsx | 12 ++---------- src/Script/ScriptHelpers.ts | 7 ++----- src/Terminal/HelpText.ts | 4 ++-- src/Terminal/commands/tail.ts | 6 +++--- src/ui/React/LogBoxManager.tsx | 2 +- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Netscript/NetscriptHelpers.tsx b/src/Netscript/NetscriptHelpers.tsx index 2737b6330..631810f08 100644 --- a/src/Netscript/NetscriptHelpers.tsx +++ b/src/Netscript/NetscriptHelpers.tsx @@ -10,7 +10,7 @@ import type { WorkerScript } from "./WorkerScript"; import React from "react"; import { killWorkerScript } from "./killWorkerScript"; -import { GetAllServers, GetServer } from "../Server/AllServers"; +import { GetServer } from "../Server/AllServers"; import { Player } from "@player"; import { ScriptDeath } from "./ScriptDeath"; import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber"; @@ -649,17 +649,9 @@ export function getRunningScriptsByArgs( 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 { if (typeof ident === "number") { - return getRunningScriptByPid(ident); + return findRunningScriptByPid(ident); } else { const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args); if (scripts === null) return null; diff --git a/src/Script/ScriptHelpers.ts b/src/Script/ScriptHelpers.ts index c87eb52dd..5bff465e8 100644 --- a/src/Script/ScriptHelpers.ts +++ b/src/Script/ScriptHelpers.ts @@ -98,12 +98,9 @@ export function findRunningScripts( return server.runningScriptMap.get(scriptKey(path, args)) ?? null; } -//Returns a RunningScript object matching the pid on the -//designated server, and false otherwise -export function findRunningScriptByPid(pid: number, server: BaseServer): RunningScript | null { +//Returns a RunningScript object with the given pid, or null +export function findRunningScriptByPid(pid: number): RunningScript | null { 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.scriptRef.server !== server.hostname) return null; return ws.scriptRef; } diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index 04954d2bc..6314cc51b 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -32,14 +32,14 @@ export const TerminalHelpText: string[] = [ " 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", " 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", " [--ram-override n] [args...]", " scan Prints all immediately-available network connections", " scan-analyze [d] [-a] Prints info for all servers up to d nodes away", " scp [files...] [server] Copies a file to a destination server", " 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", " 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", diff --git a/src/Terminal/commands/tail.ts b/src/Terminal/commands/tail.ts index 9e667a790..dfde2c85c 100644 --- a/src/Terminal/commands/tail.ts +++ b/src/Terminal/commands/tail.ts @@ -7,7 +7,7 @@ import { hasScriptExtension } from "../../Paths/ScriptFilePath"; export function tail(commandArray: (string | number | boolean)[], server: BaseServer): void { try { 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") { const [rawName, ...args] = commandArray; 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). LogBoxEvents.emit(candidates.values().next().value); } else if (typeof commandArray[0] === "number") { - const runningScript = findRunningScriptByPid(commandArray[0], server); + const runningScript = findRunningScriptByPid(commandArray[0]); 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; } LogBoxEvents.emit(runningScript); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 9bf2301fe..6dab2ece7 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -212,7 +212,7 @@ function LogWindow({ hidden, script, onClose }: LogWindowProps): React.ReactElem function run(): void { const server = GetServer(script.server); if (server === null) return; - const s = findRunningScriptByPid(script.pid, server); + const s = findRunningScriptByPid(script.pid); if (s === null) { const baseScript = server.scripts.get(script.filename); if (!baseScript) {