blib/lib/POE/Devel/Top.pm

filter: Statementstmt 22.4% 20 / 89 Branchbran 4.1% 1 / 24 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod 20.0% 1 / 5 totaltotal 20.7% 29 / 140 CC 20 SCAR 33.4
CC
20
Cov
18%
collect
72.0
store
30.0
human_size
20.0
CRAP 28.1
↑ Summary
1 package POE::Devel::Top;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POE qw< API::Peek Session >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Term::ANSIColor qw< :constants >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 our $VERSION = "0.100";
12  
13  
14 #
15 # import()
16 # ------
17 1 ▶sub import {
Subroutine import: called CC=3 SCAR=12.5
18 1 my ($class, @args) = @_;
19  
20 # if caller line is zero, it means the module was loaded from the
21 # command line, in which case we automatically spawn the session
22 1 my ($package, undef, $line) = caller;
23 1 ▶ $class->spawn(render => "console", @args)
Branch: 1 branch
BranchTrueFalseTotal
if $line == 0 or $package eq __PACKAGE__ 0 1 1
Condition33%1/3
$line == 0 or $package eq __PACKAGE__
l!l&&r!l&&!r
001
MC/DC0%0/2
$line == 0 or $package eq __PACKAGE__
$line == 0 $package eq __PACKAGE__
Truth table33%1/3
A or B
A= $line == 0B= $package eq __PACKAGE__
ABresult
1X1
011
000
24 if $line == 0 or $package eq __PACKAGE__;
25 }
26  
27  
28 #
29 # spawn()
30 # -----
31 0 ▶sub spawn {
Subroutine spawn: not called CC=2 SCAR=17.9
Pod documented
32 0 my ($class, @args) = @_;
33  
34 0 ▶ croak "Odd number of argument" if @args % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 0 0 0
35  
36 POE::Session->create(
37 heap => {
38 interval => 2,
39 @args
40 },
41  
42 inline_states => {
43 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 $_[KERNEL]->alias_set("[$class]");
45 0 $_[KERNEL]->delay(poe_devel_top_collect => $_[HEAP]->{interval});
46 },
47 0 poe_devel_top_collect => \&collect,
48 poe_devel_top_render => \&render,
49 poe_devel_top_store => \&store,
50 },
51 );
52 }
53  
54  
55 #
56 # collect()
57 # -------
58 0 ▶sub collect {
Subroutine collect: not called CC=8 SCAR=42.8
Pod undocumented
59 0 my ($kernel, $heap) = @_[ KERNEL, HEAP ];
60 0 my $poe_api = POE::API::Peek->new;
61 0 my $now = time;
62  
63 # collect general data about the current process
64 0 my @times = times;
65 0 my @pwent = getpwuid(int $>);
66 0 my $egid = (split / /, $))[0];
67 0 my @grent = getgrgid(int $egid);
68  
69 0 my %general = (
70 process => {
71 pid => $$,
72 uid => $>,
73 gid => $egid,
74 user => $pwent[0],
75 group => $grent[0],
76 },
77 resource => {
78 utime_self => $times[0],
79 utime_chld => $times[2],
80 stime_self => $times[1],
81 stime_chld => $times[3],
82 },
83 poe => {
84 sessions => $poe_api->session_count,
85 handles => $poe_api->handle_count,
86 loop => $poe_api->which_loop,
87 },
88 );
89  
90 # collect information about the sessions
91 0 my $kernel_id = $kernel->ID;
92 0 my @sessions;
93  
94 0 for my $session ($poe_api->session_list) {
95 0 ▶ push @sessions, {
Branch: 1 branch
BranchTrueFalseTotal
$session->ID eq $kernel_id ? : 0 0 0
96 $session->ID eq $kernel_id ? (
97 id => 0,
98 aliases => "[POE::Kernel] id=".$session->ID,
99 ) : (
100 id => $session->ID,
101 aliases => join(",", $poe_api->session_alias_list($session)),
102 ),
103 memory_size => $poe_api->session_memory_size($session),
104 refcount => $poe_api->get_session_refcount($session),
105 events_to => $poe_api->event_count_to($session),
106 events_from => $poe_api->event_count_from($session),
107 };
108 }
109  
110 0 @sessions = sort { $a->{id} <=> $b->{id} } @sessions;
111  
112 # collect information about the events
113 0 my @events;
114  
115 0 for my $event ($poe_api->event_queue_dump) {
116 push @events, {
117 id => $event->{ID},
118 name => $event->{event},
119 type => $event->{type},
120 priority => $event->{priority} > $now ?
121 $event->{priority} - $now : $event->{priority},
122 source => $event->{source}->ID,
123 destination => $event->{destination}->ID,
124 }
125 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$event->{'priority'} > $now ? : 0 0 0
126  
127 # create the final hash
128 0 my %stats = (
129 general => \%general,
130 sessions => \@sessions,
131 events => \@events,
132 );
133  
134 # call myself
135 0 $kernel->delay(poe_devel_top_collect => $heap->{interval});
136  
137 # call the dumper event
138 $kernel->yield(poe_devel_top_store => \%stats)
139 0 ▶ if $heap->{dump_as} and $heap->{dump_as} ne "none";
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"dump_as"} and $heap->{"dump_as"} ne "none" 0 0 0
Condition0%0/3
$heap->{"dump_as"} and $heap->{"dump_as"} ne "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$heap->{"dump_as"} and $heap->{"dump_as"} ne "none"
$heap->{"dump_as"} $heap->{"dump_as"} ne "none"
Truth table0%0/3
A and B
A= $heap->{"dump_as"}B= $heap->{"dump_as"} ne "none"
ABresult
0X0
100
111
140  
141 # call the renderer event
142 $kernel->yield(poe_devel_top_render => \%stats)
143 0 ▶ if $heap->{render} eq "console";
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"render"} eq "console" 0 0 0
144  
145 0 return \%stats
146 }
147  
148  
149 #
150 # render()
151 # ------
152 0 ▶sub render {
Subroutine render: not called CC=3 SCAR=24.8
Pod undocumented
153 0 my ($kernel, $stats) = @_[ KERNEL, ARG0 ];
154 0 my $proc = $stats->{general}{process};
155 0 my $rsrc = $stats->{general}{resource};
156  
157 0 local $Term::ANSIColor::AUTORESET = 1;
158  
159 0 my $session_head = REVERSE(BOLD "%5s %6s %8s %6s %8s %-40s").$/;
160 0 my $session_row = "%5d %6s %8d %6d %8d %-40s\n";
161 0 my @session_cols = qw< ID Memory Refcount EvtsTo EvtsFrom Aliases >;
162  
163 0 my $event_head = REVERSE(BOLD "%5s %-17s %4s %5s %5s %-40s").$/;
164 0 my $event_row = "%5d %-17s %4d %5d %5d %-40s\n";
165 0 my @event_cols = qw< ID Type Pri Src Dest Name >;
166  
167 0 print "\e[2J\e[f",
168 "Process ID: $proc->{pid}, ",
169 "UID: $proc->{uid} ($proc->{user}), ",
170 "GID: $proc->{gid} ($proc->{group})\n",
171 "Resource usage: ",
172 "user: $rsrc->{utime_self} sec (+$rsrc->{utime_chld} sec), ",
173 "system: $rsrc->{stime_self} sec (+$rsrc->{stime_chld} sec)\n",
174 "Sessions: $stats->{general}{poe}{sessions} total, ",
175 "Handles: $stats->{general}{poe}{handles} total, ",
176 "Loop: $stats->{general}{poe}{loop}\n\n";
177  
178 0 print BOLD " Sessions", $/;
179 0 printf $session_head, @session_cols;
180 printf $session_row,
181 $_->{id}, human_size( $_->{memory_size} ), $_->{refcount},
182 $_->{events_to}, $_->{events_from}, $_->{aliases}
183 0 for @{$stats->{sessions}};
184  
185 0 print $/;
186  
187 0 print BOLD " Events", $/;
188 0 printf $event_head, @event_cols;
189 printf $event_row,
190 $_->{id}, $_->{type}, $_->{priority},
191 $_->{source}, $_->{destination}, $_->{name}
192 0 for @{$stats->{events}};
193  
194 0 print $/;
195 }
196  
197  
198 #
199 # human_size()
200 # ----------
201 0 ▶sub human_size {
Subroutine human_size: not called CC=4 SCAR=30.0
Pod undocumented
202 0 my ($size) = @_;
203  
204 0 ▶ return $size if $size < 100_000;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 100000 0 0 0
205  
206 0 my $unit;
207 0 for (qw< K M G >) {
208 0 $size = int($size / 1024);
209 0 $unit = $_;
210 0 ▶ last if $size < 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 1024 0 0 0
211 }
212  
213 0 return $size.$unit;
214 }
215  
216  
217 #
218 # store()
219 # -----
220 0 ▶sub store {
Subroutine store: not called CC=5 SCAR=34.0
Pod undocumented
221 0 my ($kernel, $heap, $stats) = @_[ KERNEL, HEAP, ARG0 ];
222  
223 0 ▶ if ($heap->{dump_as} eq "yaml") {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"dump_as"} eq "yaml") 0 0 0
224 0 ▶ if (eval "require YAML; 1") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require YAML; 1') { } 0 0 0
225 0 YAML::DumpFile($heap->{dump_to}, $stats);
226 return
227 0 }
228 else {
229 0 $heap->{dump_as} = "native";
230 0 $heap->{dump_to} =~ s/\.ya?ml$/.dmp/;
231 0 carp "warning: YAML not available. Defaulting to native format."
232 }
233 }
234  
235 0 ▶ if ($heap->{dump_as} eq "native") {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"dump_as"} eq "native") 0 0 0
236 0 ▶ if (eval "require Storable; 1") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require Storable; 1') { } 0 0 0
237 0 Storable::nstore($stats, $heap->{dump_to});
238 return
239 0 }
240 else {
241 0 croak "fatal: Can't load Storable: $@"
242 }
243 }
244 }
245  
246  
247 2 __PACKAGE__
248  
249 __END__
↑ Summary