mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 06:59:32 +03:00
reserve huge pages returns actual number of pages reserved
This commit is contained in:
parent
6c43ae2bde
commit
3bbc047ba6
3 changed files with 15 additions and 10 deletions
|
@ -228,7 +228,7 @@ mi_decl_export bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_all_b
|
||||||
|
|
||||||
// Experimental
|
// Experimental
|
||||||
mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept;
|
mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept;
|
||||||
mi_decl_export int mi_reserve_huge_os_pages(size_t pages, double max_secs) mi_attr_noexcept;
|
mi_decl_export int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept;
|
||||||
mi_decl_export bool mi_is_redirected() mi_attr_noexcept;
|
mi_decl_export bool mi_is_redirected() mi_attr_noexcept;
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|
|
@ -443,7 +443,7 @@ static void mi_process_load(void) {
|
||||||
if (mi_option_is_enabled(mi_option_reserve_huge_os_pages)) {
|
if (mi_option_is_enabled(mi_option_reserve_huge_os_pages)) {
|
||||||
size_t pages = mi_option_get(mi_option_reserve_huge_os_pages);
|
size_t pages = mi_option_get(mi_option_reserve_huge_os_pages);
|
||||||
double max_secs = (double)pages / 2.0; // 0.5s per page (1GiB)
|
double max_secs = (double)pages / 2.0; // 0.5s per page (1GiB)
|
||||||
mi_reserve_huge_os_pages(pages, max_secs);
|
mi_reserve_huge_os_pages(pages, max_secs, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
src/os.c
21
src/os.c
|
@ -827,14 +827,17 @@ static void mi_os_free_huge_reserved() {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !(MI_INTPTR_SIZE >= 8 && (defined(_WIN32) || defined(MI_OS_USE_MMAP)))
|
#if !(MI_INTPTR_SIZE >= 8 && (defined(_WIN32) || defined(MI_OS_USE_MMAP)))
|
||||||
int mi_reserve_huge_os_pages(size_t pages, double max_secs) {
|
int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept {
|
||||||
return -2; // cannot allocate
|
UNUSED(pages); UNUSED(max_secs);
|
||||||
|
if (pages_reserved != NULL) *pages_reserved = 0;
|
||||||
|
return ENOMEM; // cannot allocate
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int mi_reserve_huge_os_pages( size_t pages, double max_secs ) mi_attr_noexcept
|
int mi_reserve_huge_os_pages( size_t pages, double max_secs, size_t* pages_reserved ) mi_attr_noexcept
|
||||||
{
|
{
|
||||||
if (max_secs==0) return -1; // timeout
|
if (pages_reserved != NULL) *pages_reserved = 0;
|
||||||
if (pages==0) return 0; // ok
|
if (max_secs==0) return ETIMEDOUT; // timeout
|
||||||
|
if (pages==0) return 0; // ok
|
||||||
if (!mi_atomic_cas_ptr_strong(&os_huge_reserved.start,(void*)1,NULL)) return -2; // already reserved
|
if (!mi_atomic_cas_ptr_strong(&os_huge_reserved.start,(void*)1,NULL)) return -2; // already reserved
|
||||||
|
|
||||||
// Allocate one page at the time but try to place them contiguously
|
// Allocate one page at the time but try to place them contiguously
|
||||||
|
@ -843,7 +846,7 @@ int mi_reserve_huge_os_pages( size_t pages, double max_secs ) mi_attr_noexcept
|
||||||
uint8_t* start = (uint8_t*)((uintptr_t)16 << 40); // 16TiB virtual start address
|
uint8_t* start = (uint8_t*)((uintptr_t)16 << 40); // 16TiB virtual start address
|
||||||
uint8_t* addr = start; // current top of the allocations
|
uint8_t* addr = start; // current top of the allocations
|
||||||
for (size_t page = 0; page < pages; page++, addr += MI_HUGE_OS_PAGE_SIZE ) {
|
for (size_t page = 0; page < pages; page++, addr += MI_HUGE_OS_PAGE_SIZE ) {
|
||||||
// allocate lorgu pages
|
// allocate a page
|
||||||
void* p = NULL;
|
void* p = NULL;
|
||||||
bool is_large = true;
|
bool is_large = true;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -856,6 +859,7 @@ int mi_reserve_huge_os_pages( size_t pages, double max_secs ) mi_attr_noexcept
|
||||||
|
|
||||||
// Did we succeed at a contiguous address?
|
// Did we succeed at a contiguous address?
|
||||||
if (p != addr) {
|
if (p != addr) {
|
||||||
|
// no success, issue a warning and return with an error
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
_mi_warning_message("could not allocate contiguous huge page %zu at 0x%p\n", page, addr);
|
_mi_warning_message("could not allocate contiguous huge page %zu at 0x%p\n", page, addr);
|
||||||
_mi_os_free(p, MI_HUGE_OS_PAGE_SIZE, &_mi_stats_main );
|
_mi_os_free(p, MI_HUGE_OS_PAGE_SIZE, &_mi_stats_main );
|
||||||
|
@ -868,7 +872,7 @@ int mi_reserve_huge_os_pages( size_t pages, double max_secs ) mi_attr_noexcept
|
||||||
#endif
|
#endif
|
||||||
_mi_warning_message("could not allocate huge page %zu at 0x%p, error: %i\n", page, addr, err);
|
_mi_warning_message("could not allocate huge page %zu at 0x%p, error: %i\n", page, addr, err);
|
||||||
}
|
}
|
||||||
return -2;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
// success, record it
|
// success, record it
|
||||||
if (page==0) {
|
if (page==0) {
|
||||||
|
@ -880,7 +884,8 @@ int mi_reserve_huge_os_pages( size_t pages, double max_secs ) mi_attr_noexcept
|
||||||
}
|
}
|
||||||
_mi_stat_increase(&_mi_stats_main.committed, MI_HUGE_OS_PAGE_SIZE);
|
_mi_stat_increase(&_mi_stats_main.committed, MI_HUGE_OS_PAGE_SIZE);
|
||||||
_mi_stat_increase(&_mi_stats_main.reserved, MI_HUGE_OS_PAGE_SIZE);
|
_mi_stat_increase(&_mi_stats_main.reserved, MI_HUGE_OS_PAGE_SIZE);
|
||||||
|
if (pages_reserved != NULL) { *pages_reserved = page + 1; }
|
||||||
|
|
||||||
// check for timeout
|
// check for timeout
|
||||||
double elapsed = _mi_clock_end(start_t);
|
double elapsed = _mi_clock_end(start_t);
|
||||||
if (elapsed > max_secs) return (-1); // timeout
|
if (elapsed > max_secs) return (-1); // timeout
|
||||||
|
|
Loading…
Add table
Reference in a new issue