Skip to content

Commit 5dc20d1

Browse files
committed
Exposing raw heap stats for threads.
1 parent 90b7a69 commit 5dc20d1

File tree

5 files changed

+107
-59
lines changed

5 files changed

+107
-59
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(MI_NO_PADDING "Force no use of padding even in DEBUG mode etc." OF
3434
option(MI_INSTALL_TOPLEVEL "Install directly into $CMAKE_INSTALL_PREFIX instead of PREFIX/lib/mimalloc-version" OFF)
3535
option(MI_NO_THP "Disable transparent huge pages support on Linux/Android for the mimalloc process only" OFF)
3636
option(MI_EXTRA_CPPDEFS "Extra pre-processor definitions (use as `-DMI_EXTRA_CPPDEFS=\"opt1=val1;opt2=val2\"`)" "")
37+
option(MI_STAT_LEVEL "Optionally override stats level to 1 or 2 when debug not enable" 0)
3738

3839
# negated options for vcpkg features
3940
option(MI_NO_USE_CXX "Use plain C compilation (has priority over MI_USE_CXX)" OFF)
@@ -549,6 +550,10 @@ message(STATUS "Link libraries : ${mi_libraries}")
549550
message(STATUS "Build targets : ${mi_build_targets}")
550551
message(STATUS "")
551552

553+
if (MI_STAT_LEVEL GREATER 0)
554+
add_compile_definitions(MI_STAT=${MI_STAT_LEVEL})
555+
endif ()
556+
552557
# -----------------------------------------------------------------------------
553558
# Main targets
554559
# -----------------------------------------------------------------------------
@@ -624,6 +629,10 @@ install(FILES include/mimalloc-new-delete.h DESTINATION ${mi_install_incdir})
624629
install(FILES cmake/mimalloc-config.cmake DESTINATION ${mi_install_cmakedir})
625630
install(FILES cmake/mimalloc-config-version.cmake DESTINATION ${mi_install_cmakedir})
626631

632+
if (MI_STAT_LEVEL GREATER 0 OR CMAKE_BUILD_TYPE MATCHES "Debug")
633+
install(FILES include/mimalloc-statistics.h DESTINATION ${mi_install_incdir})
634+
install(FILES include/mimalloc/stats.h DESTINATION ${mi_install_incdir}/mimalloc)
635+
endif ()
627636

628637
# single object file for more predictable static overriding
629638
if (MI_BUILD_OBJECT)

include/mimalloc-statistics.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* ----------------------------------------------------------------------------
2+
Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
3+
This is free software; you can redistribute it and/or modify it under the
4+
terms of the MIT license. A copy of the license can be found in the file
5+
"LICENSE" at the root of this distribution.
6+
-----------------------------------------------------------------------------*/
7+
#pragma once
8+
#ifndef MIMALLOC_STATISTICS_H
9+
#define MIMALLOC_STATISTICS_H
10+
11+
#include "mimalloc/stats.h"
12+
#include "mimalloc.h"
13+
14+
mi_decl_export const mi_stats_t* mi_thread_stats(void) mi_attr_noexcept;
15+
mi_decl_export const mi_stats_t* mi_thread_heap_stats(const mi_heap_t* heap) mi_attr_noexcept;
16+
17+
#endif

include/mimalloc/stats.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* ----------------------------------------------------------------------------
2+
Copyright (c) 2018-2024, Microsoft Research, Daan Leijen
3+
This is free software; you can redistribute it and/or modify it under the
4+
terms of the MIT license. A copy of the license can be found in the file
5+
"LICENSE" at the root of this distribution.
6+
-----------------------------------------------------------------------------*/
7+
#pragma once
8+
#ifndef MIMALLOC_STATS_H
9+
#define MIMALLOC_STATS_H
10+
11+
#include <stddef.h> // ptrdiff_t
12+
#include <stdint.h> // uintptr_t, uint16_t, etc
13+
14+
#ifndef MI_STAT
15+
#if (MI_DEBUG>0)
16+
#define MI_STAT 2
17+
#else
18+
#define MI_STAT 0
19+
#endif
20+
#endif
21+
22+
// Maximum number of size classes. (spaced exponentially in 12.5% increments)
23+
#define MI_BIN_HUGE (73U)
24+
25+
typedef struct mi_stat_count_s {
26+
int64_t allocated;
27+
int64_t freed;
28+
int64_t peak;
29+
int64_t current;
30+
} mi_stat_count_t;
31+
32+
typedef struct mi_stat_counter_s {
33+
int64_t total;
34+
int64_t count;
35+
} mi_stat_counter_t;
36+
37+
typedef struct mi_stats_s {
38+
mi_stat_count_t segments;
39+
mi_stat_count_t pages;
40+
mi_stat_count_t reserved;
41+
mi_stat_count_t committed;
42+
mi_stat_count_t reset;
43+
mi_stat_count_t purged;
44+
mi_stat_count_t page_committed;
45+
mi_stat_count_t segments_abandoned;
46+
mi_stat_count_t pages_abandoned;
47+
mi_stat_count_t threads;
48+
mi_stat_count_t normal;
49+
mi_stat_count_t huge;
50+
mi_stat_count_t giant;
51+
mi_stat_count_t malloc;
52+
mi_stat_count_t segments_cache;
53+
mi_stat_counter_t pages_extended;
54+
mi_stat_counter_t mmap_calls;
55+
mi_stat_counter_t commit_calls;
56+
mi_stat_counter_t reset_calls;
57+
mi_stat_counter_t purge_calls;
58+
mi_stat_counter_t page_no_retire;
59+
mi_stat_counter_t searches;
60+
mi_stat_counter_t normal_count;
61+
mi_stat_counter_t huge_count;
62+
mi_stat_counter_t arena_count;
63+
mi_stat_counter_t arena_crossover_count;
64+
mi_stat_counter_t arena_rollback_count;
65+
mi_stat_counter_t guarded_alloc_count;
66+
#if MI_STAT>1
67+
mi_stat_count_t normal_bins[MI_BIN_HUGE+1];
68+
#endif
69+
} mi_stats_t;
70+
71+
#endif

include/mimalloc/types.h

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ terms of the MIT license. A copy of the license can be found in the file
2424
#include <stddef.h> // ptrdiff_t
2525
#include <stdint.h> // uintptr_t, uint16_t, etc
2626
#include "atomic.h" // _Atomic
27+
#include "stats.h"
2728

2829
#ifdef _MSC_VER
2930
#pragma warning(disable:4214) // bitfield is not int
@@ -200,9 +201,6 @@ typedef int32_t mi_ssize_t;
200201
#define MI_LARGE_OBJ_SIZE_MAX (MI_LARGE_PAGE_SIZE/4) // 1 MiB
201202
#define MI_LARGE_OBJ_WSIZE_MAX (MI_LARGE_OBJ_SIZE_MAX/MI_INTPTR_SIZE)
202203

203-
// Maximum number of size classes. (spaced exponentially in 12.5% increments)
204-
#define MI_BIN_HUGE (73U)
205-
206204
#if (MI_LARGE_OBJ_WSIZE_MAX >= 655360)
207205
#error "mimalloc internal: define more bins"
208206
#endif
@@ -552,63 +550,9 @@ void _mi_assert_fail(const char* assertion, const char* fname, unsigned int line
552550

553551
// ------------------------------------------------------
554552
// Statistics
553+
// declare statistics functions here (not stats.h) to avoid exposing these to library consumers
555554
// ------------------------------------------------------
556555

557-
#ifndef MI_STAT
558-
#if (MI_DEBUG>0)
559-
#define MI_STAT 2
560-
#else
561-
#define MI_STAT 0
562-
#endif
563-
#endif
564-
565-
typedef struct mi_stat_count_s {
566-
int64_t allocated;
567-
int64_t freed;
568-
int64_t peak;
569-
int64_t current;
570-
} mi_stat_count_t;
571-
572-
typedef struct mi_stat_counter_s {
573-
int64_t total;
574-
int64_t count;
575-
} mi_stat_counter_t;
576-
577-
typedef struct mi_stats_s {
578-
mi_stat_count_t segments;
579-
mi_stat_count_t pages;
580-
mi_stat_count_t reserved;
581-
mi_stat_count_t committed;
582-
mi_stat_count_t reset;
583-
mi_stat_count_t purged;
584-
mi_stat_count_t page_committed;
585-
mi_stat_count_t segments_abandoned;
586-
mi_stat_count_t pages_abandoned;
587-
mi_stat_count_t threads;
588-
mi_stat_count_t normal;
589-
mi_stat_count_t huge;
590-
mi_stat_count_t giant;
591-
mi_stat_count_t malloc;
592-
mi_stat_count_t segments_cache;
593-
mi_stat_counter_t pages_extended;
594-
mi_stat_counter_t mmap_calls;
595-
mi_stat_counter_t commit_calls;
596-
mi_stat_counter_t reset_calls;
597-
mi_stat_counter_t purge_calls;
598-
mi_stat_counter_t page_no_retire;
599-
mi_stat_counter_t searches;
600-
mi_stat_counter_t normal_count;
601-
mi_stat_counter_t huge_count;
602-
mi_stat_counter_t arena_count;
603-
mi_stat_counter_t arena_crossover_count;
604-
mi_stat_counter_t arena_rollback_count;
605-
mi_stat_counter_t guarded_alloc_count;
606-
#if MI_STAT>1
607-
mi_stat_count_t normal_bins[MI_BIN_HUGE+1];
608-
#endif
609-
} mi_stats_t;
610-
611-
612556
// add to stat keeping track of the peak
613557
void _mi_stat_increase(mi_stat_count_t* stat, size_t amount);
614558
void _mi_stat_decrease(mi_stat_count_t* stat, size_t amount);
@@ -636,7 +580,6 @@ void _mi_stat_counter_increase(mi_stat_counter_t* stat, size_t amount);
636580
#define mi_heap_stat_increase(heap,stat,amount) mi_stat_increase( (heap)->tld->stats.stat, amount)
637581
#define mi_heap_stat_decrease(heap,stat,amount) mi_stat_decrease( (heap)->tld->stats.stat, amount)
638582

639-
640583
// ------------------------------------------------------
641584
// Sub processes do not reclaim or visit segments
642585
// from other sub processes

src/stats.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ static void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0)
399399

400400
static mi_msecs_t mi_process_start; // = 0
401401

402+
402403
static mi_stats_t* mi_stats_get_default(void) {
403404
mi_heap_t* heap = mi_heap_get_default();
404405
return &heap->tld->stats;
@@ -440,6 +441,13 @@ void mi_thread_stats_print_out(mi_output_fun* out, void* arg) mi_attr_noexcept {
440441
_mi_stats_print(mi_stats_get_default(), out, arg);
441442
}
442443

444+
const mi_stats_t* mi_thread_heap_stats(const mi_heap_t* heap) mi_attr_noexcept {
445+
return &heap->tld->stats;
446+
}
447+
448+
const mi_stats_t* mi_thread_stats(void) mi_attr_noexcept {
449+
return mi_thread_heap_stats(mi_heap_get_default());
450+
}
443451

444452
// ----------------------------------------------------------------
445453
// Basic timer for convenience; use milli-seconds to avoid doubles

0 commit comments

Comments
 (0)