在跟踪代码的过程中,我们可以看到alloc-page就是分配一个page。
我们在接着看代码:
函数:__alloc_pages_internal,我们可以看到注释:
/*
* This is the 'heart' of the zoned buddy allocator.
*/
从上面知道这个函数的地位了。首先会去唤醒kswapd的内核线成。
wakeup_kswapd。
接着:
unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
gfp_t gfp_mask)
{
struct scan_control sc = {
.gfp_mask = gfp_mask,
.may_writepage = !laptop_mode,
.swap_cluster_max = SWAP_CLUSTER_MAX,
.may_swap = 1,
.swappiness = vm_swappiness,
.order = order,
.mem_cgroup = NULL,
.isolate_pages = isolate_pages_global,
};
return do_try_to_free_pages(zonelist, &sc);
}
上面只是一种想当于人为的去释放pages。kswapd是内存回收的守护内核线程。
下面是kswapd的动作:
static unsigned long balance_pgdat(pg_data_t *pgdat, int order)
{
int all_zones_ok;
int priority;
int i;
unsigned long total_scanned;
unsigned long nr_reclaimed;
struct reclaim_state *reclaim_state = current->reclaim_state;
struct scan_control sc = {
.gfp_mask = GFP_KERNEL,
.may_swap = 1,
.swap_cluster_max = SWAP_CLUSTER_MAX,
.swappiness = vm_swappiness,
.order = order,
.mem_cgroup = NULL,
.isolate_pages = isolate_pages_global,
};
shrink_zone
shrink_active_list
shrink_inactive_list
shrink_slab
我们在前一篇文章说了slab页面没有放在page cache中,所以有一个独立的 shrink_slab函数来处理
对于page cache的处理。
shrink_active_list,我们也同样看看该函数的说明:
/*
* This moves pages from the active list to the inactive list.
*
* We move them the other way if the page is referenced by one or more
* processes, from rmap.
*
* If the pages are mostly unmapped, the processing is fast and it is
* appropriate to hold zone->lru_lock across the whole operation. But if
* the pages are mapped, the processing is slow (page_referenced()) so we
* should drop zone->lru_lock around each page. It's impossible to balance
* this, so instead we remove the pages from the LRU while processing them.
* It is safe to rely on PG_active against the non-LRU pages in here because
* nobody will play with that bit on a non-LRU page.
*
* The downside is that we have to touch page->_count against each page.
* But we had to alter page->flags anyway.
*/
而函数: shrink_inactive_list的说明:
/*
* shrink_inactive_list() is a helper for shrink_zone(). It returns the number
* of reclaimed pages
*/