]> git.mjollnir.org Git - moodle.git/commitdiff
"MEMORY/MDL-18540,remove hard-coded php memory limit, added system setting for it...
authordongsheng <dongsheng>
Thu, 26 Mar 2009 02:09:28 +0000 (02:09 +0000)
committerdongsheng <dongsheng>
Thu, 26 Mar 2009 02:09:28 +0000 (02:09 +0000)
admin/health.php
admin/settings/server.php
backup/lib.php
backup/restore.php
backup/try.php
lang/en_utf8/admin.php
lib/setuplib.php
search/cron_php5.php

index 86e23b639aaa0a0d789277d34c37126457bacc79..71401842e4cb5e3ac185af78688d2a8b8d82e4d8 100644 (file)
@@ -363,7 +363,11 @@ class problem_000008 extends problem_base {
         }
         $oldmemlimit = get_real_size($oldmemlimit);
         //now lets change the memory limit to something unique below 128M==134217728
-        @ini_set('memory_limit', 134217720);
+        if (empty($CFG->memorylimit)) {
+            raise_memory_limit('128M');
+        } else {
+            raise_memory_limit($CFG->memorylimit);
+        }
         $testmemlimit = get_real_size(@ini_get('memory_limit'));
         //verify the change had any effect at all
         if ($oldmemlimit == $testmemlimit) {
@@ -374,7 +378,7 @@ class problem_000008 extends problem_base {
                 return false;
             }
         }
-        @ini_set('memory_limit', $oldmemlimit);
+        reduce_memory_limit($oldmemlimit);
         return false;
     }
     function severity() {
index 34417daaf8db20588ac1e3ef9a0d3009accd79c0..0a2819543e478d08f491420ce7bca41c7e107f93 100644 (file)
@@ -233,6 +233,14 @@ $ADMIN->add('server', new admin_externalpage('phpinfo', get_string('phpinfo'), "
 
 // "performance" settingpage
 $temp = new admin_settingpage('performance', get_string('performance', 'admin'));
+
+$temp->add(new admin_setting_special_selectsetup('memorylimit', get_string('memorylimit', 'admin'),
+                                          get_string('configmemorylimit', 'admin'), '128M',
+                                          array( '64M' => '64M',
+                                                 '128M' => '128M',
+                                                 '256M' => '256M',
+                                                 '1024M' => '1024M'
+                                             )));
 $temp->add(new admin_setting_special_selectsetup('cachetype', get_string('cachetype', 'admin'),
                                           get_string('configcachetype', 'admin'), '',
                                           array( '' => get_string('none'),
index 964f69cb6b7b3f2758f6cc610a7407397a5d8e45..307ec55b3fd75f619b4b102af774d0cab87d19c5 100644 (file)
             mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
             return false;
         }
-
         @ini_set("max_execution_time","3000");
-        raise_memory_limit("192M");
+        if (empty($CFG->memorylimit)) {
+            raise_memory_limit('128M');
+        } else {
+            raise_memory_limit($CFG->memorylimit);
+        }
 
         if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
             mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
index af07a59054dfc16f9e5d966ba7f999836f27c7da..6e583ea9d9ccb8fe85e9160e9c6dded259bab36d 100644 (file)
 
     //Adjust some php variables to the execution of this script
     @ini_set("max_execution_time","3000");
-    raise_memory_limit("192M");
+    if (empty($CFG->memorylimit)) {
+        raise_memory_limit('128M');
+    } else {
+        raise_memory_limit($CFG->memorylimit);
+    }
 
     //Call the form, depending the step we are
 
index 038f7b3fe8481cea0f1d2cbcdfcc9e37c1d56354..83cf2ca557285b67a034ac217fa42a2b8ee7f8f9 100644 (file)
 
     //Adjust some php variables to the execution of this script
     @ini_set("max_execution_time","3000");
-    raise_memory_limit("192M");
+    if (empty($CFG->memorylimit)) {
+        raise_memory_limit('128M');
+    } else {
+        raise_memory_limit($CFG->memorylimit);
+    }
 
     echo "<pre>\n";
 
index 9120b2c03ef277f3cdfb6f6d7550311677fe089f..65e60cb55bde3a8f392b042c2055910fba32a868 100644 (file)
@@ -184,6 +184,7 @@ $string['configmaxeditingtime'] = 'This specifies the amount of time people have
 $string['configmaxevents'] = 'Events to Lookahead';
 $string['configmemcachedhosts'] = 'For memcached. Comma-separated list of hosts that are running the memcached daemon. Use IP addresses to avoid DNS latency. memcached does not behave well if you add/remove hosts on a running setup.';
 $string['configmemcachedpconn'] = 'For memcached. Use persistent connections. Use carefully -- it can make Apache/PHP crash after a restart of the memcached daemon.';
+$string['configmemorylimit'] = 'This sets the maximum amount of memory that a script is allowed to allocate. This option is applied to search indexing, backup/restore and admin/health scripts.';
 $string['configmessaging'] = 'Should the messaging system between site users be enabled?';
 $string['configminpassworddigits'] = 'Passwords must have at least these many digits.';
 $string['configminpasswordlength'] = 'Passwords must be at least these many characters long.';
@@ -550,6 +551,7 @@ $string['mediapluginswfnote'] = 'As a default security measure, normal users sho
 $string['mediapluginwmv'] = 'Enable .wmv filter';
 $string['memcachedhosts'] = 'memcached hosts';
 $string['memcachedpconn'] = 'memcached use persistent connections';
+$string['memorylimit'] = 'PHP memory limit';
 $string['messaging'] = 'Enable messaging system';
 $string['minpasswordlength'] = 'Password Length';
 $string['minpassworddigits'] = 'Digits';
index 67be6bca669788d07c27b90362e9ac83207da0e3..3aa0a2021456baf9fb0075a72769489622089645 100644 (file)
@@ -281,6 +281,42 @@ function raise_memory_limit($newlimit) {
     return false;
 }
 
+/**
+ * Function to reduce the memory limit to a new value.
+ * Will respect the memory limit if it is lower, thus allowing
+ * settings in php.ini, apache conf or command line switches
+ * to override it
+ *
+ * The memory limit should be expressed with a string (eg:'64M')
+ *
+ * @param string $newlimit the new memory limit
+ * @return bool
+ */
+function reduce_memory_limit ($newlimit) {
+    if (empty($newlimit)) {
+        return false;
+    }
+    $cur = @ini_get('memory_limit');
+    if (empty($cur)) {
+        // if php is compiled without --enable-memory-limits
+        // apparently memory_limit is set to ''
+        $cur=0;
+    } else {
+        if ($cur == -1){
+            return true; // unlimited mem!
+        }
+        $cur = get_real_size($cur);
+    }
+
+    $new = get_real_size($newlimit);
+    // -1 is smaller, but it means unlimited
+    if ($new < $cur && $new != -1) {
+        ini_set('memory_limit', $newlimit);
+        return true;
+    }
+    return false;
+}
+
 /**
  * Converts numbers like 10M into bytes.
  *
index a9cd57fcb5116dc4df2788301640e48e8f8cd656..b6542f55ecedf157fc7ae7114b21ecd8a8e78612 100644 (file)
 */
 
 try{
-    // overrides php limits
-    $maxtimelimit = ini_get('max_execution_time');
-    ini_set('max_execution_time', 600);
-    $maxmemoryamount = ini_get('memory_limit');
-    ini_set('memory_limit', '96M');
+    ini_set('max_execution_time', 300);
+    if (empty($CFG->memorylimit)) {
+        raise_memory_limit('128M');
+    } else {
+        raise_memory_limit($CFG->memorylimit);
+    }
 
     mtrace("\n--DELETE----");
     require_once($CFG->dirroot.'/search/delete.php');
@@ -28,13 +29,9 @@ try{
     mtrace("------------");
     //mtrace("cron finished.</pre>");
     mtrace('done');
-
-    // set back normal values for php limits
-    ini_set('max_execution_time', $maxtimelimit);
-    ini_set('memory_limit', $maxmemoryamount);
 }
 catch(Exception $ex){
     mtrace('Fatal exception from Lucene subsystem. Search engine may not have been updated.');
     mtrace($ex);
 }
-?>
\ No newline at end of file
+?>