From 0f0f0768191520dae3d3f2d9d128e8797d84ca1f Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 19 Oct 2010 09:23:08 +0000 Subject: [PATCH] MDL-24751 simplified use of raise_memory_limit() by introduction of memory use constants, set memory limit before including our large libs, adding support for G suffix introduced in PHP5 --- lib/setup.php | 6 ++-- lib/setuplib.php | 77 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 1c91ae381c4..cdd0ef51ed3 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -337,6 +337,9 @@ $CFG->httpswwwroot = $CFG->wwwroot; require_once($CFG->libdir .'/setuplib.php'); // Functions that MUST be loaded first +// Increase memory limits if possible +raise_memory_limit(MEMORY_STANDARD); + // Time to start counting init_performance_info(); @@ -403,9 +406,6 @@ require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions // make sure PHP is not severly misconfigured setup_validate_php_configuration(); -// Increase memory limits if possible -raise_memory_limit('96M'); // We should never NEED this much but just in case... - // Connect to the database setup_DB(); diff --git a/lib/setuplib.php b/lib/setuplib.php index 6e6d8b7c9c9..34cbc387b85 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -31,15 +31,27 @@ defined('MOODLE_INTERNAL') || die(); /// Debug levels /// /** no warnings at all */ -define ('DEBUG_NONE', 0); +define('DEBUG_NONE', 0); /** E_ERROR | E_PARSE */ -define ('DEBUG_MINIMAL', 5); +define('DEBUG_MINIMAL', 5); /** E_ERROR | E_PARSE | E_WARNING | E_NOTICE */ -define ('DEBUG_NORMAL', 15); +define('DEBUG_NORMAL', 15); /** E_ALL without E_STRICT for now, do show recoverable fatal errors */ -define ('DEBUG_ALL', 6143); +define('DEBUG_ALL', 6143); /** DEBUG_ALL with extra Moodle debug messages - (DEBUG_ALL | 32768) */ -define ('DEBUG_DEVELOPER', 38911); +define('DEBUG_DEVELOPER', 38911); + +/** Remove any memory limits */ +define('MEMORY_UNLIMITED', -1); +/** Standard memory limit for given platform */ +define('MEMORY_STANDARD', -2); +/** + * Large memory limit for given platform - used in cron, upgrade, and other places that need a lot of memory. + * Can be overridden with $CFG->extramemorylimit setting. + */ +define('MEMORY_EXTRA', -3); +/** Extremely large memory limit - not recommended for standard scripts */ +define('MEMORY_HUGE', -4); /** * Simple class. It is usually used instead of stdClass because it looks @@ -773,16 +785,43 @@ function during_initial_install() { * Function to raise the memory limit to a new value. * Will respect the memory limit if it is higher, thus allowing * settings in php.ini, apache conf or command line switches - * to override it + * to override it. * - * The memory limit should be expressed with a string (eg:'64M') + * The memory limit should be expressed with a constant + * MEMORY_STANDARD, MEMORY_EXTRA or MEMORY_HUGE. + * It is possible to use strings or integers too (eg:'128M'). * - * @param string $newlimit the new memory limit - * @return bool + * @param mixed $newlimit the new memory limit + * @return bool success */ function raise_memory_limit($newlimit) { + global $CFG; - if (empty($newlimit)) { + if ($newlimit == MEMORY_UNLIMITED) { + ini_set('memory_limit', -1); + return true; + + } else if ($newlimit == MEMORY_STANDARD) { + $newlimit = get_real_size('96M'); + + } else if ($newlimit == MEMORY_EXTRA) { + $newlimit = get_real_size('256M'); + if (empty($CFG->extramemorylimit)) { + $extra = get_real_size($CFG->extramemorylimit); + if ($extra > $newlimit) { + $newlimit = $extra; + } + } + + } else if ($newlimit == MEMORY_HUGE) { + $newlimit = get_real_size('2G'); + + } else { + $newlimit = get_real_size($newlimit); + } + + if ($newlimit <= 0) { + debugging('Invalid memory limit specified.'); return false; } @@ -790,16 +829,15 @@ function raise_memory_limit($newlimit) { if (empty($cur)) { // if php is compiled without --enable-memory-limits // apparently memory_limit is set to '' - $cur=0; + $cur = 0; } else { if ($cur == -1){ return true; // unlimited mem! } - $cur = get_real_size($cur); + $cur = get_real_size($cur); } - $new = get_real_size($newlimit); - if ($new > $cur) { + if ($newlimit > $cur) { ini_set('memory_limit', $newlimit); return true; } @@ -825,7 +863,7 @@ function reduce_memory_limit($newlimit) { if (empty($cur)) { // if php is compiled without --enable-memory-limits // apparently memory_limit is set to '' - $cur=0; + $cur = 0; } else { if ($cur == -1){ return true; // unlimited mem! @@ -845,14 +883,17 @@ function reduce_memory_limit($newlimit) { /** * Converts numbers like 10M into bytes. * - * @param mixed $size The size to be converted - * @return mixed + * @param string $size The size to be converted + * @return int */ -function get_real_size($size=0) { +function get_real_size($size = 0) { if (!$size) { return 0; } $scan = array(); + $scan['GB'] = 1073741824; + $scan['Gb'] = 1073741824; + $scan['G'] = 1073741824; $scan['MB'] = 1048576; $scan['Mb'] = 1048576; $scan['M'] = 1048576; -- 2.43.0