2020-11-18 08:32:45 +08:00
|
|
|
#ifndef JEMALLOC_INTERNAL_HPDATA_H
|
|
|
|
#define JEMALLOC_INTERNAL_HPDATA_H
|
|
|
|
|
|
|
|
#include "jemalloc/internal/flat_bitmap.h"
|
|
|
|
#include "jemalloc/internal/ph.h"
|
|
|
|
#include "jemalloc/internal/ql.h"
|
|
|
|
#include "jemalloc/internal/typed_list.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The metadata representation we use for extents in hugepages. While the PAC
|
|
|
|
* uses the edata_t to represent both active and inactive extents, the HP only
|
|
|
|
* uses the edata_t for active ones; instead, inactive extent state is tracked
|
|
|
|
* within hpdata associated with the enclosing hugepage-sized, hugepage-aligned
|
|
|
|
* region of virtual address space.
|
|
|
|
*
|
|
|
|
* An hpdata need not be "truly" backed by a hugepage (which is not necessarily
|
|
|
|
* an observable property of any given region of address space). It's just
|
|
|
|
* hugepage-sized and hugepage-aligned; it's *potentially* huge.
|
|
|
|
*/
|
|
|
|
typedef struct hpdata_s hpdata_t;
|
|
|
|
struct hpdata_s {
|
|
|
|
/*
|
|
|
|
* We likewise follow the edata convention of mangling names and forcing
|
|
|
|
* the use of accessors -- this lets us add some consistency checks on
|
|
|
|
* access.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The address of the hugepage in question. This can't be named h_addr,
|
|
|
|
* since that conflicts with a macro defined in Windows headers.
|
|
|
|
*/
|
|
|
|
void *h_address;
|
|
|
|
/* Its age (measured in psset operations). */
|
|
|
|
uint64_t h_age;
|
|
|
|
/* Whether or not we think the hugepage is mapped that way by the OS. */
|
|
|
|
bool h_huge;
|
2020-12-03 10:44:34 +08:00
|
|
|
|
|
|
|
/*
|
2020-12-07 01:49:26 +08:00
|
|
|
* For some properties, we keep parallel sets of bools; h_foo_allowed
|
|
|
|
* and h_in_psset_foo_container. This is a decoupling mechanism to
|
|
|
|
* avoid bothering the hpa (which manages policies) from the psset
|
|
|
|
* (which is the mechanism used to enforce those policies). This allows
|
|
|
|
* all the container management logic to live in one place, without the
|
|
|
|
* HPA needing to know or care how that happens.
|
2020-12-03 10:44:34 +08:00
|
|
|
*/
|
2020-12-07 01:49:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether or not the hpdata is allowed to be used to serve allocations,
|
|
|
|
* and whether or not the psset is currently tracking it as such.
|
|
|
|
*/
|
|
|
|
bool h_alloc_allowed;
|
|
|
|
bool h_in_psset_alloc_container;
|
|
|
|
|
2021-02-09 06:11:37 +08:00
|
|
|
/*
|
|
|
|
* The same, but with purging. There's no corresponding
|
|
|
|
* h_in_psset_purge_container, because the psset (currently) always
|
|
|
|
* removes hpdatas from their containers during updates (to implement
|
|
|
|
* LRU for purging).
|
|
|
|
*/
|
|
|
|
bool h_purge_allowed;
|
2020-12-07 01:49:26 +08:00
|
|
|
|
|
|
|
/* And with hugifying. */
|
|
|
|
bool h_hugify_allowed;
|
|
|
|
bool h_in_psset_hugify_container;
|
|
|
|
|
|
|
|
/* Whether or not a purge or hugify is currently happening. */
|
2020-12-03 10:44:34 +08:00
|
|
|
bool h_mid_purge;
|
|
|
|
bool h_mid_hugify;
|
|
|
|
|
2020-12-06 07:58:31 +08:00
|
|
|
/*
|
|
|
|
* Whether or not the hpdata is being updated in the psset (i.e. if
|
|
|
|
* there has been a psset_update_begin call issued without a matching
|
|
|
|
* psset_update_end call). Eventually this will expand to other types
|
|
|
|
* of updates.
|
|
|
|
*/
|
|
|
|
bool h_updating;
|
2020-12-03 14:24:15 +08:00
|
|
|
|
2020-12-06 09:42:04 +08:00
|
|
|
/* Whether or not the hpdata is in a psset. */
|
|
|
|
bool h_in_psset;
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
union {
|
2020-12-06 09:42:04 +08:00
|
|
|
/* When nonempty (and also nonfull), used by the psset bins. */
|
2020-11-18 08:32:45 +08:00
|
|
|
phn(hpdata_t) ph_link;
|
|
|
|
/*
|
|
|
|
* When empty (or not corresponding to any hugepage), list
|
|
|
|
* linkage.
|
|
|
|
*/
|
2020-12-06 09:42:04 +08:00
|
|
|
ql_elm(hpdata_t) ql_link_empty;
|
2020-11-18 08:32:45 +08:00
|
|
|
};
|
|
|
|
|
2020-12-07 01:49:26 +08:00
|
|
|
/*
|
|
|
|
* Linkage for the psset to track candidates for purging and hugifying.
|
|
|
|
*/
|
|
|
|
ql_elm(hpdata_t) ql_link_purge;
|
|
|
|
ql_elm(hpdata_t) ql_link_hugify;
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
/* The length of the largest contiguous sequence of inactive pages. */
|
|
|
|
size_t h_longest_free_range;
|
|
|
|
|
2020-12-03 09:01:57 +08:00
|
|
|
/* Number of active pages. */
|
|
|
|
size_t h_nactive;
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
/* A bitmap with bits set in the active pages. */
|
|
|
|
fb_group_t active_pages[FB_NGROUPS(HUGEPAGE_PAGES)];
|
2020-12-03 09:56:58 +08:00
|
|
|
|
|
|
|
/*
|
2020-12-04 10:58:58 +08:00
|
|
|
* Number of dirty or active pages, and a bitmap tracking them. One
|
|
|
|
* way to think of this is as which pages are dirty from the OS's
|
|
|
|
* perspective.
|
2020-12-03 09:56:58 +08:00
|
|
|
*/
|
2020-12-04 10:58:58 +08:00
|
|
|
size_t h_ntouched;
|
2020-12-03 09:56:58 +08:00
|
|
|
|
|
|
|
/* The dirty pages (using the same definition as above). */
|
2020-12-04 10:58:58 +08:00
|
|
|
fb_group_t touched_pages[FB_NGROUPS(HUGEPAGE_PAGES)];
|
2020-11-18 08:32:45 +08:00
|
|
|
};
|
|
|
|
|
2020-12-06 09:42:04 +08:00
|
|
|
TYPED_LIST(hpdata_empty_list, hpdata_t, ql_link_empty)
|
2020-12-07 01:49:26 +08:00
|
|
|
TYPED_LIST(hpdata_purge_list, hpdata_t, ql_link_purge)
|
|
|
|
TYPED_LIST(hpdata_hugify_list, hpdata_t, ql_link_hugify)
|
|
|
|
|
2020-12-04 10:02:23 +08:00
|
|
|
typedef ph(hpdata_t) hpdata_age_heap_t;
|
|
|
|
ph_proto(, hpdata_age_heap_, hpdata_age_heap_t, hpdata_t);
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
static inline void *
|
|
|
|
hpdata_addr_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_addr_set(hpdata_t *hpdata, void *addr) {
|
|
|
|
assert(HUGEPAGE_ADDR2BASE(addr) == addr);
|
|
|
|
hpdata->h_address = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t
|
|
|
|
hpdata_age_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_age;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_age_set(hpdata_t *hpdata, uint64_t age) {
|
|
|
|
hpdata->h_age = age;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_huge_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_huge;
|
|
|
|
}
|
|
|
|
|
2020-12-03 10:44:34 +08:00
|
|
|
static inline bool
|
2020-12-07 01:49:26 +08:00
|
|
|
hpdata_alloc_allowed_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_alloc_allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_alloc_allowed_set(hpdata_t *hpdata, bool alloc_allowed) {
|
|
|
|
hpdata->h_alloc_allowed = alloc_allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_in_psset_alloc_container_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_in_psset_alloc_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_in_psset_alloc_container_set(hpdata_t *hpdata, bool in_container) {
|
|
|
|
assert(in_container != hpdata->h_in_psset_alloc_container);
|
|
|
|
hpdata->h_in_psset_alloc_container = in_container;
|
|
|
|
}
|
|
|
|
|
2021-02-09 06:11:37 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_purge_allowed_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_purge_allowed;
|
2020-12-07 01:49:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2021-02-09 06:11:37 +08:00
|
|
|
hpdata_purge_allowed_set(hpdata_t *hpdata, bool purge_allowed) {
|
|
|
|
assert(purge_allowed == false || !hpdata->h_mid_purge);
|
|
|
|
hpdata->h_purge_allowed = purge_allowed;
|
2020-12-07 01:49:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_hugify_allowed_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_hugify_allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_hugify_allowed_set(hpdata_t *hpdata, bool hugify_allowed) {
|
2020-12-07 05:48:46 +08:00
|
|
|
assert(hugify_allowed == false || !hpdata->h_mid_hugify);
|
2020-12-07 01:49:26 +08:00
|
|
|
hpdata->h_hugify_allowed = hugify_allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_in_psset_hugify_container_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_in_psset_hugify_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_in_psset_hugify_container_set(hpdata_t *hpdata, bool in_container) {
|
|
|
|
assert(in_container != hpdata->h_in_psset_hugify_container);
|
|
|
|
hpdata->h_in_psset_hugify_container = in_container;
|
2020-12-03 10:44:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_mid_purge_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_mid_purge;
|
|
|
|
}
|
|
|
|
|
2020-12-07 01:49:26 +08:00
|
|
|
static inline void
|
|
|
|
hpdata_mid_purge_set(hpdata_t *hpdata, bool mid_purge) {
|
|
|
|
assert(mid_purge != hpdata->h_mid_purge);
|
|
|
|
hpdata->h_mid_purge = mid_purge;
|
|
|
|
}
|
|
|
|
|
2020-12-03 10:44:34 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_mid_hugify_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_mid_hugify;
|
|
|
|
}
|
|
|
|
|
2020-12-07 01:49:26 +08:00
|
|
|
static inline void
|
|
|
|
hpdata_mid_hugify_set(hpdata_t *hpdata, bool mid_hugify) {
|
|
|
|
assert(mid_hugify != hpdata->h_mid_hugify);
|
|
|
|
hpdata->h_mid_hugify = mid_hugify;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
hpdata_changing_state_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_mid_purge || hpdata->h_mid_hugify;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-03 14:24:15 +08:00
|
|
|
static inline bool
|
2020-12-06 07:58:31 +08:00
|
|
|
hpdata_updating_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_updating;
|
2020-12-03 14:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2020-12-06 07:58:31 +08:00
|
|
|
hpdata_updating_set(hpdata_t *hpdata, bool updating) {
|
|
|
|
assert(updating != hpdata->h_updating);
|
|
|
|
hpdata->h_updating = updating;
|
2020-12-03 14:24:15 +08:00
|
|
|
}
|
2020-12-03 10:44:34 +08:00
|
|
|
|
2020-12-06 09:42:04 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_in_psset_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_in_psset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_in_psset_set(hpdata_t *hpdata, bool in_psset) {
|
|
|
|
assert(in_psset != hpdata->h_in_psset);
|
|
|
|
hpdata->h_in_psset = in_psset;
|
|
|
|
}
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
static inline size_t
|
|
|
|
hpdata_longest_free_range_get(const hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_longest_free_range;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
hpdata_longest_free_range_set(hpdata_t *hpdata, size_t longest_free_range) {
|
|
|
|
assert(longest_free_range <= HUGEPAGE_PAGES);
|
|
|
|
hpdata->h_longest_free_range = longest_free_range;
|
|
|
|
}
|
|
|
|
|
2020-12-03 09:01:57 +08:00
|
|
|
static inline size_t
|
|
|
|
hpdata_nactive_get(hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_nactive;
|
|
|
|
}
|
|
|
|
|
2020-12-04 10:58:58 +08:00
|
|
|
static inline size_t
|
|
|
|
hpdata_ntouched_get(hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_ntouched;
|
|
|
|
}
|
|
|
|
|
2020-12-03 10:44:34 +08:00
|
|
|
static inline size_t
|
|
|
|
hpdata_ndirty_get(hpdata_t *hpdata) {
|
2020-12-04 10:58:58 +08:00
|
|
|
return hpdata->h_ntouched - hpdata->h_nactive;
|
2020-12-03 10:44:34 +08:00
|
|
|
}
|
|
|
|
|
2021-02-06 02:46:17 +08:00
|
|
|
static inline size_t
|
|
|
|
hpdata_nretained_get(hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_nactive - hpdata->h_ntouched;
|
|
|
|
}
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
static inline void
|
2020-11-19 06:52:19 +08:00
|
|
|
hpdata_assert_empty(hpdata_t *hpdata) {
|
|
|
|
assert(fb_empty(hpdata->active_pages, HUGEPAGE_PAGES));
|
2020-12-03 09:01:57 +08:00
|
|
|
assert(hpdata->h_nactive == 0);
|
2020-11-19 06:52:19 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 05:19:41 +08:00
|
|
|
/*
|
|
|
|
* Only used in tests, and in hpdata_assert_consistent, below. Verifies some
|
|
|
|
* consistency properties of the hpdata (e.g. that cached counts of page stats
|
|
|
|
* match computed ones).
|
|
|
|
*/
|
2020-12-02 23:04:01 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_consistent(hpdata_t *hpdata) {
|
2020-12-03 06:21:36 +08:00
|
|
|
if(fb_urange_longest(hpdata->active_pages, HUGEPAGE_PAGES)
|
|
|
|
!= hpdata_longest_free_range_get(hpdata)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-03 09:01:57 +08:00
|
|
|
if (fb_scount(hpdata->active_pages, HUGEPAGE_PAGES, 0, HUGEPAGE_PAGES)
|
|
|
|
!= hpdata->h_nactive) {
|
2020-12-03 06:21:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-04 10:58:58 +08:00
|
|
|
if (fb_scount(hpdata->touched_pages, HUGEPAGE_PAGES, 0, HUGEPAGE_PAGES)
|
|
|
|
!= hpdata->h_ntouched) {
|
2020-12-03 09:56:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-04 10:58:58 +08:00
|
|
|
if (hpdata->h_ntouched < hpdata->h_nactive) {
|
2020-12-03 09:56:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-04 10:58:58 +08:00
|
|
|
if (hpdata->h_huge && hpdata->h_ntouched != HUGEPAGE_PAGES) {
|
2020-12-03 09:56:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-07 05:48:46 +08:00
|
|
|
if (hpdata_changing_state_get(hpdata)
|
2021-02-09 06:11:37 +08:00
|
|
|
&& ((hpdata->h_purge_allowed) || hpdata->h_hugify_allowed)) {
|
2020-12-07 05:48:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hpdata_hugify_allowed_get(hpdata)
|
|
|
|
!= hpdata_in_psset_hugify_container_get(hpdata)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-03 06:21:36 +08:00
|
|
|
return true;
|
2020-12-02 23:04:01 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 06:52:19 +08:00
|
|
|
static inline void
|
|
|
|
hpdata_assert_consistent(hpdata_t *hpdata) {
|
2020-12-02 23:04:01 +08:00
|
|
|
assert(hpdata_consistent(hpdata));
|
2020-11-18 08:32:45 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 05:28:54 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_empty(hpdata_t *hpdata) {
|
2020-12-03 09:01:57 +08:00
|
|
|
return hpdata->h_nactive == 0;
|
2020-12-01 05:28:54 +08:00
|
|
|
}
|
|
|
|
|
2020-12-06 09:42:04 +08:00
|
|
|
static inline bool
|
|
|
|
hpdata_full(hpdata_t *hpdata) {
|
|
|
|
return hpdata->h_nactive == HUGEPAGE_PAGES;
|
|
|
|
}
|
|
|
|
|
2020-11-19 06:52:19 +08:00
|
|
|
void hpdata_init(hpdata_t *hpdata, void *addr, uint64_t age);
|
2020-12-03 09:56:58 +08:00
|
|
|
|
2020-11-19 06:52:19 +08:00
|
|
|
/*
|
|
|
|
* Given an hpdata which can serve an allocation request, pick and reserve an
|
|
|
|
* offset within that allocation.
|
|
|
|
*/
|
2020-12-01 06:34:27 +08:00
|
|
|
void *hpdata_reserve_alloc(hpdata_t *hpdata, size_t sz);
|
|
|
|
void hpdata_unreserve(hpdata_t *hpdata, void *begin, size_t sz);
|
2020-11-19 06:52:19 +08:00
|
|
|
|
2020-12-03 10:44:34 +08:00
|
|
|
/*
|
|
|
|
* The hpdata_purge_prepare_t allows grabbing the metadata required to purge
|
|
|
|
* subranges of a hugepage while holding a lock, drop the lock during the actual
|
|
|
|
* purging of them, and reacquire it to update the metadata again.
|
|
|
|
*/
|
|
|
|
typedef struct hpdata_purge_state_s hpdata_purge_state_t;
|
|
|
|
struct hpdata_purge_state_s {
|
|
|
|
size_t npurged;
|
|
|
|
fb_group_t to_purge[FB_NGROUPS(HUGEPAGE_PAGES)];
|
|
|
|
size_t next_purge_search_begin;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes purge state. The access to hpdata must be externally
|
|
|
|
* synchronized with other hpdata_* calls.
|
|
|
|
*
|
|
|
|
* You can tell whether or not a thread is purging or hugifying a given hpdata
|
|
|
|
* via hpdata_changing_state_get(hpdata). Racing hugification or purging
|
|
|
|
* operations aren't allowed.
|
|
|
|
*
|
|
|
|
* Once you begin purging, you have to follow through and call hpdata_purge_next
|
|
|
|
* until you're done, and then end. Allocating out of an hpdata undergoing
|
|
|
|
* purging is not allowed.
|
2020-12-07 05:16:51 +08:00
|
|
|
*
|
|
|
|
* Returns the number of pages that will be purged.
|
2020-12-03 10:44:34 +08:00
|
|
|
*/
|
2020-12-07 05:16:51 +08:00
|
|
|
size_t hpdata_purge_begin(hpdata_t *hpdata, hpdata_purge_state_t *purge_state);
|
|
|
|
|
2020-12-03 10:44:34 +08:00
|
|
|
/*
|
|
|
|
* If there are more extents to purge, sets *r_purge_addr and *r_purge_size to
|
|
|
|
* true, and returns true. Otherwise, returns false to indicate that we're
|
|
|
|
* done.
|
|
|
|
*
|
|
|
|
* This requires exclusive access to the purge state, but *not* to the hpdata.
|
|
|
|
* In particular, unreserve calls are allowed while purging (i.e. you can dalloc
|
|
|
|
* into one part of the hpdata while purging a different part).
|
|
|
|
*/
|
|
|
|
bool hpdata_purge_next(hpdata_t *hpdata, hpdata_purge_state_t *purge_state,
|
|
|
|
void **r_purge_addr, size_t *r_purge_size);
|
|
|
|
/*
|
|
|
|
* Updates the hpdata metadata after all purging is done. Needs external
|
|
|
|
* synchronization.
|
|
|
|
*/
|
|
|
|
void hpdata_purge_end(hpdata_t *hpdata, hpdata_purge_state_t *purge_state);
|
|
|
|
|
2020-12-07 01:49:26 +08:00
|
|
|
void hpdata_hugify(hpdata_t *hpdata);
|
2020-12-03 09:56:58 +08:00
|
|
|
void hpdata_dehugify(hpdata_t *hpdata);
|
|
|
|
|
2020-11-18 08:32:45 +08:00
|
|
|
#endif /* JEMALLOC_INTERNAL_HPDATA_H */
|