1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// You should run this with v8, like v8_shell alloc.js datafile.json
6
7function toHex(num) {
8  var str = "";
9  var table = "0123456789abcdef";
10  while (num != 0) {
11    str = table.charAt(num & 0xf) + str;
12    num >>= 4;
13  }
14  return str;
15}
16
17function dump(obj) {
18  for (var key in obj) {
19    print('key: ' + key);
20    print('  ' + obj[key]);
21  }
22}
23
24function TopN(n) {
25  this.n = n;
26  this.min = 0;
27  this.sorted = [ ];
28}
29
30TopN.prototype.add =
31function(num, data) {
32  if (num < this.min)
33    return;
34
35  this.sorted.push([num, data]);
36  this.sorted.sort(function(a, b) { return b[0] - a[0] });
37  if (this.sorted.length > this.n)
38    this.sorted.pop();
39
40  this.min = this.sorted[this.sorted.lenth - 1];
41};
42
43TopN.prototype.datas =
44function() {
45  var datas = [ ];
46  for (var i = 0, il = this.sorted.length; i < il; ++i) {
47    datas.push(this.sorted[i][1]);
48  }
49  return datas;
50};
51
52function parseEvents(z) {
53  var topper = new TopN(1000);
54
55  // Find the largest allocation.
56  for (var i = 0, il = z.length; i < il; ++i) {
57    var e = z[i];
58
59    if (e['eventtype'] == 'EVENT_TYPE_ALLOCHEAP') {
60      var size = e['heapsize'];
61      topper.add(e['heapsize'], e);
62    }
63  }
64
65  var datas = topper.datas();
66  for (var i = 0, il = datas.length; i < il; ++i) {
67    dump(datas[i]);
68  }
69}
70