1 /*- 2 * Copyright (c) 2005 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/sysctl.h> 31 32 #define LIBMEMSTAT /* Cause vm_page.h not to include opt_vmpage.h */ 33 #include <vm/vm.h> 34 #include <vm/vm_page.h> 35 36 #include <vm/uma.h> 37 #include <vm/uma_int.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <kvm.h> 42 #include <nlist.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "memstat.h" 48 #include "memstat_internal.h" 49 50 static struct nlist namelist[] = { 51 #define X_UMA_KEGS 0 52 { .n_name = "_uma_kegs" }, 53 #define X_MP_MAXID 1 54 { .n_name = "_mp_maxid" }, 55 { .n_name = "" }, 56 }; 57 58 /* 59 * Extract uma(9) statistics from the running kernel, and store all memory 60 * type information in the passed list. For each type, check the list for an 61 * existing entry with the right name/allocator -- if present, update that 62 * entry. Otherwise, add a new entry. On error, the entire list will be 63 * cleared, as entries will be in an inconsistent state. 64 * 65 * To reduce the level of work for a list that starts empty, we keep around a 66 * hint as to whether it was empty when we began, so we can avoid searching 67 * the list for entries to update. Updates are O(n^2) due to searching for 68 * each entry before adding it. 69 */ 70 int 71 memstat_sysctl_uma(struct memory_type_list *list, int flags) 72 { 73 struct uma_stream_header *ushp; 74 struct uma_type_header *uthp; 75 struct uma_percpu_stat *upsp; 76 struct memory_type *mtp; 77 int count, hint_dontsearch, i, j, maxcpus; 78 char *buffer, *p; 79 size_t size; 80 81 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 82 83 /* 84 * Query the number of CPUs, number of malloc types so that we can 85 * guess an initial buffer size. We loop until we succeed or really 86 * fail. Note that the value of maxcpus we query using sysctl is not 87 * the version we use when processing the real data -- that is read 88 * from the header. 89 */ 90 retry: 91 size = sizeof(maxcpus); 92 if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) { 93 if (errno == EACCES || errno == EPERM) 94 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 95 else 96 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 97 return (-1); 98 } 99 if (size != sizeof(maxcpus)) { 100 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 101 return (-1); 102 } 103 104 if (maxcpus > MEMSTAT_MAXCPU) { 105 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 106 return (-1); 107 } 108 109 size = sizeof(count); 110 if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) { 111 if (errno == EACCES || errno == EPERM) 112 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 113 else 114 list->mtl_error = MEMSTAT_ERROR_VERSION; 115 return (-1); 116 } 117 if (size != sizeof(count)) { 118 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 119 return (-1); 120 } 121 122 size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) * 123 maxcpus); 124 125 buffer = malloc(size); 126 if (buffer == NULL) { 127 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 128 return (-1); 129 } 130 131 if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) { 132 /* 133 * XXXRW: ENOMEM is an ambiguous return, we should bound the 134 * number of loops, perhaps. 135 */ 136 if (errno == ENOMEM) { 137 free(buffer); 138 goto retry; 139 } 140 if (errno == EACCES || errno == EPERM) 141 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 142 else 143 list->mtl_error = MEMSTAT_ERROR_VERSION; 144 free(buffer); 145 return (-1); 146 } 147 148 if (size == 0) { 149 free(buffer); 150 return (0); 151 } 152 153 if (size < sizeof(*ushp)) { 154 list->mtl_error = MEMSTAT_ERROR_VERSION; 155 free(buffer); 156 return (-1); 157 } 158 p = buffer; 159 ushp = (struct uma_stream_header *)p; 160 p += sizeof(*ushp); 161 162 if (ushp->ush_version != UMA_STREAM_VERSION) { 163 list->mtl_error = MEMSTAT_ERROR_VERSION; 164 free(buffer); 165 return (-1); 166 } 167 168 if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) { 169 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 170 free(buffer); 171 return (-1); 172 } 173 174 /* 175 * For the remainder of this function, we are quite trusting about 176 * the layout of structures and sizes, since we've determined we have 177 * a matching version and acceptable CPU count. 178 */ 179 maxcpus = ushp->ush_maxcpus; 180 count = ushp->ush_count; 181 for (i = 0; i < count; i++) { 182 uthp = (struct uma_type_header *)p; 183 p += sizeof(*uthp); 184 185 if (hint_dontsearch == 0) { 186 mtp = memstat_mtl_find(list, ALLOCATOR_UMA, 187 uthp->uth_name); 188 } else 189 mtp = NULL; 190 if (mtp == NULL) 191 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA, 192 uthp->uth_name); 193 if (mtp == NULL) { 194 _memstat_mtl_empty(list); 195 free(buffer); 196 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 197 return (-1); 198 } 199 200 /* 201 * Reset the statistics on a current node. 202 */ 203 _memstat_mt_reset_stats(mtp); 204 205 mtp->mt_numallocs = uthp->uth_allocs; 206 mtp->mt_numfrees = uthp->uth_frees; 207 mtp->mt_failures = uthp->uth_fails; 208 209 for (j = 0; j < maxcpus; j++) { 210 upsp = (struct uma_percpu_stat *)p; 211 p += sizeof(*upsp); 212 213 mtp->mt_percpu_cache[j].mtp_free = 214 upsp->ups_cache_free; 215 mtp->mt_free += upsp->ups_cache_free; 216 mtp->mt_numallocs += upsp->ups_allocs; 217 mtp->mt_numfrees += upsp->ups_frees; 218 } 219 220 mtp->mt_size = uthp->uth_size; 221 mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size; 222 mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size; 223 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed; 224 mtp->mt_countlimit = uthp->uth_limit; 225 mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size; 226 227 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; 228 mtp->mt_zonefree = uthp->uth_zone_free; 229 230 /* 231 * UMA secondary zones share a keg with the primary zone. To 232 * avoid double-reporting of free items, report keg free 233 * items only in the primary zone. 234 */ 235 if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) { 236 mtp->mt_kegfree = uthp->uth_keg_free; 237 mtp->mt_free += mtp->mt_kegfree; 238 } 239 mtp->mt_free += mtp->mt_zonefree; 240 } 241 242 free(buffer); 243 244 return (0); 245 } 246 247 static int 248 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size, 249 size_t offset) 250 { 251 ssize_t ret; 252 253 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address, 254 size); 255 if (ret < 0) 256 return (MEMSTAT_ERROR_KVM); 257 if ((size_t)ret != size) 258 return (MEMSTAT_ERROR_KVM_SHORTREAD); 259 return (0); 260 } 261 262 static int 263 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen) 264 { 265 ssize_t ret; 266 int i; 267 268 for (i = 0; i < buflen; i++) { 269 ret = kvm_read(kvm, (unsigned long)kvm_pointer + i, 270 &(buffer[i]), sizeof(char)); 271 if (ret < 0) 272 return (MEMSTAT_ERROR_KVM); 273 if ((size_t)ret != sizeof(char)) 274 return (MEMSTAT_ERROR_KVM_SHORTREAD); 275 if (buffer[i] == '\0') 276 return (0); 277 } 278 /* Truncate. */ 279 buffer[i-1] = '\0'; 280 return (0); 281 } 282 283 static int 284 kread_symbol(kvm_t *kvm, int index, void *address, size_t size, 285 size_t offset) 286 { 287 ssize_t ret; 288 289 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size); 290 if (ret < 0) 291 return (MEMSTAT_ERROR_KVM); 292 if ((size_t)ret != size) 293 return (MEMSTAT_ERROR_KVM_SHORTREAD); 294 return (0); 295 } 296 297 /* 298 * memstat_kvm_uma() is similar to memstat_sysctl_uma(), only it extracts 299 * UMA(9) statistics from a kernel core/memory file. 300 */ 301 int 302 memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle) 303 { 304 static LIST_HEAD(, uma_keg) uma_kegs; 305 struct memory_type *mtp; 306 struct uma_bucket *ubp, ub; 307 struct uma_cache *ucp; 308 struct uma_zone *uzp, uz; 309 struct uma_keg *kzp, kz; 310 int hint_dontsearch, i, mp_maxid, ret; 311 char name[MEMTYPE_MAXNAME]; 312 kvm_t *kvm; 313 314 kvm = (kvm_t *)kvm_handle; 315 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 316 if (kvm_nlist(kvm, namelist) != 0) { 317 list->mtl_error = MEMSTAT_ERROR_KVM; 318 return (-1); 319 } 320 if (namelist[X_UMA_KEGS].n_type == 0 || 321 namelist[X_UMA_KEGS].n_value == 0) { 322 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL; 323 return (-1); 324 } 325 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0); 326 if (ret != 0) { 327 list->mtl_error = ret; 328 return (-1); 329 } 330 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0); 331 if (ret != 0) { 332 list->mtl_error = ret; 333 return (-1); 334 } 335 for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp = 336 LIST_NEXT(&kz, uk_link)) { 337 ret = kread(kvm, kzp, &kz, sizeof(kz), 0); 338 if (ret != 0) { 339 _memstat_mtl_empty(list); 340 list->mtl_error = ret; 341 return (-1); 342 } 343 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp = 344 LIST_NEXT(&uz, uz_link)) { 345 ret = kread(kvm, uzp, &uz, sizeof(uz), 0); 346 if (ret != 0) { 347 _memstat_mtl_empty(list); 348 list->mtl_error = ret; 349 return (-1); 350 } 351 ret = kread_string(kvm, uz.uz_name, name, 352 MEMTYPE_MAXNAME); 353 if (ret != 0) { 354 _memstat_mtl_empty(list); 355 list->mtl_error = ret; 356 return (-1); 357 } 358 if (hint_dontsearch == 0) { 359 mtp = memstat_mtl_find(list, ALLOCATOR_UMA, 360 name); 361 } else 362 mtp = NULL; 363 if (mtp == NULL) 364 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA, 365 name); 366 if (mtp == NULL) { 367 _memstat_mtl_empty(list); 368 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 369 return (-1); 370 } 371 /* 372 * Reset the statistics on a current node. 373 */ 374 _memstat_mt_reset_stats(mtp); 375 mtp->mt_numallocs = uz.uz_allocs; 376 mtp->mt_numfrees = uz.uz_frees; 377 mtp->mt_failures = uz.uz_fails; 378 if (kz.uk_flags & UMA_ZFLAG_INTERNAL) 379 goto skip_percpu; 380 for (i = 0; i < mp_maxid + 1; i++) { 381 ucp = &uz.uz_cpu[i]; 382 mtp->mt_numallocs += ucp->uc_allocs; 383 mtp->mt_numfrees += ucp->uc_frees; 384 385 if (ucp->uc_allocbucket != NULL) { 386 ret = kread(kvm, ucp->uc_allocbucket, 387 &ub, sizeof(ub), 0); 388 if (ret != 0) { 389 _memstat_mtl_empty(list); 390 list->mtl_error = 391 MEMSTAT_ERROR_NOMEMORY; 392 return (-1); 393 } 394 mtp->mt_free += ub.ub_cnt; 395 } 396 if (ucp->uc_freebucket != NULL) { 397 ret = kread(kvm, ucp->uc_freebucket, 398 &ub, sizeof(ub), 0); 399 if (ret != 0) { 400 _memstat_mtl_empty(list); 401 list->mtl_error = 402 MEMSTAT_ERROR_NOMEMORY; 403 return (-1); 404 } 405 mtp->mt_free += ub.ub_cnt; 406 } 407 } 408 skip_percpu: 409 mtp->mt_size = kz.uk_size; 410 mtp->mt_memalloced = mtp->mt_numallocs * mtp->mt_size; 411 mtp->mt_memfreed = mtp->mt_numfrees * mtp->mt_size; 412 mtp->mt_bytes = mtp->mt_memalloced = mtp->mt_memfreed; 413 if (kz.uk_ppera > 1) 414 mtp->mt_countlimit = kz.uk_maxpages / 415 kz.uk_ipers; 416 else 417 mtp->mt_countlimit = kz.uk_maxpages * 418 kz.uk_ipers; 419 mtp->mt_byteslimit = mtp->mt_countlimit * mtp->mt_size; 420 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; 421 for (ubp = LIST_FIRST(&uz.uz_full_bucket); ubp != 422 NULL; ubp = LIST_NEXT(&ub, ub_link)) { 423 ret = kread(kvm, ubp, &ub, sizeof(ub), 0); 424 mtp->mt_zonefree += ub.ub_cnt; 425 } 426 if (!((kz.uk_flags & UMA_ZONE_SECONDARY) && 427 LIST_FIRST(&kz.uk_zones) != uzp)) { 428 mtp->mt_kegfree = kz.uk_free; 429 mtp->mt_free += mtp->mt_kegfree; 430 } 431 mtp->mt_free += mtp->mt_zonefree; 432 } 433 } 434 return (0); 435 } 436