Skip to content

Commit

Permalink
Switch map implementation and improve leak counters
Browse files Browse the repository at this point in the history
  • Loading branch information
boazsegev committed Jul 25, 2024
1 parent 97027c0 commit e1e7fbe
Show file tree
Hide file tree
Showing 14 changed files with 4,243 additions and 4,079 deletions.
4,156 changes: 2,119 additions & 2,037 deletions fio-stl.h

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions fio-stl/000 core.h
Original file line number Diff line number Diff line change
Expand Up @@ -916,20 +916,22 @@ typedef struct fio_index8_node_s {
/** Loops through every index in the indexed list, assuming `head` is valid. */
#define FIO_INDEXED_LIST_EACH(root, node_name, head, pos) \
for (size_t pos = (head), \
stooper___hd = (head), \
stopper___ils___ = 0, \
pos##___nxt = (root)[(head)].node_name.next; \
!stopper___ils___; \
(stopper___ils___ = ((pos = pos##___nxt) == (head))), \
(stopper___ils___ = ((pos = pos##___nxt) == stooper___hd)), \
pos##___nxt = (root)[pos].node_name.next)

/** Loops through every index in the indexed list, assuming `head` is valid. */
#define FIO_INDEXED_LIST_EACH_REVERSED(root, node_name, head, pos) \
for (size_t pos = ((root)[head].node_name.prev), \
for (size_t pos = ((root)[(head)].node_name.prev), \
pos##___nxt = \
((root)[((root)[head].node_name.prev)].node_name.prev), \
((root)[((root)[(head)].node_name.prev)].node_name.prev), \
stooper___hd = (head), \
stopper___ils___ = 0; \
!stopper___ils___; \
((stopper___ils___ = (pos == head)), \
((stopper___ils___ = (pos == stooper___hd)), \
(pos = pos##___nxt), \
(pos##___nxt = (root)[pos##___nxt].node_name.prev)))
#endif
Expand Down
5 changes: 4 additions & 1 deletion fio-stl/000 dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Memory Allocation - FIO_MALLOC as a "global" default memory allocator
***************************************************************************** */
/* FIO_MALLOC defines a "global" default memory allocator */
#if defined(FIO_MALLOC) && !defined(H___FIO_MALLOC___H)
#define H___FIO_MALLOC___H
#ifndef FIO_MEMORY_NAME
#define FIO_MEMORY_NAME fio
#endif
Expand Down Expand Up @@ -225,8 +226,10 @@ Memory Allocation - FIO_MALLOC defines a FIOBJ dedicated memory allocator
/* cache up to 64Mb */
#define FIO_MEMORY_CACHE_SLOTS 16
#endif
#endif /* FIOBJ_MALLOC */
#endif /* FIOBJ_MALLOC / FIO_MALLOC*/

#undef FIOBJ_MALLOC
#undef FIO_MALLOC
/* *****************************************************************************
FIO_SORT_NAME naming
***************************************************************************** */
Expand Down
3 changes: 2 additions & 1 deletion fio-stl/001 header.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Leak Counter Helpers
} \
static void FIO_NAME(fio___leak_counter_cleanup, name)(void *i) { \
size_t counter = FIO_NAME(fio___leak_counter, name)((size_t)(uintptr_t)i); \
FIO_LOG_DDEBUG2("testing leaks for " FIO_MACRO2STR(name)); \
FIO_LOG_DEBUG2("testing leaks for " FIO_MACRO2STR(name)); \
if (counter) \
FIO_LOG_ERROR("%zu leaks detected for " FIO_MACRO2STR(name), counter); \
} \
Expand All @@ -192,6 +192,7 @@ Leak Counter Helpers
FIO_NAME(fio___leak_counter_cleanup, name), \
NULL); \
}
#define FIO_LEAK_COUNTER_COUNT(name) FIO_NAME(fio___leak_counter, name)(0)
#define FIO_LEAK_COUNTER_ON_ALLOC(name) FIO_NAME(fio___leak_counter, name)(1)
#define FIO_LEAK_COUNTER_ON_FREE(name) \
FIO_NAME(fio___leak_counter, name)(((size_t)-1))
Expand Down
81 changes: 53 additions & 28 deletions fio-stl/004 state callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@ FIO_TYPEDEF_IMAP_ARRAY(fio___state_map,
#undef FIO_STATE_CALLBACK_CMP
#undef FIO_STATE_CALLBACK_IS_VALID

/* *****************************************************************************
Names.
***************************************************************************** */

static const char *FIO___STATE_TASKS_NAMES[FIO_CALL_NEVER + 1] = {
[FIO_CALL_ON_INITIALIZE] = "ON_INITIALIZE",
[FIO_CALL_PRE_START] = "PRE_START",
[FIO_CALL_BEFORE_FORK] = "BEFORE_FORK",
[FIO_CALL_AFTER_FORK] = "AFTER_FORK",
[FIO_CALL_IN_CHILD] = "IN_CHILD",
[FIO_CALL_IN_MASTER] = "IN_MASTER",
[FIO_CALL_ON_WORKER_THREAD_START] = "ON_WORKER_THREAD_START",
[FIO_CALL_ON_START] = "ON_START",
[FIO_CALL_RESERVED1] = "RESERVED1",
[FIO_CALL_RESERVED2] = "RESERVED2",
[FIO_CALL_ON_USER1] = "ON_USER1",
[FIO_CALL_ON_USER2] = "ON_USER2",
[FIO_CALL_ON_IDLE] = "ON_IDLE",
[FIO_CALL_ON_USER1_REVERSE] = "ON_USER1_REVERSE",
[FIO_CALL_ON_USER2_REVERSE] = "ON_USER2_REVERSE",
[FIO_CALL_RESERVED1_REVERSED] = "RESERVED1_REVERSED",
[FIO_CALL_RESERVED2_REVERSED] = "RESERVED2_REVERSED",
[FIO_CALL_ON_SHUTDOWN] = "ON_SHUTDOWN",
[FIO_CALL_ON_PARENT_CRUSH] = "ON_PARENT_CRUSH",
[FIO_CALL_ON_CHILD_CRUSH] = "ON_CHILD_CRUSH",
[FIO_CALL_ON_WORKER_THREAD_END] = "ON_WORKER_THREAD_END",
[FIO_CALL_ON_FINISH] = "ON_FINISH",
[FIO_CALL_AT_EXIT] = "AT_EXIT",
[FIO_CALL_NEVER] = "NEVER",
};

/* *****************************************************************************
State Callback Global State and Locks
***************************************************************************** */
Expand Down Expand Up @@ -206,30 +237,6 @@ FIO_SFUNC void fio_state_callback_force___task(void *fn_p, void *arg) {
*/
SFUNC void fio_state_callback_force(fio_state_event_type_e e) {
/** a type-to-string map for callback types */
static const char *fio___state_tasks_names[FIO_CALL_NEVER + 1] = {
[FIO_CALL_ON_INITIALIZE] = "ON_INITIALIZE",
[FIO_CALL_PRE_START] = "PRE_START",
[FIO_CALL_BEFORE_FORK] = "BEFORE_FORK",
[FIO_CALL_AFTER_FORK] = "AFTER_FORK",
[FIO_CALL_IN_CHILD] = "IN_CHILD",
[FIO_CALL_IN_MASTER] = "IN_MASTER",
[FIO_CALL_ON_START] = "ON_START",
[FIO_CALL_RESERVED1] = "RESERVED1",
[FIO_CALL_RESERVED2] = "RESERVED2",
[FIO_CALL_ON_USER1] = "ON_USER1",
[FIO_CALL_ON_USER2] = "ON_USER2",
[FIO_CALL_ON_IDLE] = "ON_IDLE",
[FIO_CALL_ON_USER1_REVERSE] = "ON_USER1_REVERSE",
[FIO_CALL_ON_USER2_REVERSE] = "ON_USER2_REVERSE",
[FIO_CALL_RESERVED1_REVERSED] = "RESERVED1_REVERSED",
[FIO_CALL_RESERVED2_REVERSED] = "RESERVED2_REVERSED",
[FIO_CALL_ON_SHUTDOWN] = "ON_SHUTDOWN",
[FIO_CALL_ON_PARENT_CRUSH] = "ON_PARENT_CRUSH",
[FIO_CALL_ON_CHILD_CRUSH] = "ON_CHILD_CRUSH",
[FIO_CALL_ON_FINISH] = "ON_FINISH",
[FIO_CALL_AT_EXIT] = "AT_EXIT",
[FIO_CALL_NEVER] = "NEVER",
};

if ((uintptr_t)e >= FIO_CALL_NEVER)
return;
Expand All @@ -248,10 +255,12 @@ SFUNC void fio_state_callback_force(fio_state_event_type_e e) {
fio_trylock(FIO___STATE_TASKS_ARRAY_LOCK + FIO_CALL_NEVER);
}

FIO_LOG_DDEBUG2("%d scheduling %s callbacks.",
FIO_LOG_DDEBUG2("%d scheduling %s callbacks (%zu tasks).",
(int)(fio_thread_getpid()),
fio___state_tasks_names[e]);

FIO___STATE_TASKS_NAMES[e],
(size_t)FIO___STATE_TASKS_ARRAY[e].count);
if (!FIO___STATE_TASKS_ARRAY[e].count)
return;
/* copy task queue */
fio_lock(FIO___STATE_TASKS_ARRAY_LOCK + (uintptr_t)e);
if (FIO___STATE_TASKS_ARRAY[e].w) {
Expand Down Expand Up @@ -280,8 +289,24 @@ SFUNC void fio_state_callback_force(fio_state_event_type_e e) {
}
/* cleanup */
FIO_MEM_FREE(ary, ary_capa);
(void)fio___state_tasks_names; /* if unused */
(void)FIO___STATE_TASKS_NAMES; /* if unused */
}

/* *****************************************************************************
Debug Helpers
***************************************************************************** */

FIO_IFUNC void fio_state_callback_print_state(void) {
FIO_LOG2STDERR("DEBUG: fio_state_callback maps state:");
for (size_t i = 0; i < FIO_CALL_NEVER; ++i) {
fprintf(stderr,
"\t%-32s %-4zu out of %-4zu\n",
FIO___STATE_TASKS_NAMES[i],
(size_t)FIO___STATE_TASKS_ARRAY[i].count,
fio___state_map_capa(FIO___STATE_TASKS_ARRAY + i));
}
}

/* *****************************************************************************
State constructor / destructor
***************************************************************************** */
Expand Down
12 changes: 6 additions & 6 deletions fio-stl/010 mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,20 +793,20 @@ static volatile size_t FIO_NAME(FIO_MEMORY_NAME, __malloc_total);
#define FIO_MEMORY_ON_CHUNK_ALLOC(ptr) \
do { \
FIO_LEAK_COUNTER_ON_ALLOC(FIO_NAME(FIO_MEMORY_NAME, __malloc_chunk)); \
FIO_LOG_DEBUG2("MEMORY CACHE-ALLOC allocated %p", ptr); \
FIO_LOG_DEBUG2("MEMORY CHUNK-ALLOC allocated %p", ptr); \
} while (0);
#define FIO_MEMORY_ON_CHUNK_FREE(ptr) \
do { \
FIO_LEAK_COUNTER_ON_FREE(FIO_NAME(FIO_MEMORY_NAME, __malloc_chunk)); \
FIO_LOG_DEBUG2("MEMORY CACHE-DEALLOC de-allocated %p", ptr); \
FIO_LOG_DEBUG2("MEMORY CHUNK-DEALLOC de-allocated %p", ptr); \
} while (0);
#define FIO_MEMORY_ON_CHUNK_CACHE(ptr) \
do { \
FIO_LOG_DEBUG2("MEMORY CACHE-DEALLOC placed %p in cache", ptr); \
FIO_LOG_DEBUG2("MEMORY CACHE-PUSH placed %p in cache", ptr); \
} while (0);
#define FIO_MEMORY_ON_CHUNK_UNCACHE(ptr) \
do { \
FIO_LOG_DEBUG2("MEMORY CACHE-ALLOC retrieved %p from cache", ptr); \
FIO_LOG_DEBUG2("MEMORY CACHE-POP retrieved %p from cache", ptr); \
} while (0);

#define FIO_MEMORY_ON_BLOCK_RESET_IN_LOCK(ptr, blk) \
Expand Down Expand Up @@ -2544,8 +2544,8 @@ Memory pool cleanup
#undef FIO_MEMORY_BLOCKS_PER_ALLOCATION_LOG
#undef FIO_MEMORY_BLOCKS_PER_ALLOCATION
#undef FIO_MEMORY_ENABLE_BIG_ALLOC
#undef FIO_MEMORY_ARENA_COUNT_FALLBACK
#undef FIO_MEMORY_ARENA_COUNT_MAX
// #undef FIO_MEMORY_ARENA_COUNT_FALLBACK
// #undef FIO_MEMORY_ARENA_COUNT_MAX
#undef FIO_MEMORY_WARMUP

#undef FIO_MEMORY_LOCK_NAME
Expand Down
3 changes: 3 additions & 0 deletions fio-stl/102 stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,21 @@ Stream Implementation - inlined static functions

/* do we have a constructor? */
#ifndef FIO_REF_CONSTRUCTOR_ONLY
FIO_LEAK_COUNTER_DEF(fio_stream)
/* Allocates a new object on the heap and initializes it's memory. */
FIO_IFUNC fio_stream_s *fio_stream_new(void) {
fio_stream_s *s = (fio_stream_s *)FIO_MEM_REALLOC_(NULL, 0, sizeof(*s), 0);
if (s) {
*s = (fio_stream_s)FIO_STREAM_INIT(s[0]);
}
FIO_LEAK_COUNTER_ON_ALLOC(fio_stream);
return s;
}
/* Frees any internal data AND the object's container! */
FIO_IFUNC int fio_stream_free(fio_stream_s *s) {
fio_stream_destroy(s);
FIO_MEM_FREE_(s, sizeof(*s));
FIO_LEAK_COUNTER_ON_FREE(fio_stream);
return 0;
}
#endif /* FIO_REF_CONSTRUCTOR_ONLY */
Expand Down
Loading

0 comments on commit e1e7fbe

Please sign in to comment.