taskpaper. trying to undo attempted proj rename.
[cpuHistory.git] / MemInfo.m
blob5fad89cffef6939a5ed6d93ef6b1484e164fb776
1 /*
2  *      CPU Mon
3  *      Christopher Bowns, 2008
4  *      
5  *      Formerly: Memory Monitor, by Bernhard Baehr
6  *
7  *      Copyright © 2001-2002 Bernhard Baehr
8  *
9  *      MemInfo.m - Memory Usage History Container Class
10  *
11  *      This program is free software; you can redistribute it and/or modify
12  *      it under the terms of the GNU General Public License as published by
13  *      the Free Software Foundation; either version 2 of the License, or
14  *      (at your option) any later version.
15  *
16  *      This program is distributed in the hope that it will be useful,
17  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *      GNU General Public License for more details.
20  *
21  *      You should have received a copy of the GNU General Public License
22  *      along with this program; if not, write to the Free Software
23  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
27 #import "mach/mach_host.h"
28 #import "MemInfo.h"
31 @implementation MemInfo
34 static void getVMStat (vm_statistics_t vmstat)
36         unsigned count = HOST_VM_INFO_COUNT;
37         
38         if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) vmstat, &count) != KERN_SUCCESS)
39                 NSLog (@"Failed to get VM statistics.");
43 - (MemInfo *) initWithCapacity:(unsigned)numItems
45         self = [super init];
46         size = numItems;
47         vmdata = calloc(numItems, sizeof(VMData));
48         if (vmdata == NULL) {
49                 NSLog (@"Failed to allocate buffer for MemInfo");
50                 return (nil);
51         }
52         inptr = 0;
53         outptr = -1;
54         getVMStat (&lastvmstat);
55         return (self);
59 - (void)refresh
61         vm_statistics_data_t    vmstat;
62         double                  total;
63         
64         getVMStat (&vmstat);
65         total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
66         vmdata[inptr].wired = vmstat.wire_count / total;
67         vmdata[inptr].active = vmstat.active_count / total;
68         vmdata[inptr].inactive = vmstat.inactive_count / total;
69         vmdata[inptr].free = vmstat.free_count / total;
70         vmdata[inptr].pageins =  vmstat.pageins - lastvmstat.pageins;
71         vmdata[inptr].pageouts = vmstat.pageouts - lastvmstat.pageouts;
72         lastvmstat = vmstat;
73         if (++inptr >= size)
74                 inptr = 0;
78 - (void)startIterate
80         outptr = inptr;
84 - (BOOL)getNext:(VMDataPtr)ptr
86         if (outptr == -1)
87                 return (FALSE);
88         *ptr = vmdata[outptr++];
89         if (outptr >= size)
90                 outptr = 0;
91         if (outptr == inptr)
92                 outptr = -1;
93         return (TRUE);
97 - (void)getCurrent:(VMDataPtr)ptr
99         *ptr = vmdata[inptr ? inptr - 1 : size - 1];
103 - (void)getLast:(VMDataPtr)ptr
105         *ptr = vmdata[inptr > 1 ? inptr - 2 : size + inptr - 2];
109 - (int)getSize
111         return (size);
115 @end