Skip to content
Permalink
Browse files

Also collect total bytes allocated in alloc tests (#1116)

Motivation:

It is useful/interesting to have an overview about total bytes
allocated, not only the number of malloc calls.

This is not used to fail tests though, more as an informational thing.

Modifications:

Introduce and hit new counter with allocated size whenever allocations
happen.

Result:

It is possible to inspect the total allocated bytes.
  • Loading branch information
ktoso authored and Lukasa committed Nov 27, 2019
1 parent c2c7250 commit 8c0e373bf58c6114ae8cd8953c3f164ee8b2bc05
@@ -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
git add . > /dev/null
git commit -m 'everything' > /dev/null
}
@@ -13,6 +13,9 @@
//===----------------------------------------------------------------------===//

#ifndef ATOMIC_COUNTER
#define ATOMIC_COUNTER

#include <stdatomic.h>

void inc_free_counter(void);
void reset_free_counter(void);
@@ -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);

#endif
@@ -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)
@@ -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);
}
@@ -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);
}
@@ -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()
@@ -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 */

0 comments on commit 8c0e373

Please sign in to comment.
You can’t perform that action at this time.