| 1 | package Callgrind::Parser; | ||||||||||||||
| 2 | 1 | $Callgrind::Parser::VERSION = '0.001'; | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | require Exporter; | |||||||||||||
| 6 | 1 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | BEGIN{ | ||||||||||||||
| 11 | 1 | ▶ | @EXPORT = (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | @EXPORT_OK = qw(&parseFile); | |||||||||||||
| 13 | 1 | %EXPORT_TAGS = (all=>\@EXPORT_OK); | |||||||||||||
| 14 | } | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 NAME | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Callgrind::Parser - Parses Callgrind output file into a hashref representing the call tree of the source program | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||
| 21 | |||||||||||||||
| 22 | version 0.001 | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||
| 25 | |||||||||||||||
| 26 | use Callgrind::Parser; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | my $profile = Callgrind::Parser::parseFile('t/data/helloworld.out') | ||||||||||||||
| 29 | |||||||||||||||
| 30 | print "Hello world took ".$profile->{main}{Time}." milliseconds to run\n"; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||
| 33 | |||||||||||||||
| 34 | The parser was designed to read Callgrind profile data as described L<here|http://kcachegrind.sourceforge.net/html/CallgrindFormat.html>. | ||||||||||||||
| 35 | Primarily written to read and manipulate profiling output generated by L<xdebug|http://xdebug.org/>. Thus far it has only been tested | ||||||||||||||
| 36 | with files generated by xdebug. | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =cut | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head2 parseFile | ||||||||||||||
| 43 | |||||||||||||||
| 44 | This method does all the work of the function. Takes in the full path to a file to parse | ||||||||||||||
| 45 | Returns a hash containing the metadata read from the file header, as well has a hash ref | ||||||||||||||
| 46 | which represents the full call tree of the program from which the profile was generated. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =cut | ||||||||||||||
| 49 | |||||||||||||||
| 50 | sub parseFile { | ||||||||||||||
| 51 | 2 | ▶ | my($inf) = @_; | ||||||||||||
|
Subroutine
parseFile: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 2 | ▶ | open(my $fh, '<', $inf) or croak "Couldn't open input file for reading: $!\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 1 | my(%meta)=(); | |||||||||||||
| 55 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 7 | chomp $line; | |||||||||||||
| 57 | 7 | ▶ | next if(length($line) == 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 6 | my($key, $value) = split(/:\s+/, $line); | |||||||||||||
| 59 | 6 | ▶ | if ($key eq 'positions') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 60 | 1 | $meta{$key} = [split(/\s+/, $value)]; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | elsif($key eq 'events') { | ||||||||||||||
| 63 | 1 | $meta{$key} = [split(/\s+/, $value)]; | |||||||||||||
| 64 | # discard trailing empty line | ||||||||||||||
| 65 | 1 | scalar(<$fh>); | |||||||||||||
| 66 | 1 | last; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | else { | ||||||||||||||
| 69 | 4 | $meta{$key} = $value; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 1 | my(@buffer) = (); | |||||||||||||
| 74 | 1 | my(%commands) = (); | |||||||||||||
| 75 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 5 | chomp $line; | |||||||||||||
| 77 | 5 | ▶ | if (length($line) == 0){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | # Handle summary line for program main | ||||||||||||||
| 79 | 2 | ▶ | if ($buffer[-1] eq 'fn={main}') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 1 | my $summary =<$fh>; | |||||||||||||
| 81 | 1 | $summary=~m/(\d+)/; | |||||||||||||
| 82 | 1 | $meta{total_time} = $1; | |||||||||||||
| 83 | 1 | my $j = <$fh>; | |||||||||||||
| 84 | }else{ | ||||||||||||||
| 85 | 1 | &_parseCommand(\@buffer, \%commands, \%meta); | |||||||||||||
| 86 | 1 | @buffer=(); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | } | ||||||||||||||
| 89 | else { | ||||||||||||||
| 90 | 3 | push @buffer, $line; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | 1 | close($fh); | |||||||||||||
| 94 | |||||||||||||||
| 95 | 1 | return {meta=>\%meta, main=>$commands{'{main}'}{instances}[0]}; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | sub _parseCommand { | ||||||||||||||
| 99 | 1 | ▶ | my($stack, $commands, $meta) = @_; | ||||||||||||
|
Subroutine
_parseCommand: called CC=3 SCAR=13.9
| |||||||||||||||
| 100 | # disregard file name | ||||||||||||||
| 101 | 1 | shift @$stack; | |||||||||||||
| 102 | 1 | my $function = substr(shift @$stack, 3); | |||||||||||||
| 103 | |||||||||||||||
| 104 | 1 | my($line, @fields) = split(/\s+/, shift @$stack); | |||||||||||||
| 105 | |||||||||||||||
| 106 | 1 | ▶ | $commands->{ $function } ||= {instances=>[]}; | ||||||||||||
|
Condition50%1/2
$commands->{$function} ||= {"instances", []}
MC/DC0%0/1
$commands->{$function} ||= {"instances", []}
$commands->{$function}
Truth table50%1/2
A
A= $commands->{$function}
| |||||||||||||||
| 107 | 1 | my(%instance) = (line=>$line, function=>$function); | |||||||||||||
| 108 | 1 | @instance{ @{ $meta->{events} } } = @fields; | |||||||||||||
| 109 | 1 | ▶ | while ($#$stack > 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | # disregard file name | ||||||||||||||
| 111 | 0 | shift @$stack; | |||||||||||||
| 112 | 0 | my $sfn = substr(shift @$stack, 4); | |||||||||||||
| 113 | 0 | my $ca = substr(shift @$stack, 6); | |||||||||||||
| 114 | 0 | my($cline, $time) = split(/\s+/, shift @$stack); | |||||||||||||
| 115 | 0 | my $inst = shift @{ $commands->{ $sfn }{instances} }; | |||||||||||||
| 116 | 0 | $inst->{called_from} = $cline; | |||||||||||||
| 117 | 0 | $inst->{time_inclusive} = $time; | |||||||||||||
| 118 | 0 | push @{ $instance{children} }, $inst; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | 1 | push @{ $commands->{$function}{instances} }, \%instance; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 1 | 1; | |||||||||||||
| 124 | =head1 AUTHOR | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Dave Mueller <dave@perljedi.com> | ||||||||||||||
| 127 | |||||||||||||||
| 128 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 129 | |||||||||||||||
| 130 | This software is copyright (c) 2015 by Dave Mueller. | ||||||||||||||
| 131 | |||||||||||||||
| 132 | This is free software; you can redistribute it and/or modify it under the | ||||||||||||||
| 133 | same terms as the Perl 5 programming language system itself. | ||||||||||||||