分类:
2010-08-25 22:32:16
haproxy的内存管理采用了pool机制,即通过pool提高内存的重用,减少内存频繁的申请和释放。
Pool数据结构
struct list { struct list *n; /* next */ struct list *p; /* prev */ };
struct pool_head { void **free_list; struct list list; /* list of all known pools */ unsigned int used; /* how many chunks are currently in use */ unsigned int allocated; /* how many chunks have been allocated */ unsigned int limit; /* hard limit on the number of chunks */ unsigned int minavail; /* how many chunks are expected to be used */ unsigned int size; /* chunk size */ unsigned int flags; /* MEM_F_* */ unsigned int users; /* number of pools sharing this zone */ char name[12]; /* name of the pool */ }; |
其中未说明的分量含义解释如下:
free_list:保存空闲的内存链表
Pool的逻辑结构
static struct list pools:全局变量,所有不同尺寸pool的链表head;
pool2_session:全局变量,session大小尺寸的pool;
pool2_buffer:全局变量,buffer大小尺寸的pool;
函数原型: struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags) 函数流程: list_for_each_entry(entry, &pools, list) { if (entry->size == size && entry->flags & MEM_F_SHARED) return entry; else calloc a pool; insert pool to poos; } |
函数原型: static inline void *pool_alloc2(struct pool_head *pool) 函数流程: if (pool->free_list) get first node from free_list; else malloc a buf; |
函数原型: static inline void pool_free2(struct pool_head *pool, void *ptr) 函数流程: 在相应的pool中,把ptr指向的buf插入至free_list头。 |
函数原型: void *pool_destroy2(struct pool_head *pool); 函数流程: free对应pool free_list中的buf,并且从pools中删除。 |
分析Haproxy内存管理机制,可以看出该机制存在如下缺陷:
缺少pool收缩机制,即pool中分配的buf数目只会增长不会减少;也真是这个缺陷导致haproxy占用内存不会随着请求量的下降而下降,而只会把不用的buf存放至free_list中。