File: Apache.pm

package info (click to toggle)
libdevel-nytprof-perl 4.06-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,152 kB
  • sloc: perl: 4,451; ansic: 89; makefile: 19
file content (196 lines) | stat: -rw-r--r-- 6,310 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# vim: ts=8 sw=4 expandtab:
##########################################################
# This script is part of the Devel::NYTProf distribution
#
# Copyright, contact and other information can be found
# at the bottom of this file, or by going to:
# http://search.cpan.org/dist/Devel-NYTProf/
#
###########################################################
# $Id: Apache.pm 1253 2010-05-30 08:30:17Z tim.bunce@gmail.com $
###########################################################
package Devel::NYTProf::Apache;

our $VERSION = '4.00';

BEGIN {

    # Load Devel::NYTProf before loading any other modules
    # in order that $^P settings apply to the compilation
    # of those modules.

    if (!$ENV{NYTPROF}) {
        $ENV{NYTPROF} = "file=/tmp/nytprof.$$.out";
        warn "NYTPROF env var not set, so defaulting to NYTPROF='$ENV{NYTPROF}'";
    }

    require Devel::NYTProf::Core;

    DB::set_option("endatexit", 1); # for vhost with PerlOption +Parent
    DB::set_option("addpid", 1);

    require Devel::NYTProf;
}

use strict;

use constant TRACE => ($ENV{NYTPROF} =~ /\b trace = [^0] /x);
use constant MP2   => (exists $ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2);

# https://rt.cpan.org/Ticket/Display.html?id=42862
die "Threads not supported" if $^O eq 'MSWin32';

# help identify MULTIPLICITY issues
*current_perl_id = (MP2 and eval "require ModPerl::Util")
        ? \&ModPerl::Util::current_perl_id
        : sub { 0+\$$ };

sub trace {
    return unless TRACE;
    warn sprintf "NYTProf %d.%s: %s\n",
        $$, current_perl_id(), shift
}

sub child_init {
    trace("child_init(@_)") if TRACE;
    DB::enable_profile() unless $ENV{NYTPROF} =~ m/\b start = (?: no | end ) \b/x;
}

sub child_exit {
    trace("child_exit(@_)") if TRACE;
    DB::finish_profile();
}

# arrange for the profile to be enabled in each child
# and cleanly finished when the child exits
if (MP2) {

    # For mod_perl2 we rely on profiling being active in the parent
    # and for normal fork detection to detect the new child.
    # We just need to be sure the profile is finished properly
    # and an END block works well for that (if loaded right, see docs)
    # We rely on NYTProf's own END block to finish the profile.
    #trace("adding child_exit hook") if TRACE;
    #eval q{ END { child_exit('END') } 1 } or die;
}
else {
    # the simple steps for mod_perl2 above might also be fine for mod_perl1
    # but I'm not in a position to check right now. Try it out and let me know.
    require Apache;
    if (Apache->can('push_handlers')) {
        Apache->push_handlers(PerlChildInitHandler => \&child_init);
        Apache->push_handlers(PerlChildExitHandler => \&child_exit);
        warn "$$: Apache child handlers installed" if TRACE;
    }
    else {
        Carp::carp("Apache.pm was not loaded");
    }
}

1;

__END__

=head1 NAME

Devel::NYTProf::Apache - Profile mod_perl applications with Devel::NYTProf

=head1 SYNOPSIS

  # in your Apache config file with mod_perl installed
  PerlPassEnv NYTPROF
  PerlModule Devel::NYTProf::Apache

If you're using virtual hosts with C<PerlOptions> that include either
C<+Parent> or C<+Clone> then see L</VIRTUAL HOSTS> below.

=head1 DESCRIPTION

This module allows mod_perl applications to be profiled using
C<Devel::NYTProf>. 

If the NYTPROF environment variable isn't set I<at the time
Devel::NYTProf::Apache is loaded> then Devel::NYTProf::Apache will issue a
warning and default it to:

  file=/tmp/nytprof.$$.out

The file actually created by NTProf will also have the process id appended to
it because the C<addpid> option is enabled by default.

See L<Devel::NYTProf/"ENVIRONMENT VARIABLES"> for 
more details on the settings effected by this environment variable.
Try using C<PerlPassEnv> so you can set the NYTPROF environment variable externally.

Each profiled mod_perl process will need to have terminated before you can
successfully read the profile data file. The simplest approach is to start the
httpd, make some requests (e.g., 100 of the same request), then stop it and
process the profile data.

Alternatively you could send a TERM signal to the httpd worker process to
terminate that one process. The parent httpd process will start up another one
for you ready for more profiling.

=head2 Example httpd.conf

It's often a good idea to use just one child process when profiling, which you
can do by setting the C<MaxClients> to 1 in httpd.conf.

Using an C<IfDefine> blocks lets you leave the profile configuration in place
and enable it whenever it's needed by adding C<-D NYTPROF> to the httpd startup
command line.

  <IfDefine NYTPROF>
      MaxClients 1
      PerlModule Devel::NYTProf::Apache
  </IfDefine>


=head1 VIRTUAL HOSTS

If your httpd configuration includes virtual hosts with C<PerlOptions> that
include either C<+Parent> or C<+Clone> then mod_perl2 will create a new perl
interpreter to handle requests for that virtual host.
This causes some issues for profiling.

If C<Devel::NYTProf::Apache> is loaded in the top-level configuration then
activity in any virtual hosts that use their own perl interpreter won't be
profiled. Normal virtual hosts will be profiled just fine.

You can profile a I<single> virtual host that uses its own perl interpreter by
loading C<Devel::NYTProf::Apache> I<inside the configuration for that virtual
host>. In this case I<do not> use C<PerlModule> directive. You need to use
a C<Perl> directive instead, like this:

    <VirtualHost *:1234>
        ...
        <Perl> use Devel::NYTProf::Apache; </Perl>
        ...
    </VirtualHost>

=head1 LIMITATIONS

Profiling mod_perl on Windows is not supported because NYTProf currently
doesn't support threads.

=head1 SEE ALSO

L<Devel::NYTProf>

=head1 AUTHOR

B<Adam Kaplan>, C<< <akaplan at nytimes.com> >>
B<Tim Bunce>, L<http://www.tim.bunce.name> and L<http://blog.timbunce.org>
B<Steve Peters>, C<< <steve at fisharerojo.org> >>

=head1 COPYRIGHT AND LICENSE

  Copyright (C) 2008 by Adam Kaplan and The New York Times Company.
  Copyright (C) 2008 by Steve Peters.
  Copyright (C) 2008 by Tim Bunce.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut