Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also collect total bytes allocated in alloc tests #1115 #1116

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,8 +24,10 @@ function die() {

function make_git_commit_all() {
git init > /dev/null
git config --local user.email does@really-not.matter
git config --local user.name 'Does Not Matter'
if [[ "$(git config user.email)" == "" ]]; then
git config --local user.email does@really-not.matter
git config --local user.name 'Does Not Matter'
fi
ktoso marked this conversation as resolved.
Show resolved Hide resolved
git add . > /dev/null
git commit -m 'everything' > /dev/null
}
Expand Down
Expand Up @@ -13,6 +13,9 @@
//===----------------------------------------------------------------------===//

#ifndef ATOMIC_COUNTER
#define ATOMIC_COUNTER

#include <stdatomic.h>

void inc_free_counter(void);
void reset_free_counter(void);
Expand All @@ -22,4 +25,8 @@ void inc_malloc_counter(void);
void reset_malloc_counter(void);
long read_malloc_counter(void);

void add_malloc_bytes_counter(intptr_t v);
void reset_malloc_bytes_counter(void);
intptr_t read_malloc_bytes_counter(void);

weissi marked this conversation as resolved.
Show resolved Hide resolved
#endif
Expand Up @@ -20,13 +20,17 @@
*/ atomic_fetch_add_explicit(&g_ ## name ## _counter, 1, memory_order_relaxed); /*
*/ } /*
*/ /*
*/ void add_ ## name ## _counter(intptr_t v) { /*
*/ atomic_fetch_add_explicit(&g_ ## name ## _counter, v, memory_order_relaxed); /*
*/ } /*
*/ void reset_ ## name ## _counter(void) { /*
*/ atomic_store_explicit(&g_ ## name ## _counter, 0, memory_order_relaxed); /*
*/ } /*
*/ /*
*/ long read_ ## name ## _counter(void) { /*
*/ intptr_t read_ ## name ## _counter(void) { /*
*/ return atomic_load_explicit(&g_ ## name ## _counter, memory_order_relaxed); /*
*/ }

MAKE_COUNTER(free)
MAKE_COUNTER(malloc)
MAKE_COUNTER(malloc_bytes)
Expand Up @@ -129,6 +129,7 @@ void replacement_free(void *ptr) {

void *replacement_malloc(size_t size) {
inc_malloc_counter();
add_malloc_bytes_counter((intptr_t) size);

JUMP_INTO_LIBC_FUN(malloc, size);
}
Expand All @@ -143,6 +144,7 @@ void *replacement_realloc(void *ptr, size_t size) {
}
inc_free_counter();
inc_malloc_counter();
add_malloc_bytes_counter((intptr_t) size);

JUMP_INTO_LIBC_FUN(realloc, ptr, size);
}
Expand Down
Expand Up @@ -24,6 +24,7 @@ func measureAll(_ fn: () -> Int) -> [[String: Int]] {
func measureOne(_ fn: () -> Int) -> [String: Int] {
AtomicCounter.reset_free_counter()
AtomicCounter.reset_malloc_counter()
AtomicCounter.reset_malloc_bytes_counter()
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
autoreleasepool {
_ = fn()
Expand All @@ -34,8 +35,12 @@ func measureAll(_ fn: () -> Int) -> [[String: Int]] {
usleep(100_000) // allocs/frees happen on multiple threads, allow some cool down time
let frees = AtomicCounter.read_free_counter()
let mallocs = AtomicCounter.read_malloc_counter()
return ["total_allocations": mallocs,
"remaining_allocations": mallocs - frees]
let mallocedBytes = AtomicCounter.read_malloc_bytes_counter()
return [
"total_allocations": mallocs,
"total_allocated_bytes": mallocedBytes,
"remaining_allocations": mallocs - frees
]
}

_ = measureOne(fn) /* pre-heat and throw away */
Expand Down