[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemor
From: |
Fred Kiefer |
Subject: |
Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory |
Date: |
Thu, 13 Nov 2008 09:42:18 +0100 |
User-agent: |
Thunderbird 2.0.0.17 (X11/20080922) |
Having an implementation for these methods would be nice, but going down
the /proc/something parsing route seems wrong to me. Why not call
sysconf() with _SC_NPROCESSORS_CONF (on my GUN/Linux system this is
defined in /usr/include/bits/confname.h).
This too is a system specific solution, but at least we use glibc
instead of doing the parsing ourselves. This should save us the problem
of adopting to a new kernel every time.
We still will need different solutions for non-Linux systems.
Fred
Scott Christley wrote:
> Hello,
>
> Here is my suggestion for implementation of these methods in
> NSProcessInfo. This works on systems with /proc. I don't have access
> too many different OS'es, but this works fine on an AMD64 with 16 cores
> and 16GB memory running Redhat.
>
> I think the ability to dynamically enable/disable cores is a somewhat OS
> specific task, so I just have -activeProcessorCount return the number of
> processors. Maybe somebody knows more about this?
>
> It also might be nice to add these lines into Testing/nsprocessinfo.m to
> easily display the results:
>
> printf("Processor count: %d\n", [pi processorCount]);
> printf("Active processor count: %d\n", [pi activeProcessorCount]);
> printf("Physical memory: %llu\n", [pi physicalMemory]);
>
>
> cheers
> Scott
>
>
>
> - (NSUInteger) processorCount
> {
> int i;
> static NSUInteger procCount = 0;
>
> if (procCount != 0) return procCount;
>
> NSFileManager *fileManager = [NSFileManager defaultManager];
>
> if ([fileManager fileExistsAtPath: @"/proc/cpuinfo"]) {
> NSString *cpuInfo = [NSString stringWithContentsOfFile:
> @"/proc/cpuinfo"];
> NSArray *a = [cpuInfo componentsSeparatedByCharactersInSet:
> [NSCharacterSet whitespaceAndNewlineCharacterSet]];
> // syntax is processor : #
> // count up each one
> for (i = 0; i < [a count]; ++i) {
> if ([[a objectAtIndex: i] isEqualToString: @"processor"])
> if (((i+1) < [a count]) && [[a objectAtIndex: i+1]
> isEqualToString: @":"])
> ++procCount;
> }
> } else {
> NSLog(@"Cannot determine processor count.");
> }
>
> // if unable to determine, failsafe to one processor
> if (procCount == 0) procCount = 1;
>
> return procCount;
> }
>
> - (NSUInteger) activeProcessorCount
> {
> return [self processorCount];
> }
>
> - (unsigned long long) physicalMemory
> {
> static NSUInteger availMem = 0;
>
> if (availMem != 0) return availMem;
>
> NSFileManager *fileManager = [NSFileManager defaultManager];
>
> if ([fileManager fileExistsAtPath: @"/proc/meminfo"]) {
> NSString *memInfo = [NSString stringWithContentsOfFile:
> @"/proc/meminfo"];
> NSRange r = [memInfo rangeOfString: @"MemTotal:"];
> if (r.location == NSNotFound) {
> NSLog(@"Cannot determine amount of physical memory.");
> return 0;
> }
> NSString *s = [[memInfo substringFromIndex: (r.location + r.length)]
> stringByTrimmingCharactersInSet: [NSCharacterSet
> whitespaceAndNewlineCharacterSet]];
> NSArray *a = [s componentsSeparatedByString: @" "];
> s = [a objectAtIndex: 0];
> availMem = (NSUInteger)[s longLongValue];
> availMem *= 1024;
> } else {
> NSLog(@"Cannot determine amount of physical memory.");
> }
>
> return availMem;
> }
- NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Scott Christley, 2008/11/12
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory,
Fred Kiefer <=
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Richard Frith-Macdonald, 2008/11/13
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Fred Kiefer, 2008/11/13
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Richard Frith-Macdonald, 2008/11/13
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Wolfgang Lux, 2008/11/14
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Pete French, 2008/11/14
- Re: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory, Pete French, 2008/11/13