memstat_malloc.c (192148) | memstat_malloc.c (224569) |
---|---|
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 unchanged lines hidden (view full) --- 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 * | 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 unchanged lines hidden (view full) --- 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: head/lib/libmemstat/memstat_malloc.c 192148 2009-05-15 18:25:44Z jhb $ | 26 * $FreeBSD: head/lib/libmemstat/memstat_malloc.c 224569 2011-08-01 09:43:35Z pluknet $ |
27 */ 28 29#include <sys/cdefs.h> 30#include <sys/param.h> 31#include <sys/malloc.h> 32#include <sys/sysctl.h> 33 34#include <err.h> --- 56 unchanged lines hidden (view full) --- 91 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 92 return (-1); 93 } 94 if (size != sizeof(maxcpus)) { 95 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 96 return (-1); 97 } 98 | 27 */ 28 29#include <sys/cdefs.h> 30#include <sys/param.h> 31#include <sys/malloc.h> 32#include <sys/sysctl.h> 33 34#include <err.h> --- 56 unchanged lines hidden (view full) --- 91 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 92 return (-1); 93 } 94 if (size != sizeof(maxcpus)) { 95 list->mtl_error = MEMSTAT_ERROR_DATAERROR; 96 return (-1); 97 } 98 |
99 if (maxcpus > MEMSTAT_MAXCPU) { 100 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 101 return (-1); 102 } 103 | |
104 size = sizeof(count); 105 if (sysctlbyname("kern.malloc_count", &count, &size, NULL, 0) < 0) { 106 if (errno == EACCES || errno == EPERM) 107 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 108 else 109 list->mtl_error = MEMSTAT_ERROR_VERSION; 110 return (-1); 111 } --- 43 unchanged lines hidden (view full) --- 155 p += sizeof(*mtshp); 156 157 if (mtshp->mtsh_version != MALLOC_TYPE_STREAM_VERSION) { 158 list->mtl_error = MEMSTAT_ERROR_VERSION; 159 free(buffer); 160 return (-1); 161 } 162 | 99 size = sizeof(count); 100 if (sysctlbyname("kern.malloc_count", &count, &size, NULL, 0) < 0) { 101 if (errno == EACCES || errno == EPERM) 102 list->mtl_error = MEMSTAT_ERROR_PERMISSION; 103 else 104 list->mtl_error = MEMSTAT_ERROR_VERSION; 105 return (-1); 106 } --- 43 unchanged lines hidden (view full) --- 150 p += sizeof(*mtshp); 151 152 if (mtshp->mtsh_version != MALLOC_TYPE_STREAM_VERSION) { 153 list->mtl_error = MEMSTAT_ERROR_VERSION; 154 free(buffer); 155 return (-1); 156 } 157 |
163 if (mtshp->mtsh_maxcpus > MEMSTAT_MAXCPU) { 164 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 165 free(buffer); 166 return (-1); 167 } 168 | |
169 /* 170 * For the remainder of this function, we are quite trusting about 171 * the layout of structures and sizes, since we've determined we have 172 * a matching version and acceptable CPU count. 173 */ 174 maxcpus = mtshp->mtsh_maxcpus; 175 count = mtshp->mtsh_count; 176 for (i = 0; i < count; i++) { 177 mthp = (struct malloc_type_header *)p; 178 p += sizeof(*mthp); 179 180 if (hint_dontsearch == 0) { 181 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, 182 mthp->mth_name); 183 } else 184 mtp = NULL; 185 if (mtp == NULL) 186 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC, | 158 /* 159 * For the remainder of this function, we are quite trusting about 160 * the layout of structures and sizes, since we've determined we have 161 * a matching version and acceptable CPU count. 162 */ 163 maxcpus = mtshp->mtsh_maxcpus; 164 count = mtshp->mtsh_count; 165 for (i = 0; i < count; i++) { 166 mthp = (struct malloc_type_header *)p; 167 p += sizeof(*mthp); 168 169 if (hint_dontsearch == 0) { 170 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, 171 mthp->mth_name); 172 } else 173 mtp = NULL; 174 if (mtp == NULL) 175 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC, |
187 mthp->mth_name); | 176 mthp->mth_name, maxcpus); |
188 if (mtp == NULL) { 189 _memstat_mtl_empty(list); 190 free(buffer); 191 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 192 return (-1); 193 } 194 195 /* 196 * Reset the statistics on a current node. 197 */ | 177 if (mtp == NULL) { 178 _memstat_mtl_empty(list); 179 free(buffer); 180 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 181 return (-1); 182 } 183 184 /* 185 * Reset the statistics on a current node. 186 */ |
198 _memstat_mt_reset_stats(mtp); | 187 _memstat_mt_reset_stats(mtp, maxcpus); |
199 200 for (j = 0; j < maxcpus; j++) { 201 mtsp = (struct malloc_type_stats *)p; 202 p += sizeof(*mtsp); 203 204 /* 205 * Sumarize raw statistics across CPUs into coalesced 206 * statistics. --- 83 unchanged lines hidden (view full) --- 290 291int 292memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle) 293{ 294 struct memory_type *mtp; 295 void *kmemstatistics; 296 int hint_dontsearch, j, mp_maxcpus, ret; 297 char name[MEMTYPE_MAXNAME]; | 188 189 for (j = 0; j < maxcpus; j++) { 190 mtsp = (struct malloc_type_stats *)p; 191 p += sizeof(*mtsp); 192 193 /* 194 * Sumarize raw statistics across CPUs into coalesced 195 * statistics. --- 83 unchanged lines hidden (view full) --- 279 280int 281memstat_kvm_malloc(struct memory_type_list *list, void *kvm_handle) 282{ 283 struct memory_type *mtp; 284 void *kmemstatistics; 285 int hint_dontsearch, j, mp_maxcpus, ret; 286 char name[MEMTYPE_MAXNAME]; |
298 struct malloc_type_stats mts[MEMSTAT_MAXCPU], *mtsp; | 287 struct malloc_type_stats *mts, *mtsp; |
299 struct malloc_type_internal *mtip; 300 struct malloc_type type, *typep; 301 kvm_t *kvm; 302 303 kvm = (kvm_t *)kvm_handle; 304 305 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 306 --- 10 unchanged lines hidden (view full) --- 317 318 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus, 319 sizeof(mp_maxcpus), 0); 320 if (ret != 0) { 321 list->mtl_error = ret; 322 return (-1); 323 } 324 | 288 struct malloc_type_internal *mtip; 289 struct malloc_type type, *typep; 290 kvm_t *kvm; 291 292 kvm = (kvm_t *)kvm_handle; 293 294 hint_dontsearch = LIST_EMPTY(&list->mtl_list); 295 --- 10 unchanged lines hidden (view full) --- 306 307 ret = kread_symbol(kvm, X_MP_MAXCPUS, &mp_maxcpus, 308 sizeof(mp_maxcpus), 0); 309 if (ret != 0) { 310 list->mtl_error = ret; 311 return (-1); 312 } 313 |
325 if (mp_maxcpus > MEMSTAT_MAXCPU) { 326 list->mtl_error = MEMSTAT_ERROR_TOOMANYCPUS; 327 return (-1); 328 } 329 | |
330 ret = kread_symbol(kvm, X_KMEMSTATISTICS, &kmemstatistics, 331 sizeof(kmemstatistics), 0); 332 if (ret != 0) { 333 list->mtl_error = ret; 334 return (-1); 335 } 336 | 314 ret = kread_symbol(kvm, X_KMEMSTATISTICS, &kmemstatistics, 315 sizeof(kmemstatistics), 0); 316 if (ret != 0) { 317 list->mtl_error = ret; 318 return (-1); 319 } 320 |
321 mts = malloc(sizeof(struct malloc_type_stats) * mp_maxcpus); 322 if (mts == NULL) { 323 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 324 return (-1); 325 } 326 |
|
337 for (typep = kmemstatistics; typep != NULL; typep = type.ks_next) { 338 ret = kread(kvm, typep, &type, sizeof(type), 0); 339 if (ret != 0) { 340 _memstat_mtl_empty(list); | 327 for (typep = kmemstatistics; typep != NULL; typep = type.ks_next) { 328 ret = kread(kvm, typep, &type, sizeof(type), 0); 329 if (ret != 0) { 330 _memstat_mtl_empty(list); |
331 free(mts); |
|
341 list->mtl_error = ret; 342 return (-1); 343 } 344 ret = kread_string(kvm, (void *)type.ks_shortdesc, name, 345 MEMTYPE_MAXNAME); 346 if (ret != 0) { 347 _memstat_mtl_empty(list); | 332 list->mtl_error = ret; 333 return (-1); 334 } 335 ret = kread_string(kvm, (void *)type.ks_shortdesc, name, 336 MEMTYPE_MAXNAME); 337 if (ret != 0) { 338 _memstat_mtl_empty(list); |
339 free(mts); |
|
348 list->mtl_error = ret; 349 return (-1); 350 } 351 352 /* 353 * Since our compile-time value for MAXCPU may differ from the 354 * kernel's, we populate our own array. 355 */ 356 mtip = type.ks_handle; 357 ret = kread(kvm, mtip->mti_stats, mts, mp_maxcpus * 358 sizeof(struct malloc_type_stats), 0); 359 if (ret != 0) { 360 _memstat_mtl_empty(list); | 340 list->mtl_error = ret; 341 return (-1); 342 } 343 344 /* 345 * Since our compile-time value for MAXCPU may differ from the 346 * kernel's, we populate our own array. 347 */ 348 mtip = type.ks_handle; 349 ret = kread(kvm, mtip->mti_stats, mts, mp_maxcpus * 350 sizeof(struct malloc_type_stats), 0); 351 if (ret != 0) { 352 _memstat_mtl_empty(list); |
353 free(mts); |
|
361 list->mtl_error = ret; 362 return (-1); 363 } 364 365 if (hint_dontsearch == 0) { 366 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, name); 367 } else 368 mtp = NULL; 369 if (mtp == NULL) 370 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC, | 354 list->mtl_error = ret; 355 return (-1); 356 } 357 358 if (hint_dontsearch == 0) { 359 mtp = memstat_mtl_find(list, ALLOCATOR_MALLOC, name); 360 } else 361 mtp = NULL; 362 if (mtp == NULL) 363 mtp = _memstat_mt_allocate(list, ALLOCATOR_MALLOC, |
371 name); | 364 name, mp_maxcpus); |
372 if (mtp == NULL) { 373 _memstat_mtl_empty(list); | 365 if (mtp == NULL) { 366 _memstat_mtl_empty(list); |
367 free(mts); |
|
374 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 375 return (-1); 376 } 377 378 /* 379 * This logic is replicated from kern_malloc.c, and should 380 * be kept in sync. 381 */ | 368 list->mtl_error = MEMSTAT_ERROR_NOMEMORY; 369 return (-1); 370 } 371 372 /* 373 * This logic is replicated from kern_malloc.c, and should 374 * be kept in sync. 375 */ |
382 _memstat_mt_reset_stats(mtp); | 376 _memstat_mt_reset_stats(mtp, mp_maxcpus); |
383 for (j = 0; j < mp_maxcpus; j++) { 384 mtsp = &mts[j]; 385 mtp->mt_memalloced += mtsp->mts_memalloced; 386 mtp->mt_memfreed += mtsp->mts_memfreed; 387 mtp->mt_numallocs += mtsp->mts_numallocs; 388 mtp->mt_numfrees += mtsp->mts_numfrees; 389 mtp->mt_sizemask |= mtsp->mts_size; 390 --- 18 unchanged lines hidden --- | 377 for (j = 0; j < mp_maxcpus; j++) { 378 mtsp = &mts[j]; 379 mtp->mt_memalloced += mtsp->mts_memalloced; 380 mtp->mt_memfreed += mtsp->mts_memfreed; 381 mtp->mt_numallocs += mtsp->mts_numallocs; 382 mtp->mt_numfrees += mtsp->mts_numfrees; 383 mtp->mt_sizemask |= mtsp->mts_size; 384 --- 18 unchanged lines hidden --- |