1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 var delegateList = { 29 "load scripts" : load_scripts, 30 "run" : run, 31 } 32 33 self.addEventListener("message", function(event) { 34 var call = delegateList[event.data["call"]]; 35 var result = call(event.data["args"]); 36 }, false); 37 38 39 function log(text) { 40 self.postMessage({ "call" : "log", "args" : text }); 41 } 42 43 44 function displayplot(content) { 45 self.postMessage({ "call" : "displayplot", "args" : content}); 46 } 47 48 49 function displayprof(content) { 50 self.postMessage({ "call" : "displayprof", "args" : content}); 51 } 52 53 54 function setRange(start, end) { 55 self.postMessage({ "call" : "range", 56 "args" : { "start" : start, "end" : end } }); 57 } 58 59 60 function time(name, fun) { 61 log(name + "..."); 62 var start = Date.now(); 63 fun(); 64 log(" took " + (Date.now() - start) / 1000 + "s.\n"); 65 } 66 67 68 function load_scripts(scripts) { 69 time("Loading scripts", 70 function() { for (var i in scripts) importScripts(scripts[i]); }); 71 self.postMessage({ "call" : "script" }); 72 } 73 74 75 function run(args) { 76 var file = args["file"]; 77 var resx = args["resx"]; 78 var resy = args["resy"]; 79 var distortion = args["distortion"]; 80 var range_start_override = args["range_start"]; 81 var range_end_override = args["range_end"]; 82 83 var reader = new FileReaderSync(); 84 var content_lines; 85 86 time("Reading log file (" + (file.size / 1024).toFixed(1) + " kB)", 87 function() { 88 var content = reader.readAsText(file); 89 content_lines = content.split("\n"); 90 }); 91 92 time("Producing statistical profile", 93 function() { 94 var profile = ""; 95 print = function(text) { profile += text + "\n"; }; 96 // Dummy entries provider, as we cannot call nm. 97 var entriesProvider = new UnixCppEntriesProvider("", ""); 98 var targetRootFS = ""; 99 var separateIc = false; 100 var callGraphSize = 5; 101 var ignoreUnknown = true; 102 var stateFilter = null; 103 var snapshotLogProcessor = null; 104 var range = range_start_override + "," + range_end_override; 105 106 var tickProcessor = new TickProcessor(entriesProvider, 107 separateIc, 108 callGraphSize, 109 ignoreUnknown, 110 stateFilter, 111 snapshotLogProcessor, 112 distortion, 113 range); 114 for (var i = 0; i < content_lines.length; i++) { 115 tickProcessor.processLogLine(content_lines[i]); 116 } 117 tickProcessor.printStatistics(); 118 displayprof(profile); 119 }); 120 121 var input_file_name = "input_temp"; 122 var output_file_name = "output.svg"; 123 124 var psc = new PlotScriptComposer(resx, resy); 125 var objects = 0; 126 127 time("Collecting events (" + content_lines.length + " entries)", 128 function() { 129 var line_cursor = 0; 130 var input = function() { return content_lines[line_cursor++]; }; 131 psc.collectData(input, distortion); 132 psc.findPlotRange(range_start_override, 133 range_end_override, 134 setRange); 135 }); 136 137 time("Assembling plot script", 138 function() { 139 var plot_script = ""; 140 var output = function(text) { plot_script += text + "\n"; }; 141 output("set terminal svg size " + resx + "," + resy + 142 " enhanced font \"Helvetica,10\""); 143 output("set output \""+ output_file_name + "\""); 144 objects = psc.assembleOutput(output); 145 if (FS.findObject(input_file_name)) { 146 FS.deleteFile(input_file_name); 147 } 148 var arrc = Module["intArrayFromString"](plot_script, true); 149 FS.createDataFile("/", input_file_name, arrc); 150 }); 151 152 time("Running gnuplot (" + objects + " objects)", 153 function() { Module.run([input_file_name]); }); 154 155 displayplot(FS.findObject(output_file_name)); 156 } 157 158 159 var Module = { 160 "noInitialRun": true, 161 print: function(text) { 162 self.postMessage({"call": "error", "args": text}); 163 }, 164 printErr: function(text) { 165 self.postMessage({"call": "error", "args": text}); 166 }, 167 }; 168