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 snapshotLogProcessor = null;
110         var range = range_start_override + "," + range_end_override;
111
112         var tickProcessor = new TickProcessor(entriesProvider,
113                                               separateIc,
114                                               callGraphSize,
115                                               ignoreUnknown,
116                                               stateFilter,
117                                               snapshotLogProcessor,
118                                               distortion,
119                                               range);
120         for (var i = 0; i < content_lines.length; i++) {
121           tickProcessor.processLogLine(content_lines[i]);
122         }
123         tickProcessor.printStatistics();
124         displayprof(profile);
125       });
126
127  var input_file_name = "input_temp";
128  var output_file_name = "output.svg";
129
130  var psc = new PlotScriptComposer(resx, resy, log_error);
131  var objects = 0;
132
133  time("Collecting events (" + content_lines.length + " entries)",
134       function() {
135         var line_cursor = 0;
136         var input = function() { return content_lines[line_cursor++]; };
137         psc.collectData(input, distortion);
138         psc.findPlotRange(range_start_override,
139                           range_end_override,
140                           setRange);
141       });
142
143  time("Assembling plot script",
144       function() {
145         var plot_script = "";
146         var output = function(text) { plot_script += text + "\n"; };
147         output("set terminal svg size " + resx + "," + resy +
148                " enhanced font \"Helvetica,10\"");
149         output("set output \""+ output_file_name + "\"");
150         objects = psc.assembleOutput(output);
151         if (FS.findObject(input_file_name)) {
152           FS.deleteFile(input_file_name);
153         }
154         var arrc = Module["intArrayFromString"](plot_script, true);
155         FS.createDataFile("/", input_file_name, arrc);
156       });
157
158  time("Running gnuplot (" + objects + " objects)",
159       function() { Module.run([input_file_name]); });
160
161  displayplot(FS.findObject(output_file_name));
162}
163
164
165var Module = {
166    "noInitialRun": true,
167    print: function(text) {
168        self.postMessage({"call": "error", "args": text});
169    },
170    printErr: function(text) {
171        self.postMessage({"call": "error", "args": text});
172    },
173};
174