Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / memory_inspector / memory_inspector / frontends / www_content / js / nheap.js
1 // Copyright 2014 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 nheap = new (function() {
6
7 this.COL_TOTAL = 0;
8 this.COL_RESIDENT = 1;
9 this.COL_STACKTRACE = 3;
10
11
12 this.nheapData_ = null;
13 this.nheapTable_ = null;
14 this.nheapFilter_ = null;
15
16 this.onDomReady_ = function() {
17   // Create the mmaps table.
18   this.nheapTable_ = new google.visualization.Table($('#nheap-table')[0]);
19   google.visualization.events.addListener(
20       this.nheapTable_, 'select', this.onNheapTableRowSelect_.bind(this));
21   $('#nheap-filter').on('change', this.applyTableFilters_.bind(this));
22 };
23
24 this.dumpNheapFromStorage = function(archiveName, snapshot) {
25   webservice.ajaxRequest('/storage/' + archiveName + '/' + snapshot + '/nheap',
26                          this.onDumpAjaxResponse_.bind(this));
27   rootUi.showDialog('Loading native heap allocs from archive ...');
28   this.resetTableFilters_();
29 };
30
31 this.onDumpAjaxResponse_ = function(data) {
32   this.nheapData_ = new google.visualization.DataTable(data);  // TODO remove .table form mmap
33   this.nheapFilter_ = new google.visualization.DataView(this.nheapData_);
34   this.applyTableFilters_();
35   rootUi.hideDialog();
36 };
37
38 this.resetTableFilters_ = function() {
39   $('#nheap-filter').val('');
40 }
41
42 this.applyTableFilters_ = function() {
43   // Filters the rows according to the user-provided file and prot regexps.
44   if (!this.nheapFilter_)
45     return;
46
47   var rx = $('#nheap-filter').val();
48   var rows = [];
49   var total_allocated = 0;
50   var total_resident = 0;
51
52   for (var row = 0; row < this.nheapData_.getNumberOfRows(); ++row) {
53      stackTrace = this.nheapData_.getValue(row, this.COL_STACKTRACE);
54      if (!stackTrace.match(rx))
55       continue;
56     rows.push(row);
57     total_allocated += this.nheapData_.getValue(row, this.COL_TOTAL);
58     total_resident += this.nheapData_.getValue(row, this.COL_RESIDENT);
59   }
60
61   $('#nheap-total-allocated').val(Math.floor(total_allocated / 1024) + ' KB');
62   $('#nheap-total-resident').val(Math.floor(total_resident / 1024) + ' KB');
63   this.nheapFilter_.setRows(rows);
64   this.redraw();
65 };
66
67 this.onNheapTableRowSelect_ = function() {
68   if (!this.nheapFilter_)
69     return;
70
71   var total_allocated = 0;
72   var total_resident = 0;
73
74   this.nheapTable_.getSelection().forEach(function(sel) {
75     var row = sel.row;
76     total_allocated += this.nheapFilter_.getValue(row, this.COL_TOTAL);
77     total_resident += this.nheapFilter_.getValue(row, this.COL_RESIDENT);
78   }, this);
79
80   $('#nheap-selected-allocated').val(Math.floor(total_allocated / 1024) +
81                                      ' KB');
82   $('#nheap-selected-resident').val(Math.floor(total_resident / 1024) + ' KB');
83 };
84
85 this.redraw = function() {
86   if (!this.nheapFilter_)
87     return;
88   this.nheapTable_.draw(this.nheapFilter_, {allowHtml: true,
89                                             page: 'enable',
90                                             pageSize: 25});
91 };
92
93 $(document).ready(this.onDomReady_.bind(this));
94
95 })();