forked from spiermar/node-stack-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·180 lines (157 loc) · 4.2 KB
/
index.js
File metadata and controls
executable file
·180 lines (157 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
var fs = require('fs');
var program = require('commander');
function Node(name) {
this.name = name;
this.value = 0;
this.children = {};
}
Node.prototype.add = function(frames, value) {
this.value += value;
if(frames && frames.length > 0) {
var head = frames[0];
var child = this.children[head];
if(!child) {
child = new Node(head);
this.children[head] = child;
}
frames.splice(0, 1);
child.add(frames, value);
}
}
Node.prototype.serialize = function() {
var res = {
'name': this.name,
'value': this.value
}
var children = []
for(var key in this.children) {
children.push(this.children[key].serialize());
}
if(children.length > 0) res['children'] = children;
return res;
}
function Profile() {
this.samples = new Node('root');
this.stack = null;
this.name = null;
}
Profile.prototype.openStack = function (name) {
this.stack = [];
this.name = name;
}
Profile.prototype.addFrame = function(func, mod) {
var re = /^\(/g; // Skip process names
if (!re.test(func)) {
func = func.replace(';', ':') // replace ; with :
func = func.replace('<', '') // remove '<'
func = func.replace('>', '') // remove '>'
func = func.replace('\'', '') // remove '\''
func = func.replace('"', '') // remove '"'
if(func.indexOf('(') !== -1) {
func = func.substring(0, func.indexOf('(')); // delete everything after '('
}
this.stack.unshift(func);
}
}
Profile.prototype.closeStack = function() {
this.stack.unshift(this.name);
this.samples.add(this.stack, 1);
this.stack = null;
this.name = null;
}
function Recording() {
this.profiles = {};
}
Recording.prototype.getProfile = function(timestamp) {
var profile = this.profiles[timestamp];
if (!profile) {
profile = new Profile();
this.profiles[timestamp] = profile;
}
return profile;
}
Recording.prototype.serialize = function () {
var res = {};
for(var key in this.profiles) {
res[key] = this.profiles[key].samples.serialize();
}
return res;
}
function raw(filename, live) {
var recording = new Recording();
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
var lines = data.split("\n"),
profile,
matches,
re;
if (!live) {
profile = new Profile();
}
for (var i = 0; i < lines.length; i++) {
re = /^(\S+\s*?\S*?)\s+(\d+)\/(\d+)\s+\[(\d+)\]\s+(\d+).(\d+)/g;
matches = re.exec(lines[i]);
if (matches) {
if (live) {
profile = recording.getProfile(matches[5]);
}
profile.openStack(matches[1]);
} else {
re = /^\s*(\w+)\s*(.+) \((\S*)\)/g;
matches = re.exec(lines[i]);
if (matches) {
profile.addFrame(matches[2], matches[3]);
} else {
re = /^$/g;
matches = re.exec(lines[i]);
if (matches) {
profile.closeStack();
} else {
re = /^#/g;
matches = re.exec(lines[i]);
if (matches) {
// Comment line. Do nothing.
} else {
console.log("Don't know what to do with this: " + lines[i]);
}
}
}
}
};
if (live) {
res = recording.serialize();
} else {
res = profile.samples.serialize();
}
console.log(JSON.stringify(res, null, 2));
});
}
function folded(filename) {
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
var root = new Node('root');
data.split("\n").map(function (val) {
var regex = /(.*) (.*)/g;
var matches = regex.exec(val);
if (matches) root.add(matches[1].split(";"), parseInt(matches[2]));
});
console.log(JSON.stringify(root.serialize(), null, 2));
});
}
program
.version('0.3.2')
.arguments('<filename>')
.option('-f, --folded', 'Input is a folded stack.')
.option('-l, --live', 'Output includes a timestamp dimension for live flame graphs.')
.action(function(filename, options) {
if(options.folded) {
folded(filename);
} else if (options.live) {
raw(filename, true);
} else {
raw(filename, false);
}
});
program.parse(process.argv);
if(program.args.length < 1) program.help();