Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

kill for mem overage works

  • Loading branch information...
commit e6b9e5f87f03700b4c42a1cd859b7354fb3b8831 1 parent 131faf3
authored
26  examples/kill_mem.js
... ...
@@ -0,0 +1,26 @@
  1
+var 
  2
+  sys = require("sys"),
  3
+  jefe = new require("../lib/jefe"),
  4
+  elJefe = new jefe.Jefe(),
  5
+  scriptName = "script that creates too much memory should be killed";
  6
+
  7
+var sourceCode = "A = []; while (true) A.push('a')";
  8
+
  9
+elJefe.compile(scriptName, sourceCode, { maxMem: 10240, maxTime: 0 } );     // KB
  10
+
  11
+sys.error("this will kill the script for using too much memory regardless of how long it takes ...");
  12
+
  13
+elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) {
  14
+  if (error) {
  15
+    sys.puts("error = " + error);
  16
+  } else {
  17
+    sys.puts("completed without error");
  18
+    sys.puts("sandboxOut = " + JSON.stringify(sandboxOut));
  19
+  }
  20
+
  21
+  var stats = elJefe.getScriptStats(scriptName);
  22
+  sys.p(stats);
  23
+
  24
+  process.exit(0);
  25
+});
  26
+
2  examples/while_true.js
@@ -6,7 +6,7 @@ var
6 6
 
7 7
 elJefe.compile(scriptName, "while (true) {}", { maxTime: 5000 } );
8 8
 
9  
-sys.error("this will wait up to 5s ...");
  9
+sys.error("this will wait up to 5s before killing the script ...");
10 10
 
11 11
 elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) {
12 12
   if (error) {
17  lib/jefe.js
@@ -229,12 +229,27 @@ Jefe.prototype._runRequestOnChild = function (request, childHandler) {
229 229
 };
230 230
 
231 231
 Jefe.prototype._watchMem = function (request, childHandler, initialMem, initialMemPercent) {
232  
-  var self = this;
  232
+  var 
  233
+    self = this,
  234
+    script = request.script;
233 235
 
234 236
   function checkMem() {
235 237
     childHandler.peakMemoryUsage(function (err, mem, memPercent) {
236 238
       if ((script.maxMem        > 0 && mem        - initialMem        > script.maxMem) ||
237 239
           (script.maxMemPercent > 0 && memPercent - initialMemPercent > script.maxMemPercent)) {
  240
+
  241
+        if (common.debugMode) {
  242
+          sys.debug("pid " + childHandler.pid + 
  243
+                    " mem overage! maxMem=" + script.maxMem + 
  244
+                    " initialMem=" + initialMem + 
  245
+                    " currPeak=" + mem + 
  246
+                    " deltaMem=" + (mem - initialMem) +
  247
+                    " maxMemPercent=" + script.maxMemPercent + 
  248
+                    " initialMemPercent=" + initialMemPercent + 
  249
+                    " currPeakPercent=" + memPercent + 
  250
+                    " deltaPercent=" + (memPercent - initialMemPercent));
  251
+        }
  252
+
238 253
         childHandler.kill();
239 254
         self.scripts[script.name].wasKilled("memory");
240 255
         request.callback(exports.ERR_TOO_MUCH_MEMORY, null);
5  lib/memory.js
... ...
@@ -1,3 +1,5 @@
  1
+var fs = require('fs');
  2
+
1 3
 if (process.platform.match(/linux/i)) {
2 4
 
3 5
   /** 
@@ -28,7 +30,8 @@ if (process.platform.match(/linux/i)) {
28 30
         var 
29 31
           match = contents.match(/^VmHWM:\s+(\d+)\s+kB$/m),
30 32
           peakRSS = match ? parseInt(match[1], 10) : 0,
31  
-          peakRSSPercent = (peakRSS > 0 && systemMemoryTotal > 0) ? peakRSS / systemMemoryTotal : 0;
  33
+          peakRSSPercent = (peakRSS > 0 && exports.systemMemoryTotal > 0) 
  34
+            ? peakRSS / exports.systemMemoryTotal : 0;
32 35
 
33 36
         callback(null, peakRSS, peakRSSPercent);
34 37
       }
2  lib/userscript.js
@@ -28,7 +28,7 @@ function UserScript(name, sourceCode, options) {
28 28
   if (this.maxMemPercent < 0 || this.maxMemPercent > 1) 
29 29
     throw new Error("maxMemPercent");
30 30
 
31  
-  this.maxTime = Math.max(0, opts.maxTime || 250);
  31
+  this.maxTime = Math.max(0, opts.maxTime === undefined ? 250 : opts.maxTime);
32 32
 
33 33
   this.name = name || '';
34 34
   this.sourceCode = sourceCode;

0 notes on commit e6b9e5f

Please sign in to comment.
Something went wrong with that request. Please try again.