Limit a threaded Linux app's CPU consumption

I rarely bother to "rate limit" the amount of CPU my Linux processes can do, but since I unfortunately have to share my desktop with some CPU intensive tasks, sometimes it makes sense to do so, for multi-threaded applications at least. Linux has, for some time now, a feature to set CPU affinity for a process. This is btw something that Sun/Solaris have had for almost as long as I can remember. With this feature, you can specify which CPUs (cores) a process is allowed to use.

As an example, lets assume we want the application traffic_server to only consume core 2 and 3, you'd simply start the application with

# taskset -c 2,3 traffic_server

Now, some applications have this built in (unfortuantely traffic_server does not, yet), and there are APIs in linux to control this (see the man page for sched_setaffinity() ). You can of course also "nice" your application accordingly, but the above lets you get a somewhat more controlled behavior (again, for threaded apps that can use more than one core).

Hacking: 

Comments

Re: Limit a threaded Linux app's CPU consumption

Funny that you should post about taskset. Recently I've employed this utility to manage some of my production servers/processes. I used this to set processor affinity to just one CPU... I did this to stay compliant with our vendor (per CPU) agreement/license. While I haven't verified this is acceptable to my vendor, to me it would seem to be a solid case. Any thoughts about using taskset this way?

Either way, it's nice to see others posting about this extremely useful but little known utility.

Thanks,
medi

Re: Limit a threaded Linux app's CPU consumption

That seems very reasonable to me. I know from experience that on boxes sharing the resources, it can make a (small, but noticeable) difference on performance. It avoids the kernel from rescheduling on a different core, which it generally is good at anyways, but it has no guarantees. And as we know, L1/L2 cache efficiency is important for performance.