ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/FastPing.pm
(Generate patch)

Comparing AnyEvent-FastPing/FastPing.pm (file contents):
Revision 1.13 by root, Wed Feb 2 19:26:45 2011 UTC vs.
Revision 1.14 by root, Wed Feb 2 20:04:17 2011 UTC

10 10
11This module was written for a single purpose only: sending ICMP ECHO 11This module was written for a single purpose only: sending ICMP ECHO
12REQUEST packets as quickly as possible to a large number of hosts 12REQUEST packets as quickly as possible to a large number of hosts
13(thousands to millions). 13(thousands to millions).
14 14
15It employs a sending thread and is fully event-driven (using AnyEvent), so 15It employs a separate thread and is fully event-driven (using AnyEvent),
16you have to run an event model supported by AnyEvent to use this module. 16so you have to run an event model supported by AnyEvent to use this
17module.
17 18
18=head1 FUNCTIONS 19=head1 FUNCTIONS
19 20
20=over 4 21=over 4
21 22
101When a range is exhausted, it is removed. When all ranges are exhausted, 102When a range is exhausted, it is removed. When all ranges are exhausted,
102the pinger waits another C<max_rtt> seconds and then exits, causing the 103the pinger waits another C<max_rtt> seconds and then exits, causing the
103idle callback to trigger. 104idle callback to trigger.
104 105
105Performance: On my 2 GHz Opteron system with a pretty average nvidia 106Performance: On my 2 GHz Opteron system with a pretty average nvidia
106gigabit network card I can ping around 60k to 200k adresses per second, 107gigabit network card I can ping around 60k to 200k addresses per second,
107depending on routing decisions. 108depending on routing decisions.
108 109
109Example: ping 10.0.0.1-10.0.0.15 with at most 100 packets/s, and 110Example: ping 10.0.0.1-10.0.0.15 with at most 100 packets/s, and
11011.0.0.1-11.0.255.255 with at most 1000 packets/s. Also ping the IPv6 11111.0.0.1-11.0.255.255 with at most 1000 packets/s. Also ping the IPv6
111loopback address 5 times as fast as possible. Do not, however, exceed 1000 112loopback address 5 times as fast as possible. Do not, however, exceed 1000
112packets/s overall. Dump each received reply: 113packets/s overall. Also dump each received reply.
113 114
114 use AnyEvent::Socket; 115 use AnyEvent::Socket;
115 use AnyEvent::FastPing; 116 use AnyEvent::FastPing;
116 117
117 my $done = AnyEvent->condvar; 118 my $done = AnyEvent->condvar;
144=over 4 145=over 4
145 146
146=item $pinger = new AnyEvent::FastPing 147=item $pinger = new AnyEvent::FastPing
147 148
148Creates a new pinger - right now there can be at most C<65536> pingers in 149Creates a new pinger - right now there can be at most C<65536> pingers in
149a process, although that limit might change to somethind drastically lower 150a process, although that limit might change to something drastically lower
150- you should be stringy with your pinger objects. 151- you should be stingy with your pinger objects.
151 152
152=cut 153=cut
153 154
154sub new { 155sub new {
155 my ($klass) = @_; 156 my ($klass) = @_;
164 &_free; 165 &_free;
165} 166}
166 167
167=item $pinger->on_recv ($callback->([[$host, $rtt], ...])) 168=item $pinger->on_recv ($callback->([[$host, $rtt], ...]))
168 169
169Registeres a callback to be called for ping replies. If no callback has 170Registers a callback to be called for ping replies. If no callback has
170been registered than ping replies will be ignored, otherwise this module 171been registered than ping replies will be ignored, otherwise this module
171calculates the round trip time, in seconds, for each reply and calls this 172calculates the round trip time, in seconds, for each reply and calls this
172callback. 173callback.
173 174
174The callback receives a single argument, which is an array reference 175The callback receives a single argument, which is an array reference
175with an entry for each reply packet (the replies will be batched for 176with an entry for each reply packet (the replies will be batched for
176efficiency). Each member in the array reference is again an array 177efficiency). Each member in the array reference is again an array
177reference with exactly two members: the binary host addresss (4 octets for 178reference with exactly two members: the binary host address (4 octets for
178IPv4, 16 for IPv6) and the approximate round trip time, in seconds. 179IPv4, 16 for IPv6) and the approximate round trip time, in seconds.
179 180
180The replies will be passed to the callback as soon as they arrive, and 181The replies will be passed to the callback as soon as they arrive, and
181this callback can be called many times with batches of replies. 182this callback can be called many times with batches of replies.
182 183
196 197
197 $pinger->on_recv (sub { 198 $pinger->on_recv (sub {
198 for (@{ $_[0] }) { 199 for (@{ $_[0] }) {
199 printf "%s %g\n", (AnyEvent::Socket::format_address $_->[0]), $_->[1]; 200 printf "%s %g\n", (AnyEvent::Socket::format_address $_->[0]), $_->[1];
200 } 201 }
201 }; 202 });
202 203
203Example: a single ping reply with payload of 1 from C<::1> gets passed 204Example: a single ping reply with payload of 1 from C<::1> gets passed
204like this: 205like this:
205 206
206 [ 207 [
237}; 238};
238 239
239=item $pinger->interval ($seconds) 240=item $pinger->interval ($seconds)
240 241
241Configures the minimum interval between packet sends for this pinger - the 242Configures the minimum interval between packet sends for this pinger - the
242pinger will not send packets faster than this rate (or atcually 1 / rate), 243pinger will not send packets faster than this rate (or actually 1 / rate),
243even if individual ranges have a lower interval. 244even if individual ranges have a lower interval.
244 245
245A value of C<0> selects the fastests possible speed (currently no faster 246A value of C<0> selects the fastest possible speed (currently no faster
246than 1_000_000 packets/s). 247than 1_000_000 packets/s).
247 248
248=item $pinger->max_rtt ($seconds) 249=item $pinger->max_rtt ($seconds)
249 250
250If your idle callback were called instantly after all ranges were 251If your idle callback were called instantly after all ranges were
251exhausted and you destroyed the object inside (which is common), then 252exhausted and you destroyed the object inside (which is common), then
252there would be no chance to receive some replies, as there would be no 253there would be no chance to receive some replies, as there would be no
253time fo the packet to travel over the network. 254time of the packet to travel over the network.
254 255
255This can be fixed by starting a timer in the idle callback, or more simply 256This can be fixed by starting a timer in the idle callback, or more simply
256by selecting a suitable C<max_rtt> value, which should be the maximum time 257by selecting a suitable C<max_rtt> value, which should be the maximum time
257you allow a ping packet to travel to its destinazion and back. 258you allow a ping packet to travel to its destination and back.
258 259
259The pinger thread automatically waits for this amount of time before becoming idle. 260The pinger thread automatically waits for this amount of time before becoming idle.
260 261
261The default is currently C<0.5> seconds, which is usually plenty. 262The default is currently C<0.5> seconds, which is usually plenty.
262 263
270You can convert IP addresses from text to binary form by 271You can convert IP addresses from text to binary form by
271using C<AnyEvent::Util::parse_address>, C<Socket::inet_aton>, 272using C<AnyEvent::Util::parse_address>, C<Socket::inet_aton>,
272C<Socket6::inet_pton> or any other method that you like :) 273C<Socket6::inet_pton> or any other method that you like :)
273 274
274The algorithm to select the next address is O(log n) on the number of 275The algorithm to select the next address is O(log n) on the number of
275ranges, so even a large number of ranges (many thousands) is managable. 276ranges, so even a large number of ranges (many thousands) is manageable.
276 277
277No storage is allocated per address. 278No storage is allocated per address.
278 279
279Note that, while IPv6 addresses are currently supported, the usefulness of 280Note that, while IPv6 addresses are currently supported, the usefulness of
280this option is extremely limited and might be gone in future versions - if 281this option is extremely limited and might be gone in future versions - if

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines