1 /*- 2 * Copyright (c) 2005-2006 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/cpuset.h> 31 #include <sys/sysctl.h> 32 33 #define LIBMEMSTAT /* Cause vm_page.h not to include opt_vmpage.h */ 34 #include <vm/vm.h> 35 #include <vm/vm_page.h> 36 37 #include <vm/uma.h> 38 #include <vm/uma_int.h> 39 40 #include <err.h> 41 #include <errno.h> 42 #include <kvm.h> 43 #include <nlist.h> 44 #include <stddef.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include "memstat.h" 51 #include "memstat_internal.h" 52 53 static struct nlist namelist[] = { 54 #define X_UMA_KEGS 0 55 { .n_name = "_uma_kegs" }, 56 #define X_MP_MAXID 1 57 { .n_name = "_mp_maxid" }, 58 #define X_ALL_CPUS 2 59 { .n_name = "_all_cpus" }, 60 { .n_name = "" }, 61 }; 62 63 /* 64 * Extract uma(9) statistics from the running kernel, and store all memory 65 * type information in the passed list. For each type, check the list for an 66 * existing entry with the right name/allocator -- if present, update that 67 * entry. Otherwise, add a new entry. On error, the entire list will be 68 * cleared, as entries will be in an inconsistent state. 69 * 70 * To reduce the level of work for a list that starts empty, we keep around a 71 * hint as to whether it was empty when we began, so we can avoid searching 72 * the list for entries to update. Updates are O(n^2) due to searching for 73 * each entry before adding it. 74 */ 75 int 76 memstat_sysctl_uma(struct memory_type_list *list, int flags) 77 { 78 struct uma_stream_header *ushp; 79 struct uma_type_header *uthp; 80 struct uma_percpu_stat *upsp; 81 struct memory_type *mtp; 82 int count, hint_dontsearch, i, j, maxcpus; 83 char *buffer, *p; 84 size_t size; 85 86 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 87 88 /* 89 * Query the number of CPUs, number of malloc types so that we can 90 * guess an initial buffer size. We loop until we succeed or really 91 * fail. Note that the value of maxcpus we query using sysctl is not 92 * the version we use when processing the real data -- that is read 93 * from the header. 94 */ 95 retry: 96 size = sizeof(maxcpus); 97 if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) { 98 if (errno == EACCES || errno == EPERM) 99 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 100 else 101 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 102 return (-1); 103 } 104 if (size != sizeof(maxcpus)) { 105 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 106 return (-1); 107 } 108 109 if (maxcpus > MEMSTAT_MAXCPU) { 110 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 111 return (-1); 112 } 113 114 size = sizeof(count); 115 if (sysctlbyname("vm.zone_count", &count, &size, NULL, 0) < 0) { 116 if (errno == EACCES || errno == EPERM) 117 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 118 else 119 list->mtl_error = MEMSTAT_ERROR_VERSION; 120 return (-1); 121 } 122 if (size != sizeof(count)) { 123 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 124 return (-1); 125 } 126 127 size = sizeof(*uthp) + count * (sizeof(*uthp) + sizeof(*upsp) * 128 maxcpus); 129 130 buffer = malloc(size); 131 if (buffer == NULL) { 132 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 133 return (-1); 134 } 135 136 if (sysctlbyname("vm.zone_stats", buffer, &size, NULL, 0) < 0) { 137 /* 138 * XXXRW: ENOMEM is an ambiguous return, we should bound the 139 * number of loops, perhaps. 140 */ 141 if (errno == ENOMEM) { 142 free(buffer); 143 goto retry; 144 } 145 if (errno == EACCES || errno == EPERM) 146 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 147 else 148 list->mtl_error = MEMSTAT_ERROR_VERSION; 149 free(buffer); 150 return (-1); 151 } 152 153 if (size == 0) { 154 free(buffer); 155 return (0); 156 } 157 158 if (size < sizeof(*ushp)) { 159 list->mtl_error = MEMSTAT_ERROR_VERSION; 160 free(buffer); 161 return (-1); 162 } 163 p = buffer; 164 ushp = (struct uma_stream_header *)p; 165 p += sizeof(*ushp); 166 167 if (ushp->ush_version != UMA_STREAM_VERSION) { 168 list->mtl_error = MEMSTAT_ERROR_VERSION; 169 free(buffer); 170 return (-1); 171 } 172 173 if (ushp->ush_maxcpus > MEMSTAT_MAXCPU) { 174 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 175 free(buffer); 176 return (-1); 177 } 178 179 /* 180 * For the remainder of this function, we are quite trusting about 181 * the layout of structures and sizes, since we've determined we have 182 * a matching version and acceptable CPU count. 183 */ 184 maxcpus = ushp->ush_maxcpus; 185 count = ushp->ush_count; 186 for (i = 0; i < count; i++) { 187 uthp = (struct uma_type_header *)p; 188 p += sizeof(*uthp); 189 190 if (hint_dontsearch == 0) { 191 mtp = memstat_mtl_find(list, ALLOCATOR_UMA, 192 uthp->uth_name); 193 } else 194 mtp = NULL; 195 if (mtp == NULL) 196 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA, 197 uthp->uth_name); 198 if (mtp == NULL) { 199 _memstat_mtl_empty(list); 200 free(buffer); 201 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 202 return (-1); 203 } 204 205 /* 206 * Reset the statistics on a current node. 207 */ 208 _memstat_mt_reset_stats(mtp); 209 210 mtp->mt_numallocs = uthp->uth_allocs; 211 mtp->mt_numfrees = uthp->uth_frees; 212 mtp->mt_failures = uthp->uth_fails; 213 mtp->mt_sleeps = uthp->uth_sleeps; 214 215 for (j = 0; j < maxcpus; j++) { 216 upsp = (struct uma_percpu_stat *)p; 217 p += sizeof(*upsp); 218 219 mtp->mt_percpu_cache[j].mtp_free = 220 upsp->ups_cache_free; 221 mtp->mt_free += upsp->ups_cache_free; 222 mtp->mt_numallocs += upsp->ups_allocs; 223 mtp->mt_numfrees += upsp->ups_frees; 224 } 225 226 mtp->mt_size = uthp->uth_size; 227 mtp->mt_memalloced = mtp->mt_numallocs * uthp->uth_size; 228 mtp->mt_memfreed = mtp->mt_numfrees * uthp->uth_size; 229 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed; 230 mtp->mt_countlimit = uthp->uth_limit; 231 mtp->mt_byteslimit = uthp->uth_limit * uthp->uth_size; 232 233 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; 234 mtp->mt_zonefree = uthp->uth_zone_free; 235 236 /* 237 * UMA secondary zones share a keg with the primary zone. To 238 * avoid double-reporting of free items, report keg free 239 * items only in the primary zone. 240 */ 241 if (!(uthp->uth_zone_flags & UTH_ZONE_SECONDARY)) { 242 mtp->mt_kegfree = uthp->uth_keg_free; 243 mtp->mt_free += mtp->mt_kegfree; 244 } 245 mtp->mt_free += mtp->mt_zonefree; 246 } 247 248 free(buffer); 249 250 return (0); 251 } 252 253 static int 254 kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size, 255 size_t offset) 256 { 257 ssize_t ret; 258 259 ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address, 260 size); 261 if (ret < 0) 262 return (MEMSTAT_ERROR_KVM); 263 if ((size_t)ret != size) 264 return (MEMSTAT_ERROR_KVM_SHORTREAD); 265 return (0); 266 } 267 268 static int 269 kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen) 270 { 271 ssize_t ret; 272 int i; 273 274 for (i = 0; i < buflen; i++) { 275 ret = kvm_read(kvm, (unsigned long)kvm_pointer + i, 276 &(buffer[i]), sizeof(char)); 277 if (ret < 0) 278 return (MEMSTAT_ERROR_KVM); 279 if ((size_t)ret != sizeof(char)) 280 return (MEMSTAT_ERROR_KVM_SHORTREAD); 281 if (buffer[i] == '\0') 282 return (0); 283 } 284 /* Truncate. */ 285 buffer[i-1] = '\0'; 286 return (0); 287 } 288 289 static int 290 kread_symbol(kvm_t *kvm, int index, void *address, size_t size, 291 size_t offset) 292 { 293 ssize_t ret; 294 295 ret = kvm_read(kvm, namelist[index].n_value + offset, address, size); 296 if (ret < 0) 297 return (MEMSTAT_ERROR_KVM); 298 if ((size_t)ret != size) 299 return (MEMSTAT_ERROR_KVM_SHORTREAD); 300 return (0); 301 } 302 303 /* 304 * memstat_kvm_uma() is similar to memstat_sysctl_uma(), only it extracts 305 * UMA(9) statistics from a kernel core/memory file. 306 */ 307 int 308 memstat_kvm_uma(struct memory_type_list *list, void *kvm_handle) 309 { 310 LIST_HEAD(, uma_keg) uma_kegs; 311 struct memory_type *mtp; 312 struct uma_bucket *ubp, ub; 313 struct uma_cache *ucp, *ucp_array; 314 struct uma_zone *uzp, uz; 315 struct uma_keg *kzp, kz; 316 int hint_dontsearch, i, mp_maxid, ret; 317 char name[MEMTYPE_MAXNAME]; 318 cpuset_t all_cpus; 319 long cpusetsize; 320 kvm_t *kvm; 321 322 kvm = (kvm_t *)kvm_handle; 323 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 324 if (kvm_nlist(kvm, namelist) != 0) { 325 list->mtl_error = MEMSTAT_ERROR_KVM; 326 return (-1); 327 } 328 if (namelist[X_UMA_KEGS].n_type == 0 || 329 namelist[X_UMA_KEGS].n_value == 0) { 330 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL; 331 return (-1); 332 } 333 ret = kread_symbol(kvm, X_MP_MAXID, &mp_maxid, sizeof(mp_maxid), 0); 334 if (ret != 0) { 335 list->mtl_error = ret; 336 return (-1); 337 } 338 ret = kread_symbol(kvm, X_UMA_KEGS, &uma_kegs, sizeof(uma_kegs), 0); 339 if (ret != 0) { 340 list->mtl_error = ret; 341 return (-1); 342 } 343 cpusetsize = sysconf(_SC_CPUSET_SIZE); 344 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t)) { 345 list->mtl_error = MEMSTAT_ERROR_KVM_NOSYMBOL; 346 return (-1); 347 } 348 CPU_ZERO(&all_cpus); 349 ret = kread_symbol(kvm, X_ALL_CPUS, &all_cpus, cpusetsize, 0); 350 if (ret != 0) { 351 list->mtl_error = ret; 352 return (-1); 353 } 354 ucp_array = malloc(sizeof(struct uma_cache) * (mp_maxid + 1)); 355 if (ucp_array == NULL) { 356 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 357 return (-1); 358 } 359 for (kzp = LIST_FIRST(&uma_kegs); kzp != NULL; kzp = 360 LIST_NEXT(&kz, uk_link)) { 361 ret = kread(kvm, kzp, &kz, sizeof(kz), 0); 362 if (ret != 0) { 363 free(ucp_array); 364 _memstat_mtl_empty(list); 365 list->mtl_error = ret; 366 return (-1); 367 } 368 for (uzp = LIST_FIRST(&kz.uk_zones); uzp != NULL; uzp = 369 LIST_NEXT(&uz, uz_link)) { 370 ret = kread(kvm, uzp, &uz, sizeof(uz), 0); 371 if (ret != 0) { 372 free(ucp_array); 373 _memstat_mtl_empty(list); 374 list->mtl_error = ret; 375 return (-1); 376 } 377 ret = kread(kvm, uzp, ucp_array, 378 sizeof(struct uma_cache) * (mp_maxid + 1), 379 offsetof(struct uma_zone, uz_cpu[0])); 380 if (ret != 0) { 381 free(ucp_array); 382 _memstat_mtl_empty(list); 383 list->mtl_error = ret; 384 return (-1); 385 } 386 ret = kread_string(kvm, uz.uz_name, name, 387 MEMTYPE_MAXNAME); 388 if (ret != 0) { 389 free(ucp_array); 390 _memstat_mtl_empty(list); 391 list->mtl_error = ret; 392 return (-1); 393 } 394 if (hint_dontsearch == 0) { 395 mtp = memstat_mtl_find(list, ALLOCATOR_UMA, 396 name); 397 } else 398 mtp = NULL; 399 if (mtp == NULL) 400 mtp = _memstat_mt_allocate(list, ALLOCATOR_UMA, 401 name); 402 if (mtp == NULL) { 403 free(ucp_array); 404 _memstat_mtl_empty(list); 405 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 406 return (-1); 407 } 408 /* 409 * Reset the statistics on a current node. 410 */ 411 _memstat_mt_reset_stats(mtp); 412 mtp->mt_numallocs = uz.uz_allocs; 413 mtp->mt_numfrees = uz.uz_frees; 414 mtp->mt_failures = uz.uz_fails; 415 mtp->mt_sleeps = uz.uz_sleeps; 416 if (kz.uk_flags & UMA_ZFLAG_INTERNAL) 417 goto skip_percpu; 418 for (i = 0; i < mp_maxid + 1; i++) { 419 if (!CPU_ISSET(i, &all_cpus)) 420 continue; 421 ucp = &ucp_array[i]; 422 mtp->mt_numallocs += ucp->uc_allocs; 423 mtp->mt_numfrees += ucp->uc_frees; 424 425 if (ucp->uc_allocbucket != NULL) { 426 ret = kread(kvm, ucp->uc_allocbucket, 427 &ub, sizeof(ub), 0); 428 if (ret != 0) { 429 free(ucp_array); 430 _memstat_mtl_empty(list); 431 list->mtl_error = ret; 432 return (-1); 433 } 434 mtp->mt_free += ub.ub_cnt; 435 } 436 if (ucp->uc_freebucket != NULL) { 437 ret = kread(kvm, ucp->uc_freebucket, 438 &ub, sizeof(ub), 0); 439 if (ret != 0) { 440 free(ucp_array); 441 _memstat_mtl_empty(list); 442 list->mtl_error = ret; 443 return (-1); 444 } 445 mtp->mt_free += ub.ub_cnt; 446 } 447 } 448 skip_percpu: 449 mtp->mt_size = kz.uk_size; 450 mtp->mt_memalloced = mtp->mt_numallocs * mtp->mt_size; 451 mtp->mt_memfreed = mtp->mt_numfrees * mtp->mt_size; 452 mtp->mt_bytes = mtp->mt_memalloced - mtp->mt_memfreed; 453 if (kz.uk_ppera > 1) 454 mtp->mt_countlimit = kz.uk_maxpages / 455 kz.uk_ipers; 456 else 457 mtp->mt_countlimit = kz.uk_maxpages * 458 kz.uk_ipers; 459 mtp->mt_byteslimit = mtp->mt_countlimit * mtp->mt_size; 460 mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; 461 for (ubp = LIST_FIRST(&uz.uz_full_bucket); ubp != 462 NULL; ubp = LIST_NEXT(&ub, ub_link)) { 463 ret = kread(kvm, ubp, &ub, sizeof(ub), 0); 464 mtp->mt_zonefree += ub.ub_cnt; 465 } 466 if (!((kz.uk_flags & UMA_ZONE_SECONDARY) && 467 LIST_FIRST(&kz.uk_zones) != uzp)) { 468 mtp->mt_kegfree = kz.uk_free; 469 mtp->mt_free += mtp->mt_kegfree; 470 } 471 mtp->mt_free += mtp->mt_zonefree; 472 } 473 } 474 free(ucp_array); 475 return (0); 476 } 477