Advertisement
Guest User

runjob2

a guest
Mar 1st, 2018
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // poll the specified port for jobs to run on the specified server,
  2. //  leaving the specified amount of free ram
  3. server = args[0];   // run server
  4. RAM_RESERVE = args[1];  // free ram to leave unused when launching jobs
  5. PORT_HIGH = 2;      // higher priority port (for grow/weaken jobs broken off from their hack parent jobs)
  6. PORT_LOW = 1;       // lower priority job port (for initial job launch)
  7.  
  8. // copy scripts to run server
  9. scp('weaken.script',server);
  10. scp('grow.script',server);
  11. scp('hackloop.script',server);
  12. // compile ram usage of each thread
  13. WEAK_THREAD_RAM = getScriptRam('weaken.script', server);
  14. GROW_THREAD_RAM = getScriptRam('grow.script', server);
  15. HACK_THREAD_RAM = getScriptRam('hackloop.script', server);
  16. // max ram needed by a single thread of a job script
  17. MAX_THREAD_RAM = Math.max(WEAK_THREAD_RAM,GROW_THREAD_RAM,HACK_THREAD_RAM);
  18.  
  19. function getFreeRam() {
  20.     ram = getServerRam(server);
  21.     return ram[0] - ram[1] - RAM_RESERVE;
  22. }
  23.  
  24. function pollForJob() {
  25.     job = read(PORT_HIGH);
  26.     if (job === "NULL PORT DATA") {
  27.         job = read(PORT_LOW);
  28.     }
  29.     if (job === "NULL PORT DATA") {
  30.         return null;
  31.     } else {
  32.         return job;
  33.     }
  34. }
  35.  
  36.  
  37. freeRam = getFreeRam();
  38. while (true) {
  39.    
  40.     // wait for sufficient ram to launch at least one thread
  41.     while (freeRam < MAX_THREAD_RAM) {
  42.         sleep(5000,false); // give recently launched jobs time to be reflected in server ram
  43.         freeRam = getFreeRam();
  44.     }
  45.    
  46.     // poll the port for a job
  47.     job = pollForJob();
  48.     while (job === null) {
  49.         sleep(1000,false);
  50.         job = pollForJob();
  51.     }
  52.    
  53.     // parse the job
  54.     jobArgs = job.split(',');
  55.     target = jobArgs[0];     // target server for job
  56.     hackThreads = 1 * jobArgs[1]; //
  57.     growThreads = 1 * jobArgs[2]; // convert strings to numbers
  58.     weakThreads = 1 * jobArgs[3]; //
  59.     monID = 1 * jobArgs[4];     // monitor ID (-1 for no previous job to monitor)
  60.    
  61.     freeRam = getFreeRam();
  62.     while (freeRam < MAX_THREAD_RAM) {
  63.         sleep(1000,false);
  64.         freeRam = getFreeRam();
  65.     }
  66.    
  67.     runThreads = [0,0,0];
  68.     if (hackThreads > 0) {
  69.         runThreads[0] = Math.min(hackThreads,Math.floor(freeRam / HACK_THREAD_RAM));
  70.         if (runThreads[0] < hackThreads) {
  71.             // not enough ram to run all the hack threads, scale down the grow/weak too
  72.             frac = runThreads[0] / hackThreads;
  73.             growThreads = Math.ceil(growThreads * frac);
  74.             weakThreads = Math.ceil(weakThreads * frac);
  75.             freeRam -= runThreads[0] * HACK_THREAD_RAM;
  76.         }
  77.         hackThreads = 0; // we don't split up hack jobs, just run a smaller one instead
  78.     }
  79.     if (growThreads > 0 && freeRam > GROW_THREAD_RAM) {
  80.         runThreads[1] = Math.min(growThreads,Math.floor(freeRam / GROW_THREAD_RAM));
  81.         freeRam -= runThreads[1] * GROW_THREAD_RAM;
  82.         growThreads -= runThreads[1];
  83.     }
  84.     if (weakThreads > 0 && freeRam > WEAK_THREAD_RAM) {
  85.         runThreads[2] = Math.min(weakThreads,Math.floor(freeRam / WEAK_THREAD_RAM));
  86.         freeRam -= runThreads[2] * WEAK_THREAD_RAM;
  87.         weakThreads -= runThreads[2];
  88.     }
  89.    
  90.     partial = false;
  91.     if (growThreads + weakThreads) { partial = true; }
  92.     print('partial=' + partial +
  93.             ', hackThreads=' + hackThreads +
  94.             ', growThreads=' + growThreads +
  95.             ', weakThreads=' + weakThreads
  96.             );
  97.     // remove any stale monitor file left from a previous killed run
  98.     if (monID >= 0) {
  99.         rm(target + monID);
  100.     }
  101.     run('launchmon2.script',1,  // launch script with 1 thread
  102.         partial,                // is this a partial subjob that needs to signal completion?
  103.         server,target,
  104.         runThreads[0],runThreads[1],runThreads[2],
  105.         monID                   // supplementary monitor ID
  106.         );
  107.    
  108.     // if we couldn't launch the whole job, push the remainder back to the high priority queue
  109.     if (partial) {
  110.         ++monID;
  111.         jobStrings = [target,hackThreads,growThreads,weakThreads,monID];
  112.         write(PORT_HIGH,jobStrings.join(','));
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement