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
28var delegateList = {
29  "load scripts" : load_scripts,
30  "run" : run,
31}
32
33self.addEventListener("message", function(event) {
34  var call = delegateList[event.data["call"]];
35  var result = call(event.data["args"]);
36}, false);
37
38
39function log(text) {
40  self.postMessage({ "call" : "log", "args" : text });
41}
42
43
44function displayplot(content) {
45  self.postMessage({ "call" : "displayplot", "args" : content});
46}
47
48
49function displayprof(content) {
50  self.postMessage({ "call" : "displayprof", "args" : content});
51}
52
53
54function setRange(start, end) {
55  self.postMessage({ "call" : "range",
56                     "args" : { "start" : start, "end" : end } });
57}
58
59
60function 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
68function 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
75function log_error(text) {
76  self.postMessage({"call": "error", "args": text});
77  self.postMessage({"call": "reset"});
78}
79
80
81function run(args) {
82  var file = args["file"];
83  var resx = args["resx"];
84  var resy = args["resy"];
85  var distortion = args["distortion"];
86  var range_start_override = args["range_start"];
87  var range_end_override = args["range_end"];
88
89  var reader = new FileReaderSync();
90  var content_lines;
91
92  time("Reading log file (" + (file.size / 1024).toFixed(1) + " kB)",
93       function() {
94         var content = reader.readAsText(file);
95         content_lines = content.split("\n");
96       });
97
98  time("Producing statistical profile",
99       function() {
100         var profile = "";
101         print = function(text) { profile += text + "\n"; };
102         // Dummy entries provider, as we cannot call nm.
103         var entriesProvider = new UnixCppEntriesProvider("", "");
104         var targetRootFS = "";
105         var separateIc = false;
106         var callGraphSize = 5;
107         var ignoreUnknown = true;
108         var stateFilter = null;
109         var range = range_start_override + "," + range_end_override;
110
111         var tickProcessor = new TickProcessor(entriesProvider,
112                                               separateIc,
113                                               callGraphSize,
114                                               ignoreUnknown,
115                                               stateFilter,
116                                               distortion,
117                                               range);
118         for (var i = 0; i < content_lines.length; i++) {
119           tickProcessor.processLogLine(content_lines[i]);
120         }
121         tickProcessor.printStatistics();
122         displayprof(profile);
123       });
124
125  var input_file_name = "input_temp";
126  var output_file_name = "output.svg";
127
128  var psc = new PlotScriptComposer(resx, resy, log_error);
129  var objects = 0;
130
131  time("Collecting events (" + content_lines.length + " entries)",
132       function() {
133         var line_cursor = 0;
134         var input = function() { return content_lines[line_cursor++]; };
135         psc.collectData(input, distortion);
136         psc.findPlotRange(range_start_override,
137                           range_end_override,
138                           setRange);
139       });
140
141  time("Assembling plot script",
142       function() {
143         var plot_script = "";
144         var output = function(text) { plot_script += text + "\n"; };
145         output("set terminal svg size " + resx + "," + resy +
146                " enhanced font \"Helvetica,10\"");
147         output("set output \""+ output_file_name + "\"");
148         objects = psc.assembleOutput(output);
149         if (FS.findObject(input_file_name)) {
150           FS.deleteFile(input_file_name);
151         }
152         var arrc = Module["intArrayFromString"](plot_script, true);
153         FS.createDataFile("/", input_file_name, arrc);
154       });
155
156  time("Running gnuplot (" + objects + " objects)",
157       function() { Module.run([input_file_name]); });
158
159  displayplot(FS.findObject(output_file_name));
160}
161
162
163var Module = {
164    "noInitialRun": true,
165    print: function(text) {
166        self.postMessage({"call": "error", "args": text});
167    },
168    printErr: function(text) {
169        self.postMessage({"call": "error", "args": text});
170    },
171};
172