Permalink
Browse files

refactor input/output and commands

  • Loading branch information...
1 parent c8cf4a1 commit 8a46f92e3fb3ed0be369a5f4cb080f207e7a1fe3 @davepacheco committed Apr 1, 2012
Showing with 83 additions and 24 deletions.
  1. +5 −13 cmd/flamegraph
  2. +5 −8 cmd/stackcollapse
  3. +27 −0 lib/output-collapsed.js
  4. +46 −3 lib/{input-common.js → stackvis.js}
View
@@ -5,24 +5,16 @@
*/
var mod_bunyan = require('bunyan');
-
-var mod_input_common = require('../lib/input-common');
-var mod_input_collapsed = require('../lib/input-collapsed');
-var mod_output_flamegraph = require('../lib/output-flamegraph-svg');
+var mod_stackvis = require('../lib/stackvis');
var log = new mod_bunyan({
'name': 'flamegraph',
'stream': process.stderr
});
-var reader = new mod_input_collapsed.reader(process.stdin, log);
-
-mod_input_common.collapseStacks(reader, function (err, stacks) {
- mod_output_flamegraph.emit({
- 'stacks': stacks,
- 'output': process.stdout,
- 'log': log
- }, function () {});
-});
+var reader = new mod_stackvis.readerLookup('collapsed');
+var writer = new mod_stackvis.writerLookup('flamegraph-svg');
+mod_stackvis.pipeStacks(log, process.stdin, reader, writer, process.stdout,
+ function () {});
process.stdin.resume();
View
@@ -4,20 +4,17 @@
* cmd/stackcollapse: emit collapsed stack traces from DTrace output
*/
-var mod_input_common = require('../lib/input-common');
-var mod_input_dtrace = require('../lib/input-dtrace');
var mod_bunyan = require('bunyan');
+var mod_stackvis = require('../lib/stackvis');
var log = new mod_bunyan({
'name': 'stackcollapse',
'stream': process.stderr
});
-var reader = new mod_input_dtrace.reader(process.stdin, log);
-mod_input_common.collapseStacks(reader, function (err, stacks) {
- stacks.eachStackByCount(function (frames, count) {
- process.stdout.write(frames.join(',') + ' ' + count + '\n');
- });
-});
+var reader = mod_stackvis.readerLookup('dtrace');
+var writer = mod_stackvis.writerLookup('collapsed');
+mod_stackvis.pipeStacks(log, process.stdin, reader, writer, process.stdout,
+ function () {});
process.stdin.resume();
View
@@ -0,0 +1,27 @@
+/*
+ * lib/output-collapsed.js: emits StackSets in collapsed format, compatible with
+ * Brendan Gregg's FlameGraph tool.
+ */
+
+var mod_assert = require('assert');
+
+/*
+ * Arguments:
+ *
+ * stacks StackSet Stacks to visualize
+ *
+ * output WritableStream Output file
+ */
+exports.emit = function emitCollapsed(args, callback)
+{
+ mod_assert.ok(args.stacks && args.stacks.constructor &&
+ args.stacks.constructor.name == 'StackSet',
+ 'required "stacks" argument must be a StackSet');
+ mod_assert.ok(args.output && args.output.write &&
+ typeof (args.output.write) == 'function',
+ 'required "output" argument must be a function');
+
+ args.stacks.eachStackByCount(function (frames, count) {
+ process.stdout.write(frames.join(',') + ' ' + count + '\n');
+ });
+};
@@ -1,9 +1,29 @@
/*
- * lib/input-common.js: common routines for importing stacks
+ * lib/stackvis.js: Stackvis library interface
*/
var mod_assert = require('assert');
+exports.readerLookup = readerLookup;
+exports.writerLookup = writerLookup;
+exports.pipeStacks = pipeStacks;
+
+function moduleLookup(type, name)
+{
+ var filename = './' + type + '-' + name;
+ return (require(filename));
+}
+
+function readerLookup(name)
+{
+ return (moduleLookup('input', name).reader);
+}
+
+function writerLookup(name)
+{
+ return (moduleLookup('output', name));
+}
+
function StackSet()
{
this.ss_counts = {}; /* maps serialized stack -> count */
@@ -49,7 +69,7 @@ StackSet.prototype.eachStackByStack = function (callback)
});
};
-exports.collapseStacks = function (reader, callback)
+function collapseStacks(reader, callback)
{
var stacks = new StackSet();
@@ -58,4 +78,27 @@ exports.collapseStacks = function (reader, callback)
});
reader.on('end', function () { callback(null, stacks); });
-};
+}
+
+/*
+ * This is *not* a stable interface yet. This will be changed in the future to
+ * support multiple stage pipelines.
+ */
+/* XXX use args for argument. */
+function pipeStacks(log, instream, readercons, writer, outstream, callback)
+{
+ var reader = new readercons(instream, log);
+
+ collapseStacks(reader, function (err, stacks) {
+ if (err) {
+ log.error(err);
+ return;
+ }
+
+ writer.emit({
+ 'stacks': stacks,
+ 'output': outstream,
+ 'log': log
+ }, callback);
+ });
+}

0 comments on commit 8a46f92

Please sign in to comment.