Profiler.pm untested

filter: Statementstmt 0.0% 0 / 216 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 318 CC 28 SCAR 30.7
CC
28
Cov
0%
BEGIN
56.0
_PROFILE_ALARMSET
30.0
_PROFILE_DELAYSET
30.0
CRAP 21.6
1 # Declare our package
2 package POE::Devel::Profiler;
3  
4 # Standard stuff to catch errors
5 use strict qw(subs vars refs); # Make sure we can't mess up
6 use warnings FATAL => 'all'; # Enable warnings to catch errors
7  
8 # Initialize our version
9 our $VERSION = '0.02';
10  
11 # Okay, initialize POE so we can profile it
12 use POE;
13  
14 # We need Time::HiRes to profile accurately
15 use Time::HiRes qw( time );
16  
17 # Here comes the nasty hackery
18 sub BEGIN {
19 # We need to replace several key subroutines
20  
21 # Turn off strictness for our nasty stuff
22 no strict 'refs';
23  
24 # Oh boy, no warnings too :(
25 no warnings 'redefine';
26  
27 # Replace Kernel::_data_alias_add()
28 {
29 # Get the old reference
30 my $old_ref = \&POE::Kernel::_data_alias_add;
31  
32 # Construct the closure for the wrapper
33 my $new_ref = sub {
34 # Okay, call our profiling sub
35 _PROFILE_ALIASSET( @_ );
36  
37 # When that is done, we call the original sub
38 goto &$old_ref;
39 };
40  
41 # Symbol table hackery
42 *{'POE::Kernel::_data_alias_add'} = $new_ref;
43 }
44  
45 # Replace Kernel::yield()
46 {
47 # Get the old reference
48 my $old_ref = \&POE::Kernel::yield;
49  
50 # Construct the closure for the wrapper
51 my $new_ref = sub {
52 # Okay, call our profiling sub
53 _PROFILE_YIELD( @_ );
54  
55 # When that is done, we call the original sub
56 goto &$old_ref;
57 };
58  
59 # Symbol table hackery
60 *{'POE::Kernel::yield'} = $new_ref;
61 }
62  
63 # Replace Kernel::post()
64 {
65 # Get the old reference
66 my $old_ref = \&POE::Kernel::post;
67  
68 # Construct the closure for the wrapper
69 my $new_ref = sub {
70 # Okay, call our profiling sub
71 _PROFILE_POST( @_ );
72  
73 # When that is done, we call the original sub
74 goto &$old_ref;
75 };
76  
77 # Symbol table hackery
78 *{'POE::Kernel::post'} = $new_ref;
79 }
80  
81 # Replace Kernel::call()
82 {
83 # Get the old reference
84 my $old_ref = \&POE::Kernel::call;
85  
86 # Construct the closure for the wrapper
87 my $new_ref = sub {
88 # Okay, call our profiling sub
89 _PROFILE_CALL( @_ );
90  
91 # When that is done, we call the original sub
92 goto &$old_ref;
93 };
94  
95 # Symbol table hackery
96 *{'POE::Kernel::call'} = $new_ref;
97 }
98  
99 # Replace Session::_invoke_state()
100 {
101 # Get the old reference
102 my $old_ref = \&POE::Session::_invoke_state;
103  
104 # Construct the closure for the wrapper
105 my $new_ref = sub {
106 # Okay, call our profiling sub
107 my $ret = _PROFILE_SESSINVOKE( @_ );
108  
109 # When that is done, we call the original sub
110 my $return = undef;
111 if ( wantarray ) {
112 $return = [ $old_ref->( @_ ) ];
113 } else {
114 $return = $old_ref->( @_ );
115 }
116  
117 # Print the LEAVESTATE only if we didn't get FAILSTATE
118 if ( $ret ) {
119 # Print it out!
120 # LEAVESTATE current_session_id statename time
121 print OUT "LEAVESTATE \"" . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' .
122 $_[2] . '" "' . time() . "\"\n";
123 }
124  
125 # Okay, return the data
126 if ( wantarray ) {
127 return @{ $return };
128 } else {
129 return $return;
130 }
131 };
132  
133 # Symbol table hackery
134 *{'POE::Session::_invoke_state'} = $new_ref;
135 }
136  
137 # Replace Resources::Sessions::_data_ses_allocate()
138 # NOTE: It masquerades itself into POE::Kernel's namespace
139 {
140 # Get the old reference
141 my $old_ref = \&POE::Kernel::_data_ses_allocate;
142  
143 # Construct the closure for the wrapper
144 my $new_ref = sub {
145 # Okay, call our profiling sub
146 _PROFILE_NEWSESS( @_ );
147  
148 # When that is done, we call the original sub
149 my $return = undef;
150 if ( wantarray ) {
151 $return = [ $old_ref->( @_ ) ];
152 } else {
153 $return = $old_ref->( @_ );
154 }
155  
156 # Okay, return the data
157 if ( wantarray ) {
158 return @{ $return };
159 } else {
160 return $return;
161 }
162 };
163  
164 # Symbol table hackery
165 *{'POE::Kernel::_data_ses_allocate'} = $new_ref;
166 }
167
168 # Replace Kernel::_data_ses_free()
169 {
170 # Get the old reference
171 my $old_ref = \&POE::Kernel::_data_ses_free;
172  
173 # Construct the closure for the wrapper
174 my $new_ref = sub {
175 # Okay, call our profiling sub
176 _PROFILE_SESSDIE( @_ );
177  
178 # When that is done, we call the original sub
179 goto &$old_ref;
180 };
181  
182 # Symbol table hackery
183 *{'POE::Kernel::_data_ses_free'} = $new_ref;
184 }
185
186 # Replace Kernel::alarm_set()
187 {
188 # Get the old reference
189 my $old_ref = \&POE::Kernel::alarm_set;
190  
191 # Construct the closure for the wrapper
192 my $new_ref = sub {
193 # Okay, call our profiling sub
194 _PROFILE_ALARMSET( @_ );
195  
196 # When that is done, we call the original sub
197 goto &$old_ref;
198 };
199  
200 # Symbol table hackery
201 *{'POE::Kernel::alarm_set'} = $new_ref;
202 }
203
204 # Replace Kernel::delay_set()
205 {
206 # Get the old reference
207 my $old_ref = \&POE::Kernel::delay_set;
208  
209 # Construct the closure for the wrapper
210 my $new_ref = sub {
211 # Okay, call our profiling sub
212 _PROFILE_DELAYSET( @_ );
213  
214 # When that is done, we call the original sub
215 goto &$old_ref;
216 };
217  
218 # Symbol table hackery
219 *{'POE::Kernel::delay_set'} = $new_ref;
220 }
221
222 # Replace Kernel::signal()
223 {
224 # Get the old reference
225 my $old_ref = \&POE::Kernel::signal;
226  
227 # Construct the closure for the wrapper
228 my $new_ref = sub {
229 # Okay, call our profiling sub
230 _PROFILE_SIGNAL( @_ );
231  
232 # When that is done, we call the original sub
233 goto &$old_ref;
234 };
235  
236 # Symbol table hackery
237 *{'POE::Kernel::signal'} = $new_ref;
238 }
239  
240 # Replace Kernel::_data_ses_collect_garbage()
241 {
242 # Get the old reference
243 my $old_ref = \&POE::Kernel::_data_ses_collect_garbage;
244  
245 # Construct the closure for the wrapper
246 my $new_ref = sub {
247 # Okay, call our profiling sub
248 _PROFILE_GC( @_ );
249  
250 # When that is done, we call the original sub
251 goto &$old_ref;
252 };
253  
254 # Symbol table hackery
255 *{'POE::Kernel::_data_ses_collect_garbage'} = $new_ref;
256 }
257  
258 # Check for the filename sub
259 if ( ! defined &FILENAME ) {
260 eval "sub FILENAME () { 'poep.out' }";
261 }
262 }
263  
264 # Open the filename, creating it if necessary
265 open( OUT, '> ' . FILENAME ) or die "Unable to open the output file: $!";
266  
267 # Print the first line!
268 # STARTPROGRAM name time
269 print OUT 'STARTPROGRAM "' . $0 . '" "' . time() . "\"\n";
270  
271 # Now, we wait for the stuff to come rolling in
272  
273 # Capture alias_set for logging
274 sub _PROFILE_ALIASSET {
275 # Get the alias name
276 my ( $sess, $alias ) = @_[ 1, 2 ];
277  
278 # Get the caller's filename and etc
279 # Bypass Kernel::alias_set's call frame
280 my( $file, $line ) = ( caller( 2 ) )[1,2];
281  
282 # Print it out!
283 # SESSIONALIAS session_id alias file line time
284 print OUT 'SESSIONALIAS "' . $sess->ID . '" "' . $alias . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
285  
286 # All done!
287 return;
288 }
289  
290 # Capture yields
291 sub _PROFILE_YIELD {
292 # Get the event name
293 my $event = $_[1];
294  
295 # Sanity checking
296 if ( ! defined $event ) {
297 return;
298 }
299  
300 # Get the caller's filename and etc
301 my( $file, $line ) = ( caller( 1 ) )[1,2];
302  
303 # Okay, print out what we need
304 # YIELD current_session_id statename yield_event file line time
305 print OUT 'YIELD "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . ${ $POE::Kernel::poe_kernel->[ POE::Kernel::KR_ACTIVE_EVENT ] } . '" "' .
306 $event . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
307  
308 # All done!
309 return;
310 }
311  
312 # Capture posts
313 sub _PROFILE_POST {
314 # Get the session + event name
315 my( $sess, $event ) = @_[ 1, 2];
316  
317 # Sanity checking
318 if ( ! defined $sess or ! defined $event ) {
319 return;
320 }
321  
322 # Get the destination session's ID
323 $sess = $_[0]->_resolve_session( $sess );
324  
325 # Check if it is valid
326 if ( ! defined $sess ) {
327 return;
328 } else {
329 # Get the ID
330 $sess = $sess->ID;
331 }
332  
333 # Get the caller's filename and etc
334 my( $file, $line ) = ( caller( 1 ) )[1,2];
335  
336 # Okay, print out what we need
337 # POST current_session_id statename post_session post_event file line time
338 print OUT 'POST "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . ${ $POE::Kernel::poe_kernel->[ POE::Kernel::KR_ACTIVE_EVENT ] } . '" "' .
339 $sess . '" "' . $event . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
340  
341 # All done!
342 return;
343 }
344  
345 # Capture calls
346 sub _PROFILE_CALL {
347 # Get the session + event name
348 my( $sess, $event ) = @_[ 1, 2];
349  
350 # Sanity checking
351 if ( ! defined $sess or ! defined $event ) {
352 return;
353 }
354  
355 # Get the destination session's ID
356 $sess = $_[0]->_resolve_session( $sess );
357  
358 # Check if it is valid
359 if ( ! defined $sess ) {
360 return;
361 } else {
362 # Get the ID
363 $sess = $sess->ID;
364 }
365  
366 # Get the caller's filename and etc
367 my( $file, $line ) = ( caller( 1 ) )[1,2];
368  
369 # Okay, print out what we need
370 # CALL current_session_id statename call_session call_event file line time
371 print OUT 'CALL "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . ${ $POE::Kernel::poe_kernel->[ POE::Kernel::KR_ACTIVE_EVENT ] } . '" "' .
372 $sess . '" "' . $event . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
373  
374 # All done!
375 return;
376 }
377  
378 # Capture enter state
379 # Profiles POE::Session only
380 sub _PROFILE_SESSINVOKE {
381 # my ($self, $source_session, $state, $etc, $file, $line) = @_;
382  
383 # Set up the type
384 my $type = 'ENTERSTATE';
385  
386 # Return success ( for now )
387 my $return = 1;
388  
389 # Check if this state is valid
390 if ( ! exists $_[0]->[ POE::Session::SE_STATES ]->{ $_[2] } ) {
391 # Okay, check _default
392 if ( exists $_[0]->[ POE::Session::SE_STATES ]->{ POE::Session::EN_DEFAULT } ) {
393 # Whew, log it as _default
394 $type = 'DEFAULTSTATE';
395 } else {
396 # We failed to invoke a state!
397 $type = 'FAILSTATE';
398 $return = undef;
399 }
400 }
401  
402 # Print it out!
403 # ENTERSTATE current_session_id statename caller_session_id caller_file_name caller_file_line time
404 # FAILSTATE current_session_id statename caller_session_id caller_file_name caller_file_line time
405 # DEFAULTSTATE current_session_id statename caller_session_id caller_file_name caller_file_line time
406 print OUT "$type \"" . $_[0]->ID . '" "' . $_[2] . '" "' . $_[1]->ID . '" "' . $_[4] . '" "' . $_[5] . '" "' . time() . "\"\n";
407  
408 # All done!
409 return $return;
410 }
411  
412 # A new session is created, note this!
413 sub _PROFILE_NEWSESS {
414 my( $sid, $parent ) = @_[ 2, 3 ];
415  
416 # Sanity checking
417 if ( ! defined $parent ) {
418 return;
419 }
420  
421 # Okay, check if it would have failed
422 if ( POE::Kernel::ASSERT_DATA ) {
423 if ( ! exists $POE::Kernel::poe_kernel->[ $POE::Kernel::KR_SESSIONS ]->{ $parent } ) {
424 return;
425 }
426 }
427
428 # Get the caller's filename and etc
429 # Bypass Kernel::session_alloc
430 # Bypass Session::new/create
431 my( $file, $line ) = ( caller( 4 ) )[1,2];
432  
433 # Print it out!
434 # SESSIONNEW session_id parent_id file line time
435 print OUT 'SESSIONNEW "' . $sid . '" "' . $parent->ID . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
436 }
437  
438 # A session is now dying...
439 sub _PROFILE_SESSDIE {
440 # Get the session
441 my $sess = $_[1];
442
443 # Print it out!
444 # SESSIONDIE session_id time
445 print OUT 'SESSIONDIE "' . $sess->ID . '" "' . time() . "\"\n";
446 }
447  
448 # Somebody set us up the alarm!
449 sub _PROFILE_ALARMSET {
450 my( $event, $time ) = @_[ 1, 2 ];
451  
452 # Sanity checks
453 if ( ! defined $event ) { return }
454 if ( ! defined $time ) { return }
455 if ( POE::Kernel::ASSERT_USAGE ) {
456 if ( exists $POE::Kernel::poes_own_events{ $event } ) { return }
457 }
458  
459 # Get the caller's filename and etc
460 my( $file, $line ) = ( caller( 1 ) )[1,2];
461  
462 # Print it out!
463 # ALARMSET session_id event_name time_alarm file line time
464 print OUT 'ALARMSET "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . $event . '" "' .
465 $time . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
466 }
467  
468 # Somebody set us up the delay!
469 sub _PROFILE_DELAYSET {
470 my( $event, $time ) = @_[ 1, 2 ];
471  
472 # Sanity checks
473 if ( ! defined $event ) { return }
474 if ( ! defined $time ) { return }
475 if ( POE::Kernel::ASSERT_USAGE ) {
476 if ( exists $POE::Kernel::poes_own_events{ $event } ) { return }
477 }
478  
479 # Get the caller's filename and etc
480 my( $file, $line ) = ( caller( 1 ) )[1,2];
481  
482 # Print it out!
483 # DELAYSET session_id event_name time_alarm file line time
484 print OUT 'DELAYSET "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . $event . '" "' .
485 $time . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
486 }
487  
488 # Time to send some signals!
489 sub _PROFILE_SIGNAL {
490 my( $dest, $signal ) = @_[ 1, 2 ];
491  
492 # Sanity checks
493 if ( ! defined $dest ) { return }
494 if ( ! defined $signal ) { return }
495
496 # Get the destination session's ID
497 $dest = $_[0]->_resolve_session( $dest );
498  
499 # Check if it is valid
500 if ( ! defined $dest ) {
501 return;
502 } else {
503 # Get the ID
504 $dest = $dest->ID;
505 }
506  
507 # Get the caller's filename and etc
508 my( $file, $line ) = ( caller( 1 ) )[1,2];
509  
510 # Print it out!
511 # SIGNAL session_id dest_id signal file line time
512 print OUT 'SIGNAL "' . $POE::Kernel::poe_kernel->get_active_session->ID . '" "' . $dest . '" "' .
513 $signal . '" "' . $file . '" "' . $line . '" "' . time() . "\"\n";
514 }
515  
516 # Keeps track of how many times a session was GC'ed
517 sub _PROFILE_GC {
518 my $session = $_[ 1 ];
519  
520 # Print it out!
521 # GC session_id time
522 print OUT 'GC "' . $session->ID . '" "' . time() . "\"\n";
523 }
524  
525 # Okay, when the program ends, we print our stuff!
526 sub END {
527 # Collect time statistics
528 my( $wall, $user, $system, $cuser, $csystem ) = ( ( time-$^T ), times() );
529  
530 # Print it out!
531 # ENDPROGRAM time wall user system cuser csystem
532 print OUT 'ENDPROGRAM "' . time() . '" "' . $wall . '" "' . $user . '" "' . $system . '" "' .
533 $cuser . '" "' . $csystem . "\"\n";
534  
535 # We're finally done!
536 close( OUT ) or die $!;
537 }
538  
539 # End of module
540 1;
541 __END__