summaryrefslogtreecommitdiffstats
path: root/tools/trace/trace.html
blob: e14bbaef686f5c81e5076c5b60cb1ed28ba47eb7 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<html>
<head>
<title>
Trace Events
</title>
<style>
body {
  font-family: "Courier New";
  font-size: 9pt;
}

#header {
  position: absolute;
  top: 0px;
  left: 0px;
  border-bottom: 1px dashed black;
  background-color: #F0F0F0;
  z-index: 3;
}

#outer {
  position: relative;
  height: 200px;
}

#time_scale {
  height: 15px;
  width: 100%;
}

#tooltip {
  position: absolute;
  background-color: #FFFFCC;
  display: none;
  font-family: "Courier New";
  font-size: 9pt;
  padding: 5px;
  border: 1px solid #CCCC88;
  z-index: 3;
}

#legend {
  position: fixed;
  left: 10px;
  bottom: 10px;
  padding: 5px;
  border: 1px solid silver;
  z-index: 10;
  background-color: #f0f0f0;
}

h2 {
  margin: 5px;
}

#instructions {
  position: absolute;
  top: 
  float: right;
  display: none;
}

li.time_tick {
  background-color: #FFFFCC;
  height: 15px;
}

li {
  background: pink;
  position: absolute;
  height: 10px;
  list-style: none;
  margin: 0px;
  padding: 0px;
  z-index: 2;
}

li:hover {
  border: 1px solid red;
}

.url {
  background-color: green;
}

.http {
  background-color: blue;
}

.socket {
  background-color: black;
}

.v8 {
  background-color: orange; 
}

</style>

<script src='trace_data.js'></script>
<script>
var scale = 100000;
var row_height = 15;
var trace_initial_time = 0;
var trace_threads = {};
var heartbeats = [];
var trace_total_time = 0;

function process_raw_events() {
  trace_initial_time = raw_trace_events[0].usec_begin;
  var stack = [];
  var e;
  for (var i in raw_trace_events) {
    e = raw_trace_events[i];
    var trace_events = trace_threads["e.tid"];
    if (!trace_events) {
      trace_events = [];
      trace_threads["e.tid"] = trace_events;
    }
    if (e.name.indexOf("heartbeat.") == 0) {
      heartbeats.push(e);
    } else if (e.type == "BEGIN") {
      trace_events.push(e);
      stack.unshift(e);
    } else if (e.type == "END") {
      for (var s in stack) {
        var begin = stack[s];
        if ((begin.id == e.id) && (begin.name == e.name) &&
            (begin.pid == e.pid) && (begin.tid == e.tid)) {
          begin.usec_end = e.usec_begin;
          begin.duration = begin.usec_end - begin.usec_begin;
          stack.splice(s, 1);
          break;
        } 
      }
    } else if (e.type == "INSTANT") {
      trace_events.push(e);
      e.duration = 0;
    }
  }
  if (e.usec_end)
    trace_total_time = e.usec_end - trace_initial_time;
  else
    trace_total_time = e.usec_begin - trace_initial_time;
}

function compute_scale() {
  var outer = document.getElementById("outer");
  scale = Math.floor(trace_total_time / (outer.offsetWidth - (row_height * 2)));
};

function show_details(tid, i, event) {
  var trace_events = trace_threads["e.tid"];
  var inner = trace_events[i].name + " " +
              trace_events[i].duration / 1000 + "ms<br />" + 
              trace_events[i].id  + "<br />" + 
              trace_events[i].extra  + "<br />";
  var tooltip = document.getElementById("tooltip");
  tooltip.innerHTML = inner;
  if (window.event)
    event = window.event;
  tooltip.style.top = event.pageY + 3;
  tooltip.style.left = event.pageX + 3;
  tooltip.style.display = "block";
};

function generate_time_scale() {
  var view_size = window.clientWidth;
  var body_size = document.body.scrollWidth;
  var inner = "";
  
  var step_ms = Math.floor(scale / 10); // ms per 100px
  var pow10 = Math.pow(10, Math.floor(Math.log(step_ms) / Math.log(10)));
  var round = .5 * pow10;
  step_ms = round * (Math.floor(step_ms / round)); // round to a multiple of round
  for (var i = step_ms; i < trace_total_time / 1000; i += step_ms) {
    var x = Math.floor(i * 1000 / scale);
    inner += "<li class='time_tick' style='left: " + x + "px'>" + i + "</li>";
  }
  var time_scale = document.getElementById("time_scale");
  time_scale.innerHTML = inner;
  time_scale.style.width = document.body.scrollWidth;
}

function generate_subchart(trace_events, top) {
  var inner = "";
  var last_max_time = 0;
  var last_max_x = 0;
  for (var i in trace_events) {
    var e = trace_events[i];
    var start_time = e.usec_begin - trace_initial_time;
    var left = row_height + Math.floor(start_time / scale);
    var width = Math.floor(e.duration / scale);
    if (width == 0)
      width = 1;
    if (start_time < last_max_time)
      top += row_height;
    var style = "top: " + top + "px; left: " + left + "px; width: " + width + "px;";
    var js = 'javascript:show_details("' + e.tid + '", ' + i + ', event);';
    var cls = e.name.split('.')[0];
    inner += "<li class='" + cls + "' onmouseover='" + js + "' id='li-" + i + "' style='" + style + "'></li>\n";
    last_max_time = start_time + e.duration;
    last_max_x = left + width;
  }
  var subchart = document.createElement('div');
  subchart.setAttribute("class", "subchart");
  subchart.setAttribute("id", trace_events[0].tid);
  subchart.innerHTML = inner;
  subchart.style.height = top + row_height;
  subchart.style.width = row_height + last_max_x;
  var chart = document.getElementById("chart");
  chart.appendChild(subchart);
  
  return top;
};

function generate_chart() {
  var chart = document.getElementById("chart");
  chart.innerHTML = "";
  var top = 60;
  for (var t in trace_threads) {
    top = generate_subchart(trace_threads[t], top);
  }
  generate_time_scale();
}

function change_scale(event) {
  if (!event)
    event = window.event;
  if (!event.shiftKey)
    return;
  var delta = 0;
  if (event.wheelDelta) {
    delta = event.wheelDelta / 120;
  } else if (event.detail) {
    delta = - event.detail / 3;
  }
  if (delta) {
    var tooltip = document.getElementById("tooltip");
    tooltip.style.display = "none";
    var factor = 1.1;
    if (delta < 0)
      scale = Math.floor(scale * factor);
    else
      scale = Math.floor(scale / factor);
    if (scale > 300000)
      scale = 300000;
    generate_chart();
    if (event.preventDefault)
      event.preventDefault();
  }
  event.returnValue = false;
};

function initial_load() {
  if (window.addEventListener)
    window.addEventListener('DOMMouseScroll', change_scale, false);
  window.onmousewheel = document.onmousewheel = change_scale;

  process_raw_events();
  compute_scale();
  generate_chart();
};

</script>
</head>
<body onload='initial_load();'>
<div id="header">
<h2>Trace Events</h2>
<div id="instructions">
Use shift+mouse-wheel to zoom in and out.
</div>
<div id="time_scale"></div>
</div>
<div id="legend">
<span class="url">&nbsp;</span> URL<br />
<span class="http">&nbsp;</span> HTTP<br />
<span class="socket">&nbsp;</span> Socket<br />
<span class="v8">&nbsp;</span> V8<br />
</div>
<div id="chart">
<div id="outer">
</div>
</div>
<div id="tooltip" ondblclick="this.style.display = 'none';"></div>
</body>
</html>