OLD | NEW |
---|---|
(Empty) | |
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 worker_scripts = [ | |
29 "../csvparser.js", | |
30 "../splaytree.js", | |
31 "../codemap.js", | |
32 "../profile.js", | |
33 "../profile_view.js", | |
34 "../logreader.js", | |
35 "../tickprocessor.js", | |
36 "composer.js", | |
37 "gnuplot-4.6.3-emscripten.js" | |
38 ]; | |
39 | |
40 | |
41 function plotWorker() { | |
42 var worker = null; | |
43 | |
44 var delegateList = { | |
45 "log" : log, | |
46 "error" : logError, | |
47 "display" : display, | |
48 "range" : setRange, | |
49 "script" : scriptLoaded | |
50 } | |
51 | |
52 function initialize() { | |
53 ui.freeze(); | |
54 worker = new Worker("worker.js"); | |
55 running = false; | |
56 | |
57 worker.postMessage({ "call" : "load scripts", | |
58 "args" : worker_scripts }); | |
59 | |
60 worker.addEventListener("message", function(event) { | |
61 console.log(event.data); | |
Jakob Kummerow
2013/06/24 11:03:33
is this intentional?
| |
62 var call = delegateList[event.data["call"]]; | |
63 call(event.data["args"]); | |
64 }); | |
65 } | |
66 | |
67 function scriptLoaded() { | |
68 ui.thaw(); | |
69 } | |
70 | |
71 // Public methods. | |
72 this.run = function(filename, | |
73 resx, resy, | |
74 distortion, | |
75 range_start, range_end) { | |
76 var args = { | |
77 'file' : filename, | |
78 'resx' : resx, | |
79 'resy' : resy, | |
80 'distortion' : distortion, | |
81 'range_start' : range_start, | |
82 'range_end' : range_end | |
83 } | |
84 worker.postMessage({ 'call' : 'run', 'args' : args }); | |
85 } | |
86 | |
87 this.reset = function() { | |
88 if (worker) worker.terminate(); | |
89 initialize(); | |
90 } | |
91 } | |
92 | |
93 | |
94 function UIWrapper() { | |
95 var input_elements = ["range_start", | |
96 "range_end", | |
97 "distortion", | |
98 "start", | |
99 "file"]; | |
100 | |
101 this.log = document.getElementById("log"); | |
102 this.plot = document.getElementById('plot'); | |
103 for (var i in input_elements) { | |
104 var id = input_elements[i]; | |
105 this[id] = document.getElementById(id); | |
106 } | |
107 | |
108 this.freeze = function() { | |
109 this.plot.style.webkitFilter = "grayscale(1)"; | |
110 for (var i in input_elements) { | |
111 this[input_elements[i]].disabled = true; | |
112 } | |
113 } | |
114 | |
115 this.thaw = function() { | |
116 this.plot.style.webkitFilter = ""; | |
117 for (var i in input_elements) { | |
118 this[input_elements[i]].disabled = false; | |
119 } | |
120 } | |
121 | |
122 this.reset = function() { | |
123 this.thaw(); | |
124 this.log.value = ""; | |
125 this.range_start.value = "automatic"; | |
126 this.range_end.value = "automatic"; | |
127 } | |
128 } | |
129 | |
130 | |
131 function log(text) { | |
132 ui.log.value += text; | |
133 ui.log.scrollTop = ui.log.scrollHeight; | |
134 } | |
135 | |
136 | |
137 function logError(text) { | |
138 if (ui.log.value.length > 0 && | |
139 ui.log.value[ui.log.value.length-1] != "\n") { | |
140 ui.log.value += "\n"; | |
141 } | |
142 ui.log.value += "ERROR: " + text + "\n"; | |
143 ui.log.scrollTop = ui.log.scrollHeight; | |
144 error_logged = true; | |
145 } | |
146 | |
147 | |
148 function display(args) { | |
149 if (error_logged) { | |
150 log("Plot failed.\n\n"); | |
151 } else { | |
152 log("Displaying plot. Total time: " + | |
153 (Date.now() - timer) / 1000 + "ms.\n\n"); | |
154 var blob = new Blob([new Uint8Array(args.contents).buffer], | |
155 { "type" : "image\/svg+xml" }); | |
156 window.URL = window.URL || window.webkitURL; | |
157 ui.plot.src = window.URL.createObjectURL(blob); | |
158 } | |
159 ui.thaw(); | |
160 } | |
161 | |
162 | |
163 function start(event) { | |
164 error_logged = false; | |
165 ui.freeze(); | |
166 | |
167 try { | |
168 var file = getSelectedFile(); | |
169 var distortion = getDistortion(); | |
170 var range = getRange(); | |
171 } catch (e) { | |
172 logError(e.message); | |
173 display(); | |
174 return; | |
175 } | |
176 | |
177 timer = Date.now(); | |
178 worker.run(file, kResX, kResY, distortion, range[0], range[1]); | |
179 } | |
180 | |
181 | |
182 function getSelectedFile() { | |
183 var file = ui.file.files[0]; | |
184 if (!file) throw Error("No valid file selected."); | |
185 if (!file.type.toString().match(/text/)) { | |
186 throw Error("'" + escape(file.name) + "' is not a text file."); | |
187 } | |
188 return file; | |
189 } | |
190 | |
191 | |
192 function getDistortion() { | |
193 var input_distortion = | |
194 parseInt(ui.distortion.value, 10); | |
195 if (isNaN(input_distortion)) { | |
196 input_distortion = ui.distortion.value = 4500; | |
197 } | |
198 return input_distortion / 1000000; | |
199 } | |
200 | |
201 | |
202 function getRange() { | |
203 var input_start = | |
204 parseInt(ui.range_start.value, 10); | |
205 if (isNaN(input_start)) input_start = undefined; | |
206 var input_end = | |
207 parseInt(ui.range_end.value, 10); | |
208 if (isNaN(input_end)) input_end = undefined; | |
209 return [input_start, input_end]; | |
210 } | |
211 | |
212 | |
213 function setRange(args) { | |
214 ui.range_start.value = Math.round(args.start); | |
215 ui.range_end.value = Math.round(args.end); | |
216 } | |
217 | |
218 | |
219 function onload() { | |
220 kResX = 1400; | |
221 kResY = 600; | |
222 error_logged = false; | |
223 ui = new UIWrapper(); | |
224 ui.reset(); | |
225 worker = new plotWorker(); | |
226 worker.reset(); | |
227 } | |
228 | |
229 var kResX; | |
230 var kResY; | |
231 var error_logged; | |
232 var ui; | |
233 var worker; | |
234 var timer; | |
235 | |
236 | |
OLD | NEW |