Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for a way to limit a processes disk io to a set speed limit. Ideally the program would work similar to this:

$ limitio --pid 32423 --write-limit 1M

Limiting process 32423 to 1 megabyte per second hard drive writing speed.

share|improve this question
    
If i have your problem, write a little C program that do it.Of course at first google for its code. –  Mohsen Pahlevanzadeh Sep 14 '12 at 4:52

2 Answers 2

up vote 8 down vote accepted

That is certainly not trivial task that can't be done in userspace. Fortunately, it is possible to do on Linux, using cgroup mechanizm and its blkio controller.

Setting up cgroup is somehow distribution specific as it may already be mounted or even used somewhere. Here's general idea, however (assuming you have proper kernel configuration):

mount tmpfs cgroup_root /sys/fs/cgroup
mkdir /sys/fs/cgroup/blkio
mount -t cgroup -o blkio none /sys/fs/cgroup/blkio

Now that you have blkio controller set, you can use it:

mkdir -p /sys/fs/cgroup/blkio/limit1M/
echo "X:Y  1048576" > /sys/fs/cgroup/blkio/limit1M/blkio.throttle.write_bps_device 

Now you have a cgroup limit1M that limits write speed on device with major/minor numbers X:Y to 1MB/s. As you can see, this limit is per device. All you have to do now is to put some process inside of that group and it should be limited:

echo $PID > /sys/fs/cgroup/blkio/limit1M/tasks

I don't know if/how this can be done on other operating systems.

share|improve this answer

ionice from the util-linux does something similar to what you want.

It doesn't set absolute IO limits, it sets IO priority and 'niceness' - similar to what nice does for a process' CPU priority.

From the man page:

ionice - set or get process I/O scheduling class and priority

DESCRIPTION
This  program  sets or gets the I/O scheduling class and priority for a
program.  If no arguments or just -p is given, ionice  will  query  the
current I/O scheduling class and priority for that process.

When  command  is  given,  ionice  will run this command with the given
arguments.  If no class is specified, then  command  will  be  executed
with the "best-effort" scheduling class.  The default priority level is 4.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.