2012-06-07 08:41:59 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-09-04 05:45:53 +00:00
|
|
|
#ifndef MOZ_JEMALLOC4
|
2012-12-07 08:32:24 +00:00
|
|
|
# error Should only compile this file when building with jemalloc 3
|
|
|
|
#endif
|
|
|
|
|
2012-12-07 08:32:24 +00:00
|
|
|
#define MOZ_JEMALLOC_IMPL
|
|
|
|
|
2012-12-07 08:32:24 +00:00
|
|
|
#include "mozmemory_wrap.h"
|
2012-06-07 08:41:59 +00:00
|
|
|
#include "jemalloc_types.h"
|
2012-12-07 08:32:24 +00:00
|
|
|
#include "mozilla/Types.h"
|
2012-06-07 08:41:59 +00:00
|
|
|
|
2014-12-19 14:15:00 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2012-10-17 14:36:49 +00:00
|
|
|
#if defined(MOZ_NATIVE_JEMALLOC)
|
2012-12-07 08:32:24 +00:00
|
|
|
|
|
|
|
MOZ_IMPORT_API int
|
|
|
|
je_(mallctl)(const char*, void*, size_t*, void*, size_t);
|
|
|
|
MOZ_IMPORT_API int
|
|
|
|
je_(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp);
|
|
|
|
MOZ_IMPORT_API int
|
|
|
|
je_(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
|
|
|
|
MOZ_IMPORT_API int
|
2014-12-17 00:14:48 +00:00
|
|
|
je_(nallocx)(size_t size, int flags);
|
2012-12-07 08:32:24 +00:00
|
|
|
|
2012-10-17 14:36:49 +00:00
|
|
|
#else
|
2012-12-07 08:32:24 +00:00
|
|
|
# include "jemalloc/jemalloc.h"
|
2012-10-17 14:36:49 +00:00
|
|
|
#endif
|
2012-06-07 08:41:59 +00:00
|
|
|
|
2012-10-18 14:09:07 +00:00
|
|
|
/*
|
|
|
|
* CTL_* macros are from memory/jemalloc/src/src/stats.c with changes:
|
|
|
|
* - drop `t' argument to avoid redundancy in calculating type size
|
|
|
|
* - require `i' argument for arena number explicitly
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CTL_GET(n, v) do { \
|
|
|
|
size_t sz = sizeof(v); \
|
2012-12-07 08:32:24 +00:00
|
|
|
je_(mallctl)(n, &v, &sz, NULL, 0); \
|
2012-10-18 14:09:07 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define CTL_I_GET(n, v, i) do { \
|
|
|
|
size_t mib[6]; \
|
|
|
|
size_t miblen = sizeof(mib) / sizeof(mib[0]); \
|
|
|
|
size_t sz = sizeof(v); \
|
2012-12-07 08:32:24 +00:00
|
|
|
je_(mallctlnametomib)(n, mib, &miblen); \
|
2012-10-18 14:09:07 +00:00
|
|
|
mib[2] = i; \
|
2012-12-07 08:32:24 +00:00
|
|
|
je_(mallctlbymib)(mib, miblen, &v, &sz, NULL, 0); \
|
2012-10-18 14:09:07 +00:00
|
|
|
} while (0)
|
|
|
|
|
2014-12-19 14:15:00 +00:00
|
|
|
#define CTL_IJ_GET(n, v, i, j) do { \
|
|
|
|
size_t mib[6]; \
|
|
|
|
size_t miblen = sizeof(mib) / sizeof(mib[0]); \
|
|
|
|
size_t sz = sizeof(v); \
|
|
|
|
je_(mallctlnametomib)(n, mib, &miblen); \
|
|
|
|
mib[2] = i; \
|
|
|
|
mib[4] = j; \
|
|
|
|
je_(mallctlbymib)(mib, miblen, &v, &sz, NULL, 0); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VARIABLE_ARRAY is copied from
|
|
|
|
* memory/jemalloc/src/include/jemalloc/internal/jemalloc_internal.h.in
|
|
|
|
*/
|
|
|
|
#if __STDC_VERSION__ < 199901L
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# include <malloc.h>
|
|
|
|
# define alloca _alloca
|
|
|
|
# else
|
2014-12-23 17:32:00 +00:00
|
|
|
# ifdef HAVE_ALLOCA_H
|
2014-12-19 14:15:00 +00:00
|
|
|
# include <alloca.h>
|
|
|
|
# else
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# define VARIABLE_ARRAY(type, name, count) \
|
|
|
|
type *name = alloca(sizeof(type) * (count))
|
|
|
|
#else
|
|
|
|
# define VARIABLE_ARRAY(type, name, count) type name[(count)]
|
|
|
|
#endif
|
|
|
|
|
2012-12-07 08:32:24 +00:00
|
|
|
MOZ_MEMORY_API size_t
|
|
|
|
malloc_good_size_impl(size_t size)
|
|
|
|
{
|
2014-12-17 00:14:48 +00:00
|
|
|
/* je_nallocx crashes when given a size of 0. As
|
2012-12-07 08:32:24 +00:00
|
|
|
* malloc_usable_size(malloc(0)) and malloc_usable_size(malloc(1))
|
|
|
|
* return the same value, use a size of 1. */
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
2014-12-17 00:14:48 +00:00
|
|
|
return je_(nallocx)(size, 0);
|
2012-12-07 08:32:24 +00:00
|
|
|
}
|
2012-10-17 14:36:49 +00:00
|
|
|
|
2015-08-20 01:05:29 +00:00
|
|
|
static void
|
|
|
|
compute_bin_unused_and_bookkeeping(jemalloc_stats_t *stats, unsigned int narenas)
|
2014-12-19 14:15:00 +00:00
|
|
|
{
|
|
|
|
size_t bin_unused = 0;
|
|
|
|
|
|
|
|
uint32_t nregs; // number of regions per run in the j-th bin
|
|
|
|
size_t reg_size; // size of regions served by the j-th bin
|
|
|
|
size_t curruns; // number of runs belonging to a bin
|
|
|
|
size_t curregs; // number of allocated regions in a bin
|
|
|
|
|
|
|
|
unsigned int nbins; // number of bins per arena
|
|
|
|
unsigned int i, j;
|
|
|
|
|
2015-08-20 01:05:29 +00:00
|
|
|
size_t stats_metadata;
|
|
|
|
size_t stats_ametadata = 0; // total internal allocations in all arenas
|
|
|
|
|
2014-12-19 14:15:00 +00:00
|
|
|
// narenas also counts uninitialized arenas, and initialized arenas
|
|
|
|
// are not guaranteed to be adjacent
|
|
|
|
VARIABLE_ARRAY(bool, initialized, narenas);
|
|
|
|
size_t isz = sizeof(initialized) / sizeof(initialized[0]);
|
|
|
|
|
|
|
|
je_(mallctl)("arenas.initialized", initialized, &isz, NULL, 0);
|
|
|
|
CTL_GET("arenas.nbins", nbins);
|
|
|
|
|
|
|
|
for (j = 0; j < nbins; j++) {
|
|
|
|
CTL_I_GET("arenas.bin.0.nregs", nregs, j);
|
|
|
|
CTL_I_GET("arenas.bin.0.size", reg_size, j);
|
|
|
|
|
|
|
|
for (i = 0; i < narenas; i++) {
|
|
|
|
if (!initialized[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTL_IJ_GET("stats.arenas.0.bins.0.curruns", curruns, i, j);
|
|
|
|
CTL_IJ_GET("stats.arenas.0.bins.0.curregs", curregs, i, j);
|
|
|
|
|
|
|
|
bin_unused += (nregs * curruns - curregs) * reg_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 01:05:29 +00:00
|
|
|
CTL_GET("stats.metadata", stats_metadata);
|
2015-09-03 10:53:12 +00:00
|
|
|
|
|
|
|
/* get the summation for all arenas, i == narenas */
|
|
|
|
CTL_I_GET("stats.arenas.0.metadata.allocated", stats_ametadata, narenas);
|
|
|
|
|
2015-08-20 01:05:29 +00:00
|
|
|
stats->bookkeeping = stats_metadata - stats_ametadata;
|
|
|
|
stats->bin_unused = bin_unused;
|
2014-12-19 14:15:00 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 08:32:24 +00:00
|
|
|
MOZ_JEMALLOC_API void
|
|
|
|
jemalloc_stats_impl(jemalloc_stats_t *stats)
|
2012-06-07 08:41:59 +00:00
|
|
|
{
|
2012-10-18 14:09:07 +00:00
|
|
|
unsigned narenas;
|
|
|
|
size_t active, allocated, mapped, page, pdirty;
|
2014-05-24 10:41:00 +00:00
|
|
|
size_t lg_chunk;
|
2012-10-18 14:09:07 +00:00
|
|
|
|
2014-10-17 13:55:00 +00:00
|
|
|
// Refresh jemalloc's stats by updating its epoch, see ctl_refresh in
|
|
|
|
// src/ctl.c
|
|
|
|
uint64_t epoch = 0;
|
|
|
|
size_t esz = sizeof(epoch);
|
|
|
|
int ret = je_(mallctl)("epoch", &epoch, &esz, &epoch, esz);
|
|
|
|
|
2012-10-18 14:09:07 +00:00
|
|
|
CTL_GET("arenas.narenas", narenas);
|
|
|
|
CTL_GET("arenas.page", page);
|
|
|
|
CTL_GET("stats.active", active);
|
|
|
|
CTL_GET("stats.allocated", allocated);
|
|
|
|
CTL_GET("stats.mapped", mapped);
|
2014-05-24 10:41:00 +00:00
|
|
|
CTL_GET("opt.lg_chunk", lg_chunk);
|
2012-10-18 14:09:07 +00:00
|
|
|
|
|
|
|
/* get the summation for all arenas, i == narenas */
|
|
|
|
CTL_I_GET("stats.arenas.0.pdirty", pdirty, narenas);
|
|
|
|
|
2015-02-27 07:19:00 +00:00
|
|
|
stats->chunksize = (size_t) 1 << lg_chunk;
|
2012-10-18 14:09:07 +00:00
|
|
|
stats->mapped = mapped;
|
2013-08-01 23:37:54 +00:00
|
|
|
stats->allocated = allocated;
|
|
|
|
stats->waste = active - allocated;
|
|
|
|
stats->page_cache = pdirty * page;
|
2015-08-20 01:05:29 +00:00
|
|
|
compute_bin_unused_and_bookkeeping(stats, narenas);
|
2014-11-12 18:00:00 +00:00
|
|
|
stats->waste -= stats->bin_unused;
|
2012-06-07 08:41:59 +00:00
|
|
|
}
|
2012-12-07 08:32:24 +00:00
|
|
|
|
|
|
|
MOZ_JEMALLOC_API void
|
|
|
|
jemalloc_purge_freed_pages_impl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_JEMALLOC_API void
|
|
|
|
jemalloc_free_dirty_pages_impl()
|
|
|
|
{
|
2014-12-23 07:41:00 +00:00
|
|
|
unsigned narenas;
|
|
|
|
size_t mib[3];
|
|
|
|
size_t miblen = sizeof(mib) / sizeof(mib[0]);
|
|
|
|
|
|
|
|
CTL_GET("arenas.narenas", narenas);
|
|
|
|
je_(mallctlnametomib)("arena.0.purge", mib, &miblen);
|
|
|
|
mib[1] = narenas;
|
|
|
|
je_(mallctlbymib)(mib, miblen, NULL, NULL, NULL, 0);
|
2012-12-07 08:32:24 +00:00
|
|
|
}
|