Debian Codegraph @

agent/mibgroup/hardware/memory/memory_darwin.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/memory.h>
5
6 #include <dirent.h>
7 #include <unistd.h>
8
9 #include <sys/stat.h>
10 #include <sys/sysctl.h>
11
12 #include <mach/mach.h>
13 #include <mach/mach_time.h>
14 #include <CoreFoundation/CoreFoundation.h>
15 #include <IOKit/ps/IOPowerSources.h>
16
17 /*
18 * Retained from UCD implementation
19 */
20
21 /*
22 * Get the number of pages that are swapped out.
23 * We think this is correct and are valid values
24 * but not sure. Time will tell if it's correct.
25 * Note: this routine is _expensive_!!! we run this
26 * as little as possible by caching it's return so
27 * it's not run on every poll.
28 * Apple, please give us a better way! :)
29 */
30 int pages_swapped(void) {
31 boolean_t retval;
32 kern_return_t error;
33 processor_set_t *psets, pset;
34 task_t *tasks;
35 unsigned i, j, pcnt, tcnt;
36 int pid;
37 mach_msg_type_number_t count;
38 vm_address_t address;
39 mach_port_t object_name;
40 vm_region_extended_info_data_t info;
41 vm_size_t size;
42 mach_port_t mach_port;
43 int swapped_pages;
44 int swapped_pages_total = 0;
45 char errmsg[1024];
46
47
48 mach_port = mach_host_self();
49 error = host_processor_sets(mach_port, &psets, &pcnt);
50 if (error != KERN_SUCCESS) {
51 snmp_log(LOG_ERR, "Error in host_processor_sets(): %s\n", mach_error_string(error));
52 return(0);
53 }
54
55 for (i = 0; i < pcnt; i++) {
56 error = host_processor_set_priv(mach_port, psets[i], &pset);
57 if (error != KERN_SUCCESS) {
58 snprintf(errmsg, sizeof(errmsg),"Error in host_processor_set_priv(): %s\n", mach_error_string(error));
59 snmp_log_perror(errmsg);
60 return(0);
61 }
62
63 error = processor_set_tasks(pset, &tasks, &tcnt);
64 if (error != KERN_SUCCESS) {
65 snprintf(errmsg, sizeof(errmsg),"Error in processor_set_tasks(): %s\n", mach_error_string(error));
66 snmp_log_perror(errmsg);
67 return(0);
68 }
69
70 for (j = 0; j < tcnt; j++) {
71 error = pid_for_task(tasks[j], &pid);
72 if (error != KERN_SUCCESS) {
73 /* Not a process, or the process is gone. */
74 continue;
75 }
76
77 swapped_pages = 0;
78 for (address = 0;; address += size) {
79 kern_return_t ret = KERN_FAILURE;
80
81 /* Get memory region. */
82 count = VM_REGION_EXTENDED_INFO_COUNT;
83 #ifdef HAVE_VM_REGION_64
84 ret = vm_region_64(tasks[j], &address, &size,
85 VM_REGION_EXTENDED_INFO, (void *)&info, &count,
86 &object_name);
87 #elif defined(HAVE_VM_REGION)
88 ret = vm_region(tasks[j], &address, &size,
89 VM_REGION_EXTENDED_INFO, (void *)&info, &count,
90 &object_name);
91 #else
92 #error How to query memory protection information?
93 #endif
94 if (ret != KERN_SUCCESS) {
95 /* No more memory regions. */
96 break;
97 }
98
99 if(info.pages_swapped_out > 0) {
100 swapped_pages += info.pages_swapped_out;
101 }
102 }
103
104 if(swapped_pages > 0) {
105 swapped_pages_total += swapped_pages;
106 }
107
108 if (tasks[j] != mach_task_self()) {
109 mach_port_deallocate(mach_task_self(), tasks[j]);
110 }
111 }
112 }
113
114 return(swapped_pages_total);
115 }
116
117 off_t
118 swapsize(void)
119 {
120 int pagesize;
121 int i, n;
122 DIR *dirp;
123 struct dirent *dp;
124 struct stat buf;
125 char errmsg[1024];
126 char full_name[1024];
127 off_t swapSize;
128
129 /* we set the size to -1 if we're not supported */
130 swapSize = -1;
131
132 #if defined(SWAPFILE_DIR) && defined(SWAPFILE_PREFIX)
133 dirp = opendir((const char *) SWAPFILE_DIR);
134 while((dp = readdir(dirp)) != NULL) {
135 /* if the file starts with the same as SWAPFILE_PREFIX
136 * we want to stat the file to get it's size
137 */
138 if(strspn(dp->d_name,(char *) SWAPFILE_PREFIX) == strlen((char *) SWAPFILE_PREFIX)) {
139 snprintf(full_name, sizeof(full_name),"%s/%s",SWAPFILE_DIR,dp->d_name);
140 /* we need to stat each swapfile to get it's size */
141 if(stat(full_name,&buf) != 0) {
142 snprintf(errmsg, sizeof(errmsg), "swapsize: can't stat file %s",full_name);
143 snmp_log_perror(errmsg);
144 } else {
145 /* total swap allocated is the size of
146 * all the swapfile's that exist in
147 * the SWAPFILE_DIR dir
148 */
149 swapSize += buf.st_size;
150 }
151 }
152
153 }
154 closedir(dirp);
155
156 #else
157 /* we set the size to -1 if we're not supported */
158 swapSize = -1;
159 #endif
160
161 return swapSize;
162 }
163
164 /*
165 * Load the latest memory usage statistics
166 *
167 * HW_PHYSMEM is capped at 2 Gigs so we use HW_MEMSIZE
168 */
169 int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {
170
171 netsnmp_memory_info *mem;
172
173 uint64_t phys_mem; /* bytes */
174 size_t phys_mem_size = sizeof(phys_mem);
175 int phys_mem_mib[] = { CTL_HW, HW_MEMSIZE };
176
177 int pagesize; /* bytes */
178 size_t pagesize_size = sizeof(pagesize);
179 int pagesize_mib[] = { CTL_HW, HW_PAGESIZE };
180
181 uint64_t pages_used;
182 off_t swapSize;
183 off_t swapUsed;
184 vm_statistics_data_t vm_stat;
185 unsigned int count = HOST_VM_INFO_COUNT;
186
187 sysctl(phys_mem_mib, 2, &phys_mem, &phys_mem_size, NULL, 0);
188 sysctl(pagesize_mib, 2, &pagesize, &pagesize_size, NULL, 0);
189 host_statistics(mach_host_self(),HOST_VM_INFO,(host_info_t)&vm_stat,&count);
190 pages_used = vm_stat.active_count + vm_stat.inactive_count
191 + vm_stat.wire_count;
192 swapSize = swapsize(); /* in bytes */
193 swapUsed = pages_swapped();
194
195 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
196 if (!mem) {
197 snmp_log_perror("No Memory info entry");
198 } else {
199 if (!mem->descr)
200 mem->descr = strdup( "Physical memory" );
201 mem->units = pagesize; /* 4096 */
202 mem->size = phys_mem/pagesize;
203 mem->free = (phys_mem/pagesize) - pages_used;
204 mem->other = -1;
205 }
206
207 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
208 if (!mem) {
209 snmp_log_perror("No Swap info entry");
210 } else {
211 if (!mem->descr)
212 mem->descr = strdup( "Swap space" );
213 mem->units = pagesize; /* 4096 */
214 mem->size = swapSize/pagesize;
215 mem->free = (swapSize/pagesize) - swapUsed;
216 mem->other = -1;
217 }
218
219 /*
220 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MISC, 1 );
221 if (!mem) {
222 snmp_log_perror("No Buffer, etc info entry");
223 } else {
224 mem->units = pagesize;
225 mem->size = -1;
226 mem->free = (phys_mem - pages_used) + (swapSize - swapUsed);
227 mem->other = -1;
228 }
229 */
230 return 0;
231 }