-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add C test; add version that is partially working
- Loading branch information
Showing
4 changed files
with
170 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Huge Page Demo | ||
|
||
This is a demonstration of using huge pages on Linux to get better performance. | ||
|
||
This will compile and run on non-Linux platforms, but won't use huge pages. | ||
|
||
For more details, see [Reliably allocating huge pages in Linux](https://mazzo.li/posts/check-huge-page.html). | ||
|
||
|
||
# Malloc/Mmap behaviour | ||
|
||
On Ubuntu 20.04.5 with kernel 5.15.0-1023-aws and glibc 2.31-0ubuntu9.9, malloc 4 GiB calls mmap to allocate 4 GiB + 4 KiB, then returns a pointer that is +0x10 (+16) from the pointer actually returned by mmap. Using aligned_alloc calls mmap to allocate 5 GiB + 4 KiB (size + alignment + 1 page?), then returns an aligned pointer. Calling mmap to allocate 4 GiB returns a pointer that is not aligned. E.g. On my system, I get one that is 32 kiB aligned. | ||
|
||
On Mac OS X 13.1 on an M1 ARM CPU, using mmap to request 4 GiB of memory returns a block that is aligned to a 1 GiB boundary. The same appears to be true for using malloc. I didn't fight to get dtruss to work to see what malloc is actually doing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <assert.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
static const size_t FOUR_GIB_IN_BYTES = UINT64_C(4) << 30; | ||
static const size_t HUGE_2MIB_ALIGNMENT = UINT64_C(2) << 20; | ||
static const size_t HUGE_2MIB_MASK = HUGE_2MIB_ALIGNMENT - 1; | ||
static const size_t HUGE_1GIB_ALIGNMENT = UINT64_C(1) << 30; | ||
static const size_t HUGE_1GIB_MASK = HUGE_1GIB_ALIGNMENT - 1; | ||
|
||
void *plain_malloc = malloc(FOUR_GIB_IN_BYTES); | ||
printf("malloc 4GiB = %p; 2MiB aligned? %d; 1GiB aligned? %d\n", plain_malloc, | ||
((size_t)plain_malloc & HUGE_2MIB_MASK) == 0, | ||
((size_t)plain_malloc & HUGE_1GIB_MASK) == 0); | ||
free(plain_malloc); | ||
|
||
void *aligned = aligned_alloc(HUGE_1GIB_ALIGNMENT, FOUR_GIB_IN_BYTES); | ||
printf("aligned_alloc 4GiB = %p; 2MiB aligned? %d; 1GiB aligned? %d\n", aligned, | ||
((size_t)aligned & HUGE_2MIB_MASK) == 0, ((size_t)aligned & HUGE_1GIB_MASK) == 0); | ||
free(aligned); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters