-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathshare.js
91 lines (82 loc) · 2.47 KB
/
share.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { GetAllServers } from "utils.js";
import { RunScript, MemoryMap } from "ram.js";
/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog('ALL');
let [pct = 0.95] = ns.args;
if (ns.args.includes('auto')) {
const ram = new MemoryMap(ns, true);
if (ram.total < 5000)
return;
else if (ram.total < 5000)
pct = 0.15;
else if (ram.total < 10000)
pct = 0.2;
else if (ram.total < 15000)
pct = 0.25;
else if (ram.total < 25000)
pct = 0.30;
else
pct = 0.35
}
if (ns.args.includes('stop')) {
const data = FindInstances(ns)
// Kill all existing instances of share-forever.js
for (const proc of data.shares) {
ns.tprint('Killing share-forever.js PID ' + proc.pid);
ns.kill(proc.pid);
}
for (const proc of data.dupes) {
ns.tprint('Killing share.js PID ' + proc.pid);
ns.kill(proc.pid);
}
return;
}
for (; ;) {
ns.print('');
ns.print('');
await AdjustUsage(ns, pct);
ns.print('Current share power: ' + ns.getSharePower());
await ns.sleep(5000);
}
}
function FindInstances(ns) {
let allProcs = [];
let dupes = [];
let totalRam = 0;
for (const server of GetAllServers(ns)) {
let procs = ns.ps(server);
allProcs.push(...procs.filter(s => s.filename == 'share-forever.js'));
dupes.push(...procs.filter(s => s.filename == 'share.js' && s.args[0] != 'stop'));
if (ns.hasRootAccess(server))
totalRam += ns.getServerMaxRam(server);
}
return {
shares: allProcs.sort((a, b) => a.threads - b.threads),
dupes: dupes,
totalRam: totalRam
};
}
async function AdjustUsage(ns, pct) {
let data = FindInstances(ns);
let shareThreads = data.shares.reduce((a, s) => a += s.threads, 0);
let scriptRam = ns.getScriptRam('share-forever.js');
let sharePct = (shareThreads * scriptRam) / data.totalRam;
let targetThreads = Math.ceil(data.totalRam * pct / scriptRam);
if (shareThreads > targetThreads) {
let needToKill = shareThreads - targetThreads;
while (needToKill > 0 && data.shares.length > 0) {
ns.print('Killing ' + data.shares[0].threads + ' share threads');
shareThreads -= data.shares[0].threads;
needToKill -= data.shares[0].threads;
ns.kill(data.shares[0].pid);
data.shares.shift();
}
sharePct = (shareThreads * scriptRam) / data.totalRam;
}
if (sharePct < pct) {
let missingThreads = targetThreads - shareThreads;
ns.print('Attempting to start ' + missingThreads + ' share threads.');
await RunScript(ns, 'share-forever.js', missingThreads, ['', performance.now(), true], true, true);
}
}