Moodle Slow Page Loads - PHP running preg_match_all() for 30+ seconds

Moodle Slow Page Loads - PHP running preg_match_all() for 30+ seconds

by Andy Sayler -
Number of replies: 2

We're running a Moodle 2.9 install on a 16 core, 2.6GHZ, 48GB server with a separate database server backend. We've been seeing an issue recently where the page loads for things like the Assignment "Grade Submission" pages take 40+ seconds for courses with ~250 students.

It looks like most of this time is being spent in PHP. For example, loading the following url leads to teh stats below.

https://<url>/mod/assign/view.php?action=grading&id=<id>&tilast

43.990729 secs
RAM: 31.6MB
RAM peak: 35.9MB
Included 310 files
get_string calls: 2005
DB reads/writes: 354/1
DB queries time: 0.29162 secs
ticks: 4399 user: 4176 sys: 96 cuser: 0 csys: 0
Load average: 0.72
Session (core\session\database): 15.7KB

To get more insight, we enabled xhprof. That leads to the following stats for the same page show above (sorted by Excl. Wall Time, truncated to top-10):

Function NameCallsCalls%Incl. Wall Time
(microsec)
IWall%Excl. Wall Time
(microsec)
EWall%Incl. CPU
(microsecs)
ICpu%Excl. CPU
(microsec)
ECPU%Incl.
MemUse
(bytes)
IMemUse%Excl.
MemUse
(bytes)
EMemUse%Incl.
PeakMemUse
(bytes)
IPeakMemUse%Excl.
PeakMemUse
(bytes)
EPeakMemUse%
preg_match_all130,2789.8%37,800,50585.8%37,800,50585.8%37,176,00087.1%37,176,00087.1%13,529,48845.3%13,529,48845.3%162,3920.5%162,3920.5%
preg_replace245,67918.5%1,723,9473.9%1,723,9473.9%1,792,0004.2%1,792,0004.2%1,086,893,3443636.4%1,086,893,3443636.4%366,3201.1%366,3201.1%
filter_phrases4140.0%40,951,65793.0%1,473,1543.3%40,136,00094.0%1,044,0002.4%8,774,00029.4%-1,022,890,400-3422.2%703,5922.1%3,0720.0%
filter_urltolink::convert_urls_into_links4140.0%1,047,4202.4%646,3681.5%992,0002.3%312,0000.7%13,0080.0%-103,655,240-346.8%719,0642.1%202,7920.6%
mysqli::query3500.0%340,7130.8%340,7130.8%28,0000.1%28,0000.1%61,4880.2%61,4880.2%1,2480.0%1,2480.0%
preg_match24,0951.8%211,4610.5%211,4610.5%232,0000.5%232,0000.5%37,2480.1%37,2480.1%10,3840.0%10,3840.0%
Memcached::get5080.0%192,1680.4%192,1680.4%64,0000.1%64,0000.1%5,552,48018.6%5,552,48018.6%4,974,88814.8%4,974,88814.8%
intval125,8569.5%48,5590.1%48,5590.1%156,0000.4%156,0000.4%8400.0%8400.0%00.0%00.0%
cache::get3,8360.3%447,2051.0%41,4770.1%328,0000.8%36,0000.1%14,430,96048.3%-2,111,032-7.1%6,917,05620.5%1,0080.0%

When we watch the CPU usage, you can see the page load pegging a single core for the duration.

So it appears we're being limited by the PHP preg_match_all() performance on a single core. The other cores are pretty much unused during this time. Any idea what's going on here or how we might be able to fix it?

Average of ratings: Useful (2)
In reply to Andy Sayler

Re: Moodle Slow Page Loads - PHP running preg_match_all() for 30+ seconds

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can draw the xhprof call graph, to see which path of function calls leads to all those preg_matches.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Moodle Slow Page Loads - PHP running preg_match_all() for 30+ seconds

by Andy Sayler -

Yep. That's what we did. I'd attach it but it's a 500kB png the forum seems limited to 100 kB.

It looks like most of the time is coming via:

filer_activity_names -> filter_phrases

So we turned off activity name autolinking as described at https://docs.moodle.org/29/en/Activity_names_auto-linking_filter. That brings the page load time back under 5 seconds or so.

So now the question is - why does enabling activity name autolinking cause such a huge performance hit - especially on pages where it isn't even really necessary (e.g. while viewing/modifying submission grades)? Is there any way to fix that? We have a number of instructors who use that feature for assignment descriptions and other user-generated content, so I'd prefer to to have to keep it disabled permanently.

Average of ratings: Useful (3)