| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2010-2011 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package IO::Socket::Netlink::Taskstats; | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use base qw( IO::Socket::Netlink::Generic ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | our $VERSION = '0.03'; | |||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use Socket::Netlink qw( :DEFAULT ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Socket::Netlink::Taskstats; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | __PACKAGE__->register_family_name( "TASKSTATS" ); | |||||||||
| 20 | |||||||||||
| 21 | =head1 NAME | ||||||||||
| 22 | |||||||||||
| 23 | C<IO::Socket::Netlink::Taskstats> - Object interface to C<Taskstats> generic | ||||||||||
| 24 | netlink protocol sockets | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | use IO::Socket::Netlink::Taskstats; | ||||||||||
| 29 | |||||||||||
| 30 | my $sock = IO::Socket::Netlink::Taskstats->new; | ||||||||||
| 31 | |||||||||||
| 32 | my $stats = $sock->get_process_info_by_pid( $$ ); | ||||||||||
| 33 | |||||||||||
| 34 | printf "So far, %s has consumed %d usec in userland and %d in kernel\n", | ||||||||||
| 35 | $stats->{ac_comm}, | ||||||||||
| 36 | $stats->{ac_utime}, | ||||||||||
| 37 | $stats->{ac_stime}; | ||||||||||
| 38 | |||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||
| 40 | |||||||||||
| 41 | This subclass of L<IO::Socket::Netlink::Generic> implements the C<Taskstats> | ||||||||||
| 42 | generic netlink protocol. This protocol allows detailed statistics gathering | ||||||||||
| 43 | of resource usage on a per-process basis, and notification of resources used | ||||||||||
| 44 | by processes at the time they exit. | ||||||||||
| 45 | |||||||||||
| 46 | This module is currently a work-in-progress, and this documentation is fairly | ||||||||||
| 47 | minimal. The reader is expected to be familiar with C<Taskstats>, only a | ||||||||||
| 48 | fairly minimal description of the Perl-level wrapping is given here. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | =head1 METHODS | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | sub message_class | ||||||||||
| 57 | 4 | ▶ | { | ||||||||
|
Subroutine
message_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 58 | 4 | return "IO::Socket::Netlink::Taskstats::_Message"; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub command_class | ||||||||||
| 62 | 1 | ▶ | { | ||||||||
|
Subroutine
command_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 1 | return "IO::Socket::Netlink::Taskstats::_Command"; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub _get_process_info | ||||||||||
| 67 | 1 | ▶ | { | ||||||||
|
Subroutine
_get_process_info: called CC=3 SCAR=13.3
| |||||||||||
| 68 | 1 | my $self = shift; | |||||||||
| 69 | 1 | my %searchattrs = @_; | |||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | $self->send_nlmsg( $self->new_command( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | cmd => CMD_GET, | ||||||||||
| 73 | nlattrs => \%searchattrs, | ||||||||||
| 74 | ) ) or croak "Cannot send - $!"; | ||||||||||
| 75 | |||||||||||
| 76 | 1 | ▶ | $self->recv_nlmsg( my $message, 32768 ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | croak "Cannot recv - $!"; | ||||||||||
| 78 | |||||||||||
| 79 | 0 | return $message->nlattrs; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head2 $info = $sock->get_process_info_by_pid( $pid ) | ||||||||||
| 83 | |||||||||||
| 84 | Returns an information structure containing the statistics about the process | ||||||||||
| 85 | with the given PID. | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | sub get_process_info_by_pid | ||||||||||
| 90 | 1 | ▶ | { | ||||||||
|
Subroutine
get_process_info_by_pid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 91 | 1 | my $self = shift; | |||||||||
| 92 | 1 | my ( $pid ) = @_; | |||||||||
| 93 | 1 | return $self->_get_process_info( pid => $pid )->{aggr_pid}{stats}; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 $sock->register_cpumask( $mask ) | ||||||||||
| 97 | |||||||||||
| 98 | =head2 $sock->deregister_cpumask( $mask ) | ||||||||||
| 99 | |||||||||||
| 100 | Register or deregister this socket to receive process exit notifications, for | ||||||||||
| 101 | processes exiting on CPUs given by the C<$mask>. | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | sub register_cpumask | ||||||||||
| 106 | 0 | ▶ | { | ||||||||
|
Subroutine
register_cpumask: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 107 | 0 | my $self = shift; | |||||||||
| 108 | 0 | my ( $mask ) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 0 | $self->send_nlmsg( $self->new_command( | |||||||||
| 111 | cmd => CMD_GET, | ||||||||||
| 112 | nlattrs => { | ||||||||||
| 113 | register_cpumask => $mask, | ||||||||||
| 114 | }, | ||||||||||
| 115 | ) ); | ||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | $self->recv_nlmsg( my $message, 32768 ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | croak "Cannot recv - $!"; | ||||||||||
| 119 | 0 | ▶ | ( $! = $message->nlerr_error ) and croak "Received NLMSG_ERROR - $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | sub deregister_cpumask | ||||||||||
| 123 | 0 | ▶ | { | ||||||||
|
Subroutine
deregister_cpumask: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 124 | 0 | my $self = shift; | |||||||||
| 125 | 0 | my ( $mask ) = @_; | |||||||||
| 126 | |||||||||||
| 127 | 0 | $self->send_nlmsg( $self->new_command( | |||||||||
| 128 | cmd => CMD_GET, | ||||||||||
| 129 | nlattrs => { | ||||||||||
| 130 | deregister_cpumask => $mask, | ||||||||||
| 131 | }, | ||||||||||
| 132 | ) ); | ||||||||||
| 133 | |||||||||||
| 134 | 0 | ▶ | $self->recv_nlmsg( my $message, 32768 ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | croak "Cannot recv - $!"; | ||||||||||
| 136 | 0 | ▶ | ( $! = $message->nlerr_error ) and croak "Received NLMSG_ERROR - $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | =head1 MESSAGE OBJECTS | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | package IO::Socket::Netlink::Taskstats::_Message; | ||||||||||
| 144 | |||||||||||
| 145 | 2 | ▶ | use base qw( IO::Socket::Netlink::Generic::_Message ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 146 | |||||||||||
| 147 | 2 | ▶ | use Socket::Netlink::Taskstats qw( :DEFAULT ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 148 | |||||||||||
| 149 | =pod | ||||||||||
| 150 | |||||||||||
| 151 | Provides the following netlink attributes | ||||||||||
| 152 | |||||||||||
| 153 | =over 4 | ||||||||||
| 154 | |||||||||||
| 155 | =item * pid => INT | ||||||||||
| 156 | |||||||||||
| 157 | =item * tgid => INT | ||||||||||
| 158 | |||||||||||
| 159 | =item * stats => HASH | ||||||||||
| 160 | |||||||||||
| 161 | =item * aggr_pid => HASH | ||||||||||
| 162 | |||||||||||
| 163 | =item * aggr_tgid => HASH | ||||||||||
| 164 | |||||||||||
| 165 | =back | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | 2 | __PACKAGE__->has_nlattrs( | |||||||||
| 170 | "genlmsg", | ||||||||||
| 171 | pid => [ TYPE_PID, "u32" ], | ||||||||||
| 172 | tgid => [ TYPE_TGID, "u32" ], | ||||||||||
| 173 | stats => [ TYPE_STATS, "stats" ], | ||||||||||
| 174 | aggr_pid => [ TYPE_AGGR_PID, "nested" ], | ||||||||||
| 175 | aggr_tgid => [ TYPE_AGGR_TGID, "nested" ], | ||||||||||
| 176 | ); | ||||||||||
| 177 | |||||||||||
| 178 | 0 | ▶ | sub pack_nlattr_stats { pack_taskstats $_[1] } | ||||||||
|
Subroutine
pack_nlattr_stats: not called CC=1 SCAR=6.9
| |||||||||||
| 179 | 0 | ▶ | sub unpack_nlattr_stats { unpack_taskstats $_[1] } | ||||||||
|
Subroutine
unpack_nlattr_stats: not called CC=1 SCAR=6.9
| |||||||||||
| 180 | |||||||||||
| 181 | package IO::Socket::Netlink::Taskstats::_Command; | ||||||||||
| 182 | |||||||||||
| 183 | 2 | ▶ | use base qw( IO::Socket::Netlink::Generic::_Message ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 184 | |||||||||||
| 185 | 2 | ▶ | use Socket::Netlink::Taskstats qw( :DEFAULT ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 186 | |||||||||||
| 187 | 2 | __PACKAGE__->has_nlattrs( | |||||||||
| 188 | "genlmsg", | ||||||||||
| 189 | pid => [ CMD_ATTR_PID, "u32" ], | ||||||||||
| 190 | tgid => [ CMD_ATTR_TGID, "u32" ], | ||||||||||
| 191 | register_cpumask => [ CMD_ATTR_REGISTER_CPUMASK, "raw" ], | ||||||||||
| 192 | deregister_cpumask => [ CMD_ATTR_DEREGISTER_CPUMASK, "raw" ], | ||||||||||
| 193 | ); | ||||||||||
| 194 | |||||||||||
| 195 | =head1 SEE ALSO | ||||||||||
| 196 | |||||||||||
| 197 | =over 4 | ||||||||||
| 198 | |||||||||||
| 199 | =item * | ||||||||||
| 200 | |||||||||||
| 201 | L<Socket::Netlink::Taskstats> - interface to Linux's C<Taskstats> generic | ||||||||||
| 202 | netlink socket protocol | ||||||||||
| 203 | |||||||||||
| 204 | =item * | ||||||||||
| 205 | |||||||||||
| 206 | L<IO::Socket::Netlink> - Object interface to C<AF_NETLINK> domain sockets | ||||||||||
| 207 | |||||||||||
| 208 | =back | ||||||||||
| 209 | |||||||||||
| 210 | =head1 AUTHOR | ||||||||||
| 211 | |||||||||||
| 212 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 213 | |||||||||||
| 214 | =cut | ||||||||||
| 215 | |||||||||||
| 216 | 2 | 0x55AA; | |||||||||