add debug api to show current service c memory

This commit is contained in:
Cloud Wu
2015-12-17 22:13:51 +08:00
parent 9d6bde01a3
commit 20610723a1
4 changed files with 31 additions and 0 deletions

View File

@@ -41,6 +41,12 @@ lexpandshrtbl(lua_State *L) {
return 0;
}
static int
lcurrent(lua_State *L) {
lua_pushinteger(L, malloc_current_memory());
return 1;
}
int
luaopen_memory(lua_State *L) {
luaL_checkversion(L);
@@ -53,6 +59,7 @@ luaopen_memory(lua_State *L) {
{ "info", dump_mem_lua },
{ "ssinfo", luaS_shrinfo },
{ "ssexpand", lexpandshrtbl },
{ "current", lcurrent },
{ NULL, NULL },
};

View File

@@ -3,6 +3,7 @@
#include <assert.h>
#include <stdlib.h>
#include <lua.h>
#include <stdio.h>
#include "malloc_hook.h"
#include "skynet.h"
@@ -248,3 +249,24 @@ dump_mem_lua(lua_State *L) {
}
return 1;
}
size_t
malloc_current_memory(void) {
uint32_t handle = skynet_current_handle();
int i;
for(i=0; i<SLOT_SIZE; i++) {
mem_data* data = &mem_stats[i];
if(data->handle == (uint32_t)handle && data->allocated != 0) {
return (size_t) data->allocated;
}
}
return 0;
}
void
skynet_debug_memory(const char *info) {
// for debug use
uint32_t handle = skynet_current_handle();
size_t mem = malloc_current_memory();
fprintf(stderr, "[:%08x] %s %p\n", handle, info, (void *)mem);
}

View File

@@ -11,6 +11,7 @@ extern size_t mallctl_int64(const char* name, size_t* newval);
extern int mallctl_opt(const char* name, int* newval);
extern void dump_c_mem(void);
extern int dump_mem_lua(lua_State *L);
extern size_t malloc_current_memory(void);
#endif /* SKYNET_MALLOC_HOOK_H */

View File

@@ -39,5 +39,6 @@ void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb);
uint32_t skynet_current_handle(void);
uint64_t skynet_now(void);
void skynet_debug_memory(const char *info); // for debug use, output current service memory to stderr
#endif