#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32)
#include <pthread.h>
#include <unistd.h>
#endif
#include "mimalloc.h"
#include "mimalloc-profile.h"
#include "testhelper.h"
#define TEST_THRESHOLD (1 * 1024)
#define PROFILE_BASE_NAME "profile/test-pprof-profile"
static void pprof_snapshot_file_name(char* buf, size_t bufsize, const char* base, unsigned seq) {
snprintf(buf, bufsize, "%s.%04u.heap", base, seq);
}
static void pprof_remove_snapshot_files(const char* base, unsigned max_seq) {
char fname[1024];
for (unsigned seq = 1; seq <= max_seq; seq++) {
pprof_snapshot_file_name(fname, sizeof(fname), base, seq);
remove(fname);
}
}
static void allocate_and_free(size_t n, size_t block_size) {
for (size_t i = 0; i < n; i++) {
void* p = mi_malloc(block_size + (i % 37));
mi_free(p);
}
}
#define MI_TEST_PPROF_SITE_COUNT 10
static void* volatile mi_test_pprof_sink;
#if defined(_MSC_VER)
#define mi_test_noinline __declspec(noinline)
#elif defined(__GNUC__)
#define mi_test_noinline __attribute__((noinline))
#else
#define mi_test_noinline
#endif
#define MI_TEST_PPROF_DEFINE_SITE(N) \
static mi_test_noinline void* alloc_site_##N(size_t block_size, size_t i) { \
void* p = mi_malloc(block_size + (i % 37)); \
mi_test_pprof_sink = p; \
return p; \
}
MI_TEST_PPROF_DEFINE_SITE(0)
MI_TEST_PPROF_DEFINE_SITE(1)
MI_TEST_PPROF_DEFINE_SITE(2)
MI_TEST_PPROF_DEFINE_SITE(3)
MI_TEST_PPROF_DEFINE_SITE(4)
MI_TEST_PPROF_DEFINE_SITE(5)
MI_TEST_PPROF_DEFINE_SITE(6)
MI_TEST_PPROF_DEFINE_SITE(7)
MI_TEST_PPROF_DEFINE_SITE(8)
MI_TEST_PPROF_DEFINE_SITE(9)
typedef void* (*mi_test_alloc_site_fun_t)(size_t block_size, size_t i);
static const mi_test_alloc_site_fun_t alloc_sites[MI_TEST_PPROF_SITE_COUNT] = {
&alloc_site_0, &alloc_site_1, &alloc_site_2, &alloc_site_3, &alloc_site_4,
&alloc_site_5, &alloc_site_6, &alloc_site_7, &alloc_site_8, &alloc_site_9
};
static void** alloc_site_kept = NULL;
static size_t alloc_site_kept_count = 0;
static size_t alloc_site_kept_capacity = 0;
static void alloc_site_keep_alive(void* p) {
if (alloc_site_kept_count == alloc_site_kept_capacity) {
const size_t new_capacity = (alloc_site_kept_capacity == 0 ? 4096 : alloc_site_kept_capacity * 2);
alloc_site_kept = (void**)mi_realloc(alloc_site_kept, new_capacity * sizeof(void*));
alloc_site_kept_capacity = new_capacity;
}
alloc_site_kept[alloc_site_kept_count++] = p;
}
static void allocate_and_free_multi_site(mi_profiler_t* prof, size_t n, size_t block_size) {
for (size_t site = 0; site < MI_TEST_PPROF_SITE_COUNT; site++) {
if (site == MI_TEST_PPROF_SITE_COUNT/2) {
mi_profiler_snapshot(prof);
}
for (size_t i = 0; i < n; i++) {
void* p = (*alloc_sites[site])(block_size, i);
alloc_site_keep_alive(p);
}
}
}
static void allocate_and_free_multi_site_cleanup(void) {
for (size_t i = 0; i < alloc_site_kept_count; i++) {
mi_free(alloc_site_kept[i]);
}
mi_free(alloc_site_kept);
alloc_site_kept = NULL;
alloc_site_kept_count = 0;
alloc_site_kept_capacity = 0;
}
static size_t count_location_entries(const char* contents) {
size_t count = 0;
const char* p = contents;
while ((p = strstr(p, "] @ 0x")) != NULL) {
count++;
p += 6;
}
return count;
}
static int64_t parse_total_inuse_objects(const char* contents) {
const char* p = (contents == NULL ? NULL : strstr(contents, "heap profile:"));
if (p == NULL) return -1;
p += strlen("heap profile:");
return (int64_t)strtoll(p, NULL, 10);
}
static char* read_file(const char* fname) {
FILE* f = fopen(fname, "rb");
if (f == NULL) return NULL;
fseek(f, 0, SEEK_END);
const long len = ftell(f);
fseek(f, 0, SEEK_SET);
if (len < 0) { fclose(f); return NULL; }
char* buf = (char*)mi_malloc((size_t)len + 1);
if (buf != NULL) {
const size_t nread = fread(buf, 1, (size_t)len, f);
buf[nread] = 0;
}
fclose(f);
return buf;
}
bool test_pprof_snapshot_creates_file(void) {
CHECK_BODY("pprof: snapshot creates a <base>.0001.heap file") {
pprof_remove_snapshot_files(PROFILE_BASE_NAME, 1);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
allocate_and_free(200000, 64);
mi_profiler_stop(prof);
mi_profiler_snapshot(prof);
char fname[1024];
pprof_snapshot_file_name(fname, sizeof(fname), PROFILE_BASE_NAME, 1);
char* contents = read_file(fname);
result = (contents != NULL && contents[0] != 0);
mi_free(contents);
mi_profile(NULL); mi_pprof_profiler_delete(prof);
}
pprof_remove_snapshot_files(PROFILE_BASE_NAME, 1);
}
return true;
}
bool test_pprof_snapshot_format(void) {
CHECK_BODY("pprof: snapshot uses the pprof heap profile text format") {
pprof_remove_snapshot_files(PROFILE_BASE_NAME, 1);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
allocate_and_free(200000, 64);
mi_profiler_stop(prof);
mi_profiler_snapshot(prof);
char fname[1024];
pprof_snapshot_file_name(fname, sizeof(fname), PROFILE_BASE_NAME, 1);
char* contents = read_file(fname);
result = (contents != NULL &&
strncmp(contents, "heap profile:", 13) == 0 &&
strstr(contents, "@ heapprofile") != NULL &&
strstr(contents, "MAPPED_LIBRARIES:") != NULL);
mi_free(contents);
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
pprof_remove_snapshot_files(PROFILE_BASE_NAME, 1);
}
return true;
}
#define PROFILE_PROTO_BASE_NAME "test-pprof-profile-proto"
static uint64_t pb_read_varint(const uint8_t** p, const uint8_t* end) {
uint64_t v = 0;
int shift = 0;
while (*p < end) {
const uint8_t byte = *(*p)++;
v |= ((uint64_t)(byte & 0x7F)) << shift;
if ((byte & 0x80) == 0) break;
shift += 7;
}
return v;
}
static bool pb_looks_like_valid_profile(const uint8_t* data, size_t size) {
const uint8_t* p = data;
const uint8_t* end = data + size;
bool saw_sample = false;
bool saw_location = false;
while (p < end) {
const uint64_t tag = pb_read_varint(&p, end);
const uint32_t field = (uint32_t)(tag >> 3);
const uint32_t wiretype = (uint32_t)(tag & 0x7);
if (field == 2) saw_sample = true;
if (field == 4) saw_location = true;
if (wiretype == 0) { pb_read_varint(&p, end);
}
else if (wiretype == 2) { const uint64_t len = pb_read_varint(&p, end);
if ((size_t)(end - p) < len) return false;
p += len;
}
else {
return false; }
}
return (p == end && saw_sample && saw_location);
}
bool test_pprof_snapshot_proto_format(void) {
CHECK_BODY("pprof: snapshot can use the protobuf pprof format") {
char fname[1024];
snprintf(fname, sizeof(fname), "%s.0001.pb", PROFILE_PROTO_BASE_NAME);
remove(fname);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_PROTO_BASE_NAME, 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
allocate_and_free(200000, 64);
mi_profiler_stop(prof);
mi_profiler_snapshot(prof);
FILE* f = fopen(fname, "rb");
result = (f != NULL);
if (result) {
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fseek(f, 0, SEEK_SET);
result = (size > 0);
if (result) {
uint8_t* buf = (uint8_t*)mi_malloc((size_t)size);
const size_t nread = fread(buf, 1, (size_t)size, f);
result = (nread == (size_t)size && pb_looks_like_valid_profile(buf, nread));
mi_free(buf);
}
fclose(f);
}
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
remove(fname);
}
return true;
}
#define PROFILE_INTERVAL_BASE_NAME "profile/test-pprof-profile-interval"
#define TEST_INTERVAL_SIZE (8 * 1024)
#define TEST_INTERVAL_MAX_SEQ 64
bool test_pprof_snapshot_interval(void) {
CHECK_BODY("pprof: interval_size triggers automatic snapshots from on_alloc") {
pprof_remove_snapshot_files(PROFILE_INTERVAL_BASE_NAME, TEST_INTERVAL_MAX_SEQ);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_INTERVAL_BASE_NAME ".heap", TEST_INTERVAL_SIZE, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
allocate_and_free(200000, 64);
mi_profiler_stop(prof);
char fname1[1024];
pprof_snapshot_file_name(fname1, sizeof(fname1), PROFILE_INTERVAL_BASE_NAME, 1);
char* contents1 = read_file(fname1);
result = (contents1 != NULL && strncmp(contents1, "heap profile:", 13) == 0);
mi_free(contents1);
if (result) {
char fname6[1024];
pprof_snapshot_file_name(fname6, sizeof(fname6), PROFILE_INTERVAL_BASE_NAME, 6);
char* contents6 = read_file(fname6);
result = (contents6 != NULL && strncmp(contents6, "heap profile:", 13) == 0);
mi_free(contents6);
}
mi_profile(NULL); mi_pprof_profiler_delete(prof);
}
pprof_remove_snapshot_files(PROFILE_INTERVAL_BASE_NAME, TEST_INTERVAL_MAX_SEQ);
}
return true;
}
bool test_pprof_snapshot_records_samples(void) {
CHECK_BODY("pprof: snapshot records samples from multiple distinct call sites") {
#define MI_TEST_PPROF_RECORDS_SNAPSHOT_COUNT 4
#define MI_TEST_PPROF_FINAL_SNAPSHOT_SEQ (MI_TEST_PPROF_RECORDS_SNAPSHOT_COUNT + 1)
pprof_remove_snapshot_files(PROFILE_BASE_NAME, MI_TEST_PPROF_FINAL_SNAPSHOT_SEQ);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
allocate_and_free_multi_site(prof, 20000, 64);
mi_profiler_snapshot(prof);
allocate_and_free_multi_site(prof, 20000, 64);
mi_profiler_snapshot(prof);
allocate_and_free_multi_site_cleanup(); mi_profiler_snapshot(prof);
char fname1[1024];
pprof_snapshot_file_name(fname1, sizeof(fname1), PROFILE_BASE_NAME, 1);
char* contents1 = read_file(fname1);
result = (contents1 != NULL && parse_total_inuse_objects(contents1) > 0);
mi_free(contents1);
char fname[1024];
pprof_snapshot_file_name(fname, sizeof(fname), PROFILE_BASE_NAME, MI_TEST_PPROF_RECORDS_SNAPSHOT_COUNT);
char* contents = read_file(fname);
const size_t entry_count = (contents == NULL ? 0 : count_location_entries(contents));
result = (result && contents != NULL && entry_count >= MI_TEST_PPROF_SITE_COUNT);
mi_free(contents);
char fname_final[1024];
pprof_snapshot_file_name(fname_final, sizeof(fname_final), PROFILE_BASE_NAME, MI_TEST_PPROF_FINAL_SNAPSHOT_SEQ);
char* contents_final = read_file(fname_final);
result = (result && contents_final != NULL && parse_total_inuse_objects(contents_final) == 0);
mi_free(contents_final);
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
#undef MI_TEST_PPROF_FINAL_SNAPSHOT_SEQ
#undef MI_TEST_PPROF_RECORDS_SNAPSHOT_COUNT
}
return true;
}
bool test_pprof_snapshot_increments_sequence(void) {
CHECK_BODY("pprof: repeated snapshots use an incrementing sequence number") {
#define MI_TEST_PPROF_SNAPSHOT_COUNT 3
pprof_remove_snapshot_files(PROFILE_BASE_NAME, MI_TEST_PPROF_SNAPSHOT_COUNT);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
for (unsigned seq = 1; seq <= MI_TEST_PPROF_SNAPSHOT_COUNT && result; seq++) {
allocate_and_free(20000, 64);
mi_profiler_snapshot(prof);
char fname[1024];
pprof_snapshot_file_name(fname, sizeof(fname), PROFILE_BASE_NAME, seq);
char* contents = read_file(fname);
result = (contents != NULL && contents[0] != 0);
mi_free(contents);
}
mi_profiler_stop(prof);
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
pprof_remove_snapshot_files(PROFILE_BASE_NAME, MI_TEST_PPROF_SNAPSHOT_COUNT);
#undef MI_TEST_PPROF_SNAPSHOT_COUNT
}
return true;
}
bool test_pprof_profiler_new_delete(void) {
CHECK_BODY("pprof: profiler can be created and deleted without use") {
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (prof != NULL) {
mi_pprof_profiler_delete(prof);
}
}
return true;
}
#if !defined(_WIN32)
#define PROFILE_THREADS_BASE_NAME "profile/test-pprof-profile-threads"
#define MI_TEST_PPROF_THREAD_COUNT 8
#define MI_TEST_PPROF_THREAD_ITERS 50000
#define MI_TEST_PPROF_THREAD_POOL_SIZE 1024
#define MI_TEST_PPROF_THREAD_SNAPSHOT_COUNT 4
static pthread_mutex_t thread_pool_mutex = PTHREAD_MUTEX_INITIALIZER;
static void* thread_pool[MI_TEST_PPROF_THREAD_POOL_SIZE];
typedef struct {
unsigned seed;
} thread_arg_t;
static void* thread_alloc_free_fun(void* varg) {
thread_arg_t* arg = (thread_arg_t*)varg;
unsigned seed = arg->seed;
for (size_t i = 0; i < MI_TEST_PPROF_THREAD_ITERS; i++) {
const size_t idx = (size_t)(rand_r(&seed) % MI_TEST_PPROF_THREAD_POOL_SIZE);
pthread_mutex_lock(&thread_pool_mutex);
void* p = thread_pool[idx];
if (p != NULL) { thread_pool[idx] = NULL; }
pthread_mutex_unlock(&thread_pool_mutex);
if (p != NULL) {
mi_free(p); }
else {
const size_t size = 8 + (rand_r(&seed) % 249);
void* np = mi_malloc(size);
pthread_mutex_lock(&thread_pool_mutex);
if (thread_pool[idx] == NULL) {
thread_pool[idx] = np;
np = NULL;
}
pthread_mutex_unlock(&thread_pool_mutex);
if (np != NULL) { mi_free(np); } }
}
return NULL;
}
static void thread_pool_cleanup(void) {
for (size_t i = 0; i < MI_TEST_PPROF_THREAD_POOL_SIZE; i++) {
mi_free(thread_pool[i]);
thread_pool[i] = NULL;
}
}
bool test_pprof_concurrent_threads(void) {
CHECK_BODY("pprof: snapshot is thread safe with concurrently allocating/freeing threads") {
pprof_remove_snapshot_files(PROFILE_THREADS_BASE_NAME, MI_TEST_PPROF_THREAD_SNAPSHOT_COUNT);
mi_profiler_t* prof = mi_pprof_profiler_new(TEST_THRESHOLD, PROFILE_THREADS_BASE_NAME ".heap", 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
pthread_t threads[MI_TEST_PPROF_THREAD_COUNT];
thread_arg_t args[MI_TEST_PPROF_THREAD_COUNT];
for (int t = 0; t < MI_TEST_PPROF_THREAD_COUNT && result; t++) {
args[t].seed = (unsigned)(t + 1);
result = (pthread_create(&threads[t], NULL, &thread_alloc_free_fun, &args[t]) == 0);
}
for (unsigned seq = 1; seq < MI_TEST_PPROF_THREAD_SNAPSHOT_COUNT; seq++) {
usleep(2000); mi_profiler_snapshot(prof);
}
for (int t = 0; t < MI_TEST_PPROF_THREAD_COUNT; t++) {
pthread_join(threads[t], NULL);
}
thread_pool_cleanup();
mi_profiler_snapshot(prof);
for (unsigned seq = 1; seq <= MI_TEST_PPROF_THREAD_SNAPSHOT_COUNT && result; seq++) {
char fname[1024];
pprof_snapshot_file_name(fname, sizeof(fname), PROFILE_THREADS_BASE_NAME, seq);
char* contents = read_file(fname);
result = (contents != NULL &&
strncmp(contents, "heap profile:", 13) == 0 &&
strstr(contents, "MAPPED_LIBRARIES:") != NULL &&
parse_total_inuse_objects(contents) >= 0);
mi_free(contents);
}
mi_profiler_stop(prof);
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
}
return true;
}
#endif
#if defined(__linux__)
#define PPROF_OOM_BASE_NAME "profile/test-pprof-oom"
#define PPROF_OOM_MAX_BLOCKS (20000)
#define PPROF_OOM_BLOCK_SIZE (64 * 1024)
#define PPROF_OOM_RECURSION_DEPTH (120)
#define PPROF_OOM_RECURSION_REPS (4)
static void* pprof_oom_blocks[PPROF_OOM_MAX_BLOCKS];
static void* volatile pprof_oom_sink;
#define PPROF_OOM_DEFINE_RECURSION(N) \
static void pprof_oom_rec_##N(size_t depth) { \
void* p = mi_malloc(24 + (depth & 31)); \
pprof_oom_sink = p; \
if (depth < PPROF_OOM_RECURSION_DEPTH) { pprof_oom_rec_##N(depth + 1); } \
mi_free(p); \
}
PPROF_OOM_DEFINE_RECURSION(0)
PPROF_OOM_DEFINE_RECURSION(1)
PPROF_OOM_DEFINE_RECURSION(2)
PPROF_OOM_DEFINE_RECURSION(3)
static long file_size(const char* fname) {
FILE* f = fopen(fname, "rb");
if (f == NULL) return -1;
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fclose(f);
return size;
}
bool test_pprof_snapshot_oom(void) {
CHECK_BODY("pprof: snapshot degrades gracefully when profiler allocations fail") {
char proto_fname[1024];
snprintf(proto_fname, sizeof(proto_fname), "%s.0001.pb", PPROF_OOM_BASE_NAME);
char recovery_fname[1024];
snprintf(recovery_fname, sizeof(recovery_fname), "%s.0002.pb", PPROF_OOM_BASE_NAME);
remove(proto_fname);
remove(recovery_fname);
mi_profiler_t* prof = mi_pprof_profiler_new(1, PPROF_OOM_BASE_NAME, 0, 0, 0);
result = (prof != NULL);
if (result) {
mi_profile(prof);
mi_profiler_start(prof);
const long old_show_errors = mi_option_get(mi_option_show_errors);
mi_option_set(mi_option_show_errors, 0);
mi_option_set(mi_option_disallow_os_alloc, 1);
size_t block_count = 0;
while (block_count < PPROF_OOM_MAX_BLOCKS) {
void* b = mi_malloc(PPROF_OOM_BLOCK_SIZE);
if (b == NULL) break;
pprof_oom_blocks[block_count++] = b;
}
result = (block_count > 0);
for (size_t rep = 0; rep < PPROF_OOM_RECURSION_REPS; rep++) {
pprof_oom_rec_0(1);
pprof_oom_rec_1(1);
pprof_oom_rec_2(1);
pprof_oom_rec_3(1);
}
mi_profiler_snapshot(prof);
result = result && (file_size(proto_fname) < 0);
for (size_t i = 0; i < block_count; i++) { mi_free(pprof_oom_blocks[i]); }
block_count = 0;
mi_option_set(mi_option_disallow_os_alloc, 0);
mi_option_set(mi_option_show_errors, old_show_errors);
allocate_and_free(20000, 64);
mi_profiler_snapshot(prof);
result = result && (file_size(recovery_fname) > 0);
remove(proto_fname);
remove(recovery_fname);
mi_profile(NULL);
mi_pprof_profiler_delete(prof);
}
}
return true;
}
#endif
int main(void) {
test_pprof_profiler_new_delete();
test_pprof_snapshot_creates_file();
test_pprof_snapshot_format();
test_pprof_snapshot_proto_format();
test_pprof_snapshot_interval();
test_pprof_snapshot_increments_sequence();
test_pprof_snapshot_records_samples();
#if !defined(_WIN32)
test_pprof_concurrent_threads();
#endif
#if defined(__linux__)
test_pprof_snapshot_oom();
#endif
return print_test_summary();
}