processes.js revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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
5processes = new (function() {
6
7this.PS_INTERVAL_SEC_ = 2;
8this.DEV_STATS_INTERVAL_SEC_ = 2;
9this.PROC_STATS_INTERVAL_SEC_ = 1;
10
11this.selProcUri_ = null;
12this.selProcName_ = null;
13this.psTable_ = null;
14this.psTableData_ = null;
15this.memChart_ = null;
16this.memChartData_ = null;
17this.cpuChart_ = null;
18this.cpuChartData_ = null;
19this.procCpuChart_ = null;
20this.procCpuChartData_ = null;
21this.procMemChart_ = null;
22this.procMemChartData_ = null;
23
24this.onDomReady_ = function() {
25  $('#device_tabs').tabs();
26  $('#device_tabs').on('tabsactivate', this.redrawPsStats_.bind(this));
27  $('#device_tabs').on('tabsactivate', this.redrawDevStats_.bind(this));
28
29  // Initialize the toolbar.
30  $('#ps-dump_mmaps').button({icons:{primary: 'ui-icon-calculator'}})
31      .click(this.dumpSelectedProcessMmaps_.bind(this));
32
33  // Create the process table.
34  this.psTable_ = new google.visualization.Table($('#ps-table')[0]);
35  google.visualization.events.addListener(
36      this.psTable_, 'select', this.onPsTableRowSelect_.bind(this));
37
38  // Create the device stats charts.
39  this.memChart_ = new google.visualization.PieChart($('#os-mem_chart')[0]);
40  this.cpuChart_ = new google.visualization.BarChart($('#os-cpu_chart')[0]);
41
42  // Create the selected process stats charts.
43  this.procCpuChart_ =
44      new google.visualization.ComboChart($('#proc-cpu_chart')[0]);
45  this.procMemChart_ =
46      new google.visualization.ComboChart($('#proc-mem_chart')[0]);
47};
48
49this.getSelectedProcessURI = function() {
50  return this.selProcUri_;
51};
52
53this.dumpSelectedProcessMmaps_ = function() {
54  if (!this.selProcUri_)
55    return alert('Must select a process!');
56  mmap.dumpMmaps(this.selProcUri_, false);
57  rootUi.showTab('mm');
58};
59
60this.refreshPsTable = function() {
61  var targetDevUri = devices.getSelectedURI();
62  if (!targetDevUri)
63    return this.stopPsTable();
64
65  var showAllParam = $('#ps-show_all').prop('checked') ? '?all=1' : '';
66  webservice.ajaxRequest('/ps/' + targetDevUri + showAllParam,
67                         this.onPsAjaxResponse_.bind(this),
68                         this.stopPsTable.bind(this));
69};
70
71this.startPsTable = function() {
72  timers.start('ps_table',
73               this.refreshPsTable.bind(this),
74               this.PS_INTERVAL_SEC_);
75};
76
77this.stopPsTable = function() {
78  this.selProcUri_ = null;
79  this.selProcName_ = null;
80  timers.stop('ps_table');
81};
82
83this.onPsTableRowSelect_ = function() {
84  var targetDevUri = devices.getSelectedURI();
85  if (!targetDevUri)
86    return;
87
88  var sel = this.psTable_.getSelection();
89  if (!sel.length || !this.psTableData_)
90    return;
91  var pid = this.psTableData_.getValue(sel[0].row, 0);
92  this.selProcUri_ = targetDevUri + '/' + pid;
93  this.selProcName_ = this.psTableData_.getValue(sel[0].row, 1);
94  this.startSelectedProcessStats();
95};
96
97this.onPsAjaxResponse_ = function(data) {
98  this.psTableData_ = new google.visualization.DataTable(data);
99  this.redrawPsTable_();
100};
101
102this.redrawPsTable_ = function(data) {
103  if (!this.psTableData_)
104    return;
105
106  // Redraw table preserving sorting info.
107  var sort = this.psTable_.getSortInfo() || {column: -1, ascending: false};
108  this.psTable_.draw(this.psTableData_, {sortColumn: sort.column,
109                                         sortAscending: sort.ascending});
110};
111
112this.refreshDeviceStats = function() {
113  var targetDevUri = devices.getSelectedURI();
114  if (!targetDevUri)
115    return this.stopDeviceStats();
116
117  webservice.ajaxRequest('/stats/' + targetDevUri,
118                         this.onDevStatsAjaxResponse_.bind(this),
119                         this.stopDeviceStats.bind(this));
120};
121
122this.startDeviceStats = function() {
123  timers.start('device_stats',
124               this.refreshDeviceStats.bind(this),
125               this.DEV_STATS_INTERVAL_SEC_);
126};
127
128this.stopDeviceStats = function() {
129  timers.stop('device_stats');
130};
131
132this.onDevStatsAjaxResponse_ = function(data) {
133  this.memChartData_ = new google.visualization.DataTable(data.mem);
134  this.cpuChartData_ = new google.visualization.DataTable(data.cpu);
135  this.redrawDevStats_();
136};
137
138this.redrawDevStats_ = function(data) {
139  if (!this.memChartData_ || !this.cpuChartData_)
140    return;
141
142  this.memChart_.draw(this.memChartData_,
143                       {title: 'System Memory Usage (MB)', is3D: true});
144  this.cpuChart_.draw(this.cpuChartData_,
145                       {title: 'CPU Usage',
146                        isStacked: true,
147                        hAxis: {maxValue: 100, viewWindow: {max: 100}}});
148};
149
150this.refreshSelectedProcessStats = function() {
151  if (!this.selProcUri_)
152    return this.stopSelectedProcessStats();
153
154  webservice.ajaxRequest('/stats/' + this.selProcUri_,
155                         this.onPsStatsAjaxResponse_.bind(this),
156                         this.stopSelectedProcessStats.bind(this));
157};
158
159this.startSelectedProcessStats = function() {
160  timers.start('proc_stats',
161               this.refreshSelectedProcessStats.bind(this),
162               this.PROC_STATS_INTERVAL_SEC_);
163  $('#device_tabs').tabs('option', 'active', 1);
164};
165
166this.stopSelectedProcessStats = function() {
167  timers.stop('proc_stats');
168};
169
170this.onPsStatsAjaxResponse_ = function(data) {
171  this.procCpuChartData_ = new google.visualization.DataTable(data.cpu);
172  this.procMemChartData_ = new google.visualization.DataTable(data.mem);
173  this.redrawPsStats_();
174};
175
176this.redrawPsStats_ = function() {
177  if (!this.procCpuChartData_ || !this.procMemChartData_)
178    return;
179
180  this.procCpuChart_.draw(this.procCpuChartData_, {
181      title: 'CPU stats for ' + this.selProcUri_,
182      seriesType: 'line',
183      vAxes: {0: {title: 'CPU %', maxValue: 100}, 1: {title: '# Threads'}},
184      series: {1: {type: 'bars', targetAxisIndex: 1}},
185      hAxis: {title: 'Run Time'},
186      legend: {alignment: 'end'},
187  });
188  this.procMemChart_.draw(this.procMemChartData_, {
189      title: 'Memory stats for ' + this.selProcUri_,
190      seriesType: 'line',
191      vAxes: {0: {title: 'VM Rss KB'}, 1: {title: '# Page Faults'}},
192      series: {1: {type: 'bars', targetAxisIndex: 1}},
193      hAxis: {title: 'Run Time'},
194      legend: {alignment: 'end'},
195  });
196};
197
198this.redraw = function() {
199  this.redrawPsTable_();
200  if ($('#device_tabs').tabs('option', 'active') == 0)
201    this.redrawDevStats_();
202  else
203    this.redrawPsStats_();
204};
205
206$(document).ready(this.onDomReady_.bind(this));
207
208})();