2022-09-06 15:07:12 +02:00
|
|
|
import { Terminal } from "../../Terminal";
|
2021-09-16 01:50:44 +02:00
|
|
|
import { numeralWrapper } from "../../ui/numeralFormat";
|
2022-01-11 11:12:59 +01:00
|
|
|
import { Settings } from "../../Settings/Settings";
|
2021-09-16 01:50:44 +02:00
|
|
|
|
2022-09-06 15:07:12 +02:00
|
|
|
export function mem(args: (string | number | boolean)[]): void {
|
2021-09-16 01:50:44 +02:00
|
|
|
try {
|
|
|
|
if (args.length !== 1 && args.length !== 3) {
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.error("Incorrect usage of mem command. usage: mem [scriptname] [-t] [number threads]");
|
2021-09-16 01:50:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptName = args[0] + "";
|
|
|
|
let numThreads = 1;
|
|
|
|
if (args.length === 3 && args[1] === "-t") {
|
|
|
|
numThreads = Math.round(parseInt(args[2] + ""));
|
|
|
|
if (isNaN(numThreads) || numThreads < 1) {
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.error("Invalid number of threads specified. Number of threads must be greater than 1");
|
2021-09-16 01:50:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:07:12 +02:00
|
|
|
const script = Terminal.getScript(scriptName);
|
2021-09-16 01:50:44 +02:00
|
|
|
if (script == null) {
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.error("mem failed. No such script exists!");
|
2021-09-16 01:50:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ramUsage = script.ramUsage * numThreads;
|
|
|
|
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.print(
|
2021-09-16 01:50:44 +02:00
|
|
|
`This script requires ${numeralWrapper.formatRAM(ramUsage)} of RAM to run for ${numThreads} thread(s)`,
|
|
|
|
);
|
2022-01-05 22:41:48 +01:00
|
|
|
|
|
|
|
const verboseEntries = script.ramUsageEntries?.sort((a, b) => b.cost - a.cost) ?? [];
|
2022-01-11 11:12:59 +01:00
|
|
|
const padding = Settings.UseIEC60027_2 ? 9 : 8;
|
2022-01-05 22:41:48 +01:00
|
|
|
for (const entry of verboseEntries) {
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.print(
|
2022-04-07 01:30:08 +02:00
|
|
|
`${numeralWrapper.formatRAM(entry.cost * numThreads).padStart(padding)} | ${entry.name} (${entry.type})`,
|
|
|
|
);
|
2022-01-05 22:41:48 +01:00
|
|
|
}
|
2022-01-07 03:13:38 +01:00
|
|
|
|
|
|
|
if (ramUsage > 0 && verboseEntries.length === 0) {
|
|
|
|
// Let's warn the user that he might need to save his script again to generate the detailed entries
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.warn("You might have to open & save this script to see the detailed RAM usage information.");
|
2022-01-07 03:13:38 +01:00
|
|
|
}
|
2021-09-16 01:50:44 +02:00
|
|
|
} catch (e) {
|
2022-09-06 15:07:12 +02:00
|
|
|
Terminal.error(e + "");
|
2021-09-16 01:50:44 +02:00
|
|
|
}
|
|
|
|
}
|