cputhrottle lets you limit cpu consumption on osx

I’ve been having some issues with a particular App consuming a serious amount of processing lately, and it’s really been driving me nuts. having 7/8th of your total processing on a macbookpro (2.6 GHz Intel Core 2 Duo w/ 3 GB ram) will make it run really hot. After seeing that my cpu temp was hovering at around 170 degrees, and the cpu fans maxed out at 6000 rpm, I opened Activity Monitor and saw this: (note: the App that’s eating so much CPU is not shown to protect the guilty)
Activity Monitor

So I posted a message on twitter to see if there’s anyone who follows me who might be able to suggest a solution.

if anyone can tell me how to limit the % of cpu a process can be allowed to use on osx, please let me know, and i'll be your best friend.

Then just a short 2 minutes later, Ryan Barr (@ryanbarr) came through and pointed me in the right direction.

cputhrottle is a small OS X command-line utility designed to limit the CPU usage of a process to which it attaches.

I found that even though I have a utility installed to increase the fan speed when CPU temperature increases, sometimes I would run jobs that maxed out the CPU for long periods — and this caused my MacBook to get extremely hot. Within the space of a couple minutes, the temperature displayed had jumped by over 40 degrees Fahrenheit.

The utility takes a process ID (pid) and a percentage amount to limit. For example:

sudo ./cputhrottle 328 25

Running this command would limit process 328 to a maximum of 25% CPU usage.

Note that cputhrottle needs to be run with root privileges to be able to attach to the process to control. I recommend doing this via the sudo command.

Source code is below, as well as a pre-compiled binary (OS X 10.5.1, Intel). If there is any interest in a PPC version I would be willing to cross-compile one. The program requires that Boost 1.33.1 be installed to compile. Run make all to build cputhrottle and a test program.

The source hopefully will be interesting to those looking into how to manipulate processes in Mac OS via mach system calls. The code is a subset of code I was working on to create a simple debugger as an alternative to gdb. Obviously, this project was much simpler and ultimately more useful.

cputhrottle makes use of the task_info, task_suspend, and task_resume calls. task_info and task_threads are used to collect CPU usage statistics on the process, and the program then suspends/resumes the attached process appropriately until the CPU usage has stabilized. Any errors occurring are assumed to be a result of the attached process exiting, and result in cputhrottle also exiting. Control-C is intercepted, and the attached process allowed to resume gracefully before cputhrottle exits.

Source: cputhrottle.tar.gz

Binary: cputhrottle.gz

Note: Thanks to Nikolaj Schumacher for pointing out a race condition whereby a thread being sampled could have ceased to exist after having been enumerated as existing within the process. Specifically, he noticed this while throttling Handbrake, which creates/kills threads in rapid succession. I have applied his patch and the new sources and binary reflect his changes. I tested the fix and cputhrottle behaved as before (for my test case).

Contact email: nolanw, followed by an underbar and then the word “commerce” at willnolan dot com

http://www.willnolan.com/cputhrottle/cputhrottle.html.

5 Responses a “cputhrottle lets you limit cpu consumption on osx”

  1. Hey, that's cool. There are a couple apps I'd really like to throttle.

  2. Hey, that's cool. There are a couple apps I'd really like to throttle.

  3. Hey, that's cool. There are a couple apps I'd really like to throttle.

  4. Ok, with this utility I have to launch a program, find its PID(s) and manually use cputhrottle. Every time I launch it.

    Is there something that allows me to tell the system: “From today always run this program with a maximum of 25% CPU” ?

    Thank you!

  5. how do i run this i have been trying for a while and can’t get it to work will someone post the sudo command part

Leave a comment

Categories

Archives

About

Nater Kane is freelance developer and user experience & technology consultant based in Brooklyn, NY.

Nater's focus is to make the web a better place, one decision at a time.

He likes to spend time playing with his cats, riding bicycles around the city, working on his diesel vw rabbit or motorcycle, and enjoying a decent espresso.

RSS

Twitter » What I'm Up To

Elsewhere

JavaScript JS Documentation: JS String toUpperCase, JavaScript String toUpperCase, JS String .toUpperCase, JavaScript String .toUpperCase

Basecamp

Meta


Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206