1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// found in the LICENSE file.
4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochstorage = new (function() {
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.table_ = null;
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.tableData_ = null;
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)this.profileMenu_ = null;
10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.onDomReady_ = function() {
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Create the menus for mmap and nheap profiling.
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_ = $('#storage-profile-menu');
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      .mouseleave(this.profileMenu_.hide.bind(
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          this.profileMenu_, {duration: 0}))
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      .hide();
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Initialize the toolbar.
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  $('#storage-profile').button({icons:{primary: 'ui-icon-image'}})
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      .click(this.profileMmapForSelectedSnapshots.bind(this, null))
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      .mouseenter(this.showProfileMenu_.bind(this));
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  $('#storage-dump-mmaps').button({icons:{primary: 'ui-icon-calculator'}})
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      .click(this.dumpMmapForSelectedSnapshot_.bind(this));
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  $('#storage-dump-nheap').button({icons:{primary: 'ui-icon-calculator'}})
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      .click(this.dumpNheapForSelectedSnapshot_.bind(this));
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Create the table.
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  this.table_ = new google.visualization.Table($('#storage-table')[0]);
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  $('#storage-table').on('contextmenu', this.showProfileMenu_.bind(this));
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.reload = function() {
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  webservice.ajaxRequest('/storage/list', this.onListAjaxResponse_.bind(this));
36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.onListAjaxResponse_ = function(data) {
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  this.tableData_ = new google.visualization.DataTable(data);
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  this.redraw();
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)this.profileMmapForSelectedSnapshots = function(ruleset) {
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Generates a mmap profile for the selected snapshots.
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  var sel = this.table_.getSelection();
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!sel.length || !this.tableData_) {
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    alert('No snapshots selected!');
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  var archiveName = null;
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  var snapshots = [];
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  for (var i = 0; i < sel.length; ++i) {
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    var row = sel[i].row;
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    var curArchive = this.tableData_.getValue(row, 0);
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (archiveName && curArchive != archiveName) {
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      alert('All the selected snapshots must belong to the same archive!');
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    archiveName = curArchive;
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    snapshots.push(this.tableData_.getValue(row, 1));
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profiler.profileArchivedMmaps(archiveName, snapshots, ruleset);
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  rootUi.showTab('prof');
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.dumpMmapForSelectedSnapshot_ = function() {
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  var sel = this.table_.getSelection();
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (sel.length != 1) {
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    alert('Please select only one snapshot.')
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  var row = sel[0].row;
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  mmap.dumpMmapsFromStorage(this.tableData_.getValue(row, 0),
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                            this.tableData_.getValue(row, 1))
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  rootUi.showTab('mm');
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochthis.dumpNheapForSelectedSnapshot_ = function() {
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  var sel = this.table_.getSelection();
820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (sel.length != 1) {
830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    alert('Please select only one snapshot.')
840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  var row = sel[0].row;
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!this.checkHasNativeHapDump_(row))
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  nheap.dumpNheapFromStorage(this.tableData_.getValue(row, 0),
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                             this.tableData_.getValue(row, 1))
920529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rootUi.showTab('nheap');
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch};
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)this.profileNativeForSelectedSnapshots = function(ruleset) {
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Generates a native heap profile for the selected snapshots.
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  var sel = this.table_.getSelection();
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!sel.length || !this.tableData_) {
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    alert('No snapshots selected!');
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
1020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  var archiveName = null;
1030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  var snapshots = [];
104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  for (var i = 0; i < sel.length; ++i) {
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    var row = sel[i].row;
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    var curArchive = this.tableData_.getValue(row, 0);
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (archiveName && curArchive != archiveName) {
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      alert('All the selected snapshots must belong to the same archive!');
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    }
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    if (!this.checkHasNativeHapDump_(row))
1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      return;
1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    archiveName = curArchive;
1150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    snapshots.push(this.tableData_.getValue(row, 1));
1160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profiler.profileArchivedNHeaps(archiveName, snapshots, ruleset);
1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  rootUi.showTab('prof');
119effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
120effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochthis.checkHasNativeHapDump_ = function(row) {
1220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!this.tableData_.getValue(row, 3)) {
1230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    alert('The selected snapshot doesn\'t have a heap dump!');
1240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
1250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
1260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return true;
1270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)this.rebuildMenu_ = function() {
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_.empty();
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_.append(
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      $('<li/>').addClass('header').text('Memory map rules'));
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profiler.rulesets['mmap'].forEach(function(rule) {
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    this.profileMenu_.append(
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        $('<li/>').text(rule).click(
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            this.profileMmapForSelectedSnapshots.bind(this, rule)));
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }, this);
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_.append(
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      $('<li/>').addClass('header').text('Native heap rules'));
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  profiler.rulesets['nheap'].forEach(function(rule) {
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    this.profileMenu_.append(
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        $('<li/>').text(rule).click(
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            this.profileNativeForSelectedSnapshots.bind(this, rule)));
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }, this);
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_.menu();
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)this.showProfileMenu_ = function(evt) {
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  console.log(evt);
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  var pos;
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (evt.type == 'contextmenu')
155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    pos = {my: "left top", at: "left bottom", of: evt};
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  else
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    pos = {my: "left top", at: "left bottom", of: evt.target};
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.profileMenu_.show({duration: 0}).position(pos);
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  evt.preventDefault();
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
162effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochthis.redraw = function() {
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.rebuildMenu_();
164effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!this.tableData_)
165effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return;
166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  this.table_.draw(this.tableData_);
167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
168effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
169effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch$(document).ready(this.onDomReady_.bind(this));
170effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch})();