source: trunk/FACT++/www/viewer/index.php@ 17722

Last change on this file since 17722 was 17722, checked in by tbretz, 12 years ago
By never using more than one php array for a single pixel at a time instead of 1440 the memory consumption could be significantly decreased, however, I do not know how much memory the corresponding JS array uses, but I guess it is much less.
File size: 8.1 KB
Line 
1<?php
2
3if (!extension_loaded('v8js'))
4 die("V8Js missing");
5
6if (isset($_POST['editor1']) || isset($_POST['editor2']))
7{
8 $isOne = isset($_POST['editor1']);
9
10 $source = $isOne ? $_POST['editor1'] : $_POST['editor2'];
11 if (!isset($_POST['files']))
12 $name = $isOne ? "proc.js" : "main.js";
13 else
14 $name = $_POST['files'][0];
15
16 header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
17 header("Cache-Control: public"); // needed for i.e.
18 header("Content-Type: text/plain");
19 header("Content-Transfer-Encoding: Text");
20 header("Content-Disposition: attachment; filename=\"".$name."\"");
21 print($source);
22 return;
23}
24
25if (!isset($_POST['file']) || !isset($_POST['event']) || !isset($_POST['pixel']))
26{
27 /*
28 function getList($path)
29 {
30 $hasdir = false;
31
32 $list = array();
33 foreach (new DirectoryIterator($path) as $file)
34 {
35 if ($file->isDot())
36 continue;
37
38 $name = $file->getFilename();
39
40
41 if ($file->isDir())
42 {
43 $list[$name] = getList($path."/".$name);
44 $hasdir = true;
45 }
46
47 if ($file->isFile() && $file->isReadable())
48 {
49 if (substr($name, 12)!=".fits.fz")
50 continue;
51
52 array_push($list, substr($name, 9, 3));
53 }
54 }
55
56 if (!$hasdir)
57 sort($list);
58 return $list;
59 }
60 */
61
62 function getList($path, $sub)
63 {
64 $hasdir = false;
65
66 $list = array();
67 foreach (new DirectoryIterator($path."/".$sub) as $file)
68 {
69 if ($file->isDot())
70 continue;
71
72 $name = $file->getFilename();
73
74 if ($file->isDir())
75 $list = array_merge($list, getList($path, $sub."/".$name));
76
77 if ($file->isFile() && $file->isReadable())
78 {
79 if (substr($name, 12)!=".fits.fz")
80 continue;
81
82 $rc = substr($name, 0, 4)."/".substr($name, 4, 2)."/".substr($name, 6, 2)."-".substr($name, 9, 3);
83
84 array_push($list, $rc);
85 }
86 }
87
88 return $list;
89 }
90
91 try
92 {
93 $list = getList("/daq/raw", "");
94 sort($list);
95 }
96 catch (Exception $e)
97 {
98 return header('HTTP/1.0 400 '.$e->getMessage());
99 }
100
101 print(json_encode($list));
102 return;
103}
104
105$event = intval($_POST['event']);
106$pixel = intval($_POST['pixel']);
107
108function get($handle, $format, $count = 1)
109{
110 $size = 0;
111 switch ($format)
112 {
113 case 'L': $size = 4; break;
114 case 'S': $size = 2; break;
115 case 's': $size = 2; break;
116 }
117
118 if ($size==0)
119 return;
120
121 $binary = fread($handle, $size*$count);
122 $data = unpack($format.$count, $binary);
123
124 return $count==1 ? $data[1] : $data;
125}
126
127//ini_set("memory_limit", "64M");
128
129//define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
130// E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
131
132$rc = array();
133$rc['startPhp'] = microtime(true);
134
135// ============================ Read data from file ==========================
136$file = $_POST['file'];
137
138$y = substr($file, 0, 4);
139$m = substr($file, 5, 2);
140$d = substr($file, 8, 2);
141$r = substr($file, 11, 3);
142
143$filename = "/daq/raw/".$y."/".$m."/".$d."/".$y.$m.$d."_".$r.".fits.fz";
144
145$file = popen("/home/fact/FACT++/getevent ".$filename." ".$event." 2> /dev/null", "r");
146if (!$file)
147 return header('HTTP/1.0 400 Could not open file.');
148
149$evt = array();
150
151$evt['numEvents'] = get($file, "L");
152$evt['numRoi'] = get($file, "L");
153$evt['numPix'] = get($file, "L");
154$evt['eventNumber'] = get($file, "L");
155$evt['triggerNumber'] = get($file, "L");
156$evt['triggerType'] = get($file, "S");
157$evt['unixTime'] = get($file, "L", 2);
158
159if (isset($_POST['source1']))
160{
161 // Read the data and copy it from an associative array to an Array
162 // (this is just nicer and seems more logical)
163 //$data = array();
164 $binary = array();
165 for ($i=0; $i<$evt['numPix']; $i++)
166 $binary[$i] = fread($file, 2*$evt['numRoi']);
167 /*
168 $data = array();
169 for ($i=0; $i<$evt['numPix']; $i++)
170 {
171 $var = get($file, "s", $evt['numRoi']);
172 $data[$i] = array();
173 for ($j=0; $j<$evt['numRoi']; $j++)
174 $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
175 }*/
176}
177
178//if (feof($file))
179// return;
180
181pclose($file);
182
183if ($evt['numEvents']==0)
184 return header('HTTP/1.0 400 Could not read event.');
185
186// =============================== Copy waveform =============================
187
188$rc['event'] = $evt;
189$rc['event']['index'] = $event;
190$rc['event']['pixel'] = $pixel;
191//$rc['waveform'] = $data[$pixel];
192
193// Get execution times
194$now = microtime(true);
195
196if (isset($_POST['source1']))
197{
198 // =============================== Run Javascript ============================
199
200/*
201$JS = <<< EOT
202require("path/to/module1");
203EOT;
204
205$v8 = new V8Js();
206$v8->setModuleLoader(function($module) {
207 switch ($module) {
208 case 'path/to/module1':
209 return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
210
211 case 'path/to/module2':
212 return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
213
214 default:
215 return 'print(' . json_encode($module . PHP_EOL) . ');';
216 }
217});
218*/
219
220 // V8Js global methods: exit, print, sleep, var_dump, require
221 //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
222 $v8 = new V8Js("$"/*, array("data"=>"data")*/);
223
224 //$v8 = new V8Js("$", array("data"=>"data"));
225
226 //V8Js::registerExtension("exit", "1");
227 //$v8 = new V8Js("$", array(), array("exit"));
228
229 //$v8->func = function($a) { echo "Closure with param $a\n"; };
230 //$v8->greeting = "From PHP with love!";
231
232 // This is much faster than the variables option in the constructor
233
234 $roi = $evt['numRoi'];
235
236 //$v8->data = $data;
237 //$v8->test = array();;
238 //$v8->data = array();
239 $v8->event = $evt;
240 $v8->clone = function($data) { return $data; };
241 $v8->unpack = function($i)
242 {
243 global $binary, $roi;
244 $u = unpack("s".$roi, $binary[$i]);
245 $arr = array();
246 for ($i=0; $i<$roi; $i++)
247 $arr[$i] = $u[$i+1]*0.48828125;
248 return $arr;
249 };
250
251 // 10, 445, 91.75 MiB
252 // 376, 720, 91.75 MiB
253
254 $rc['startJs'] = microtime(true);
255 $rc['timeJS'] = array();
256
257 // Buffer output from javascript
258 ob_start();
259
260 try
261 {
262
263 // We unpack the data pixel by pixel and copy the array directly to the
264 // Javasscript array. This significantly decreases memory usage because
265 // we need only one of the super memory hungry php arrays of size ROI
266 // instead of 1440.
267 $JS = "$.data = new Array($.event.numPix); for (var i=0; i<$.event.numPix; i++) $.data[i] = $.unpack(i);";
268 $v8->executeString($JS, 'internal');
269
270 $rc['timeJs'][0] = (microtime(true) - $rc['startJs']);
271
272 $JS = "function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
273 $rc['waveform'] = $v8->executeString($JS, 'proc');
274
275 $rc['timeJs'][1] = (microtime(true) - $rc['startJs']);
276
277 if (isset($_POST['source2']))
278 {
279 $JS = "(function main(){\n".$_POST['source2']."\n})();";
280 $rc['ret'] = $v8->executeString($JS, 'main');
281 }
282
283 // This is supposed to work, but it seems it does not...
284 //$v8->proc = $rc['ret']->proc;
285 //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
286 }
287 catch (V8JsException $e)
288 {
289 $rc['err'] = array();
290 $rc['err']['message'] = $e->getMessage();
291 $rc['err']['file'] = $e->getJsFileName();
292 $rc['err']['sourceLine'] = $e->getJsSourceLine();
293 $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
294 $rc['err']['trace'] = $e->getJsTrace();
295 }
296
297 // Copy output buffer and clean it
298 $rc['debug'] = ob_get_contents();
299 ob_end_clean();
300
301 // Get execution times
302 $now = microtime(true);
303
304 $rc['timeJs'][2] = ($now - $rc['startJs']);
305}
306
307$rc['timePhp'] = ($now - $rc['startPhp']);
308
309$rc['memory'] = memory_get_peak_usage(true)/1024/1024;
310
311// Output result as JSON object
312print(json_encode($rc));
313
314?>
Note: See TracBrowser for help on using the repository browser.