worker.js 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

var delegateList = {
  "load scripts" : load_scripts,
  "run" : run,
}

self.addEventListener("message", function(event) {
  var call = delegateList[event.data["call"]];
  var result = call(event.data["args"]);
}, false);


function log(text) {
  self.postMessage({ "call" : "log", "args" : text });
}


44 45 46 47 48 49 50
function displayplot(content) {
  self.postMessage({ "call" : "displayplot", "args" : content});
}


function displayprof(content) {
  self.postMessage({ "call" : "displayprof", "args" : content});
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}


function setRange(start, end) {
  self.postMessage({ "call" : "range",
                     "args" : { "start" : start, "end" : end } });
}


function time(name, fun) {
  log(name + "...");
  var start = Date.now();
  fun();
  log(" took " + (Date.now() - start) / 1000 + "s.\n");
}


function load_scripts(scripts) {
  time("Loading scripts",
       function() { for (var i in scripts) importScripts(scripts[i]); });
  self.postMessage({ "call" : "script" });
}


75 76 77 78 79 80
function log_error(text) {
  self.postMessage({"call": "error", "args": text});
  self.postMessage({"call": "reset"});
}


81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
function run(args) {
  var file = args["file"];
  var resx = args["resx"];
  var resy = args["resy"];
  var distortion = args["distortion"];
  var range_start_override = args["range_start"];
  var range_end_override = args["range_end"];

  var reader = new FileReaderSync();
  var content_lines;

  time("Reading log file (" + (file.size / 1024).toFixed(1) + " kB)",
       function() {
         var content = reader.readAsText(file);
         content_lines = content.split("\n");
       });

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  time("Producing statistical profile",
       function() {
         var profile = "";
         print = function(text) { profile += text + "\n"; };
         // Dummy entries provider, as we cannot call nm.
         var entriesProvider = new UnixCppEntriesProvider("", "");
         var targetRootFS = "";
         var separateIc = false;
         var callGraphSize = 5;
         var ignoreUnknown = true;
         var stateFilter = null;
         var snapshotLogProcessor = null;
         var range = range_start_override + "," + range_end_override;

         var tickProcessor = new TickProcessor(entriesProvider,
                                               separateIc,
                                               callGraphSize,
                                               ignoreUnknown,
                                               stateFilter,
                                               snapshotLogProcessor,
                                               distortion,
                                               range);
         for (var i = 0; i < content_lines.length; i++) {
           tickProcessor.processLogLine(content_lines[i]);
         }
         tickProcessor.printStatistics();
         displayprof(profile);
       });

127 128 129
  var input_file_name = "input_temp";
  var output_file_name = "output.svg";

130
  var psc = new PlotScriptComposer(resx, resy, log_error);
131 132
  var objects = 0;

133
  time("Collecting events (" + content_lines.length + " entries)",
134 135 136 137 138 139 140 141 142 143 144 145
       function() {
         var line_cursor = 0;
         var input = function() { return content_lines[line_cursor++]; };
         psc.collectData(input, distortion);
         psc.findPlotRange(range_start_override,
                           range_end_override,
                           setRange);
       });

  time("Assembling plot script",
       function() {
         var plot_script = "";
146
         var output = function(text) { plot_script += text + "\n"; };
147 148 149 150 151 152 153 154 155 156 157
         output("set terminal svg size " + resx + "," + resy +
                " enhanced font \"Helvetica,10\"");
         output("set output \""+ output_file_name + "\"");
         objects = psc.assembleOutput(output);
         if (FS.findObject(input_file_name)) {
           FS.deleteFile(input_file_name);
         }
         var arrc = Module["intArrayFromString"](plot_script, true);
         FS.createDataFile("/", input_file_name, arrc);
       });

158
  time("Running gnuplot (" + objects + " objects)",
159 160
       function() { Module.run([input_file_name]); });

161
  displayplot(FS.findObject(output_file_name));
162 163 164 165 166 167 168 169 170 171 172 173
}


var Module = {
    "noInitialRun": true,
    print: function(text) {
        self.postMessage({"call": "error", "args": text});
    },
    printErr: function(text) {
        self.postMessage({"call": "error", "args": text});
    },
};