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 void 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 // return processorInfo;
59 - (CPUInfo *) initWithCapacity:(unsigned)numItems
66 //We could get the number of processors the same way that we get the CPU usage info, but that allocates memory.
67 /* enum { miblen = 2U };
68 int mib[miblen] = { CTL_HW, HW_NCPU };
69 size_t sizeOfNumCPUs = sizeof(numCPUs);
70 int status = sysctl(mib, miblen,
71 &numCPUs, &sizeOfNumCPUs,
72 */ /*newp*/ // NULL, /*newlen*/ 0U);
74 numCPUs = 1; // TODO we're going to assume one CPU for the moment.
75 // NSLog(@"%s error status, assuming one CPU", _cmd);
81 cpudata = calloc(numItems, sizeof(CPUData));
82 if (cpudata == NULL) {
83 NSLog (@"Failed to allocate buffer for CPUInfo");
89 getCPUStat(&lastcpustat, &numlastcpustat);
96 processor_info_array_t cpustat;
97 mach_msg_type_number_t numcpustat;
98 getCPUStat(&cpustat, &numcpustat);
100 TODO make this multicore. First, we're gonna need a multicore machine to test it on.
102 // for(unsigned i = 0U; i < numCPUs; ++i) {
104 unsigned int inUse, total, user, sys, nice, idle;
105 user = cpustat[CPU_STATE_USER];
106 sys = cpustat[CPU_STATE_SYSTEM];
107 nice = cpustat[CPU_STATE_NICE];
108 idle = cpustat[CPU_STATE_IDLE];
110 inUse = user + sys + nice;
111 total = inUse + idle;
113 cpudata[inptr].user = (double)user / (double)total;
114 cpudata[inptr].sys = (double)sys / (double)total;
115 cpudata[inptr].nice = (double)nice / (double)total;
116 cpudata[inptr].idle = (double)idle / (double)total;
119 size_t lastcpustatSize = sizeof(integer_t) * numlastcpustat;
120 vm_deallocate(mach_task_self(), (vm_address_t)lastcpustat, lastcpustatSize);
123 lastcpustat = cpustat;
124 numlastcpustat = numcpustat;
126 if (++inptr >= size) // advance our data ptr
137 - (BOOL)getNext:(CPUDataPtr)ptr
141 *ptr = cpudata[outptr++];
150 - (void)getCurrent:(CPUDataPtr)ptr
152 *ptr = cpudata[inptr ? inptr - 1 : size - 1];
156 - (void)getLast:(CPUDataPtr)ptr
158 *ptr = cpudata[inptr > 1 ? inptr - 2 : size + inptr - 2];