Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // poll the specified port for jobs to run on the specified server,
- // leaving the specified amount of free ram
- server = args[0]; // run server
- RAM_RESERVE = args[1]; // free ram to leave unused when launching jobs
- PORT_HIGH = 2; // higher priority port (for grow/weaken jobs broken off from their hack parent jobs)
- PORT_LOW = 1; // lower priority job port (for initial job launch)
- // copy scripts to run server
- scp('weaken.script',server);
- scp('grow.script',server);
- scp('hackloop.script',server);
- // compile ram usage of each thread
- WEAK_THREAD_RAM = getScriptRam('weaken.script', server);
- GROW_THREAD_RAM = getScriptRam('grow.script', server);
- HACK_THREAD_RAM = getScriptRam('hackloop.script', server);
- // max ram needed by a single thread of a job script
- MAX_THREAD_RAM = Math.max(WEAK_THREAD_RAM,GROW_THREAD_RAM,HACK_THREAD_RAM);
- function getFreeRam() {
- ram = getServerRam(server);
- return ram[0] - ram[1] - RAM_RESERVE;
- }
- function pollForJob() {
- job = read(PORT_HIGH);
- if (job === "NULL PORT DATA") {
- job = read(PORT_LOW);
- }
- if (job === "NULL PORT DATA") {
- return null;
- } else {
- return job;
- }
- }
- freeRam = getFreeRam();
- while (true) {
- // wait for sufficient ram to launch at least one thread
- while (freeRam < MAX_THREAD_RAM) {
- sleep(5000,false); // give recently launched jobs time to be reflected in server ram
- freeRam = getFreeRam();
- }
- // poll the port for a job
- job = pollForJob();
- while (job === null) {
- sleep(1000,false);
- job = pollForJob();
- }
- // parse the job
- jobArgs = job.split(',');
- target = jobArgs[0]; // target server for job
- hackThreads = 1 * jobArgs[1]; //
- growThreads = 1 * jobArgs[2]; // convert strings to numbers
- weakThreads = 1 * jobArgs[3]; //
- monID = 1 * jobArgs[4]; // monitor ID (-1 for no previous job to monitor)
- freeRam = getFreeRam();
- while (freeRam < MAX_THREAD_RAM) {
- sleep(1000,false);
- freeRam = getFreeRam();
- }
- runThreads = [0,0,0];
- if (hackThreads > 0) {
- runThreads[0] = Math.min(hackThreads,Math.floor(freeRam / HACK_THREAD_RAM));
- if (runThreads[0] < hackThreads) {
- // not enough ram to run all the hack threads, scale down the grow/weak too
- frac = runThreads[0] / hackThreads;
- growThreads = Math.ceil(growThreads * frac);
- weakThreads = Math.ceil(weakThreads * frac);
- freeRam -= runThreads[0] * HACK_THREAD_RAM;
- }
- hackThreads = 0; // we don't split up hack jobs, just run a smaller one instead
- }
- if (growThreads > 0 && freeRam > GROW_THREAD_RAM) {
- runThreads[1] = Math.min(growThreads,Math.floor(freeRam / GROW_THREAD_RAM));
- freeRam -= runThreads[1] * GROW_THREAD_RAM;
- growThreads -= runThreads[1];
- }
- if (weakThreads > 0 && freeRam > WEAK_THREAD_RAM) {
- runThreads[2] = Math.min(weakThreads,Math.floor(freeRam / WEAK_THREAD_RAM));
- freeRam -= runThreads[2] * WEAK_THREAD_RAM;
- weakThreads -= runThreads[2];
- }
- partial = false;
- if (growThreads + weakThreads) { partial = true; }
- print('partial=' + partial +
- ', hackThreads=' + hackThreads +
- ', growThreads=' + growThreads +
- ', weakThreads=' + weakThreads
- );
- // remove any stale monitor file left from a previous killed run
- if (monID >= 0) {
- rm(target + monID);
- }
- run('launchmon2.script',1, // launch script with 1 thread
- partial, // is this a partial subjob that needs to signal completion?
- server,target,
- runThreads[0],runThreads[1],runThreads[2],
- monID // supplementary monitor ID
- );
- // if we couldn't launch the whole job, push the remainder back to the high priority queue
- if (partial) {
- ++monID;
- jobStrings = [target,hackThreads,growThreads,weakThreads,monID];
- write(PORT_HIGH,jobStrings.join(','));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement