sezal has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: PL_strtab/SHAREKEYS and copy-on-write leak
by dave_the_m (Monsignor) on Jul 20, 2018 at 21:59 UTC | |
by eserte (Deacon) on Aug 16, 2018 at 13:44 UTC | |
by dave_the_m (Monsignor) on Aug 16, 2018 at 18:22 UTC | |
by sezal (Novice) on Jul 30, 2018 at 08:19 UTC | |
Re: PL_strtab/SHAREKEYS and copy-on-write leak
by haukex (Archbishop) on Jul 20, 2018 at 22:38 UTC | |
by sezal (Novice) on Jul 30, 2018 at 07:22 UTC | |
Re: PL_strtab/SHAREKEYS and copy-on-write leak
by eserte (Deacon) on Aug 16, 2018 at 13:33 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. | |
A reply falls below the community's threshold of quality. You may see it by logging in. |