5 // Created by Peter Hosey on 2006-06-21.
6 // Copyright 2006 Peter Hosey. All rights reserved.
8 // Modified by Christopher Bowns, starting 2007-1-1.
16 #include <sys/types.h>
19 //sysctl and its parameters
20 #include <sys/sysctl.h>
22 #include <sys/errno.h>
25 @implementation CPUInfo
27 static processor_info_array_t getCPUStat (processor_info_array_t *cpustat, mach_msg_type_number_t *numcpustat)
29 processor_info_array_t processorInfo;
30 natural_t numProcessors_nobodyCares = 0U;
31 mach_msg_type_number_t numProcessorInfo;
33 kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, (natural_t *)&numProcessors_nobodyCares, (processor_info_array_t *)&processorInfo, (mach_msg_type_number_t *)&numProcessorInfo);
34 if(err != KERN_SUCCESS) {
35 NSLog(@"getCPUStat: failed to get cpu statistics");
39 unsigned int inUse, total, user, sys, nice, idle;
40 user = processorInfo[CPU_STATE_USER];
41 sys = processorInfo[CPU_STATE_SYSTEM];
42 nice = processorInfo[CPU_STATE_NICE];
43 idle = processorInfo[CPU_STATE_IDLE];
45 inUse = user + sys + nice;
48 double dbluser = (double)user / (double)total;
49 double dblsys = (double)sys / (double)total;
50 double dblnice = (double)nice / (double)total;
51 double dblidle = (double)idle / (double)total;
56 - (CPUInfo *) initWithCapacity:(unsigned)numItems
63 //We could get the number of processors the same way that we get the CPU usage info, but that allocates memory.
64 /* enum { miblen = 2U };
65 int mib[miblen] = { CTL_HW, HW_NCPU };
66 size_t sizeOfNumCPUs = sizeof(numCPUs);
67 int status = sysctl(mib, miblen,
68 &numCPUs, &sizeOfNumCPUs,
69 */ /*newp*/ // NULL, /*newlen*/ 0U);
71 numCPUs = 1; // TODO we're going to assume one CPU for the moment.
72 // NSLog(@"%s error status, assuming one CPU", _cmd);
78 cpudata = calloc(numItems, sizeof(CPUData));
79 if (cpudata == NULL) {
80 NSLog (@"Failed to allocate buffer for CPUInfo");
86 getCPUStat(lastcpustat, numlastcpustat);
93 processor_info_array_t cpustat;
95 cpustat = getCPUStat();
97 TODO make this multicore. First, we're gonna need a multicore machine to test it on.
99 // for(unsigned i = 0U; i < numCPUs; ++i) {
101 unsigned int inUse, total, user, sys, nice, idle;
102 user = cpustat[CPU_STATE_USER];
103 sys = cpustat[CPU_STATE_SYSTEM];
104 nice = cpustat[CPU_STATE_NICE];
105 idle = cpustat[CPU_STATE_IDLE];
107 inUse = user + sys + nice;
108 total = inUse + idle;
110 cpudata[inptr].user = (double)user / (double)total;
111 cpudata[inptr].sys = (double)sys / (double)total;
112 cpudata[inptr].nice = (double)nice / (double)total;
113 cpudata[inptr].idle = (double)idle / (double)total;
115 size_t lastcpustatSize = sizeof(integer_t) * numLastProcessorInfo;
116 vm_deallocate(target_task, (vm_address_t)lastProcessorInfo, lastProcessorInfoSize);
119 lastProcessorInfo = processorInfo;
120 numLastProcessorInfo = numProcessorInfo;
121 lastcpustat = cpustat;
122 if (++inptr >= size) // advance our data ptr
133 - (BOOL)getNext:(CPUDataPtr)ptr
137 *ptr = cpudata[outptr++];
146 - (void)getCurrent:(CPUDataPtr)ptr
148 *ptr = cpudata[inptr ? inptr - 1 : size - 1];
152 - (void)getLast:(CPUDataPtr)ptr
154 *ptr = cpudata[inptr > 1 ? inptr - 2 : size + inptr - 2];