Skip to content

Commit caba5a2

Browse files
authored
Merge pull request #4284 from smowton/smowton/admin/benchmark-script-process-groups
Benchmark script: use process groups to manage children
2 parents 1fdd2f5 + 0a62867 commit caba5a2

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

scripts/benchmark/benchmark_java_project.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
let program = require('commander');
4-
let execSync = require('child_process').execSync;
4+
let execFileSync = require('child_process').execFileSync;
55
let fs = require('fs');
66

77
// Parse options and launch jbmc
@@ -27,24 +27,27 @@ program
2727
})
2828
.parse(process.argv);
2929

30-
/// Convert the args to a string of the form "--arg-name arg-value ..."
31-
function argsToString(args) {
32-
let string = "";
30+
/// Convert the args to an argument array
31+
function argsToArray(args) {
32+
let result = [];
3333
const keys = Object.keys(args);
3434
for (let i = 0; i < keys.length; i++) {
3535
if (typeof args[keys[i]] === "boolean") {
3636
if (args[keys[i]])
37-
string += ` --${keys[i]}`;
37+
result.push(`--${keys[i]}`);
3838
}
3939
else if (Array.isArray(args[keys[i]])) {
40-
for (let j = 0; j < args[keys[i]].length; j++)
41-
string += ` --${keys[i]} "${args[keys[i]][j]}"`;
40+
for (let j = 0; j < args[keys[i]].length; j++) {
41+
result.push(`--${keys[i]}`);
42+
result.push(`${args[keys[i]][j]}`);
43+
}
4244
}
4345
else {
44-
string += ` --${keys[i]} "${args[keys[i]]}"`;
46+
result.push(`--${keys[i]}`);
47+
result.push(`${args[keys[i]]}`);
4548
}
4649
}
47-
return string;
50+
return result;
4851
}
4952

5053
/// Get the coverage info from the json output if available
@@ -92,18 +95,16 @@ function run(executable, modelsPath, argumentsFile, functionName,
9295
// timeout isn't a jbmc option but only used by this script
9396
const timeout = config.timeout;
9497
config['timeout'] = false;
95-
commandLine = `${executable} ${classFile} ${argsToString(config)}`
98+
let command = [executable, classFile, ...argsToArray(config)];
9699
const startTime = new Date();
97100
try {
98-
const timeCommand = "export TIME=\"%U\"; /usr/bin/time --quiet -o tmp_time.out "
99-
+ commandLine;
100-
const output =
101-
execSync(timeCommand, { timeout: 1000 * timeout }).toString();
101+
const wrapper = __dirname + "/process_wrapper.sh";
102+
const output = execFileSync(wrapper, command, { timeout: 1000 * timeout }).toString();
102103
let execTime = fs.readFileSync('tmp_time.out', 'utf8').split('\n')[0];
103104
let result = {
104105
classFile: classFile, function: functionName,
105106
execTime: execTime, success: true,
106-
commandLine: commandLine
107+
commandLine: command
107108
}
108109
if (showProperties) {
109110
result.goals = readNumberOfGoals(output);
@@ -115,7 +116,7 @@ function run(executable, modelsPath, argumentsFile, functionName,
115116
return JSON.stringify({
116117
classFile: classFile, function: functionName,
117118
execTime: (new Date() - startTime) / 1000, success: false,
118-
commandLine: commandLine
119+
commandLine: command
119120
});
120121
}
121122
}

scripts/benchmark/process_wrapper.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
_term() {
4+
# Remove the handler first
5+
trap - SIGTERM SIGINT
6+
# Note use of negative PID to forward the kill signal to the whole process group
7+
kill -TERM -$child
8+
}
9+
10+
trap _term SIGTERM SIGINT
11+
12+
export TIME="%U"
13+
14+
# setsid: start the child in a fresh group, so we can kill its whole group when
15+
# signalled
16+
setsid -w /usr/bin/time --quiet -o tmp_time.out "$@" &
17+
child=$!
18+
19+
# Need to use 'wait' so we can receive signals
20+
wait $child

0 commit comments

Comments
 (0)