grah, just figured out pointer issues.
[cpuHistory.git] / CPUInfo.m
blob68286f1d068f379a47b2462f01284fbc05aec204
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)
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, &numProcessors_nobodyCares, &processorInfo, &numProcessorInfo);
34         if(err == KERN_SUCCESS) {
35                 cpustat = processorInfo;
36         }
37         else {
38                 NSLog(@"getCPUStat: failed to get cpu statistics");
39         }
42 - (CPUInfo *) initWithCapacity:(unsigned)numItems
44         self = [super init];
45         
46         /*
47                 from CPU usage
48         */      
49         //We could get the number of processors the same way that we get the CPU usage info, but that allocates memory.
50 /*      enum { miblen = 2U };
51         int mib[miblen] = { CTL_HW, HW_NCPU };
52         size_t sizeOfNumCPUs = sizeof(numCPUs);
53         int status = sysctl(mib, miblen,
54                    &numCPUs, &sizeOfNumCPUs,
55 */                 /*newp*/ // NULL, /*newlen*/ 0U);
56 //      if(status != 0) {
57                 numCPUs = 1; // TODO we're going to assume one CPU for the moment.
58 //              NSLog(@"%s error status, assuming one CPU", _cmd);
59 //      }       
60         /*
61                 from meminfo
62         */
63         size = numItems;
64         cpudata = calloc(numItems, sizeof(CPUData));
65         if (cpudata == NULL) {
66                 NSLog (@"Failed to allocate buffer for CPUInfo");
67                 return (nil);
68         }
69         inptr = 0;
70         outptr = -1;
71         getCPUStat (lastcpustat);
73         return (self);
76 - (void)refresh
78         processor_info_array_t cpustat;
79                 
80         getCPUStat (cpustat);
81         /*
82                 TODO make this multicore. First, we're gonna need a multicore machine to test it on.
83         */
84         // for(unsigned i = 0U; i < numCPUs; ++i) {
85         unsigned i = 0U;
86         
87         double inUse, total, user, sys, nice, idle;
88         user = cpustat[CPU_STATE_USER];
89         sys  = cpustat[CPU_STATE_SYSTEM];
90         nice = cpustat[CPU_STATE_NICE];
91         idle = cpustat[CPU_STATE_IDLE];
92         
93         inUse = user + sys + nice;
94         total = inUse + idle;
95         
96         #ifdef NSLOG_DEBUG
97         NSLog(@"%s in use: %f   idle: %f", _cmd, inUse, idle);
98         #endif
99         
100         cpudata[inptr].user = (double)user;
101         cpudata[inptr].sys = (double)sys;
102         cpudata[inptr].nice = (double)nice;
103         cpudata[inptr].idle = (double)idle;
104         lastcpustat = cpustat;
105         if (++inptr >= size) // advance our data ptr
106                 inptr = 0;
110 - (void)startIterate
112         outptr = inptr;
116 - (BOOL)getNext:(CPUDataPtr)ptr
118         if (outptr == -1)
119                 return (FALSE);
120         *ptr = cpudata[outptr++];
121         if (outptr >= size)
122                 outptr = 0;
123         if (outptr == inptr)
124                 outptr = -1;
125         return (TRUE);
129 - (void)getCurrent:(CPUDataPtr)ptr
131         *ptr = cpudata[inptr ? inptr - 1 : size - 1];
135 - (void)getLast:(CPUDataPtr)ptr
137         *ptr = cpudata[inptr > 1 ? inptr - 2 : size + inptr - 2];
141 - (int)getSize
143         return (size);
146 @end