Network & disk bandwidth limiting on Linux
Recently, in my research, I want to throttle the I/O and network bandwidth of my program so as to show my program is robust in different senarios. In practice, we may also want to limit the resources used by certain programs (rsync
, wget
, …). Fortunately, this is possible in Linux. Next, I will show how to do it on Ubuntu 14.04. I believe it’s doable on any Linux distributions.
For network bandwidth limiting, we will use a program called trickle
.
- Install
trickle
:
1sudo apt-get install trickle - Usage of
trickle
:1trickle -s -u <up link limit in KB/s> -d <down link limit in KB/s> <path-to-your-program> [args...] - Example: limit the bandwidth for
scp
under 10MB/s
1trickle -s -u 10240 -d 10240 scp ~/graph-data-origin/twitter_rv.net linux15:~/
An alternative for network bandwidth limiting is wondershaper
. But I found that tirckle
works better.
For disk bandwidth limiting, you can use the cgroup
mechanism and its blkio controller.
- Install
cgroup-bin
:
1sudo apt-get install cgroup-bin - Create a cgroup named
limit
underblkio
:
1sudo cgcreate -t qliu -g blkio:limit - Set the read/write limit to 1 MB/s (see below for explanation)
12sudo cgset -r blkio.throttle.read_bps_device="8:0 1000000" /limitsudo cgset -r blkio.throttle.write_bps_device="8:0 1000000" /limit
Here, the format ofblkio.throttle.read_bps_device
andblkio.throttle.write_bps_device
is
1<major>:<minor> <bytes_per_second>
To find out<major>:<minor>
for a certain disk like/dev/sda
, we just run
12$ ls -l /dev/sdabrw-rw---- 1 root disk 8, 0 Sep 9 20:36 /dev/sda
Then the outputdisk 8, 0
means<major>:<minor>
should be8:0
. - Finally, we can limit the disk bandwidth for
scp
withcgexec
(suppose the source file is stored on disk/dev/sda
):
1cgexec -g blkio:limit scp ~/graph-data-origin/twitter_rv.net linux1:~/