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. shared_ptr:资源管理利器

    如果你还在使用传统的C++,那么可以肯定堆内存的管理让你头痛过!在传统的C++领域,堆内存管理上我们能借用的现成工具就只有auto_ptr.但是很不幸用auto_ptr管理堆内存简直就是个错误.aut ...

  2. atexit函数

    使用该函数注册的退出函数是在进程正常退出时,才会被调用.这里强调是进程正常退出,使用exit退出或使用main中最后的return语句退出.但如果是因为收到信号signal而导致程序退出,如kill ...

  3. 区分苹果Safari浏览器

    区分苹果Safari浏览器 (function() { var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua ...

  4. Ubuntu系统下Xen虚拟机的基本安装方法(代码创建)

    Ubuntu上Xen安装虚拟机方法一dd一个空的磁盘sudo dd if=/dev/zero of=/home/vm1.img bs=1G count=8 下载Xen VM通用配置文件 sudo wg ...

  5. 用vc生成可被python调用的dll文件

    前提已经有.c 和.i文件 用swid编译了.i文件生成了wrap.c文件和.py文件 vc创建dll工程 将.h加入到头文件中.c文件和wrap.c文件添加到源文件中 将.i文件添加到工程目录下To ...

  6. Spring的Bean的基本概念

    Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.Spring容器能够生产哪些产品,取决于配置文件的配置. 对于我们而言,使用Spring框架做两件事:开发Bean. ...

  7. SqlServer性能优化 即席查询(十三)

    执行计划,查询类别: 1.即席查询     2.预定义查询 select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Siz ...

  8. array_unshift() 、

    定义和用法 array_unshift() 函数在数组开头插入一个或多个元素. 被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样. 该函数会返回数组中元素的个数. 语法 arr ...

  9. Chrome和IE中使用window.open函数

    做前端开发的人员经常回遇到使用windows.open这个函数来打开一个新的网页窗口,使用这个函数的时候有些需要注意的地方,在Chrome和IE下该函数还是有一些细节性的区别. 以下是我在项目中使用的 ...

  10. RealProxy实现AOP编程(2)

    稍微变化一下!注意区别. Program.cs class Program { static void Main(string[] args) { User user = " }; var ...