| 1 | # vim: ts=8 sw=4 expandtab: | ||
| 2 | ########################################################## | ||
| 3 | # This script is part of the Devel::NYTProf distribution | ||
| 4 | # | ||
| 5 | # Copyright, contact and other information can be found | ||
| 6 | # at the bottom of this file, or by going to: | ||
| 7 | # http://metacpan.org/release/Devel-NYTProf/ | ||
| 8 | # | ||
| 9 | ########################################################### | ||
| 10 | package Devel::NYTProf::Apache; | ||
| 11 | |||
| 12 | our $VERSION = '6.16'; | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | |||
| 16 | # Load Devel::NYTProf before loading any other modules | ||
| 17 | # in order that $^P settings apply to the compilation | ||
| 18 | # of those modules. | ||
| 19 | |||
| 20 | if (!$ENV{NYTPROF}) { | ||
| 21 | $ENV{NYTPROF} = "file=/tmp/nytprof.$$.out"; | ||
| 22 | warn "NYTPROF env var not set, so defaulting to NYTPROF='$ENV{NYTPROF}'"; | ||
| 23 | } | ||
| 24 | |||
| 25 | require Devel::NYTProf::Core; | ||
| 26 | |||
| 27 | DB::set_option("endatexit", 1); # for vhost with PerlOption +Parent | ||
| 28 | DB::set_option("addpid", 1); | ||
| 29 | |||
| 30 | require Devel::NYTProf; | ||
| 31 | } | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | |||
| 35 | use constant TRACE => ($ENV{NYTPROF} =~ /\b trace = [^0] /x); | ||
| 36 | use constant MP2 => (exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2); | ||
| 37 | |||
| 38 | # https://rt.cpan.org/Ticket/Display.html?id=42862 | ||
| 39 | die "Threads not supported" if $^O eq 'MSWin32'; | ||
| 40 | |||
| 41 | # help identify MULTIPLICITY issues | ||
| 42 | *current_perl_id = (MP2 and eval "require ModPerl::Util") | ||
| 43 | ? \&ModPerl::Util::current_perl_id | ||
| 44 | : sub { 0+\$$ }; | ||
| 45 | |||
| 46 | sub trace { | ||
| 47 | return unless TRACE; | ||
| 48 | warn sprintf "NYTProf %d.%s: %s\n", | ||
| 49 | $$, current_perl_id(), shift | ||
| 50 | } | ||
| 51 | |||
| 52 | sub child_init { | ||
| 53 | trace("child_init(@_)") if TRACE; | ||
| 54 | warn "Apache2::SizeLimit is loaded and will corrupt NYTProf profile if it terminates the process\n" | ||
| 55 | if $Apache2::SizeLimit::VERSION # doubled just to avoid typo warning | ||
| 56 | && $Apache2::SizeLimit::VERSION; | ||
| 57 | DB::enable_profile() unless $ENV{NYTPROF} =~ m/\b start = (?: no | end ) \b/x; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub child_exit { | ||
| 61 | trace("child_exit(@_)") if TRACE; | ||
| 62 | DB::finish_profile(); | ||
| 63 | } | ||
| 64 | |||
| 65 | # arrange for the profile to be enabled in each child | ||
| 66 | # and cleanly finished when the child exits | ||
| 67 | if (MP2) { | ||
| 68 | |||
| 69 | # For mod_perl2 we rely on profiling being active in the parent | ||
| 70 | # and for normal fork detection to detect the new child. | ||
| 71 | # We just need to be sure the profile is finished properly | ||
| 72 | # and an END block works well for that (if loaded right, see docs) | ||
| 73 | # We rely on NYTProf's own END block to finish the profile. | ||
| 74 | #trace("adding child_exit hook") if TRACE; | ||
| 75 | #eval q{ END { child_exit('END') } 1 } or die; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | # the simple steps for mod_perl2 above might also be fine for mod_perl1 | ||
| 79 | # but I'm not in a position to check right now. Try it out and let me know. | ||
| 80 | require Apache; | ||
| 81 | if (Apache->can('push_handlers')) { | ||
| 82 | Apache->push_handlers(PerlChildInitHandler => \&child_init); | ||
| 83 | Apache->push_handlers(PerlChildExitHandler => \&child_exit); | ||
| 84 | warn "$$: Apache child handlers installed" if TRACE; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | Carp::carp("Apache.pm was not loaded"); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; | ||
| 92 | |||
| 93 | __END__ |