Re: System resources and number of operations on an NSOperationQueue
Re: System resources and number of operations on an NSOperationQueue
- Subject: Re: System resources and number of operations on an NSOperationQueue
- From: James Bucanek <email@hidden>
- Date: Sun, 14 Nov 2010 09:16:03 -0700
Antonio Nunes <mailto:email@hidden> wrote (Sunday,
November 14, 2010 2:58 AM -0000):
The issue I am seeing is that the operation queue appears to indiscriminately
run operations as they are added to the queue, regardless of system resources,
thus bringing the whole system to a crawl. I thought, from reading the
documentation, that the queue would be intelligent about how many operations
it would have running at the same time, depending on system resources such as
number of cores and available memory. Since this doesn't seem to be the case,
I have to assume something is not quite right with my implementation.
NSOperationQueue uses Grand Central Dispatch, which will attempt
to load-balance the number of running operations based on _CPU_
resources. However, that doesn't mean GCD will (or even can)
automatically find the most efficient run schedule for your code.
My question: how can I ensure that no more operations are launched at any one
time than the system can handle?
-[NSOperationQueue setMaxConcurrentOperationCount:]
Also, is there a way to find out the number
of cores on a machine so that I can set that as a hard limit of number of
operations on an NSOperationQueue?
#include <unistd.h>
#include <sys/sysctl.h>
static unsigned int sNumberOfCPUs;
int name[2];
size_t len = sizeof(sNumberOfCPUs);
u_int value;
name[0] = CTL_HW;
name[1] = HW_NCPU;
if (sysctl(name,2,&value,&len,NULL,0)==0 &&
len==sizeof(sNumberOfCPUs) && value!=0)
{
sNumberOfCPUs = value;
[Logger log:kLogDebug format:@"detected %u CPUs",sNumberOfCPUs];
}
else
{
sNumberOfCPUs = 1;
[Logger log:kLogCurious message:@"Unable to determine the
number of CPU cores; assuming 1"];
}
}
Personally, if your operation is mostly I/O bound, the best
solution is to set maxConcurrentOperationCount to 1 and run the
operations sequentially. The biggest problem in trying get
better performance from I/O heavy processes is the HD trashing
that occurs when you have more than one working simultaneously.
This can easily mean that two operations will run slower than one.
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden