building again. cool. incidentally, i think it's the changes that needed to be made...
[cpuHistory.git] / CPUInfo.m
blob374bf116ede4a2f8cb4c9ead04a5fbf72b0fd2a2
1 //
2 //  CPUInfo.m
3 //  CPU Usage
4 //
5 //  Created by Peter Hosey on 2006-06-21.
6 //  Copyright 2006 Peter Hosey. All rights reserved.
7 //
8 //  Modified by Christopher Bowns, starting 2007-1-1.
10 #ifndef NSLOG_DEBUG
11 #define NSLOG_DEBUG
12 #endif
14 #import "CPUInfo.h"
16 #include <sys/types.h>
17 //sqrtf, ceilf
18 #include <math.h>
19 //sysctl and its parameters
20 #include <sys/sysctl.h>
21 //errno, strerror
22 #include <sys/errno.h>
23 #include <string.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");
36         }
37         
38         
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];
44         
45         inUse = user + sys + nice;
46         total = inUse + idle;
47                 
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;
52         dbluser++;
53         dblsys++;
54         dblnice++;
55         dblidle++;
56         // return processorInfo;
59 - (CPUInfo *) initWithCapacity:(unsigned)numItems
61         self = [super init];
62         
63         /*
64                 from CPU usage
65         */      
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);
73 //      if(status != 0) {
74                 numCPUs = 1; // TODO we're going to assume one CPU for the moment.
75 //              NSLog(@"%s error status, assuming one CPU", _cmd);
76 //      }       
77         /*
78                 from meminfo
79         */
80         size = numItems;
81         cpudata = calloc(numItems, sizeof(CPUData));
82         if (cpudata == NULL) {
83                 NSLog (@"Failed to allocate buffer for CPUInfo");
84                 return (nil);
85         }
86         inptr = 0;
87         outptr = -1;
88         
89         getCPUStat(&lastcpustat, &numlastcpustat);
91         return (self);
94 - (void)refresh
96         processor_info_array_t cpustat;
97         mach_msg_type_number_t numcpustat;
98         getCPUStat(&cpustat, &numcpustat);
99         /*
100                 TODO make this multicore. First, we're gonna need a multicore machine to test it on.
101         */
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];
109         
110         inUse = user + sys + nice;
111         total = inUse + idle;
112                 
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;
117         
118         if(lastcpustat) {
119                 size_t lastcpustatSize = sizeof(integer_t) * numlastcpustat;
120                 vm_deallocate(mach_task_self(), (vm_address_t)lastcpustat, lastcpustatSize);
121         }
123         lastcpustat = cpustat;
124         numlastcpustat = numcpustat;
125         
126         if (++inptr >= size) // advance our data ptr
127                 inptr = 0;
131 - (void)startIterate
133         outptr = inptr;
137 - (BOOL)getNext:(CPUDataPtr)ptr
139         if (outptr == -1)
140                 return (FALSE);
141         *ptr = cpudata[outptr++];
142         if (outptr >= size)
143                 outptr = 0;
144         if (outptr == inptr)
145                 outptr = -1;
146         return (TRUE);
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];
162 - (int)getSize
164         return (size);
167 @end