PageRenderTime 76ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/diagnosis/index.php

https://github.com/alepharchives/xcache
PHP | 409 lines | 383 code | 10 blank | 16 comment | 91 complexity | f490d73a951fa118b7a3f9cbb84deb93 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. include "../common/common.php";
  3. include get_language_file("./lang");
  4. $knownUnstablePhpVersions = array(
  5. array('=', '5.3.14', 'random corrupt memory on high concurrent'),
  6. );
  7. $module = "diagnosis";
  8. $notes = array();
  9. $activeNote = null;
  10. function checking($item) // {{{
  11. {
  12. global $activeNote;
  13. $activeNote = array('item' => $item);
  14. }
  15. // }}}
  16. function result($type, $result, $suggestion = "") // {{{
  17. {
  18. global $notes, $activeNote;
  19. $notes[] = array(
  20. 'type' => $type
  21. , 'result' => ($type != 'skipped' && !$suggestion ? "OK. " : "") . $result
  22. , 'suggestion' => $suggestion
  23. ) + $activeNote;
  24. }
  25. // }}}
  26. function getCacheInfos() // {{{
  27. {
  28. $phpCacheCount = xcache_count(XC_TYPE_PHP);
  29. $varCacheCount = xcache_count(XC_TYPE_VAR);
  30. $cacheInfos = array();
  31. for ($i = 0; $i < $phpCacheCount; $i ++) {
  32. $cacheInfo = xcache_info(XC_TYPE_PHP, $i);
  33. $cacheInfo['type'] = XC_TYPE_PHP;
  34. $cacheInfos[] = $cacheInfo;
  35. }
  36. for ($i = 0; $i < $varCacheCount; $i ++) {
  37. $cacheInfo = xcache_info(XC_TYPE_VAR, $i);
  38. $cacheInfo['type'] = XC_TYPE_VAR;
  39. $cacheInfos[] = $cacheInfo;
  40. }
  41. return $cacheInfos;
  42. }
  43. // }}}
  44. function getIniFileInfo() // {{{
  45. {
  46. ob_start();
  47. phpinfo(INFO_GENERAL);
  48. $info = ob_get_clean();
  49. ob_start();
  50. if (preg_match_all("!<tr>[^<]*<td[^>]*>[^<]*(?:Configuration|ini|Server API)[^<]*</td>[^<]*<td[^>]*>[^<]*</td>[^<]*</tr>!s", $info, $m)) {
  51. $iniInfo = '<table class="phpinfo">'
  52. . implode('', $m[0])
  53. . '</table>';
  54. }
  55. else {
  56. $iniInfo = '';
  57. }
  58. $loadedIni = '';
  59. $iniDirectory = '';
  60. if (preg_match('!<td class="v">(.*?\\.ini)!', $info, $m)) {
  61. $loadedIni = $m[1];
  62. }
  63. else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) {
  64. $iniDirectory = $m[1];
  65. }
  66. return array($loadedIni, $iniDirectory, $iniInfo);
  67. }
  68. // }}}
  69. $xcacheLoaded = extension_loaded('XCache');
  70. checking(_T("XCache extension")); // {{{
  71. if (!$xcacheLoaded) {
  72. list($loadedIni, $iniDirectory, $iniInfo) = getIniFileInfo();
  73. if ($loadedIni) {
  74. echo sprintf(_T("Add extension=xcache.so (or xcache.dll) in %s"), $loadedIni);
  75. }
  76. else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) {
  77. echo sprintf(_T("Please put a php.ini in %s and add extension=xcache.so (or xcache.dll) in it"), $iniDirectory);
  78. }
  79. else {
  80. echo _T("Cannot detect php.ini location");
  81. }
  82. echo " ", _T("(See above)");
  83. result(N_("error"), _T('Not loaded'), ob_get_clean());
  84. }
  85. else {
  86. result(N_("info"), _T('Loaded'));
  87. }
  88. // }}}
  89. if ($xcacheLoaded) { // {{{ load XCache summary
  90. $cacheInfos = getCacheInfos();
  91. $ooms = 0;
  92. $errors = 0;
  93. $disabled = 0;
  94. $compiling = 0;
  95. $readonlyProtection = false;
  96. $phpCacheCount = xcache_count(XC_TYPE_PHP);
  97. $phpCached = 0;
  98. $varCached = 0;
  99. foreach ($cacheInfos as $cacheInfo) {
  100. $ooms += $cacheInfo['ooms'];
  101. $errors += $cacheInfo['errors'];
  102. $disabled += $cacheInfo['disabled'] ? 1 : 0;
  103. if ($cacheInfo['type'] == XC_TYPE_PHP) {
  104. $compiling += $cacheInfo['compiling'] ? 1 : 0;
  105. $phpCached += $cacheInfo['cached'];
  106. }
  107. if ($cacheInfo['type'] == XC_TYPE_VAR && $cacheInfo['cached']) {
  108. $varCached += $cacheInfo['cached'];
  109. }
  110. if ($cacheInfo['can_readonly']) {
  111. $readonlyProtection = true;
  112. }
  113. }
  114. }
  115. // }}}
  116. checking(_T("Enabling PHP Cacher")); // {{{
  117. if (!$xcacheLoaded) {
  118. result(N_("skipped"), "XCache not loaded");
  119. }
  120. else if (!ini_get("xcache.size")) {
  121. result(N_("error")
  122. , _T("Not enabled")
  123. , _T("Your PHP pages is not accelerated by XCache. Set xcache.size to non-zero, set xcache.cacher = On")
  124. );
  125. }
  126. else if (!$phpCached) {
  127. result(N_("error")
  128. , _T("No php script cached")
  129. , _T("Your PHP pages is not accelerated by XCache. Set xcache.cacher = On")
  130. );
  131. }
  132. else {
  133. result(N_("info"), _T('Enabled'));
  134. }
  135. // }}}
  136. checking(_T("PHP Compile Time Error")); // {{{
  137. if (!$xcacheLoaded) {
  138. result(N_("skipped"), "XCache not loaded");
  139. }
  140. else if (!$phpCacheCount) {
  141. result(N_("skipped"), "XCache PHP cacher not enabled");
  142. }
  143. else if ($errors) {
  144. result(N_("warning")
  145. , _T("Error happened when compiling one or some of your PHP code")
  146. , _T("PHP code failed to be compiled cannot be cached. This usually means there was syntax error in your PHP code. Enable PHP error_log to see what parser error is it, fix your code. This warning won't go away until PHP is restarted")
  147. );
  148. }
  149. else {
  150. result(N_("info"), _T('No error happened'));
  151. }
  152. // }}}
  153. checking(_T("Busy Compiling")); // {{{
  154. if (!$xcacheLoaded) {
  155. result(N_("skipped"), "XCache not loaded");
  156. }
  157. else if (!$phpCacheCount) {
  158. result(N_("skipped"), "XCache PHP cacher not enabled");
  159. }
  160. else if ($compiling) {
  161. result(N_("warning")
  162. , _T("Cache marked as busy for compiling")
  163. , _T("It's ok if this status don't stay for long. Otherwise, it could be a sign of PHP crash/coredump, report to XCache devs")
  164. );
  165. }
  166. else {
  167. result(N_("info"), _T('Idle'));
  168. }
  169. // }}}
  170. checking(_T("Enabling VAR Cacher")); // {{{
  171. if (!$xcacheLoaded) {
  172. result(N_("skipped"), "XCache not loaded");
  173. }
  174. else if (!ini_get("xcache.var_size")) {
  175. result(N_("warning")
  176. , _T("Not enabled")
  177. , _T("PHP code that use XCache caching backend have to use other caching backend instead. Set xcache.var_size to non-zero if you have any PHP code that can use XCache Data Caching API")
  178. );
  179. }
  180. else {
  181. result(N_("info"), _T('Enabled'));
  182. checking(_T("Using VAR Cacher")); // {{{
  183. if (!$varCached) {
  184. result(N_("warning")
  185. , _T("No variable data cached")
  186. , _T("Var Cacher won't work simply by enabling it."
  187. . " PHP code must call XCache APIs like xcache_set() to use it as cache backend. 3rd party web apps may come with XCache support, config it to use XCache as caching backend")
  188. );
  189. }
  190. else {
  191. result(N_("info"), _T('Cache in use'));
  192. }
  193. // }}}
  194. }
  195. // }}}
  196. checking(_T("Cache Size")); // {{{
  197. if (!$xcacheLoaded) {
  198. result(N_("skipped"), "XCache not loaded");
  199. }
  200. else if ($ooms) {
  201. result(N_("warning")
  202. , _T("Out of memory happened when trying to write to cache")
  203. , _T("Increase xcache.size and/or xcache.var_size")
  204. );
  205. }
  206. else {
  207. result(N_("info"), _T('Enough'));
  208. }
  209. // }}}
  210. checking(_T("Hash Slots")); // {{{
  211. if (!$xcacheLoaded) {
  212. result(N_("skipped"), "XCache not loaded");
  213. }
  214. else {
  215. $slotsTooBig = null;
  216. $slotsTooSmall = null;
  217. foreach ($cacheInfos as $cacheInfo) {
  218. if ($cacheInfo['size'] < '1024000' && $cacheInfo['slots'] >= '8192') {
  219. $slotsTooBig = $cacheInfo['type'];
  220. break;
  221. }
  222. if ($cacheInfo['slots'] < $cacheInfo['cached'] / 2) {
  223. $slotsTooSmall = $cacheInfo['type'];
  224. break;
  225. }
  226. }
  227. if (isset($slotsTooBig)) {
  228. $prefix = $slotsTooBig == XC_TYPE_PHP ? '' : 'var_';
  229. result(N_("warning")
  230. , _T("Slots value too big")
  231. , sprintf(_T("A very small value is set to %s value and leave %s value is too big.\n"
  232. . "Decrease %s if small cache is really what you want"), "xcache.{$prefix}size", "xcache.{$prefix}slots", "xcache.{$prefix}slots")
  233. );
  234. }
  235. else if (isset($slotsTooSmall)) {
  236. $prefix = $slotsTooSmall == XC_TYPE_PHP ? '' : 'var_';
  237. result(N_("warning")
  238. , _T("Slots value too small")
  239. , sprintf(_T("So many item are cached. Increase %s to a more proper value"), "xcache.{$prefix}slots")
  240. );
  241. }
  242. else {
  243. result(N_("info"), _T('Looks good'));
  244. }
  245. }
  246. // }}}
  247. checking(_T("Cache Status")); // {{{
  248. if (!$xcacheLoaded) {
  249. result(N_("skipped"), "XCache not loaded");
  250. }
  251. else if ($disabled) {
  252. result(N_("warning")
  253. , _T("At least one of the caches is disabled. ")
  254. , _T("Enable the cache.")
  255. . (ini_get("xcache.crash_on_coredump") ? " " . _T("It was disabled by PHP crash/coredump handler or you disabled it manually.") : _T('You disabled it manually.'))
  256. . (ini_get("xcache.crash_on_coredump") ? " " . _T("If it was caused by PHP crash/coredump, report to XCache devs") : "")
  257. );
  258. }
  259. else {
  260. result(N_("info"), _T('Idle'));
  261. }
  262. // }}}
  263. checking(_T("Coredump Directory")); // {{{
  264. if (!$xcacheLoaded) {
  265. result(N_("skipped"), "XCache not loaded");
  266. }
  267. else if (!ini_get("xcache.coredump_directory")) {
  268. result(N_("info")
  269. , _T("Not enabled")
  270. , _T("Enable coredump to save debugging information in case when PHP crash. It can also be enabled in other module like php-fpm beside XCache")
  271. );
  272. }
  273. else if (ini_get("xcache.coredump_directory")) {
  274. $coreDir = ini_get("xcache.coredump_directory");
  275. if (substr($coreDir, -1) != DIRECTORY_SEPARATOR) {
  276. $coreDir .= DIRECTORY_SEPARATOR;
  277. }
  278. $coreFiles = glob($coreDir . "core*");
  279. if ($coreFiles) {
  280. result(N_("error")
  281. , _T("Core files found:\n") . implode("\n", $coreFiles)
  282. , _T("Disable XCache PHP Cacher (set xcache.size=0), remove the core file(s), then restart PHP. If core file appears again, report call stack backtrace in the core to XCache devs")
  283. );
  284. }
  285. else {
  286. result(N_("info")
  287. , _T("Enabled")
  288. , sprintf(_T("You can see core files if PHP crash in %s if PHP crash"), ini_get("xcache.coredump_directory"))
  289. );
  290. }
  291. }
  292. // }}}
  293. checking(_T("Readonly Protection")); // {{{
  294. if (!$xcacheLoaded) {
  295. result(N_("skipped"), "XCache not loaded");
  296. }
  297. else if (ini_get("xcache.readonly_protection") && !$readonlyProtection) {
  298. result(N_("error")
  299. , _T("Set to enabled but not available")
  300. , _T("Use xcache.mmap_path other than /dev/zero")
  301. );
  302. }
  303. else {
  304. result(N_("info")
  305. , $readonlyProtection ? _T("Enabled") : _T("Disabled")
  306. , _T("Enable readonly_protection == --performance & ++stability. "
  307. . "Disable readonly_protection == ++performance & --stability")
  308. );
  309. }
  310. // }}}
  311. checking(_T("XCache modules")); // {{{
  312. if (!$xcacheLoaded) {
  313. result(N_("skipped"), "XCache not loaded");
  314. }
  315. else {
  316. $xcacheModules = explode(" ", XCACHE_MODULES);
  317. $unexpectedModules = array_intersect($xcacheModules, array("coverager", "disassembler"));
  318. if ($unexpectedModules) {
  319. result(N_("warning")
  320. , implode("\n", $unexpectedModules)
  321. , _T("Acceptable. Module(s) listed are built into XCache but not for production server.\n"
  322. . "Leave it as is if you're feeling good.\n"
  323. . "Re-configure XCache with the module(s) disabled if you're strict with server security.")
  324. );
  325. }
  326. else {
  327. result(N_("info"), _T('Idle'));
  328. }
  329. }
  330. // }}}
  331. checking(_T("XCache test setting")); // {{{
  332. if (!$xcacheLoaded) {
  333. result(N_("skipped"), "XCache not loaded");
  334. }
  335. else if ((int) ini_get('xcache.test') == 1) {
  336. result(N_("warning")
  337. , _T("Enabled")
  338. , _T("xcache.test is for testing only, not for server. set it to off")
  339. );
  340. }
  341. else {
  342. result(N_("info"), _T('Disabled'));
  343. }
  344. // }}}
  345. checking(_T("PHP Version")); // {{{
  346. $phpVersion = phpversion();
  347. $unstablePhpVersionReason = null;
  348. foreach ($knownUnstablePhpVersions as $knownUnstablePhpVersion) {
  349. list($compareOp, $unstablePhpVersion, $reason) = $knownUnstablePhpVersion;
  350. if ($compareOp) {
  351. $isUnstable = version_compare($phpVersion, $unstablePhpVersion, $compareOp);
  352. }
  353. else {
  354. $isUnstable = substr($phpVersion, 0, strlen($unstablePhpVersion)) == $unstablePhpVersion;
  355. }
  356. if ($isUnstable) {
  357. $unstablePhpVersionReason = $reason;
  358. break;
  359. }
  360. }
  361. if ($unstablePhpVersionReason) {
  362. result(N_("error")
  363. , _T("The version of PHP you're using is known to be unstable: ") . $unstablePhpVersionReason
  364. , _T("Upgrade to new version of PHP"));
  365. }
  366. else {
  367. result(N_("info"), _T("Looks good"));
  368. }
  369. // }}}
  370. checking(_T("Extension Compatibility")); // {{{
  371. $loadedZendExtensions = get_loaded_extensions(true);
  372. if (array_search("Zend Optimizer", $loadedZendExtensions) !== false) {
  373. result(N_("info")
  374. , _T("Zend Optimizer loaded")
  375. , _T("Optimizer feature of 'Zend Optimizer' is disabled by XCache due to compatibility reason; the Loader of it is still available, encoded files are still supported")
  376. );
  377. }
  378. else {
  379. result(N_("info"), _T("Looks good"));
  380. }
  381. // }}}
  382. checking(_T("SAPI Compatibility")); // {{{
  383. if (php_sapi_name() == "cgi" || php_sapi_name() == "cgi-fcgi" && !isset($_SERVER["FCGI_ROLE"])) {
  384. result(N_("error"), _T("CGI is not supported"), _T("Use FastCGI or FPM instead"));
  385. }
  386. else if (php_sapi_name() == "cgi-fcgi" && isset($_SERVER["FCGI_ROLE"]) && (int) getenv("PHP_FCGI_CHILDREN") < 1) {
  387. result(N_("error")
  388. , "PHP_FCGI_CHILDREN<1"
  389. , _T("PHP_FCGI_CHILDREN should be >= 1 and use 1 group of parent/childs model. See http://xcache.lighttpd.net/wiki/Faq"));
  390. }
  391. else {
  392. result(N_("info"), _T("Looks good"));
  393. }
  394. // }}}
  395. include "./diagnosis.tpl.php";