Skip to content

Commit 13fac22

Browse files
committed
Refactor profiling system
1 parent 2a961d3 commit 13fac22

File tree

6 files changed

+3312
-43
lines changed

6 files changed

+3312
-43
lines changed

lib/probes/profiling.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Profiling.exposeProfiling = function(pmx, profiler_path) {
3030
* Heap snapshot
3131
*/
3232
pmx.action('km:heapdump', function(reply) {
33-
var dump_file = path.join(os.tmpDir(), Date.now() + '.heapsnapshot');
33+
var dump_file = path.join(os.tmpdir(), Date.now() + '.heapsnapshot');
3434

3535
var snapshot = profiler.takeSnapshot('km-heap-snapshot');
3636
var buffer = '';
@@ -64,7 +64,7 @@ Profiling.exposeProfiling = function(pmx, profiler_path) {
6464
* CPU profiling snapshot
6565
*/
6666
var ns_cpu_profiling = 'km-cpu-profiling';
67-
var cpu_dump_file = path.join(os.tmpDir(), Date.now() + '.cpuprofile');
67+
var cpu_dump_file = path.join(os.tmpdir(), Date.now() + '.cpuprofile');
6868

6969
pmx.action('km:cpu:profiling:start', function(reply) {
7070
profiler.startProfiling(ns_cpu_profiling);
@@ -94,19 +94,24 @@ Profiling.exposeProfiling = function(pmx, profiler_path) {
9494
/**
9595
* Discover v8-profiler
9696
*/
97-
Profiling.detectV8Profiler = function(cb) {
97+
Profiling.detectV8Profiler = function(module_name, cb) {
9898
var require_paths = require.main.paths.slice();
9999

100100
(function look_for_profiler(require_paths) {
101-
if (!require_paths[0])
102-
return cb(new Error('Module not found'));
103-
104-
var profiler_path = path.join(require_paths[0], 'v8-profiler');
105-
106-
fs.exists(profiler_path, function(exist) {
107-
if (exist)
101+
if (!require_paths[0]) {
102+
debug('[x] %s NOT FOUND', module_name);
103+
return cb(new Error('v8-profiler not found'));
104+
}
105+
var profiler_path = path.join(require_paths[0], module_name);
106+
107+
debug('Checking %s in path %s', module_name, profiler_path);
108+
fs.access(profiler_path, fs.R_OK || fs.constants.R_OK, function(err) {
109+
if (!err) {
110+
debug('[+] %s detected in path %s', module_name, profiler_path);
108111
return cb(null, profiler_path);
112+
}
109113

114+
debug('[-] %s not found in path %s', module_name, profiler_path);
110115
require_paths.shift();
111116
return look_for_profiler(require_paths);
112117
});
@@ -115,9 +120,14 @@ Profiling.detectV8Profiler = function(cb) {
115120
};
116121

117122
Profiling.v8Profiling = function(pmx) {
118-
Profiling.detectV8Profiler(function(err, profiler_path) {
119-
if (err)
120-
return false;
123+
Profiling.detectV8Profiler('v8-profiler', function(err, profiler_path) {
124+
if (err) {
125+
return Profiling.detectV8Profiler('v8-profiler-node8', function(err, profiler_path) {
126+
if (err)
127+
return false;
128+
return Profiling.exposeProfiling(pmx, profiler_path);
129+
});
130+
}
121131
return Profiling.exposeProfiling(pmx, profiler_path);
122132
});
123133
};

0 commit comments

Comments
 (0)