Closed Bug 2091167 Opened 15 days ago Closed 12 days ago

SizeLimit plugin parses all of /proc/self/smaps in pure Perl after every request (~3 ms per request)

Categories

(bugzilla.mozilla.org :: General, enhancement)

Production
enhancement

Tracking

()

RESOLVED FIXED

People

(Reporter: dkl, Assigned: dkl)

Details

Bugzilla::App::Plugin::SizeLimit stops a worker once its memory grows past size_limit. To check, it calls Linux::Smaps::Tiny::get_smaps_summary() in an after_dispatch hook, which runs after every request, including REST calls and static files.

The module is supposed to use its compiled (XS) parser and fall back to pure Perl only if that's unavailable. But version 0.10, the current CPAN release, always uses the pure-Perl parser, even though the compiled library is installed and loads fine:

BEGIN {
    local ($@, $!);
    eval {
        require XSLoader;
        XSLoader::load(__PACKAGE__, $Linux::Smaps::Tiny::VERSION || '0.01');
    };
}
...
{
  $Linux::Smaps::Tiny::VERSION = '0.10';   # plain block, runs after BEGIN
}

The BEGIN block runs before $VERSION is set, so the loader passes '0.01', and the version check fails:

Linux::Smaps::Tiny object version 0.10 does not match bootstrap parameter 0.01

local ($@, $!) throws that error away, and the module quietly uses Linux::Smaps::Tiny::PP. Every request then reads and parses the whole smaps file line by line. That's about 8,800 lines in a process with the BMO app loaded, and about 19,000 in a dev hypnotoad worker.

Measured

In a process with the full BMO app loaded (Bugzilla::App plus all extensions), averaged over 200 calls:

┌──────────────────────────────────────────────────┬───────────────┬───────────────┐
│ Method │ Size reported │ Time per call │
├──────────────────────────────────────────────────┼───────────────┼───────────────┤
│ get_smaps_summary()->{Size} (pure-Perl fallback) │ 161,828 kB │ 3.045 ms │
├──────────────────────────────────────────────────┼───────────────┼───────────────┤
│ VmSize from /proc/self/status │ 161,824 kB │ 0.009 ms │
└──────────────────────────────────────────────────┴───────────────┴───────────────┘

That adds about 3 ms to every request handled by every worker. For a small REST call like /rest/bug/1, it was a noticeable share of server time in profiling.

Steps to reproduce

  1. In the BMO container, run perl -I/app/local/lib/perl5 -MLinux::Smaps::Tiny -e 'print $INC{"Linux/Smaps/Tiny/PP.pm"} ? "PP\n" : "XS\n"'. It prints PP.
  2. Or profile any request with USE_NYTPROF=1. Linux::Smaps::Tiny::PP::__get_smaps_summary shows up near the top by exclusive time.

Expected

The memory check after each request should cost a negligible amount of time.

Proposed fix

Read VmSize from /proc/self/status instead of summing every mapping in /proc/self/smaps. It's the same total (the sum of each mapping's Size, in kB), and the kernel already keeps it calculated. That file is a few dozen lines, so the check takes about 0.009 ms.

If /proc/self/status can't be read (non-Linux), the check is skipped, just as it is today when Linux::Smaps::Tiny isn't installed. The size_limit and MIN_SIZE_LIMIT behaviour, the Connection: close header and stop_gracefully are all unchanged.

A new test, t/app-size-limit.t, covers reading the real value and the under-limit, at-limit and size-unavailable cases.

Follow-up (not in this patch)

After this change, nothing in BMO uses Linux::Smaps::Tiny. It can be removed from cpanfile, Makefile.PL (the linux_smaps feat a separate change. The CPAN loading bug could also be reported upstream.

Authored by https://github.com/dklawren
https://github.com/mozilla/bmo/commit/28a8b3a7f61d1afb6777e6cacdc08f17b6a86722
[master] Bug 2091167 - SizeLimit plugin parses all of /proc/self/smaps in pure Perl after every request (~3 ms per request)

(via GitHub webhook, authenticated as shared-secret)

Status: ASSIGNED → RESOLVED
Closed: 12 days ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.