XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
7.7 KiB

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