views:

321

answers:

4

I have an objective-c application for OS X that compares two sqlite DB's and produces a diff in json format. The db are quite large (10,000 items with many fields). Sometimes this applications runs in about 55 sec(using 95% of the cpu). Sometimes it takes around 8 min (using 12% of the cpu). This is with the same DB's. When it is only using a small portion of the cpu the rest is available. There does not appear to be anything taking priority over the process. Adding "nice -20" on the command seems to assure I get the cpu usage. My questions are

  1. If nothing else is using the cpu why does my app not take advantage of it?

  2. Is there something I can do programatically to change this?

  3. Is there something I can do to OS X to change this?

+1  A: 

It sounds like you're IO bound in the long cases. Are you doing anything else on the machine? The CPU isn't throttling itself - it's definitely waiting for something.

You can use some of the developer tools to look at your app while it's running - perhaps most useful would be "Instruments", which is a GUI on top of dtrace. You should have this installed if you're running the most recent Xcode. You can also use Shark, which is somewhat easier to use at first glance, but less informative in the long run.

Nick Bastin
I absolutely disagree on the comment on shark. Shark is in my opinion by far the best tool to measure System performance, especially tracing system calls, page faults etc. And, unlike Instruments, it shows correct thread state when sampling.
Nikolai Ruhe
Maybe, but Joe doesn't *want* "system performance", at least not if I can judge the problem properly. What he wants is application characterization, which is best gained through Instruments. On the other hand, I could be completely wrong about judging the problem.
Nick Bastin
+1  A: 

Usually you get all the performance that's available. If the CPU is not at 100% there's something blocking it. In case of databases it's often locking. Use Shark to find out what's going on in your application.

Nikolai Ruhe
+4  A: 

Question 1:

Since, I assume, you have to read in the databases from disk, you aren't making full use of the CPU because your code is blocking on disk reads. On Mac OS X there is a lot of stuff running in the background that doesn't use a lot of CPU time but does send out a lot of disk reads, like Spotlight.

Question 2:

Probably not, other than make the most efficient use of disk access possible.

Question 3:

Shut down any other processes that are accessing the disk. This includes many system processes that you really shouldn't shut down, so I don't think there's much you can do here other than try running it on Darwin without all the Mac OS X fanciness.

Gordon Worley
Another possibility: you're pulling the database into memory and it's paging out and in as you access it because there's other stuff running that needs the memory, too. You get your fast runs when there's nothing else using the memory and the whole database fits in memory. It all depends on your code to read the databases.
Gordon Worley
+1  A: 

When your program uses little CPU, probably because it is waiting for disk, especially when other processes are accessing to the disk at the same time. Another possibility is your program uses too much memory and the OS begins to use swap space.