views:

1185

answers:

4

My application running on Mac OS X that needs to retrieve details about the machine it is running on to report for system information. One of the items I need is details about the processor(s) installed in the computer.

My code currently works, but is far from an ideal solution, in fact I consider it a bad solution, but I have had no luck in finding a better one.

The information I report currently and after some formatting looks like:

Processor: Intel Core 2 Duo 2.1 GHz, Family 6 Model 23 Stepping 6

All of the info I get is through command-line utilities called from a popen(). The readable part of the processor description is taken from the "system_profiler" command output and the Family, Model, and Stepping values are taken from the "sysctl" command.

These command-line utilities must be getting there information from somewhere. I'm wondering if there is an programmatic interface available to get this same info?


Related:

+3  A: 

sysctl(3) is probably a good place to start. You probably want the stuff defined by the CTL_HW selectors.

D.Shawley
I remember looking at the sysctl() method with the CTL_HW selector that you mention, but it does not appear to provide the Family, Model, and Stepping integer values that I am getting now and I think it only provides a very generic x86 type of description of the processor. The sysctlbyname() might have better luck though. I'm not sure if I ever looked closely at that. I will investigate and report my findings. Thanks!
brader
+5  A: 
Jim Dovey
A: 

Hello, If you specifically want CPU information then use cpuid (in C __asm cpuid) instruction. It gives all possible information of a CPU including its family, model, company, number of cores etc. Primarily all APIs use this instruction to retrieve CPU information. You can get detailed information on CPUID on the web, including sample code and tutorials.

Neeraj Shah
+1  A: 

Use sysctlbyname rather than sysctl, e.g.

#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/sysctl.h>

uint64_t get_cpu_freq(void)
{
    uint64_t freq = 0;
    size_t size = sizeof(freq);

    if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0)
    {
        perror("sysctl");
    }
    return freq;
}

You can get a list of the names that can be passed to systctlbyname by looking at the output of sysctl -a from the command line.

Paul R