tools: run tick processor without forking
authorMatt Loring <mattloring@google.com>
Wed, 9 Dec 2015 19:37:51 +0000 (11:37 -0800)
committerMyles Borins <mborins@us.ibm.com>
Wed, 2 Mar 2016 22:01:11 +0000 (14:01 -0800)
Using the tick processor no longer creates temporary files or spawns a
child process.

PR-URL: https://github.com/nodejs/node/pull/4224
Reviewed-By: bnoordhuis - Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: jasnell - James M Snell <jasnell@gmail.com>
.eslintignore
lib/internal/v8_prof_polyfill.js
lib/internal/v8_prof_processor.js
node.gyp

index 9b5c5fc..0be7057 100644 (file)
@@ -1,4 +1,5 @@
 lib/internal/v8_prof_polyfill.js
+lib/internal/v8_prof_processor.js
 lib/punycode.js
 test/addons/??_*/
 test/fixtures
index 1beae0e..755f8f0 100644 (file)
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 // Node polyfill
-var fs = require('fs');
-var os = {
+const fs = require('fs');
+const cp = require('child_process');
+const os = {
   system: function(name, args) {
     if (process.platform === 'linux' && name === 'nm') {
       // Filter out vdso and vsyscall entries.
-      var arg = args[args.length - 1];
+      const arg = args[args.length - 1];
       if (arg === '[vdso]' ||
           arg == '[vsyscall]' ||
           /^[0-9a-f]+-[0-9a-f]+$/.test(arg)) {
         return '';
       }
+    } else if (process.platform === 'darwin') {
+      args.unshift('-c', name);
+      name = '/bin/sh';
     }
-    return require('child_process').execFileSync(
-        name, args, {encoding: 'utf8'});
+    return cp.spawnSync(name, args).stdout.toString();
   }
 };
-var print = console.log;
+const print = console.log;
 function read(fileName) {
   return fs.readFileSync(fileName, 'utf8');
 }
-arguments = process.argv.slice(2);
-var quit = process.exit;
+const quit = process.exit;
 
 // Polyfill "readline()".
-var logFile = arguments[arguments.length - 1];
+const logFile = arguments[arguments.length - 1];
 try {
   fs.accessSync(logFile);
 } catch(e) {
   console.error('Please provide a valid isolate file as the final argument.');
   process.exit(1);
 }
-var fd = fs.openSync(logFile, 'r');
-var buf = new Buffer(4096);
-var dec = new (require('string_decoder').StringDecoder)('utf-8');
+const fd = fs.openSync(logFile, 'r');
+const buf = new Buffer(4096);
+const dec = new (require('string_decoder').StringDecoder)('utf-8');
 var line = '';
 versionCheck();
 function readline() {
@@ -85,7 +87,7 @@ function versionCheck() {
   var firstLine = readline();
   line = firstLine + '\n' + line;
   firstLine = firstLine.split(',');
-  var curVer = process.versions.v8.split('.');
+  const curVer = process.versions.v8.split('.');
   if (firstLine.length !== 6 && firstLine[0] !== 'v8-version') {
     console.log('Unable to read v8-version from log file.');
     return;
index 543f977..05452c1 100644 (file)
@@ -1,9 +1,4 @@
-'use strict';
-var cp = require('child_process');
-var fs = require('fs');
-var path = require('path');
-
-var scriptFiles = [
+const scriptFiles = [
   'internal/v8_prof_polyfill',
   'v8/tools/splaytree',
   'v8/tools/codemap',
@@ -16,29 +11,19 @@ var scriptFiles = [
   'v8/tools/SourceMap',
   'v8/tools/tickprocessor-driver'
 ];
-var tempScript = 'tick-processor-tmp-' + process.pid;
-var tempNm = 'mac-nm-' + process.pid;
+var script = '';
 
-process.on('exit', function() {
-  try { fs.unlinkSync(tempScript); } catch (e) {}
-  try { fs.unlinkSync(tempNm); } catch (e) {}
-});
-process.on('uncaughtException', function(err) {
-  try { fs.unlinkSync(tempScript); } catch (e) {}
-  try { fs.unlinkSync(tempNm); } catch (e) {}
-  throw err;
+scriptFiles.forEach(function(s) {
+  script += process.binding('natives')[s] + '\n';
 });
 
-scriptFiles.forEach(function(script) {
-  fs.appendFileSync(tempScript, process.binding('natives')[script]);
-});
-var tickArguments = [tempScript];
+var tickArguments = [];
 if (process.platform === 'darwin') {
-  fs.writeFileSync(tempNm, process.binding('natives')['v8/tools/mac-nm'],
-    { mode: 0o555 });
-  tickArguments.push('--mac', '--nm=' + path.join(process.cwd(), tempNm));
+  const nm = 'foo() { nm "$@" | (c++filt -p -i || cat) }; foo $@';
+  tickArguments.push('--mac', '--nm=' + nm);
 } else if (process.platform === 'win32') {
   tickArguments.push('--windows');
 }
 tickArguments.push.apply(tickArguments, process.argv.slice(1));
-cp.spawn(process.execPath, tickArguments, { stdio: 'inherit' });
+script = 'arguments = ' + JSON.stringify(tickArguments) + ';\n' + script;
+eval(script);
index f6f1846..49c3cc1 100644 (file)
--- a/node.gyp
+++ b/node.gyp
@@ -90,7 +90,6 @@
       'deps/v8/tools/tickprocessor.js',
       'deps/v8/tools/SourceMap.js',
       'deps/v8/tools/tickprocessor-driver.js',
-      'deps/v8/tools/mac-nm',
     ],
   },