How to get UDP buffer statistics information on Linux

Well, however UDP is an unreliable protocol, maybe you would like to reach high performance using it due to several reasons, for example because your network devices (routers, switches) can only use this protocol instead of TCP. In that cases, the major problem is the packet loss because the kernel will drop the packages if its receiver or transmitter buffers are full.
To prevent it, you should fine-tune the buffers, but how will you know what the best sizes are?
If you increase the size of the buffers too high, you will lose more packages that you want when something evil happens for example your computer is restarted or crashed. If you are using too low values, kernel will drop the packages when the buffers are full.

The current size of the RX (incoming) and TX (outgoing) buffers and the number of dropped packages are stored in /proc/net/udp on Linux. This is not a very human readable file, thus I’ve written a smart python script that can collect the above statistics information and display them or write them to a CSV format file.

You can download it from here: https://github.com/pzolee/udpstat
Just download udpstat.py and get the help:

:~/udp-environment# ./udpstat.py -h
Usage: udpstat.py [options] port

UDP buffer size (RX, TX) and dropped packages statistics program, written by
PZolee (pzoleex @ freemail.hu), 2012

Options:
--version show program's version number and exit
-h, --help show this help message and exit
-o , --output=
The name of the output file
-r, --rx Measure the size of RX(incoming) buffer
-t, --tx Measure the size of TX(outgoing) buffer
-d, --drops Measure the number of dropped packages for RX and TX
buffers
-b BLOCK_SIZE, --displayed-blocks=BLOCK_SIZE
The displayed block size for the TX, RX queues.
Possible values: B, K, M, all; default: all
-l , --listened-port-type=
The type of the listened port, default: local
-f FREQ, --freq=FREQ The time between two polls in sec, default: 1
-u RUNTIME, --run-time=RUNTIME
The running time if given. Default: untill CTRL+C
-c, --csv Generate CSV output format

For example if you want to see both tx and rx buffers including the number of dropped packages when your application receives data via UDP, just start it giving the listened port (that your application uses) and the time for the collecting (if it is not given it will collect the statistics information until CTRL+C is pressed):

:~# ./udpstat.py 5001 -u 10
Start collecting RX and TX queue statistics information and dropped packages result for port (5001)
2012-09-03T14:28:38.550504;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 0 bytes, 0 KB, 0 MB; dropped packages: 0;
2012-09-03T14:28:39.551747;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 0 bytes, 0 KB, 0 MB; dropped packages: 0;
2012-09-03T14:28:40.552996;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 0 bytes, 0 KB, 0 MB; dropped packages: 0;
2012-09-03T14:28:41.554350;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 332055496 bytes, 324272 KB, 316 MB; dropped packages: 0;
2012-09-03T14:28:42.555650;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 536869432 bytes, 524286 KB, 511 MB; dropped packages: 118733;
2012-09-03T14:28:43.556819;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 536869432 bytes, 524286 KB, 511 MB; dropped packages: 374088;
2012-09-03T14:28:44.558018;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 509777816 bytes, 497829 KB, 486 MB; dropped packages: 493162;
2012-09-03T14:28:45.559211;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 481418456 bytes, 470135 KB, 459 MB; dropped packages: 493162;
2012-09-03T14:28:46.560400;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 481418456 bytes, 470135 KB, 459 MB; dropped packages: 493162;
2012-09-03T14:28:47.561667;tx queue: 0 bytes, 0 KB, 0 MB; rx queue: 443327512 bytes, 432937 KB, 422 MB; dropped packages: 493162;
Maximum tx queue: 0 bytes, 0 KB, 0 MB;
Maximum rx queue: 536869432 bytes, 524286 KB, 511 MB;
Dropped packages: 493162

Running this program you can detect easily how full the buffers are and when the kernel starts to drop packages. Increasing the buffer size step by step, you will be able to specify the proper values.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: