前面分析了memblock算法、内核页表的建立、内存管理框架的构建,这些都是x86处理的setup_arch()函数里面初始化的,因地制宜,具有明显处理器的特征。而start_kernel()接下来的初始化则是linux通用的内存管理算法框架了。

build_all_zonelists()用来初始化内存分配器使用的存储节点中的管理区链表,是为内存管理算法(伙伴管理算法)做准备工作的。具体实现:

  1. file:/mm/page_alloc.c
  2. /*
  3. * Called with zonelists_mutex held always
  4. * unless system_state == SYSTEM_BOOTING.
  5. */
  6. void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
  7. {
  8. set_zonelist_order();
  9. if (system_state == SYSTEM_BOOTING) {
  10. __build_all_zonelists(NULL);
  11. mminit_verify_zonelist();
  12. cpuset_init_current_mems_allowed();
  13. } else {
  14. #ifdef CONFIG_MEMORY_HOTPLUG
  15. if (zone)
  16. setup_zone_pageset(zone);
  17. #endif
  18. /* we have to stop all cpus to guarantee there is no user
  19. of zonelist */
  20. stop_machine(__build_all_zonelists, pgdat, NULL);
  21. /* cpuset refresh routine should be here */
  22. }
  23. vm_total_pages = nr_free_pagecache_pages();
  24. /*
  25. * Disable grouping by mobility if the number of pages in the
  26. * system is too low to allow the mechanism to work. It would be
  27. * more accurate, but expensive to check per-zone. This check is
  28. * made on memory-hotadd so a system can start with mobility
  29. * disabled and enable it later
  30. */
  31. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  32. page_group_by_mobility_disabled = 1;
  33. else
  34. page_group_by_mobility_disabled = 0;
  35. printk("Built %i zonelists in %s order, mobility grouping %s. "
  36. "Total pages: %ld\n",
  37. nr_online_nodes,
  38. zonelist_order_name[current_zonelist_order],
  39. page_group_by_mobility_disabled ? "off" : "on",
  40. vm_total_pages);
  41. #ifdef CONFIG_NUMA
  42. printk("Policy zone: %s\n", zone_names[policy_zone]);
  43. #endif
  44. }

首先看到set_zonelist_order():

  1. file:/mm/page_alloc.c
  2. static void set_zonelist_order(void)
  3. {
  4. current_zonelist_order = ZONELIST_ORDER_ZONE;
  5. }

此处用于设置zonelist的顺序,ZONELIST_ORDER_ZONE用于表示顺序(-zonetype, [node] distance),另外还有ZONELIST_ORDER_NODE表示顺序([node] distance, -zonetype)。但其仅限于对NUMA环境存在区别,非NUMA环境则毫无差异。

如果系统状态system_state为SYSTEM_BOOTING,系统状态只有在start_kernel执行到最后一个函数rest_init后,才会进入SYSTEM_RUNNING,于是初始化时将会接着是__build_all_zonelists()函数:

  1. file:/mm/page_alloc.c
  2. /* return values int ....just for stop_machine() */
  3. static int __build_all_zonelists(void *data)
  4. {
  5. int nid;
  6. int cpu;
  7. pg_data_t *self = data;
  8. #ifdef CONFIG_NUMA
  9. memset(node_load, 0, sizeof(node_load));
  10. #endif
  11. if (self && !node_online(self->node_id)) {
  12. build_zonelists(self);
  13. build_zonelist_cache(self);
  14. }
  15. for_each_online_node(nid) {
  16. pg_data_t *pgdat = NODE_DATA(nid);
  17. build_zonelists(pgdat);
  18. build_zonelist_cache(pgdat);
  19. }
  20. /*
  21. * Initialize the boot_pagesets that are going to be used
  22. * for bootstrapping processors. The real pagesets for
  23. * each zone will be allocated later when the per cpu
  24. * allocator is available.
  25. *
  26. * boot_pagesets are used also for bootstrapping offline
  27. * cpus if the system is already booted because the pagesets
  28. * are needed to initialize allocators on a specific cpu too.
  29. * F.e. the percpu allocator needs the page allocator which
  30. * needs the percpu allocator in order to allocate its pagesets
  31. * (a chicken-egg dilemma).
  32. */
  33. for_each_possible_cpu(cpu) {
  34. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  35. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  36. /*
  37. * We now know the "local memory node" for each node--
  38. * i.e., the node of the first zone in the generic zonelist.
  39. * Set up numa_mem percpu variable for on-line cpus. During
  40. * boot, only the boot cpu should be on-line; we'll init the
  41. * secondary cpus' numa_mem as they come on-line. During
  42. * node/memory hotplug, we'll fixup all on-line cpus.
  43. */
  44. if (cpu_online(cpu))
  45. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  46. #endif
  47. }
  48. return 0;
  49. }

其中build_zonelists_node()函数实现:

  1. file:/mm/page_alloc.c
  2. /*
  3. * Builds allocation fallback zone lists.
  4. *
  5. * Add all populated zones of a node to the zonelist.
  6. */
  7. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  8. int nr_zones)
  9. {
  10. struct zone *zone;
  11. enum zone_type zone_type = MAX_NR_ZONES;
  12. do {
  13. zone_type--;
  14. zone = pgdat->node_zones + zone_type;
  15. if (populated_zone(zone)) {
  16. zoneref_set_zone(zone,
  17. &zonelist->_zonerefs[nr_zones++]);
  18. check_highest_zone(zone_type);
  19. }
  20. } while (zone_type);
  21. return nr_zones;
  22. }

populated_zone()用于判断管理区zone的present_pages成员是否为0,如果不为0的话,表示该管理区存在页面,那么则通过zoneref_set_zone()将其设置到zonelist的_zonerefs里面,而check_highest_zone()在没有开启NUMA的情况下是个空函数。由此可以看出build_zonelists_node()实则上是按照ZONE_HIGHMEM—>ZONE_NORMAL—>ZONE_DMA的顺序去迭代排布到_zonerefs里面的,表示一个申请内存的代价由低廉到昂贵的顺序,这是一个分配内存时的备用次序。

回到build_zonelists()函数中,而它代码显示将本地的内存管理区进行分配备用次序排序,接着再是分配内存代价低于本地的,最后才是分配内存代价高于本地的。

分析完build_zonelists(),再回到__build_all_zonelists()看一下build_zonelist_cache():

  1. file:/mm/page_alloc.c
  2. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  3. static void build_zonelist_cache(pg_data_t *pgdat)
  4. {
  5. pgdat->node_zonelists[0].zlcache_ptr = NULL;
  6. }

该函数与CONFIG_NUMA相关,用来设置zlcache相关的成员。由于没有开启该配置,故直接设置为NULL。

基于build_all_zonelists()调用__build_all_zonelists()入参为NULL,由此可知__build_all_zonelists()运行的代码是:

  1. for_each_online_node(nid) {
  2. pg_data_t *pgdat = NODE_DATA(nid);
  3. build_zonelists(pgdat);
  4. build_zonelist_cache(pgdat);
  5. }

主要是设置各个内存管理节点node里面各自的内存管理分区zone的内存分配次序。

__build_all_zonelists()接着的是:

  1. for_each_possible_cpu(cpu) {
  2. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  3. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  4. if (cpu_online(cpu))
  5. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  6. #endif
  7. }

其中CONFIG_HAVE_MEMORYLESS_NODES未配置,主要分析一下setup_pageset():

  1. file:/mm/page_alloc.c
  2. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  3. {
  4. pageset_init(p);
  5. pageset_set_batch(p, batch);
  6. }

setup_pageset()里面调用的两个函数较为简单,就直接过一下。先是:

  1. file:/mm/page_alloc.c
  2. static void pageset_init(struct per_cpu_pageset *p)
  3. {
  4. struct per_cpu_pages *pcp;
  5. int migratetype;
  6. memset(p, 0, sizeof(*p));
  7. pcp = &p->pcp;
  8. pcp->count = 0;
  9. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  10. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  11. }

pageset_init()主要是将struct per_cpu_pages结构体进行初始化,而pageset_set_batch()则是对其进行设置。pageset_set_batch()实现:

  1. file:/mm/page_alloc.c
  2. /*
  3. * pcp->high and pcp->batch values are related and dependent on one another:
  4. * ->batch must never be higher then ->high.
  5. * The following function updates them in a safe manner without read side
  6. * locking.
  7. *
  8. * Any new users of pcp->batch and pcp->high should ensure they can cope with
  9. * those fields changing asynchronously (acording the the above rule).
  10. *
  11. * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
  12. * outside of boot time (or some other assurance that no concurrent updaters
  13. * exist).
  14. */
  15. static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
  16. unsigned long batch)
  17. {
  18. /* start with a fail safe value for batch */
  19. pcp->batch = 1;
  20. smp_wmb();
  21. /* Update high, then batch, in order */
  22. pcp->high = high;
  23. smp_wmb();
  24. pcp->batch = batch;
  25. }
  26. /* a companion to pageset_set_high() */
  27. static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
  28. {
  29. pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
  30. }

setup_pageset()函数入参p是一个struct per_cpu_pageset结构体的指针,per_cpu_pageset结构是内核的各个zone用于每CPU的页面高速缓存管理结构。该高速缓存包含一些预先分配的页面,以用于满足本地CPU发出的单一内存请求。而struct per_cpu_pages定义的pcp是该管理结构的成员,用于具体页面管理。原本是每个管理结构有两个pcp数组成员,里面的两条队列分别用于冷页面和热页面管理,而当前分析的3.14.12版本已经将两者合并起来,统一管理冷热页,热页面在队列前面,而冷页面则在队列后面。暂且先记着这么多,后续在Buddy算法的时候再详细分析了。

至此,可以知道__build_all_zonelists()是内存管理框架向后续的内存页面管理算法做准备,排布了内存管理区zone的分配次序,同时初始化了冷热页管理。

最后回到build_all_zonelists()函数。由于没有开启内存初始化调试功能CONFIG_DEBUG_MEMORY_INIT,mminit_verify_zonelist()是一个空函数。

基于CONFIG_CPUSETS配置项开启的情况下,而cpuset_init_current_mems_allowed()实现如下:

  1. file:/kernel/cpuset.c
  2. void cpuset_init_current_mems_allowed(void)
  3. {
  4. nodes_setall(current->mems_allowed);
  5. }

这里面的current 是一个cpuset的数据结构,用来管理cgroup中的任务能够使用的cpu和内存节点。而成员mems_allowed,该成员是nodemask_t类型的结构体

  1. file:/include/linux/nodemask.h
  2. typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;

该结构其实就是定义了一个位域,每个位对应一个内存结点,如果置1表示该节点内存可用。而nodes_setall则是将这个位域中每个位都置1。

末了看一下build_all_zonelists()里面nr_free_pagecache_pages()的实现:

  1. file:/mm/page_alloc.c
  2. /**
  3. * nr_free_pagecache_pages - count number of pages beyond high watermark
  4. *
  5. * nr_free_pagecache_pages() counts the number of pages which are beyond the
  6. * high watermark within all zones.
  7. */
  8. unsigned long nr_free_pagecache_pages(void)
  9. {
  10. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  11. }

而里面调用的nr_free_zone_pages()实现为:

  1. file:/mm/page_alloc.c
  2. /**
  3. * nr_free_zone_pages - count number of pages beyond high watermark
  4. * @offset: The zone index of the highest zone
  5. *
  6. * nr_free_zone_pages() counts the number of counts pages which are beyond the
  7. * high watermark within all zones at or below a given zone index. For each
  8. * zone, the number of pages is calculated as:
  9. * managed_pages - high_pages
  10. */
  11. static unsigned long nr_free_zone_pages(int offset)
  12. {
  13. struct zoneref *z;
  14. struct zone *zone;
  15. /* Just pick one node, since fallback list is circular */
  16. unsigned long sum = 0;
  17. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  18. for_each_zone_zonelist(zone, z, zonelist, offset) {
  19. unsigned long size = zone->managed_pages;
  20. unsigned long high = high_wmark_pages(zone);
  21. if (size > high)
  22. sum += size - high;
  23. }
  24. return sum;
  25. }

可以看到nr_free_zone_pages()遍历所有内存管理区并将各管理区的内存空间求和,其实质是用于统计所有的管理区可以用于分配的内存页面数。

接着在build_all_zonelists()后面则是判断当前系统中的内存页框数目,以决定是否启用流动分组机制(Mobility Grouping),该机制可以在分配大内存块时减少内存碎片。通常只有内存足够大时才会启用该功能,否则将会提升消耗降低性能。其中pageblock_nr_pages表示伙伴系统中的最高阶页块所能包含的页面数。

至此,内存管理框架算法基本准备完毕。

Linux-3.14.12内存管理笔记【伙伴管理算法(1)】的更多相关文章

  1. Linux-3.14.12内存管理笔记【伙伴管理算法(4)】

    此处承接前面未深入分析的页面释放部分,主要详细分析伙伴管理算法中页面释放的实现.页面释放的函数入口是__free_page(),其实则是一个宏定义. 具体实现: [file:/include/linu ...

  2. Linux-3.14.12内存管理笔记【伙伴管理算法(2)】

    前面已经分析了linux内存管理算法(伙伴管理算法)的准备工作. 具体的算法初始化则回到start_kernel()函数接着往下走,下一个函数是mm_init(): [file:/init/main. ...

  3. Linux-3.14.12内存管理笔记【伙伴管理算法(3)】

    前面分析了伙伴管理算法的初始化,在切入分析代码实现之前,例行先分析一下其实现原理. 伙伴管理算法(也称之为Buddy算法),该算法将所有空闲的页面分组划分为MAX_ORDER个页面块链表进行管理,其中 ...

  4. Linux-3.14.12内存管理笔记【构建内存管理框架(1)】

    传统的计算机结构中,整个物理内存都是一条线上的,CPU访问整个内存空间所需要的时间都是相同的.这种内存结构被称之为UMA(Uniform Memory Architecture,一致存储结构).但是随 ...

  5. 2. Linux-3.14.12内存管理笔记【系统启动阶段的memblock算法(2)】

    memory:表示可用可分配的内存: 结束完memblock算法初始化前的准备工作,回到memblock算法初始化及其算法实现上面.memblock是一个很简单的算法. memblock算法的实现是, ...

  6. Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】

    本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...

  7. Linux-3.14.12内存管理笔记【构建内存管理框架(5)】

    前面已经分析了内存管理框架的构建实现过程,有部分内容未完全呈现出来,这里主要做个补充. 如下图,这是前面已经看到过的linux物理内存管理框架的层次关系. 现着重分析一下各个管理结构体的成员功能作用. ...

  8. Linux-3.14.12内存管理笔记【建立内核页表(1)】

    前面已经分析过了Intel的内存映射和linux的基本使用情况,已知head_32.S仅是建立临时页表,内核还是要建立内核页表,做到全面映射的.下面就基于RAM大于896MB,而小于4GB ,切CON ...

  9. 1. Linux-3.14.12内存管理笔记【系统启动阶段的memblock算法(1)】

    memblock算法是linux内核初始化阶段的一个内存分配器(它取代了原来的bootmem算法),实现较为简单.负责page allocator初始化之前的内存管理和分配请求. 分析memblock ...

随机推荐

  1. HTTP 400 与 SpringMVC的 HttpPutFormContentFilter、FormContentFilter

    前端发送了一个http PUT请求,如下, json_xml: {,},,}},,},,}},},}},,},{,}],,},,}},,,,},,}},},}},,},{,}],,},,}},,},{ ...

  2. C# 使用 csc.exe 实现命令行生成

    概述 CSC是什么呢?CSC就是 C-Sharp Compiler (中文就是C#编译器),作用是把我们的 cs 源文件变异成dll 或者是exe ,    一般安装完VS 后,就会有这个文件: 这里 ...

  3. Redis Python(二)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.NoSQL(Not only SQL)1.泛指非关系数据库2.不支持SQL语法3.存储结构与传统的关系型数据库 ...

  4. [document.cookie]为什么cookie不在window下的呢.奇怪了[未完待续]

    什么是cookie,怎么就叫cookis,它能干嘛 我猜吧,就是登录页面的时候传值,二次登录的时候可以给你说句'hello xxx'; 下面这堆比较啰嗦,随意看吧 //cookie 用户储存在用户本地 ...

  5. HTML 表单模板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 5、netty第四个例子,空闲检测handle

    netty可支持空闲检测的处理器,用于心态检测,当服务器端超出等待时间,没发生事件时,会触发handler中的方法 userEventTriggered. initializer import io. ...

  7. MySQL5.7脚本自动安装

    脚本里面没有把同步时间写进去,这个写在最前面yum install -y ntp ntpdatecp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localti ...

  8. python 基础学习笔记(5)--文件操作

    **python 的文件操作** - [ ] 使用python来读写文件是非常简单的操作,我们使用open()来打开一个文件,获取到文件的语柄,然后通过文件语柄就可以进行各种各样的操作了. - [ ] ...

  9. (办公)记事本_通过xshell连接Liunx服务器

    任务:需要用xshell连接到Liunx服务器,装配环境,放置项目,查看日志,以后就要做,磁盘扩容,均衡负载,以及病毒错误. 第一步,先连接上: 1.xshell新建会话,刚才买的liunx的公网地址 ...

  10. uiautomatorviewer 报错 Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

    在进行自动化时经常需要使用到 uiautomatorviewer获取控件的各个属性,然后在脚本中通过各个控件的属性来操作. 如果使用的是uiautomator2的话,一般都是使用weditor这个来查 ...