内存探测组件,预留热插拔。
This commit is contained in:
@@ -57,6 +57,7 @@ static void set_default_config(AppConfig *cfg)
|
||||
cfg->master_port = 8888;
|
||||
cfg->persistence = PERSIST_NONE;
|
||||
cfg->allocator = ALLOC_JEMALLOC;
|
||||
cfg->leak_mode = MEMLEAK_DETECT_OFF;
|
||||
}
|
||||
|
||||
/* ---- 字符串转枚举 ---- */
|
||||
@@ -90,9 +91,18 @@ static void parse_allocator(const char *s, AllocatorType *out)
|
||||
if (!s || !out) return;
|
||||
if (!strcasecmp(s, "jemalloc")) *out = ALLOC_JEMALLOC;
|
||||
else if (!strcasecmp(s, "malloc")) *out = ALLOC_MALLOC;
|
||||
else if (!strcasecmp(s, "mypool")) *out = ALLOC_MYPOOL;
|
||||
else *out = ALLOC_OTHER;
|
||||
}
|
||||
|
||||
static void parse_leakage(const char *s, MemLeakDetectMode *out)
|
||||
{
|
||||
if (!s || !out) return;
|
||||
if (!strcasecmp(s, "enable")) *out = MEMLEAK_DETECT_ON;
|
||||
else if (!strcasecmp(s, "disable")) *out = MEMLEAK_DETECT_OFF;
|
||||
else *out = MEMLEAK_DETECT_OFF;
|
||||
}
|
||||
|
||||
static int read_file_mmap(const char *filename, void **out_addr, size_t *out_len, int *out_fd) {
|
||||
if (!filename || !out_addr || !out_len || !out_fd) return -1;
|
||||
|
||||
@@ -174,11 +184,21 @@ const char *allocator_to_string(AllocatorType a)
|
||||
switch (a) {
|
||||
case ALLOC_JEMALLOC: return "jemalloc";
|
||||
case ALLOC_MALLOC: return "malloc";
|
||||
case ALLOC_MYPOOL: return "mypool";
|
||||
case ALLOC_OTHER: return "other";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char *leakage_to_string(MemLeakDetectMode a)
|
||||
{
|
||||
switch (a) {
|
||||
case MEMLEAK_DETECT_ON: return "enable";
|
||||
case MEMLEAK_DETECT_OFF: return "disable";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- 主函数:从 XML 加载配置 ---- */
|
||||
|
||||
/* server 部分 */
|
||||
@@ -333,6 +353,15 @@ void memory_load(xmlNodePtr *root, AppConfig *out_cfg){
|
||||
xmlFree(txt);
|
||||
}
|
||||
}
|
||||
|
||||
xmlNodePtr leakage_node = find_child(mem, "leakage");
|
||||
if (leakage_node) {
|
||||
xmlChar *txt = xmlNodeGetContent(leakage_node);
|
||||
if (txt) {
|
||||
parse_leakage((char *)txt, &out_cfg->leak_mode);
|
||||
xmlFree(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define __CONFIG_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <memory/alloc_dispatch.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_DEBUG,
|
||||
@@ -19,11 +20,17 @@ typedef enum {
|
||||
PERSIST_NONE
|
||||
} PersistenceType;
|
||||
|
||||
typedef enum {
|
||||
ALLOC_JEMALLOC,
|
||||
ALLOC_MALLOC,
|
||||
ALLOC_OTHER
|
||||
} AllocatorType;
|
||||
// typedef enum {
|
||||
// ALLOC_JEMALLOC,
|
||||
// ALLOC_MALLOC,
|
||||
// ALLOC_MYPOOL,
|
||||
// ALLOC_OTHER
|
||||
// } AllocatorType;
|
||||
|
||||
// typedef enum {
|
||||
// MEMLEAK_DETECT_OFF = 0, // 关闭检测
|
||||
// MEMLEAK_DETECT_ON = 1 // 开启检测
|
||||
// } MemLeakDetectMode;
|
||||
|
||||
typedef struct {
|
||||
char ip[64];
|
||||
@@ -43,6 +50,7 @@ typedef struct {
|
||||
char hash_file[256];
|
||||
|
||||
AllocatorType allocator;
|
||||
MemLeakDetectMode leak_mode;
|
||||
} AppConfig;
|
||||
|
||||
/**
|
||||
@@ -55,5 +63,6 @@ const char *log_level_to_string(LogLevel lvl);
|
||||
const char *server_mode_to_string(ServerMode mode);
|
||||
const char *persistence_to_string(PersistenceType p);
|
||||
const char *allocator_to_string(AllocatorType a);
|
||||
const char *leakage_to_string(MemLeakDetectMode a);
|
||||
|
||||
#endif /* CONFIG_H */
|
||||
|
||||
Reference in New Issue
Block a user