discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory


From: Scott Christley
Subject: NSProcessInfo -processorCount, -activeProcessorCount, -physicalMemory
Date: Wed, 12 Nov 2008 18:21:21 -0800

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;
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]