mblock stat function test ok

pull/5/head
yuqing 2015-10-30 15:34:22 +08:00
parent e2ef7c87b6
commit c17d55ec7d
2 changed files with 46 additions and 16 deletions

View File

@ -4,11 +4,11 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <pthread.h> #include <pthread.h>
#include <assert.h> #include <assert.h>
#include "fast_mblock.h"
#include "logger.h" #include "logger.h"
#include "shared_func.h" #include "shared_func.h"
#include "pthread_func.h" #include "pthread_func.h"
#include "sched_thread.h" #include "sched_thread.h"
#include "fast_mblock.h"
struct _fast_mblock_manager struct _fast_mblock_manager
{ {
@ -18,7 +18,7 @@ struct _fast_mblock_manager
}; };
#define INIT_HEAD(head) (head)->next = (head)->prev = head #define INIT_HEAD(head) (head)->next = (head)->prev = head
#define IS_EMPTY(head) ((head)->next == (head)->prev) #define IS_EMPTY(head) ((head)->next == head)
static struct _fast_mblock_manager mblock_manager = {false}; static struct _fast_mblock_manager mblock_manager = {false};
@ -75,9 +75,10 @@ static void add_to_mblock_list(struct fast_mblock_man *mblock)
} }
mblock->next = current; mblock->next = current;
current->prev = mblock;
mblock->prev = current->prev; mblock->prev = current->prev;
current->prev->next = mblock; current->prev->next = mblock;
current->prev = mblock;
pthread_mutex_unlock(&(mblock_manager.lock)); pthread_mutex_unlock(&(mblock_manager.lock));
} }
@ -96,6 +97,16 @@ static void delete_from_mblock_list(struct fast_mblock_man *mblock)
INIT_HEAD(mblock); INIT_HEAD(mblock);
} }
#define STAT_DUP(pStat, current) \
do { \
strcpy(pStat->name, current->info.name); \
pStat->element_size = current->info.element_size; \
pStat->total_count += current->info.total_count; \
pStat->used_count += current->info.used_count; \
/* logInfo("name: %s, element_size: %d, total_count: %d, used_count: %d", */ \
/* pStat->name, pStat->element_size, pStat->total_count, pStat->used_count); */\
} while (0)
int fast_mblock_manager_stat(struct fast_mblock_info *stats, int fast_mblock_manager_stat(struct fast_mblock_info *stats,
const int size, int *count) const int size, int *count)
{ {
@ -122,10 +133,6 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats,
current = mblock_manager.head.next; current = mblock_manager.head.next;
while (current != &mblock_manager.head) while (current != &mblock_manager.head)
{ {
strcpy(pStat->name, current->info.name);
pStat->element_size = current->info.element_size;
pStat->total_count += current->info.total_count;
pStat->used_count += current->info.used_count;
if (current->prev != &mblock_manager.head) if (current->prev != &mblock_manager.head)
{ {
if (cmp_mblock_info(current, current->prev) != 0) if (cmp_mblock_info(current, current->prev) != 0)
@ -135,6 +142,7 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats,
result = EOVERFLOW; result = EOVERFLOW;
break; break;
} }
STAT_DUP(pStat, current->prev);
pStat++; pStat++;
} }
} }
@ -149,6 +157,7 @@ int fast_mblock_manager_stat(struct fast_mblock_info *stats,
} }
else else
{ {
STAT_DUP(pStat, current->prev);
pStat++; pStat++;
} }
} }
@ -185,14 +194,16 @@ int fast_mblock_manager_stat_print()
if (result == 0) if (result == 0)
{ {
logInfo("mblock manager stat count: %d", count); logInfo("mblock stat count: %d", count);
logInfo("name element_size alloc_count used_count used_ratio"); logInfo("%32s %12s %12s %12s %12s", "name", "element_size",
"alloc_count", "used_count", "used_ratio");
stat_end = stats + count; stat_end = stats + count;
for (pStat=stats; pStat<stat_end; pStat++) for (pStat=stats; pStat<stat_end; pStat++)
{ {
logInfo("%32s %8d %8d %8d %4.2f", pStat->name, pStat->total_count, logInfo("%32s %12d %12d %12d %12.4f", pStat->name, pStat->element_size,
pStat->used_count, (double)pStat->used_count / pStat->total_count, pStat->used_count,
(double)pStat->total_count); pStat->total_count > 0 ? (double)pStat->used_count /
(double)pStat->total_count : 0.00);
} }
} }
@ -200,6 +211,14 @@ int fast_mblock_manager_stat_print()
return 0; return 0;
} }
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
const int element_size, const int alloc_elements_once,
fast_mblock_alloc_init_func init_func, const bool need_lock)
{
return fast_mblock_init_ex2(mblock, NULL, element_size,
alloc_elements_once, init_func, need_lock);
}
int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name, int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
const int element_size, const int alloc_elements_once, const int element_size, const int alloc_elements_once,
fast_mblock_alloc_init_func init_func, const bool need_lock) fast_mblock_alloc_init_func init_func, const bool need_lock)

View File

@ -18,7 +18,7 @@
#include "common_define.h" #include "common_define.h"
#include "chain.h" #include "chain.h"
#define FAST_MBLOCK_NAME_SIZE 64 #define FAST_MBLOCK_NAME_SIZE 32
/* free node chain */ /* free node chain */
struct fast_mblock_node struct fast_mblock_node
@ -72,10 +72,21 @@ extern "C" {
#endif #endif
#define fast_mblock_init(mblock, element_size, alloc_elements_once) \ #define fast_mblock_init(mblock, element_size, alloc_elements_once) \
fast_mblock_init_ex2(mblock, NULL, element_size, alloc_elements_once, NULL, true) fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL, true)
#define fast_mblock_init_ex(mblock, element_size, alloc_elements_once, init_func, need_lock) \ /**
fast_mblock_init_ex2(mblock, NULL, element_size, alloc_elements_once, init_func, need_lock) mblock init
parameters:
mblock: the mblock pointer
element_size: element size, such as sizeof(struct xxx)
alloc_elements_once: malloc elements once, 0 for malloc 1MB memory once
init_func: the init function
need_lock: if need lock
return error no, 0 for success, != 0 fail
*/
int fast_mblock_init_ex(struct fast_mblock_man *mblock,
const int element_size, const int alloc_elements_once,
fast_mblock_alloc_init_func init_func, const bool need_lock);
/** /**
mblock init mblock init