glusterfs中的内存管理方式:

  首先来看看glusterfs的内存管理结构吧:

 struct mem_pool {
struct list_head list;
int hot_count;
int cold_count;
gf_lock_t lock;
unsigned long padded_sizeof_type;
void *pool;
void *pool_end;
int real_sizeof_type;
uint64_t alloc_count;
uint64_t pool_misses;
int max_alloc;
int curr_stdalloc;
int max_stdalloc;
char *name;
struct list_head global_list;
};

管理结构的信息量很简单,核心的数据项是list,每个要分配的内存块被一个双向链表串连起来管理。

  接下来是创建内存池的接口:

 struct mem_pool *
mem_pool_new_fn (unsigned long sizeof_type,
unsigned long count, char *name)
{
struct mem_pool *mem_pool = NULL;
unsigned long padded_sizeof_type = ;
void *pool = NULL;
int i = ;
int ret = ;
struct list_head *list = NULL;
glusterfs_ctx_t *ctx = NULL; if (!sizeof_type || !count) {
gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
return NULL;
}
padded_sizeof_type = sizeof_type + GF_MEM_POOL_PAD_BOUNDARY; mem_pool = GF_CALLOC (sizeof (*mem_pool), , gf_common_mt_mem_pool);
if (!mem_pool)
return NULL; ret = gf_asprintf (&mem_pool->name, "%s:%s", THIS->name, name);
if (ret < )
return NULL; if (!mem_pool->name) {
GF_FREE (mem_pool);
return NULL;
} LOCK_INIT (&mem_pool->lock);
INIT_LIST_HEAD (&mem_pool->list);
INIT_LIST_HEAD (&mem_pool->global_list); mem_pool->padded_sizeof_type = padded_sizeof_type;
mem_pool->cold_count = count;
mem_pool->real_sizeof_type = sizeof_type; pool = GF_CALLOC (count, padded_sizeof_type, gf_common_mt_long);
if (!pool) {
GF_FREE (mem_pool->name);
GF_FREE (mem_pool);
return NULL;
} for (i = ; i < count; i++) {
list = pool + (i * (padded_sizeof_type));
INIT_LIST_HEAD (list);
list_add_tail (list, &mem_pool->list);
} mem_pool->pool = pool;
mem_pool->pool_end = pool + (count * (padded_sizeof_type)); /* add this pool to the global list */
ctx = THIS->ctx;
if (!ctx)
goto out; list_add (&mem_pool->global_list, &ctx->mempool_list); out:
return mem_pool;
}

在第19行中申请了一个mem_pool内存管理结构,在初始化这个结构体后,40行申请了真正要使用的内存pool并把用mem_pool->list链表串起来。之后再记录内存池的开始和结束地址(53-54),再把这个结构加入全局管理。

再看一下申请后的内存是如何使用的呢?

 void *
mem_get (struct mem_pool *mem_pool)
{
struct list_head *list = NULL;
void *ptr = NULL;
int *in_use = NULL;
struct mem_pool **pool_ptr = NULL; if (!mem_pool) {
gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
return NULL;
} LOCK (&mem_pool->lock);
{
mem_pool->alloc_count++;
if (mem_pool->cold_count) {
list = mem_pool->list.next;
list_del (list); mem_pool->hot_count++;
mem_pool->cold_count--; if (mem_pool->max_alloc < mem_pool->hot_count)
mem_pool->max_alloc = mem_pool->hot_count; ptr = list;
in_use = (ptr + GF_MEM_POOL_LIST_BOUNDARY +
GF_MEM_POOL_PTR);
*in_use = ; goto fwd_addr_out;
} /* This is a problem area. If we've run out of
* chunks in our slab above, we need to allocate
* enough memory to service this request.
* The problem is, these individual chunks will fail
* the first address range check in __is_member. Now, since
* we're not allocating a full second slab, we wont have
* enough info perform the range check in __is_member.
*
* I am working around this by performing a regular allocation
* , just the way the caller would've done when not using the
* mem-pool. That also means, we're not padding the size with
* the list_head structure because, this will not be added to
* the list of chunks that belong to the mem-pool allocated
* initially.
*
* This is the best we can do without adding functionality for
* managing multiple slabs. That does not interest us at present
* because it is too much work knowing that a better slab
* allocator is coming RSN.
*/
mem_pool->pool_misses++;
mem_pool->curr_stdalloc++;
if (mem_pool->max_stdalloc < mem_pool->curr_stdalloc)
mem_pool->max_stdalloc = mem_pool->curr_stdalloc;
ptr = GF_CALLOC (, mem_pool->padded_sizeof_type,
gf_common_mt_mem_pool);
gf_log_callingfn ("mem-pool", GF_LOG_DEBUG, "Mem pool is full. "
"Callocing mem"); /* Memory coming from the heap need not be transformed from a
* chunkhead to a usable pointer since it is not coming from
* the pool.
*/
}
fwd_addr_out:
pool_ptr = mem_pool_from_ptr (ptr);
*pool_ptr = (struct mem_pool *)mem_pool; //保存分配者地址
ptr = mem_pool_chunkhead2ptr (ptr);
UNLOCK (&mem_pool->lock); return ptr;
}

从17行到33行可以看出,当需要内存时,glusterfs从mem_pool->list中分配内存。关键是:当内存不足时,mem_pool如何处理呢?55-63行处理这个问题:当内存不足时,它向系统申请了内存,并处理了内存的管理信息后,直接将内存返回给调用者。

最后看看内存的释放过程:

 void
mem_put (void *ptr)
{
struct list_head *list = NULL;
int *in_use = NULL;
void *head = NULL;
struct mem_pool **tmp = NULL;
struct mem_pool *pool = NULL; if (!ptr) {
gf_log_callingfn ("mem-pool", GF_LOG_ERROR, "invalid argument");
return;
} list = head = mem_pool_ptr2chunkhead (ptr);
tmp = mem_pool_from_ptr (head); //取出分配者地址
if (!tmp) {
gf_log_callingfn ("mem-pool", GF_LOG_ERROR,
"ptr header is corrupted");
return;
} pool = *tmp;
if (!pool) {
gf_log_callingfn ("mem-pool", GF_LOG_ERROR,
"mem-pool ptr is NULL");
return;
}
LOCK (&pool->lock);
{ switch (__is_member (pool, ptr))
{
case :
in_use = (head + GF_MEM_POOL_LIST_BOUNDARY +
GF_MEM_POOL_PTR);
if (!is_mem_chunk_in_use(in_use)) {
gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL,
"mem_put called on freed ptr %p of mem "
"pool %p", ptr, pool);
break;
}
pool->hot_count--;
pool->cold_count++;
*in_use = ;
list_add (list, &pool->list);
break;
case -:
/* For some reason, the address given is within
* the address range of the mem-pool but does not align
* with the expected start of a chunk that includes
* the list headers also. Sounds like a problem in
* layers of clouds up above us. ;)
*/
abort ();
break;
case :
/* The address is outside the range of the mem-pool. We
* assume here that this address was allocated at a
* point when the mem-pool was out of chunks in mem_get
* or the programmer has made a mistake by calling the
* wrong de-allocation interface. We do
* not have enough info to distinguish between the two
* situations.
*/
pool->curr_stdalloc--;
GF_FREE (list);
break;
default:
/* log error */
break;
}
}
UNLOCK (&pool->lock);
}

在switch语句中,在case 1中处理了内存池分配的过程。在case 0中处理内存不足的情况,从这里看出,glusterfs直接将内存释放了,正好与分配的过程完美的结合。

glusterfs 内存管理方式的更多相关文章

  1. 24小时学通Linux内核之内存管理方式

    昨天分析的进程的代码让自己还在头昏目眩,脑子中这几天都是关于Linux内核的,对于自己出现的一些问题我会继续改正,希望和大家好好分享,共同进步.今天将会讲诉Linux如何追踪和管理用户空间进程的可用内 ...

  2. windows内存管理方式以及优缺点

    Windows内存管理方式:页式管理,段式管理,段页式管理 页式管理 将各进程的虚拟空间(逻辑地址)划分为若干个长度相等的页,业内管理把内存空间(物理内存)按照页的大小划分为片或者页面,从而实现了离散 ...

  3. 十天学Linux内核之第三天---内存管理方式

    原文:十天学Linux内核之第三天---内存管理方式 昨天分析的进程的代码让自己还在头昏目眩,脑子中这几天都是关于Linux内核的,对于自己出现的一些问题我会继续改正,希望和大家好好分享,共同进步.今 ...

  4. ObjC如何通过runtime修改Ivar的内存管理方式

    ObjC如何通过runtime修改Ivar的内存管理方式 为什么要这么做? 在iOS 9之前,UITableView(或者更确切的说是 UIScrollView)有一个众所周知的问题: propert ...

  5. 这篇关于Oracle内存管理方式的介绍太棒了!我必须要转发,很全面。哈哈~

    "Oracle内存管理可分为两大类,自动内存管理和手动内存管理.其中手动内存管理又可分为自动共享内存管理,手动共享内存管理,自动PGA内存管理以及手动PGA内存管理.本文会简单的介绍不同的内 ...

  6. GlusterFS源代码解析 —— GlusterFS 内存分配方式

    原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564891 GlusterFS 的内存分配主要有两种方式,一种是内存 ...

  7. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  8. Linux内核学习笔记——内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  9. glibc内存管理方式

    程序员接触的内存空间和系统接触的物理内存空间是有所区别的.对于一般进程来讲,他面对的是一个线性虚拟内存空间:地址从0到最大值.每一个进程面对的虚拟内存空间都是一样的,都享有全部的内存地址.虚拟内存空间 ...

随机推荐

  1. hibernate中validate的使用(转)

    原文链接:http://blog.csdn.net/xing_sky/article/details/8484551 首先是要加入下面两个包 hibernate-validator-4.1.0.Fin ...

  2. js⑦

    立即执行函数or自执行函数 为了避免全局变量的产生.(function(){ //var a = 10; //var b = 20;//console.log(a,b); -------------v ...

  3. 转:js中this、call、apply的用法

    (一)关于this首先关于this我想说一句话,这句话记住了this的用法你也就差不多都能明白了:this指的是当前函数的对象.这句话可能比较绕,我会举出很多例子和这句话呼应的!(看下文)1.首先看下 ...

  4. Android Fragment (一)

    1.Fragment的产生与介绍   Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以 ...

  5. .NET网页打印以及使用打印需要注意的事项(可能会引起VS崩溃的现象、打印预览后关闭功能不管用)

    这两天进行给网页添加打印.打印预览.页面设置的功能.遇到了以下几个问题 [1]在网上查找了一些打印方法,一开始还可以用,后来不知道动到了哪里,点击vs中拆分或者切换到另一个设计和源代码显示方式,就会引 ...

  6. Android计算器尝试

    学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...

  7. IDEA安装配置(SVN)

    IntelliJ IDEA 14 注册码 Win7系统上配置使用Intellij Idea 13的SVN插件 IntelliJ 设置JDK http://blog.sina.com.cn/s/blog ...

  8. 【其他】win7创建wifi热点共享给手机使用

    出门在外,有时候网络有诸多不便,需要用笔记本创建wifi热点给手机用:本人测试xp怎么配置都不好使,但win7有可行的方案,不依赖第三方软件. 详述如下: 场景一:win7 + A(PC机)(用无线连 ...

  9. 用js编程输出100以内所有的质数和个数(提示:一个大于1的自然数,除了1和它本身外,不能被其他自然数整除的数都是质数)

    <script type="text/javascript"> for(var i = 3; i <= 100; i ++) {//控制2-100所有的数i fo ...

  10. EventBus的使用

    # EventBus用于android线程间的通信,方便将子线程的数据发送的UI线程,对UI界面更新:总体来说对于这个过程可以分为3个步骤: 1.创建event,用于传递信息: 比如你需要传List集 ...