设为首页 收藏本站
查看: 2104|回复: 0

[经验分享] glusterfs 内存管理方式

[复制链接]

尚未签到

发表于 2015-9-10 01:13:03 | 显示全部楼层 |阅读模式
  glusterfs中的内存管理方式:
  首先来看看glusterfs的内存管理结构吧:



1 struct mem_pool {
2         struct list_head  list;
3         int               hot_count;
4         int               cold_count;
5         gf_lock_t         lock;
6         unsigned long     padded_sizeof_type;
7         void             *pool;
8         void             *pool_end;
9         int               real_sizeof_type;
10         uint64_t          alloc_count;
11         uint64_t          pool_misses;
12         int               max_alloc;
13         int               curr_stdalloc;
14         int               max_stdalloc;
15         char             *name;
16         struct list_head  global_list;
17 };
  管理结构的信息量很简单,核心的数据项是list,每个要分配的内存块被一个双向链表串连起来管理。
  接下来是创建内存池的接口:



1 struct mem_pool *
2 mem_pool_new_fn (unsigned long sizeof_type,
3                  unsigned long count, char *name)
4 {
5         struct mem_pool  *mem_pool = NULL;
6         unsigned long     padded_sizeof_type = 0;
7         void             *pool = NULL;
8         int               i = 0;
9         int               ret = 0;
10         struct list_head *list = NULL;
11         glusterfs_ctx_t  *ctx = NULL;
12
13         if (!sizeof_type || !count) {
14                 gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
15                 return NULL;
16         }
17         padded_sizeof_type = sizeof_type + GF_MEM_POOL_PAD_BOUNDARY;
18
19         mem_pool = GF_CALLOC (sizeof (*mem_pool), 1, gf_common_mt_mem_pool);
20         if (!mem_pool)
21                 return NULL;
22
23         ret = gf_asprintf (&mem_pool->name, "%s:%s", THIS->name, name);
24         if (ret < 0)
25                 return NULL;
26
27         if (!mem_pool->name) {
28                 GF_FREE (mem_pool);
29                 return NULL;
30         }
31
32         LOCK_INIT (&mem_pool->lock);
33         INIT_LIST_HEAD (&mem_pool->list);
34         INIT_LIST_HEAD (&mem_pool->global_list);
35
36         mem_pool->padded_sizeof_type = padded_sizeof_type;
37         mem_pool->cold_count = count;
38         mem_pool->real_sizeof_type = sizeof_type;
39
40         pool = GF_CALLOC (count, padded_sizeof_type, gf_common_mt_long);
41         if (!pool) {
42                 GF_FREE (mem_pool->name);
43                 GF_FREE (mem_pool);
44                 return NULL;
45         }
46
47         for (i = 0; i < count; i++) {
48                 list = pool + (i * (padded_sizeof_type));
49                 INIT_LIST_HEAD (list);
50                 list_add_tail (list, &mem_pool->list);
51         }
52
53         mem_pool->pool = pool;
54         mem_pool->pool_end = pool + (count * (padded_sizeof_type));
55
56         /* add this pool to the global list */
57         ctx = THIS->ctx;
58         if (!ctx)
59                 goto out;
60
61         list_add (&mem_pool->global_list, &ctx->mempool_list);
62
63 out:
64         return mem_pool;
65 }
  在第19行中申请了一个mem_pool内存管理结构,在初始化这个结构体后,40行申请了真正要使用的内存pool并把用mem_pool->list链表串起来。之后再记录内存池的开始和结束地址(53-54),再把这个结构加入全局管理。
  再看一下申请后的内存是如何使用的呢?



1 void *
2 mem_get (struct mem_pool *mem_pool)
3 {
4         struct list_head *list = NULL;
5         void             *ptr = NULL;
6         int             *in_use = NULL;
7         struct mem_pool **pool_ptr = NULL;
8
9         if (!mem_pool) {
10                 gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
11                 return NULL;
12         }
13
14         LOCK (&mem_pool->lock);
15         {
16                 mem_pool->alloc_count++;
17                 if (mem_pool->cold_count) {
18                         list = mem_pool->list.next;
19                         list_del (list);
20
21                         mem_pool->hot_count++;
22                         mem_pool->cold_count--;
23
24                         if (mem_pool->max_alloc < mem_pool->hot_count)
25                                 mem_pool->max_alloc = mem_pool->hot_count;
26
27                         ptr = list;
28                         in_use = (ptr + GF_MEM_POOL_LIST_BOUNDARY +
29                                   GF_MEM_POOL_PTR);
30                         *in_use = 1;
31
32                         goto fwd_addr_out;
33                 }
34
35                 /* This is a problem area. If we've run out of
36                  * chunks in our slab above, we need to allocate
37                  * enough memory to service this request.
38                  * The problem is, these individual chunks will fail
39                  * the first address range check in __is_member. Now, since
40                  * we're not allocating a full second slab, we wont have
41                  * enough info perform the range check in __is_member.
42                  *
43                  * I am working around this by performing a regular allocation
44                  * , just the way the caller would've done when not using the
45                  * mem-pool. That also means, we're not padding the size with
46                  * the list_head structure because, this will not be added to
47                  * the list of chunks that belong to the mem-pool allocated
48                  * initially.
49                  *
50                  * This is the best we can do without adding functionality for
51                  * managing multiple slabs. That does not interest us at present
52                  * because it is too much work knowing that a better slab
53                  * allocator is coming RSN.
54                  */
55                 mem_pool->pool_misses++;
56                 mem_pool->curr_stdalloc++;
57                 if (mem_pool->max_stdalloc < mem_pool->curr_stdalloc)
58                         mem_pool->max_stdalloc = mem_pool->curr_stdalloc;
59                 ptr = GF_CALLOC (1, mem_pool->padded_sizeof_type,
60                                  gf_common_mt_mem_pool);
61                 gf_log_callingfn ("mem-pool", GF_LOG_DEBUG, "Mem pool is full. "
62                                   "Callocing mem");
63
64                 /* Memory coming from the heap need not be transformed from a
65                  * chunkhead to a usable pointer since it is not coming from
66                  * the pool.
67                  */
68         }
69 fwd_addr_out:
70         pool_ptr = mem_pool_from_ptr (ptr);
71         *pool_ptr = (struct mem_pool *)mem_pool; //保存分配者地址
72         ptr = mem_pool_chunkhead2ptr (ptr);
73         UNLOCK (&mem_pool->lock);
74
75         return ptr;
76 }
  从17行到33行可以看出,当需要内存时,glusterfs从mem_pool->list中分配内存。关键是:当内存不足时,mem_pool如何处理呢?55-63行处理这个问题:当内存不足时,它向系统申请了内存,并处理了内存的管理信息后,直接将内存返回给调用者。
  最后看看内存的释放过程:



1 void
2 mem_put (void *ptr)
3 {
4         struct list_head *list = NULL;
5         int    *in_use = NULL;
6         void   *head = NULL;
7         struct mem_pool **tmp = NULL;
8         struct mem_pool *pool = NULL;
9
10         if (!ptr) {
11                 gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
12                 return;
13         }
14
15         list = head = mem_pool_ptr2chunkhead (ptr);
16         tmp = mem_pool_from_ptr (head); //取出分配者地址
17         if (!tmp) {
18                 gf_log_callingfn ("mem-pool", GF_LOG_ERROR,
19                                   "ptr header is corrupted");
20                 return;
21         }
22
23         pool = *tmp;
24         if (!pool) {
25                 gf_log_callingfn ("mem-pool", GF_LOG_ERROR,
26                                   "mem-pool ptr is NULL");
27                 return;
28         }
29         LOCK (&pool->lock);
30         {
31
32                 switch (__is_member (pool, ptr))
33                 {
34                 case 1:
35                         in_use = (head + GF_MEM_POOL_LIST_BOUNDARY +
36                                   GF_MEM_POOL_PTR);
37                         if (!is_mem_chunk_in_use(in_use)) {
38                                 gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL,
39                                                   "mem_put called on freed ptr %p of mem "
40                                                   "pool %p", ptr, pool);
41                                 break;
42                         }
43                         pool->hot_count--;
44                         pool->cold_count++;
45                         *in_use = 0;
46                         list_add (list, &pool->list);
47                         break;
48                 case -1:
49                         /* For some reason, the address given is within
50                          * the address range of the mem-pool but does not align
51                          * with the expected start of a chunk that includes
52                          * the list headers also. Sounds like a problem in
53                          * layers of clouds up above us. ;)
54                          */
55                         abort ();
56                         break;
57                 case 0:
58                         /* The address is outside the range of the mem-pool. We
59                          * assume here that this address was allocated at a
60                          * point when the mem-pool was out of chunks in mem_get
61                          * or the programmer has made a mistake by calling the
62                          * wrong de-allocation interface. We do
63                          * not have enough info to distinguish between the two
64                          * situations.
65                          */
66                         pool->curr_stdalloc--;
67                         GF_FREE (list);
68                         break;
69                 default:
70                         /* log error */
71                         break;
72                 }
73         }
74         UNLOCK (&pool->lock);
75 }
  在switch语句中,在case 1中处理了内存池分配的过程。在case 0中处理内存不足的情况,从这里看出,glusterfs直接将内存释放了,正好与分配的过程完美的结合。
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111600-1-1.html 上篇帖子: 【转载】GlusterFS六大卷模式說明 下篇帖子: GlusterFS安装和系统要求(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表