Skip to content

Commit

Permalink
Updated allocator hooks stats api to have four default stats
Browse files Browse the repository at this point in the history
This basically says says that any allocator that uses this api
must be able to at least tell us about how much memory has been
allocated, how much memory the process is using, how much memory
is free, and how much fragmentation there is. Anything extra can
be included as an allocator specific stat.

Change-Id: I6cda3240f49c8437b27eac776be2904056f73b34
Reviewed-on: http://review.couchbase.org/14561
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
  • Loading branch information
Mike Wiederhold committed Apr 5, 2012
1 parent 1a9a139 commit 3a64fd5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
36 changes: 18 additions & 18 deletions daemon/alloc_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,30 @@ bool mc_remove_delete_hook(void (*hook)(const void* ptr)) {
return (removeDelHook.func)(hook) ? true : false;
}

int mc_get_stats_size() {
int mc_get_extra_stats_size() {
if (type == tcmalloc) {
return 7;
return 3;
}
return 0;
}

void mc_get_allocator_stats(allocator_stat* stats) {
void mc_get_allocator_stats(allocator_stats* stats) {
if (type == tcmalloc) {
strcpy(stats[0].key, "total_allocated_bytes");
strcpy(stats[1].key, "total_heap_size");
strcpy(stats[2].key, "total_free_bytes");
strcpy(stats[3].key, "total_fragmented_bytes");
strcpy(stats[4].key, "tcmalloc_unmapped_bytes");
strcpy(stats[5].key, "tcmalloc_max_thread_cache_bytes");
strcpy(stats[6].key, "tcmalloc_current_thread_cache_bytes");

(getStatsProp.func)("generic.current_allocated_bytes", &stats[0].value);
(getStatsProp.func)("generic.heap_size", &stats[1].value);
(getStatsProp.func)("tcmalloc.pageheap_free_bytes", &stats[2].value);
stats[3].value = stats[1].value - stats[0].value - stats[2].value;
(getStatsProp.func)("tcmalloc.pageheap_unmapped_bytes", &stats[4].value);
(getStatsProp.func)("tcmalloc.max_total_thread_cache_bytes", &stats[5].value);
(getStatsProp.func)("tcmalloc.current_total_thread_cache_bytes", &stats[6].value);
(getStatsProp.func)("generic.current_allocated_bytes", &(stats->allocated_size));
(getStatsProp.func)("generic.heap_size", &(stats->heap_size));
(getStatsProp.func)("tcmalloc.pageheap_free_bytes", &(stats->free_size));
stats->fragmentation_size = stats->heap_size - stats->allocated_size - stats->free_size;

strcpy(stats->ext_stats[0].key, "tcmalloc_unmapped_bytes");
strcpy(stats->ext_stats[1].key, "tcmalloc_max_thread_cache_bytes");
strcpy(stats->ext_stats[2].key, "tcmalloc_current_thread_cache_bytes");

(getStatsProp.func)("tcmalloc.pageheap_unmapped_bytes",
&(stats->ext_stats[0].value));
(getStatsProp.func)("tcmalloc.max_total_thread_cache_bytes",
&(stats->ext_stats[1].value));
(getStatsProp.func)("tcmalloc.current_total_thread_cache_bytes",
&(stats->ext_stats[2].value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/alloc_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ bool mc_add_new_hook(void (*)(const void* ptr, size_t size));
bool mc_remove_new_hook(void (*)(const void* ptr, size_t size));
bool mc_add_delete_hook(void (*)(const void* ptr));
bool mc_remove_delete_hook(void (*)(const void* ptr));
void mc_get_allocator_stats(allocator_stat*);
int mc_get_stats_size(void);
void mc_get_allocator_stats(allocator_stats*);
int mc_get_extra_stats_size(void);
size_t mc_get_allocation_size(void*);

#endif /* MEM_HOOKS_H */
2 changes: 1 addition & 1 deletion daemon/memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -6836,7 +6836,7 @@ static SERVER_HANDLE_V1 *get_server_api(void)
.remove_new_hook = mc_remove_new_hook,
.add_delete_hook = mc_add_delete_hook,
.remove_delete_hook = mc_remove_delete_hook,
.get_stats_size = mc_get_stats_size,
.get_extra_stats_size = mc_get_extra_stats_size,
.get_allocator_stats = mc_get_allocator_stats,
.get_allocation_size = mc_get_allocation_size
};
Expand Down
29 changes: 19 additions & 10 deletions include/memcached/allocator_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@
extern "C" {
#endif

typedef struct allocator_stat {
typedef struct allocator_ext_stat {
char key[48];
size_t value;
} allocator_stat;
} allocator_ext_stat;

typedef struct allocator_stats {
size_t allocated_size;
size_t heap_size;
size_t free_size;
size_t fragmentation_size;
allocator_ext_stat *ext_stats;
size_t ext_stats_size;
} allocator_stats;

/**
* Engine allocator hooks for memory tracking.
Expand Down Expand Up @@ -61,17 +70,17 @@ typedef struct engine_allocator_hooks_v1 {
bool (*remove_delete_hook)(void (*)(const void* ptr));

/**
* Returns the number of stats for the current allocator.
* Returns the number of extra stats for the current allocator.
*/
int (*get_stats_size)(void);
int (*get_extra_stats_size)(void);

/**
* Obtains relevant statistics from the allocator. These statistics
* may vary depending on which allocator is in use. This function
* fills the empty array (passed as a parameter) with allocator
* stats. In order to correctly ize your array use the
* get_stats_size() function.
* Obtains relevant statistics from the allocator. Every allocator
* is required to return total allocated bytes, total heap bytes,
* total free bytes, and toal fragmented bytes. An allocator will
* also provide a varying number of allocator specific stats
*/
void (*get_allocator_stats)(allocator_stat*);
void (*get_allocator_stats)(allocator_stats*);

/**
* Returns the total bytes allocated by the allocator. This value
Expand Down
9 changes: 5 additions & 4 deletions programs/mock_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,13 @@ static bool mock_remove_delete_hook(void (*hook)(const void* ptr)) {
return false;
}

static int mock_get_stats_size() {
static int mock_get_extra_stats_size() {
return 0;
}

static void mock_get_allocator_stats(allocator_stat* stats) {

static void mock_get_allocator_stats(allocator_stats* stats) {
(void) stats;
return;
}

static size_t mock_get_allocation_size(void* ptr) {
Expand Down Expand Up @@ -332,7 +333,7 @@ SERVER_HANDLE_V1 *get_mock_server_api(void)
.remove_new_hook = mock_remove_new_hook,
.add_delete_hook = mock_add_delete_hook,
.remove_delete_hook = mock_remove_delete_hook,
.get_stats_size = mock_get_stats_size,
.get_extra_stats_size = mock_get_extra_stats_size,
.get_allocator_stats = mock_get_allocator_stats,
.get_allocation_size = mock_get_allocation_size
};
Expand Down

0 comments on commit 3a64fd5

Please sign in to comment.