Skip to content
Permalink
Browse files

OPS-6: USE_SHM_STATS is once again functional

  • Loading branch information
liviuchircu committed Mar 14, 2019
1 parent 7a1cf1d commit 8d43fe53cccf445e95ddd3c5318d70960afcbf9a
Showing with 275 additions and 190 deletions.
  1. +5 −7 Makefile.conf.template
  2. +2 −0 mem/common.c
  3. +3 −0 mem/common.h
  4. +12 −27 mem/f_malloc.c
  5. +24 −11 mem/f_malloc.h
  6. +4 −3 mem/f_malloc_dyn.h
  7. +12 −25 mem/hp_malloc.c
  8. +26 −11 mem/hp_malloc.h
  9. +6 −6 mem/hp_malloc_dyn.h
  10. +11 −14 mem/module_info.c
  11. +9 −20 mem/q_malloc.c
  12. +25 −14 mem/q_malloc.h
  13. +3 −3 mem/qm_malloc_dyn.h
  14. +53 −0 mem/shm_mem.c
  15. +80 −49 mem/shm_mem.h
@@ -63,28 +63,26 @@ exclude_modules?= aaa_radius b2b_logic cachedb_cassandra cachedb_couchbase cache

include_modules?=

#DEFS_GROUP_START
DEFS+= -DPKG_MALLOC #Uses a faster malloc
#DEFS_GROUP_END
DEFS+= -DSHM_MMAP #Use mmap instead of SYSV shared memory
DEFS+= -DUSE_MCAST #Compile in support for IP Multicast
DEFS+= -DDISABLE_NAGLE #Disabled the TCP NAgle Algorithm ( lower delay )
DEFS+= -DSTATISTICS #Enables the statistics manager
DEFS+= -DHAVE_RESOLV_RES #Support for changing some of the resolver parameters
#DEFS_GROUP_START
DEFS+= -DQM_MALLOC #Quality assurance memory allocator with runtime safety checks
#DEFS+= -DF_MALLOC #Fast memory allocator with minimal runtime overhead
#DEFS+= -DHP_MALLOC #High performance allocator with fine-grained locking
#DEFS_GROUP_END
DEFS+= -DF_MALLOC #Fast memory allocator with minimal runtime overhead
DEFS+= -DHP_MALLOC #High performance allocator with fine-grained locking
DEFS+= -DDBG_MALLOC #Enables debugging for memory allocators
#DEFS+= -DNO_DEBUG #Turns off all debug messages
#DEFS+= -DNO_LOG #Completely turns off all the logging
#DEFS_GROUP_START
#DEFS+= -DFAST_LOCK #Uses fast architecture specific locking
#DEFS+= -DUSE_FUTEX #Uses linux futexs with fast architecture specific locking
#DEFS+= -DUSE_SYSV_SEM #Uses SYSV sems for locking ( slower & limited number of locks
#DEFS+= -DUSE_PTHREAD_MUTEX #Uses pthread mutexes for locking
#DEFS+= -DUSE_UMUTEX #Uses FreeBSD-specific low-level mutexes for locking
#DEFS+= -DUSE_POSIX_SEM #Uses POSIX sems for locking
#DEFS_GROUP_END
#DEFS+= -DUSE_FUTEX #Uses linux futexes with fast architecture specific locking
#DEFS+= -DBUSY_WAIT #Uses busy waiting on the lock
#DEFS+= -DDBG_LOCK #Attach debug info to all lock structures
#DEFS+= -DDBG_STRUCT_HIST #Include support for struct history logging
@@ -1,4 +1,6 @@
/*
* shared code between all memory allocators
*
* Copyright (C) 2019 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
@@ -1,4 +1,6 @@
/*
* shared code between all memory allocators
*
* Copyright (C) 2014-2019 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
@@ -61,6 +63,7 @@ typedef void (*osips_free_f) (void *block, void *ptr);
typedef void (*osips_mem_info_f) (void *block, struct mem_info *i);
typedef void (*osips_mem_status_f) (void *block);
typedef unsigned long (*osips_get_mmstat_f) (void *block);
typedef void (*osips_shm_stats_init_f) (void *block, int core_index);

#if defined F_MALLOC
#include "f_malloc.h"
@@ -44,10 +44,6 @@
#define FRAG_NEXT(f) \
((struct fm_frag *)((char *)(f) + sizeof(struct fm_frag) + (f)->size))

/* get the fragment which corresponds to a pointer */
#define FRAG_OF(p) \
((struct fm_frag *)((char *)(p) - sizeof(struct fm_frag)))

#define max(a,b) ( (a)>(b)?(a):(b))

/* ROUNDTO= 2^k so the following works */
@@ -71,14 +67,6 @@
#define F_MALLOC_DEFRAG_LIMIT (F_MALLOC_LARGE_LIMIT * 5)
#define F_MALLOC_DEFRAG_PERCENT 5

#ifdef SHM_EXTRA_STATS
unsigned long frag_size(void* p){
if(!p)
return 0;
return (((struct fm_frag*) ((char*)p-sizeof(struct fm_frag)))->size);
}
#endif

static inline void free_minus(struct fm_block* qm, unsigned long size )
{

@@ -127,24 +115,20 @@ inline static unsigned long big_hash_idx(unsigned long s)

#ifdef SHM_EXTRA_STATS
#include "module_info.h"
void set_stat_index (void *ptr, unsigned long idx) {
struct fm_frag *f;

unsigned long fm_stats_get_index(void *ptr)
{
if (!ptr)
return;
return GROUP_IDX_INVALID;

f = (struct fm_frag *)((char*)ptr - sizeof(struct fm_frag));
f->statistic_index = idx;
return FM_FRAG(ptr)->statistic_index;
}

unsigned long get_stat_index(void *ptr) {
struct fm_frag *f;

void fm_stats_set_index(void *ptr, unsigned long idx)
{
if (!ptr)
return GROUP_IDX_INVALID;
return;

f = (struct fm_frag *)((char*)ptr - sizeof(struct fm_frag));
return f->statistic_index;
FM_FRAG(ptr)->statistic_index = idx;
}
#endif

@@ -287,10 +271,11 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size, char *name)
#endif

#ifdef SHM_EXTRA_STATS
void set_indexes(int core_index) {
void fm_stats_core_init(struct fm_block *fm, int core_index)
{
struct fm_frag *f;

struct fm_frag* f;
for (f=shm_block->first_frag; (char*)f<(char*)shm_block->last_frag; f=FRAG_NEXT(f))
for (f=fm->first_frag; (char *)f < (char *)fm->last_frag; f=FRAG_NEXT(f))
if (!f->is_free)
f->statistic_index = core_index;
}
@@ -50,6 +50,10 @@
#define F_HASH_SIZE (F_MALLOC_OPTIMIZE/ROUNDTO + \
(sizeof(long)*8-F_MALLOC_OPTIMIZE_FACTOR)+1)

/* get the fragment which corresponds to a pointer */
#define FM_FRAG(p) \
((struct fm_frag *)((char *)(p) - sizeof(struct fm_frag)))

/* hash structure:
* 0 .... F_MALLOC_OPTIMIZE/ROUNDTO - small buckets, size increases with
* ROUNDTO from bucket to bucket
@@ -69,6 +73,8 @@ struct fm_frag {
unsigned long check;
#endif

#define FM_FRAG_OVERHEAD (sizeof(struct fm_frag))

#if (defined DBG_MALLOC) || (defined SHM_EXTRA_STATS)
char is_free;
#endif
@@ -103,7 +109,13 @@ struct fm_block {
};

#ifdef SHM_EXTRA_STATS
unsigned long frag_size(void* p);
static inline unsigned long fm_frag_size(void *p)
{
if (!p)
return 0;

return FM_FRAG(p)->size;
}
#endif

struct fm_block* fm_malloc_init(char* address, unsigned long size, char* name);
@@ -133,18 +145,19 @@ void fm_status(struct fm_block*);
void fm_info(struct fm_block*, struct mem_info*);

#ifdef SHM_EXTRA_STATS
void set_stat_index (void *ptr, unsigned long idx);
unsigned long get_stat_index(void *ptr);
void set_indexes(int core_index);
#endif
void fm_stats_core_init(struct fm_block *fm, int core_index);
unsigned long fm_stats_get_index(void *ptr);
void fm_stats_set_index(void *ptr, unsigned long idx);

#ifdef DBG_MALLOC
#undef _FRAG_FILE
#undef _FRAG_FUNC
#undef _FRAG_LINE
#define _FRAG_FILE(_p) ((struct fm_frag*)((char *)_p - sizeof(struct fm_frag)))->file
#define _FRAG_FUNC(_p) ((struct fm_frag*)((char *)_p - sizeof(struct fm_frag)))->func
#define _FRAG_LINE(_p) ((struct fm_frag*)((char *)_p - sizeof(struct fm_frag)))->line
static inline const char *fm_frag_file(void *p) { return FM_FRAG(p)->file; }
static inline const char *fm_frag_func(void *p) { return FM_FRAG(p)->func; }
static inline unsigned long fm_frag_line(void *p) { return FM_FRAG(p)->line; }
#else
static inline const char *fm_frag_file(void *p) { return NULL; }
static inline const char *fm_frag_func(void *p) { return NULL; }
static inline unsigned long fm_frag_line(void *p) { return 0; }
#endif
#endif

#ifdef STATISTICS
@@ -237,7 +237,8 @@ void fm_free(struct fm_block* qm, void* p, const char* file,
LM_GEN1(memlog, "free(0) called\n");
return;
}
f=(struct fm_frag*) ((char*)p-sizeof(struct fm_frag));

f = FM_FRAG(p);

check_double_free(p, f, qm);

@@ -302,7 +303,7 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size,

#ifdef DBG_MALLOC
LM_GEN1(memlog, "%s_realloc(%p, %lu->%lu), called from %s: %s(%d)\n",
qm->name, p, p ? FRAG_OF(p)->size : 0, size, file, func, line);
qm->name, p, p ? FM_FRAG(p)->size : 0, size, file, func, line);
if (p && (p > (void *)qm->last_frag || p < (void *)qm->first_frag)) {
LM_CRIT("bad pointer %p (out of memory block!) - aborting\n", p);
abort();
@@ -331,7 +332,7 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size,
return fm_malloc(qm, size, file, func, line);
#endif

f = FRAG_OF(p);
f = FM_FRAG(p);

#ifdef DBG_MALLOC
LM_GEN1(memlog, "realloc'ing frag %p alloc'ed from %s: %s(%ld)\n",
@@ -45,10 +45,6 @@
#define FRAG_NEXT(f) ((struct hp_frag *) \
((char *)(f) + sizeof(struct hp_frag) + ((struct hp_frag *)(f))->size))

/* get the fragment which corresponds to a pointer */
#define FRAG_OF(p) \
((struct hp_frag *)((char *)(p) - sizeof(struct hp_frag)))

#define FRAG_OVERHEAD (sizeof(struct hp_frag))
#define frag_is_free(_f) ((_f)->prev)

@@ -166,31 +162,21 @@ inline static unsigned long big_hash_idx(unsigned long s)
}

#ifdef SHM_EXTRA_STATS
unsigned long frag_size(void* p){
if(!p)
return 0;
return ((struct hp_frag*) ((char*)p - sizeof(struct hp_frag)))->size;
}

#include "module_info.h"
void set_stat_index (void *ptr, unsigned long idx) {
struct hp_frag *f;

unsigned long hp_stats_get_index(void *ptr)
{
if (!ptr)
return;
return GROUP_IDX_INVALID;

f = (struct hp_frag *)((char*)ptr - sizeof(struct hp_frag));
f->statistic_index = idx;
return HP_FRAG(ptr)->statistic_index;
}

unsigned long get_stat_index(void *ptr) {
struct hp_frag *f;

void hp_stats_set_index(void *ptr, unsigned long idx)
{
if (!ptr)
return GROUP_IDX_INVALID;
return;

f = (struct hp_frag *)((char*)ptr - sizeof(struct hp_frag));
return f->statistic_index;
HP_FRAG(ptr)->statistic_index = idx;
}
#endif

@@ -635,10 +621,11 @@ struct hp_block *hp_shm_malloc_init(char *address, unsigned long size,


#ifdef SHM_EXTRA_STATS
void set_indexes(int core_index) {
void hp_stats_core_init(struct hp_block *hp, int core_index)
{
struct hp_frag *f;

struct hp_frag* f;
for (f=shm_block->first_frag; (char*)f<(char*)shm_block->last_frag; f=FRAG_NEXT(f))
for (f=hp->first_frag; (char*)f<(char*)hp->last_frag; f=FRAG_NEXT(f))
if (!f->is_free)
f->statistic_index = core_index;
}
@@ -44,6 +44,7 @@ extern stat_var *shm_frags;
#include "meminfo.h"

#undef ROUNDTO

#define ROUNDTO 8UL

#define HP_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
@@ -60,6 +61,10 @@ extern stat_var *shm_frags;

#define HP_TOTAL_HASH_SIZE (HP_HASH_SIZE + HP_EXTRA_HASH_SIZE)

/* get the fragment which corresponds to a pointer */
#define HP_FRAG(p) \
((struct hp_frag *)((char *)(p) - sizeof(struct hp_frag)))

/* hash structure:
* 0 .... HP_MALLOC_OPTIMIZE/ROUNDTO - small buckets, size increases with
* ROUNDTO from bucket to bucket
@@ -97,6 +102,8 @@ struct hp_frag {
#endif
};

#define HP_FRAG_OVERHEAD (sizeof(struct hp_frag))

struct hp_frag_lnk {
/*
* optimized buckets are further split into
@@ -198,19 +205,27 @@ void *hp_pkg_realloc(struct hp_block *, void *p, unsigned long size);
#endif

#ifdef SHM_EXTRA_STATS
unsigned long frag_size(void* p);
void set_stat_index (void *ptr, unsigned long idx);
unsigned long get_stat_index(void *ptr);
void set_indexes(int core_index);
#endif
static inline unsigned long hp_frag_size(void *p)
{
if (!p)
return 0;

return HP_FRAG(p)->size;
}

void hp_stats_core_init(struct hp_block *hp, int core_index);
unsigned long hp_stats_get_index(void *ptr);
void hp_stats_set_index(void *ptr, unsigned long idx);

#ifdef DBG_MALLOC
#undef _FRAG_FILE
#undef _FRAG_FUNC
#undef _FRAG_LINE
#define _FRAG_FILE(_p) ((struct hp_frag*)((char *)_p - sizeof(struct hp_frag)))->file
#define _FRAG_FUNC(_p) ((struct hp_frag*)((char *)_p - sizeof(struct hp_frag)))->func
#define _FRAG_LINE(_p) ((struct hp_frag*)((char *)_p - sizeof(struct hp_frag)))->line
static inline const char *hp_frag_file(void *p) { return HP_FRAG(p)->file; }
static inline const char *hp_frag_func(void *p) { return HP_FRAG(p)->func; }
static inline unsigned long hp_frag_line(void *p) { return HP_FRAG(p)->line; }
#else
static inline const char *hp_frag_file(void *p) { return NULL; }
static inline const char *hp_frag_func(void *p) { return NULL; }
static inline unsigned long hp_frag_line(void *p) { return 0; }
#endif
#endif

void hp_status(struct hp_block *);

0 comments on commit 8d43fe5

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