profiler.js revision effb81e5f8246d0db0270817048dc992db66e9fb
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// This module handles the UI for the profiling. A memory profile is obtained 6// asking the webservice to profile some data sources (through /profile/create) 7// as, for instance, some mmaps dumps or some heap traces. 8// Regardless of the data source, a profile consists of three main concepts: 9// 1. A tree of buckets, each one having a name and a set of 1+ values (see 10// /classification/rules.py,results.py), one value per metric (see below). 11// 2. A set of snapshots, identifying the times when the dump was taken. All the 12// snapshots have the same shape and the same nodes (but their values can 13// obviously differ). 14// 3. A set of metrics, identifying the cardinality (how many) and the semantic 15// (what do they mean) of the values of the nodes in the results tree. 16// 17// From a graphical viewpoint a profile is displayed using two charts: 18// - A tree (organizational) chart which shows, for a given snapshot and metric, 19// the taxonomy of the buckets and their corresponding value. 20// - A time series (scattered area) chart which shows, for a given metric and a 21// given bucket, its evolution over time (and of its direct children). 22 23profiler = new (function() { 24 25this.treeData_ = null; 26this.treeChart_ = null; 27this.timeSeriesData_ = null; 28this.timeSeriesChart_ = null; 29this.isRedrawing_ = false; 30this.profileId_ = null; // The profile id retrieved on /ajax/profile/create. 31this.times_ = []; // Snapshot times: [0,4,8] -> 3 snapshots x 4 sec. 32this.metrics_ = []; // Keys in the result tree, e.g., ['RSS', 'PSS']. 33this.curTime_ = null; // Time of the snapshot currently displayed. 34this.curMetric_ = null; // Index (rel. to |metrics_|) currently displayed. 35this.curBucket_ = null; // Index (rel. to the tree) currently displayed. 36 37this.onDomReady_ = function() { 38 this.treeChart_ = new google.visualization.OrgChart($('#prof-tree_chart')[0]); 39 this.timeSeriesChart_ = new google.visualization.SteppedAreaChart( 40 $('#prof-time_chart')[0]); 41 42 // TODO(primiano): De-hardcodify the ruleset and list/load from the server. 43 $('#prof-ruleset').append( 44 $('<option>').val('default/mmap-android.py').text('mmap-Android')); 45 46 // Setup the UI event listeners to trigger the onUiParamsChange_ event. 47 google.visualization.events.addListener(this.treeChart_, 'select', 48 this.onUiParamsChange_.bind(this)); 49 $('#prof-metric').on('change', this.onUiParamsChange_.bind(this)); 50 $('#prof-time').slider({range: 'max', min: 0, max: 0, value: 0, 51 change: this.onUiParamsChange_.bind(this)}); 52}; 53 54this.profileCachedMmapDump = function(mmapDumpId) { 55 // Creates a profile using the data grabbed during a recent mmap dump. 56 // This is used to get a quick overview (only one snapshot), of the memory 57 // without doing a full periodic trace first. 58 webservice.ajaxRequest('/profile/create', // This is a POST request. 59 this.onProfileAjaxResponse_.bind(this), 60 null, // use the default error handler. 61 {type: 'mmap', 62 source: 'cache', 63 id: mmapDumpId, 64 ruleset: $('#prof-ruleset').val()}); 65}; 66 67this.profileArchivedMmaps = function(archiveName, snapshots) { 68 // Creates a profile using the data from the storage. 69 webservice.ajaxRequest('/profile/create', // This is a POST request. 70 this.onProfileAjaxResponse_.bind(this), 71 null, // use the default error handler. 72 {type: 'mmap', 73 source: 'archive', 74 archive: archiveName, 75 snapshots: snapshots, 76 ruleset: $('#prof-ruleset').val()}); 77}; 78 79this.onProfileAjaxResponse_ = function(data) { 80 // This AJAX response contains a summary of the profile requested via the 81 // /profile endpoint, which consists of: 82 // - The number of snapshots (and their corresponding time) in an array. 83 // e.g., [0, 3 ,6] indicates that the profile contains three snapshots taken 84 // respectively at T=0, T=3 and T=6 sec. 85 // - A list of profile metrics, e.g., ['RSS', 'P. Dirty'] indicates that every 86 // node in the result tree is a 2-tuple. 87 // After this response, the concrete data for the charts can be fetched using 88 // the /ajax/profile/{ID}/tree and /ajax/profile/{ID}/time_serie endpoints. 89 this.profileId_ = data.id; 90 this.times_ = data.times; // An array of integers. 91 this.metrics_ = data.metrics; // An array of strings. 92 this.curBucket_ = data.rootBucket; // URI of the bucket, e.g., Total/Libs/. 93 this.curTime_ = data.times[0]; 94 this.curMetric_ = 0; 95 96 // Populate the "metrics" select box. 97 $('#prof-metric').empty(); 98 this.metrics_.forEach(function(metric) { 99 $('#prof-metric').append($('<option/>').text(metric)); 100 }, this); 101 102 // Setup the bounds of the snapshots slider. 103 $('#prof-time').slider('option', 'max', this.times_.length - 1); 104 105 // Fetch the actual chart data (via /profile/{ID}/...) and redraw the charts. 106 this.updateCharts(); 107}; 108 109this.onUiParamsChange_ = function() { 110 // Triggered whenever any of the UI params (the metric select, the snapshot 111 // slider or the selected bucket in the tree) changes. 112 this.curMetric_ = $('#prof-metric').prop('selectedIndex'); 113 this.curTime_ = this.times_[$('#prof-time').slider('value')]; 114 $('#prof-time_label').text(this.curTime_); 115 var selBucket = this.treeChart_.getSelection(); 116 if (selBucket.length) 117 this.curBucket_ = this.treeData_.getValue(selBucket[0].row, 0); 118 this.updateCharts(); 119}; 120 121this.updateCharts = function() { 122 if (!this.profileId_) 123 return; 124 125 var profileUri = '/profile/' + this.profileId_; 126 webservice.ajaxRequest( 127 profileUri +'/tree/' + this.curMetric_ + '/' + this.curTime_, 128 this.onTreeAjaxResponse_.bind(this)); 129 webservice.ajaxRequest( 130 profileUri +'/time_serie/' + this.curMetric_ + '/' + this.curBucket_, 131 this.onTimeSerieAjaxResponse_.bind(this)); 132}; 133 134this.onTreeAjaxResponse_ = function(data) { 135 this.treeData_ = new google.visualization.DataTable(data); 136 this.redrawTree_(); 137}; 138 139this.onTimeSerieAjaxResponse_ = function(data) { 140 this.timeSeriesData_ = new google.visualization.DataTable(data); 141 this.redrawTimeSerie_(); 142}; 143 144this.redrawTree_ = function() { 145 // isRedrawing_ is used here to break the avalanche chain that would be caused 146 // by redraw changing the node selection, triggering in turn another redraw. 147 if (!this.treeData_ || this.isRedrawing_) 148 return; 149 150 this.isRedrawing_ = true; 151 var savedSelection = this.treeChart_.getSelection(); 152 this.treeChart_.draw(this.treeData_, {allowHtml: true}); 153 154 // "If we want things to stay as they are, things will have to change." 155 // (work around GChart bug, as if we didn't have enough problems on our own). 156 this.treeChart_.setSelection([{row: null, column: null}]); 157 this.treeChart_.setSelection(savedSelection); 158 this.isRedrawing_ = false; 159}; 160 161this.redrawTimeSerie_ = function() { 162 if (!this.timeSeriesData_) 163 return; 164 165 var metric = this.metrics_[this.curMetric_]; 166 this.timeSeriesChart_.draw(this.timeSeriesData_, { 167 title: metric + ' over time for ' + this.curBucket_, 168 isStacked: true, 169 hAxis: {title: 'Time [sec.]'}, 170 vAxis: {title: this.metrics_[this.curMetric_] + ' [KB]'}}); 171}; 172 173this.redraw = function() { 174 this.redrawTree_(); 175 this.redrawTimeSerie_(); 176}; 177 178$(document).ready(this.onDomReady_.bind(this)); 179 180})();