1 | <?php |
---|
2 | |
---|
3 | include("./common.php"); |
---|
4 | |
---|
5 | function freeblock_to_graph($freeblocks, $size) |
---|
6 | { |
---|
7 | global $config; |
---|
8 | |
---|
9 | // cached in static variable |
---|
10 | static $graph_initial; |
---|
11 | if (!isset($graph_initial)) { |
---|
12 | $graph_initial = array_fill(0, $config['percent_graph_width'], 0); |
---|
13 | } |
---|
14 | $graph = $graph_initial; |
---|
15 | foreach ($freeblocks as $b) { |
---|
16 | $begin = $b['offset'] / $size * $config['percent_graph_width']; |
---|
17 | $end = ($b['offset'] + $b['size']) / $size * $config['percent_graph_width']; |
---|
18 | |
---|
19 | if ((int) $begin == (int) $end) { |
---|
20 | $v = $end - $begin; |
---|
21 | $graph[(int) $v] += $v - (int) $v; |
---|
22 | } |
---|
23 | else { |
---|
24 | $graph[(int) $begin] += 1 - ($begin - (int) $begin); |
---|
25 | $graph[(int) $end] += $end - (int) $end; |
---|
26 | for ($i = (int) $begin + 1, $e = (int) $end; $i < $e; $i ++) { |
---|
27 | $graph[$i] += 1; |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | $html = array(); |
---|
32 | $c = 255; |
---|
33 | foreach ($graph as $k => $v) { |
---|
34 | if ($config['percent_graph_type'] != 'free') { |
---|
35 | $v = 1 - $v; |
---|
36 | } |
---|
37 | $v = (int) ($v * $c); |
---|
38 | $r = $g = $c - $v; |
---|
39 | $b = $c; |
---|
40 | $html[] = '<div style="background: rgb(' . "$r,$g,$b" . ')"></div>'; |
---|
41 | } |
---|
42 | return implode('', $html); |
---|
43 | } |
---|
44 | |
---|
45 | function calc_total(&$total, $data) |
---|
46 | { |
---|
47 | foreach ($data as $k => $v) { |
---|
48 | switch ($k) { |
---|
49 | case 'type': |
---|
50 | case 'cache_name': |
---|
51 | case 'cacheid': |
---|
52 | case 'free_blocks': |
---|
53 | continue 2; |
---|
54 | } |
---|
55 | if (!isset($total[$k])) { |
---|
56 | $total[$k] = $v; |
---|
57 | } |
---|
58 | else { |
---|
59 | switch ($k) { |
---|
60 | case 'hits_by_hour': |
---|
61 | case 'hits_by_second': |
---|
62 | foreach ($data[$k] as $kk => $vv) { |
---|
63 | $total[$k][$kk] += $vv; |
---|
64 | } |
---|
65 | break; |
---|
66 | |
---|
67 | default: |
---|
68 | $total[$k] += $v; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | function array_avg($a) |
---|
75 | { |
---|
76 | if (count($a) == 0) { |
---|
77 | return ''; |
---|
78 | } |
---|
79 | return array_sum($a) / count($a); |
---|
80 | } |
---|
81 | |
---|
82 | function bar_hits_percent($v, $percent, $active) |
---|
83 | { |
---|
84 | $r = 220 + (int) ($percent * 25); |
---|
85 | $g = $b = 220 - (int) ($percent * 220); |
---|
86 | $percent = (int) ($percent * 100); |
---|
87 | $a = $active ? ' active' : ''; |
---|
88 | return '<div title="' . $v . '">' |
---|
89 | . '<div class="barf' . $a . '" style="height: ' . (100 - $percent) . '%"></div>' |
---|
90 | . '<div class="barv' . $a . '" style="background: rgb(' . "$r,$g,$b" . '); height: ' . $percent . '%"></div>' |
---|
91 | . '</div>'; |
---|
92 | } |
---|
93 | |
---|
94 | function get_cache_hits_graph($ci, $key) |
---|
95 | { |
---|
96 | if ($ci['cacheid'] == -1) { |
---|
97 | $max = max($ci[$key]); |
---|
98 | } |
---|
99 | else { |
---|
100 | $max = $GLOBALS['maxhits_by_hour'][$ci['type']]; |
---|
101 | } |
---|
102 | if (!$max) { |
---|
103 | $max = 1; |
---|
104 | } |
---|
105 | $t = (time() / (60 * 60)) % 24; |
---|
106 | $html = array(); |
---|
107 | foreach ($ci[$key] as $i => $v) { |
---|
108 | $html[] = bar_hits_percent($v, $v / $max, $i == $t); |
---|
109 | } |
---|
110 | return implode('', $html); |
---|
111 | } |
---|
112 | |
---|
113 | $module = "cacher"; |
---|
114 | if (!extension_loaded('XCache')) { |
---|
115 | include("../common/header.tpl.php"); |
---|
116 | echo '<h1>XCache is not loaded</h1>'; |
---|
117 | ob_start(); |
---|
118 | phpinfo(INFO_GENERAL); |
---|
119 | $info = ob_get_clean(); |
---|
120 | if (preg_match_all("!<tr>[^<]*<td[^>]*>[^<]*(?:Configuration|ini|Server API)[^<]*</td>[^<]*<td[^>]*>[^<]*</td>[^<]*</tr>!s", $info, $m)) { |
---|
121 | echo '<div class="phpinfo">'; |
---|
122 | echo 'PHP Info'; |
---|
123 | echo '<table>'; |
---|
124 | echo implode('', $m[0]); |
---|
125 | echo '</table>'; |
---|
126 | echo '</div>'; |
---|
127 | } |
---|
128 | if (preg_match('!<td class="v">(.*?\\.ini)!', $info, $m)) { |
---|
129 | echo "Please check $m[1]"; |
---|
130 | } |
---|
131 | else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) { |
---|
132 | echo "Please put a php.ini in $m[1] and load XCache extension"; |
---|
133 | } |
---|
134 | else { |
---|
135 | echo "You don't even have a php.ini yet?"; |
---|
136 | } |
---|
137 | echo "(See above)"; |
---|
138 | include("../common/footer.tpl.php"); |
---|
139 | exit; |
---|
140 | } |
---|
141 | $pcnt = xcache_count(XC_TYPE_PHP); |
---|
142 | $vcnt = xcache_count(XC_TYPE_VAR); |
---|
143 | |
---|
144 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
---|
145 | $remove = @ $_POST['remove']; |
---|
146 | if ($remove && is_array($remove)) { |
---|
147 | foreach ($remove as $name) { |
---|
148 | xcache_unset($name); |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | $moduleinfo = null; |
---|
154 | $type_none = -1; |
---|
155 | if (!isset($_GET['type'])) { |
---|
156 | $_GET['type'] = $type_none; |
---|
157 | } |
---|
158 | $_GET['type'] = $type = (int) $_GET['type']; |
---|
159 | |
---|
160 | // {{{ process clear, enable, disable |
---|
161 | function processAction() |
---|
162 | { |
---|
163 | $type = isset($_POST['type']) ? $_POST['type'] : null; |
---|
164 | if ($type != XC_TYPE_PHP && $type != XC_TYPE_VAR) { |
---|
165 | $type = null; |
---|
166 | } |
---|
167 | if (isset($type)) { |
---|
168 | $cacheid = (int) (isset($_POST['cacheid']) ? $_POST['cacheid'] : 0); |
---|
169 | if (isset($_POST['clearcache'])) { |
---|
170 | xcache_clear_cache($type, $cacheid); |
---|
171 | } |
---|
172 | if (isset($_POST['enable'])) { |
---|
173 | xcache_enable_cache($type, $cacheid); |
---|
174 | } |
---|
175 | if (isset($_POST['disable'])) { |
---|
176 | xcache_enable_cache($type, $cacheid, false); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | processAction(); |
---|
181 | if (isset($_POST['coredump'])) { |
---|
182 | xcache_coredump(); |
---|
183 | } |
---|
184 | // }}} |
---|
185 | // {{{ load info/list |
---|
186 | $cacheinfos = array(); |
---|
187 | $total = array(); |
---|
188 | $maxhits_by_hour = array(0, 0); |
---|
189 | for ($i = 0; $i < $pcnt; $i ++) { |
---|
190 | $data = xcache_info(XC_TYPE_PHP, $i); |
---|
191 | if ($type === XC_TYPE_PHP) { |
---|
192 | $data += xcache_list(XC_TYPE_PHP, $i); |
---|
193 | } |
---|
194 | $data['type'] = XC_TYPE_PHP; |
---|
195 | $data['cache_name'] = "php#$i"; |
---|
196 | $data['cacheid'] = $i; |
---|
197 | $cacheinfos[] = $data; |
---|
198 | $maxhits_by_hour[XC_TYPE_PHP] = max($maxhits_by_hour[XC_TYPE_PHP], max($data['hits_by_hour'])); |
---|
199 | if ($pcnt >= 2) { |
---|
200 | calc_total($total, $data); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | if ($pcnt >= 2) { |
---|
205 | $total['type'] = XC_TYPE_PHP; |
---|
206 | $total['cache_name'] = _('Total'); |
---|
207 | $total['cacheid'] = -1; |
---|
208 | $total['gc'] = null; |
---|
209 | $total['istotal'] = true; |
---|
210 | unset($total['compiling']); |
---|
211 | $cacheinfos[] = $total; |
---|
212 | } |
---|
213 | |
---|
214 | $total = array(); |
---|
215 | for ($i = 0; $i < $vcnt; $i ++) { |
---|
216 | $data = xcache_info(XC_TYPE_VAR, $i); |
---|
217 | if ($type === XC_TYPE_VAR) { |
---|
218 | $data += xcache_list(XC_TYPE_VAR, $i); |
---|
219 | } |
---|
220 | $data['type'] = XC_TYPE_VAR; |
---|
221 | $data['cache_name'] = "var#$i"; |
---|
222 | $data['cacheid'] = $i; |
---|
223 | $cacheinfos[] = $data; |
---|
224 | $maxhits_by_hour[XC_TYPE_VAR] = max($maxhits_by_hour[XC_TYPE_VAR], max($data['hits_by_hour'])); |
---|
225 | if ($vcnt >= 2) { |
---|
226 | calc_total($total, $data); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | if ($vcnt >= 2) { |
---|
231 | $total['type'] = XC_TYPE_VAR; |
---|
232 | $total['cache_name'] = _('Total'); |
---|
233 | $total['cacheid'] = -1; |
---|
234 | $total['gc'] = null; |
---|
235 | $total['istotal'] = true; |
---|
236 | $cacheinfos[] = $total; |
---|
237 | } |
---|
238 | // }}} |
---|
239 | // {{{ merge the list |
---|
240 | switch ($type) { |
---|
241 | case XC_TYPE_PHP: |
---|
242 | case XC_TYPE_VAR: |
---|
243 | $cachelist = array('type' => $type, 'cache_list' => array(), 'deleted_list' => array()); |
---|
244 | if ($type == XC_TYPE_VAR) { |
---|
245 | $cachelist['type_name'] = 'var'; |
---|
246 | } |
---|
247 | else { |
---|
248 | $cachelist['type_name'] = 'php'; |
---|
249 | } |
---|
250 | foreach ($cacheinfos as $i => $c) { |
---|
251 | if (!empty($c['istotal'])) { |
---|
252 | continue; |
---|
253 | } |
---|
254 | if ($c['type'] == $type && isset($c['cache_list'])) { |
---|
255 | foreach ($c['cache_list'] as $e) { |
---|
256 | $e['cache_name'] = $c['cache_name']; |
---|
257 | $cachelist['cache_list'][] = $e; |
---|
258 | } |
---|
259 | foreach ($c['deleted_list'] as $e) { |
---|
260 | $e['cache_name'] = $c['cache_name']; |
---|
261 | $cachelist['deleted_list'][] = $e; |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | if ($type == XC_TYPE_PHP) { |
---|
266 | $inodes = array(); |
---|
267 | $haveinode = false; |
---|
268 | foreach ($cachelist['cache_list'] as $e) { |
---|
269 | if (isset($e['file_inode'])) { |
---|
270 | $haveinode = true; |
---|
271 | break; |
---|
272 | } |
---|
273 | } |
---|
274 | if (!$haveinode) { |
---|
275 | foreach ($cachelist['deleted_list'] as $e) { |
---|
276 | if (isset($e['file_inode'])) { |
---|
277 | $haveinode = true; |
---|
278 | break; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | unset($data); |
---|
284 | break; |
---|
285 | |
---|
286 | default: |
---|
287 | $_GET['type'] = $type_none; |
---|
288 | $cachelist = array(); |
---|
289 | ob_start(); |
---|
290 | phpinfo(INFO_MODULES); |
---|
291 | $moduleinfo = ob_get_clean(); |
---|
292 | if (preg_match_all('!(XCache[^<>]*)</a></h2>(.*?)<h2>!is', $moduleinfo, $m)) { |
---|
293 | $moduleinfo = array(); |
---|
294 | foreach ($m[1] as $i => $dummy) { |
---|
295 | $moduleinfo[] = '<h3>' . trim($m[1][$i]) . '</h3>'; |
---|
296 | $moduleinfo[] = str_replace('<br />', '', trim($m[2][$i])); |
---|
297 | } |
---|
298 | $moduleinfo = implode('', $moduleinfo); |
---|
299 | } |
---|
300 | else { |
---|
301 | $moduleinfo = null; |
---|
302 | } |
---|
303 | break; |
---|
304 | } |
---|
305 | // }}} |
---|
306 | |
---|
307 | $type_php = XC_TYPE_PHP; |
---|
308 | $type_var = XC_TYPE_VAR; |
---|
309 | $listTypes = array($type_none => _('Statistics'), $type_php => _('List PHP'), $type_var => _('List Var Data')); |
---|
310 | |
---|
311 | include("cacher.tpl.php"); |
---|
312 | |
---|
313 | ?> |
---|