Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Perl internally uses dedicated hash PL_strtab as shared storage for hash's keys, but in fork environment like apache/mod_perl this creates a big issue. Best practice says to preload modules in parent process, but nobody says it's eventually allocates memory for PL_strtab and these pages of memory tend to be implicitly modified in child processes. There are seems to be 2 major reasons of modification:

Reason 1: reallocation (hsplit()) may happen when PL_strtab growths in child process.

Reason 2: REFCNT every time new reference created.

Example below shows 16MB copy-on-write leak in attempt to use hash. Attempts to recompile perl with -DNODEFAULT_SHAREKEYS fails (https://rt.perl.org/SelfService/Display.html?id=133384). I was able to get access to PL_strtab via XS module and ideally I'm looking for a way to downgrade all hashes created in parent to keep hash keys within a hash (HE object) rather than PL_strtab, i.e. turn off SHAREKEYS flag. This should allow to shrink PL_strtab to minimum possible size. Ideally it should have 0 keys in parent. Please let me know you think it's theoretically possible via XS.

#!/usr/bin/env perl use strict; use warnings; use Linux::Smaps; $SIG{CHLD} = sub { waitpid(-1, 1) }; # comment this block { my %h; # pre-growth PL_strtab hash, kind of: keys %$PL_strtab = 2_000_000 +; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $pid = fork // die "Cannot fork: $!"; unless ($pid) { # child my $s = Linux::Smaps->new($$)->all; my $before = $s->shared_clean + $s->shared_dirty; { my %h; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $s2 = Linux::Smaps->new($$)->all; my $after = $s2->shared_clean + $s2->shared_dirty; warn 'COPY-ON-WRITE: ' . ($before - $after) . ' KB'; exit 0; } sleep 1000; print "DONE\n";

In reply to PL_strtab/SHAREKEYS and copy-on-write leak by sezal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-19 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found