Skip to content

Commit

Permalink
kill for mem overage works
Browse files Browse the repository at this point in the history
  • Loading branch information
brian committed Apr 26, 2010
1 parent 131faf3 commit e6b9e5f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
26 changes: 26 additions & 0 deletions examples/kill_mem.js
@@ -0,0 +1,26 @@
var
sys = require("sys"),
jefe = new require("../lib/jefe"),
elJefe = new jefe.Jefe(),
scriptName = "script that creates too much memory should be killed";

var sourceCode = "A = []; while (true) A.push('a')";

elJefe.compile(scriptName, sourceCode, { maxMem: 10240, maxTime: 0 } ); // KB

sys.error("this will kill the script for using too much memory regardless of how long it takes ...");

elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) {
if (error) {
sys.puts("error = " + error);
} else {
sys.puts("completed without error");
sys.puts("sandboxOut = " + JSON.stringify(sandboxOut));
}

var stats = elJefe.getScriptStats(scriptName);
sys.p(stats);

process.exit(0);
});

2 changes: 1 addition & 1 deletion examples/while_true.js
Expand Up @@ -6,7 +6,7 @@ var

elJefe.compile(scriptName, "while (true) {}", { maxTime: 5000 } );

sys.error("this will wait up to 5s ...");
sys.error("this will wait up to 5s before killing the script ...");

elJefe.run(scriptName, {}, function (error, sandboxIn, sandboxOut) {
if (error) {
Expand Down
17 changes: 16 additions & 1 deletion lib/jefe.js
Expand Up @@ -229,12 +229,27 @@ Jefe.prototype._runRequestOnChild = function (request, childHandler) {
};

Jefe.prototype._watchMem = function (request, childHandler, initialMem, initialMemPercent) {
var self = this;
var
self = this,
script = request.script;

function checkMem() {
childHandler.peakMemoryUsage(function (err, mem, memPercent) {
if ((script.maxMem > 0 && mem - initialMem > script.maxMem) ||
(script.maxMemPercent > 0 && memPercent - initialMemPercent > script.maxMemPercent)) {

if (common.debugMode) {
sys.debug("pid " + childHandler.pid +
" mem overage! maxMem=" + script.maxMem +
" initialMem=" + initialMem +
" currPeak=" + mem +
" deltaMem=" + (mem - initialMem) +
" maxMemPercent=" + script.maxMemPercent +
" initialMemPercent=" + initialMemPercent +
" currPeakPercent=" + memPercent +
" deltaPercent=" + (memPercent - initialMemPercent));
}

childHandler.kill();
self.scripts[script.name].wasKilled("memory");
request.callback(exports.ERR_TOO_MUCH_MEMORY, null);
Expand Down
5 changes: 4 additions & 1 deletion lib/memory.js
@@ -1,3 +1,5 @@
var fs = require('fs');

if (process.platform.match(/linux/i)) {

/**
Expand Down Expand Up @@ -28,7 +30,8 @@ if (process.platform.match(/linux/i)) {
var
match = contents.match(/^VmHWM:\s+(\d+)\s+kB$/m),
peakRSS = match ? parseInt(match[1], 10) : 0,
peakRSSPercent = (peakRSS > 0 && systemMemoryTotal > 0) ? peakRSS / systemMemoryTotal : 0;
peakRSSPercent = (peakRSS > 0 && exports.systemMemoryTotal > 0)
? peakRSS / exports.systemMemoryTotal : 0;

callback(null, peakRSS, peakRSSPercent);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/userscript.js
Expand Up @@ -28,7 +28,7 @@ function UserScript(name, sourceCode, options) {
if (this.maxMemPercent < 0 || this.maxMemPercent > 1)
throw new Error("maxMemPercent");

this.maxTime = Math.max(0, opts.maxTime || 250);
this.maxTime = Math.max(0, opts.maxTime === undefined ? 250 : opts.maxTime);

this.name = name || '';
this.sourceCode = sourceCode;
Expand Down

0 comments on commit e6b9e5f

Please sign in to comment.