Skip to content

Commit

Permalink
[test] Improve test_emmalloc_trim output. NFC (#23342)
Browse files Browse the repository at this point in the history
Also, add some assertions.
  • Loading branch information
sbc100 authored Jan 8, 2025
1 parent 90f2c57 commit ff25a12
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 59 deletions.
58 changes: 33 additions & 25 deletions test/core/test_emmalloc_trim.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <emscripten/emmalloc.h>
Expand All @@ -7,40 +8,47 @@ size_t round_to_4k(size_t val){
return (val + 4095) & ~4095;
}

void print_stats(int cnt) {
printf("dynamic heap %d: %zu\n", cnt, round_to_4k(emmalloc_dynamic_heap_size()));
printf("free dynamic memory %d: %zu\n", cnt, round_to_4k(emmalloc_free_dynamic_memory()));
printf("unclaimed heap memory %d: %zu\n", cnt, round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("sbrk %d: %#zx\n", cnt, round_to_4k((size_t)sbrk(0)));
void print_stats(const char* title) {
printf("%s: dynamic heap: %zu\n", title, round_to_4k(emmalloc_dynamic_heap_size()));
printf("%s: free dynamic memory: %zu\n", title, round_to_4k(emmalloc_free_dynamic_memory()));
printf("%s: unclaimed heap memory: %zu\n", title, round_to_4k(emmalloc_unclaimed_heap_memory()));
printf("%s: sbrk: %#zx\n", title, round_to_4k((size_t)sbrk(0)));
}

int main() {
int did_free;
printf("heap size: %zu\n", emscripten_get_heap_size());
print_stats(0);
print_stats("init");

void *ptr = malloc(32*1024*1024);
void *ptr2 = malloc(4*1024*1024);
printf("%d\n", (int)(ptr && ptr2));
print_stats(1);

int success = emmalloc_trim(0);
printf("1st trim: %d\n", success);
print_stats(1);

success = emmalloc_trim(0);
printf("2nd trim: %d\n", success);
print_stats(2);
assert(ptr);
assert(ptr2);
print_stats("after alloc");

did_free = emmalloc_trim(0);
assert(did_free);
printf("1st trim: did_free=%d\n", did_free);
print_stats("1");

did_free = emmalloc_trim(0);
assert(!did_free);
printf("2nd trim: did_free=%d\n", did_free);
print_stats("2");
free(ptr2);

success = emmalloc_trim(100000);
printf("3rd trim: %d\n", success);
print_stats(3);
did_free = emmalloc_trim(100000);
assert(did_free);
printf("3rd trim: did_free=%d\n", did_free);
print_stats("3");

success = emmalloc_trim(100000);
printf("4th trim: %d\n", success);
print_stats(4);
did_free = emmalloc_trim(100000);
assert(!did_free);
printf("4th trim: did_free=%d\n", did_free);
print_stats("4");

success = emmalloc_trim(0);
printf("5th trim: %d\n", success);
print_stats(5);
did_free = emmalloc_trim(0);
assert(did_free);
printf("5th trim: did_free=%d\n", did_free);
print_stats("5");
}
67 changes: 33 additions & 34 deletions test/core/test_emmalloc_trim.out
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
heap size: 134217728
dynamic heap 0: 4096
free dynamic memory 0: 4096
unclaimed heap memory 0: 2147348480
sbrk 0: 0x12000
1
dynamic heap 1: 37752832
free dynamic memory 1: 4096
unclaimed heap memory 1: 2109599744
sbrk 1: 0x2412000
1st trim: 1
dynamic heap 1: 37752832
free dynamic memory 1: 0
unclaimed heap memory 1: 2109599744
sbrk 1: 0x2412000
2nd trim: 0
dynamic heap 2: 37752832
free dynamic memory 2: 0
unclaimed heap memory 2: 2109599744
sbrk 2: 0x2412000
3rd trim: 1
dynamic heap 3: 33656832
free dynamic memory 3: 102400
unclaimed heap memory 3: 2109599744
sbrk 3: 0x2412000
4th trim: 0
dynamic heap 4: 33656832
free dynamic memory 4: 102400
unclaimed heap memory 4: 2109599744
sbrk 4: 0x2412000
5th trim: 1
dynamic heap 5: 33558528
free dynamic memory 5: 0
unclaimed heap memory 5: 2109599744
sbrk 5: 0x2412000
init: dynamic heap: 4096
init: free dynamic memory: 4096
init: unclaimed heap memory: 2147348480
init: sbrk: 0x12000
after alloc: dynamic heap: 37752832
after alloc: free dynamic memory: 4096
after alloc: unclaimed heap memory: 2109599744
after alloc: sbrk: 0x2412000
1st trim: did_free=1
1: dynamic heap: 37752832
1: free dynamic memory: 0
1: unclaimed heap memory: 2109599744
1: sbrk: 0x2412000
2nd trim: did_free=0
2: dynamic heap: 37752832
2: free dynamic memory: 0
2: unclaimed heap memory: 2109599744
2: sbrk: 0x2412000
3rd trim: did_free=1
3: dynamic heap: 33656832
3: free dynamic memory: 102400
3: unclaimed heap memory: 2109599744
3: sbrk: 0x2412000
4th trim: did_free=0
4: dynamic heap: 33656832
4: free dynamic memory: 102400
4: unclaimed heap memory: 2109599744
4: sbrk: 0x2412000
5th trim: did_free=1
5: dynamic heap: 33558528
5: free dynamic memory: 0
5: unclaimed heap memory: 2109599744
5: sbrk: 0x2412000

0 comments on commit ff25a12

Please sign in to comment.