緩存回收對象基於以下原則
1.本地高速緩存的空間還可以容納空閒對象,則直接將對象放回本地高速緩存
2.本地高速緩存的空間已滿,則按batchcount的值將對象從本地高速緩存轉移到slab中,轉移是基於先進先出的原則的,也就是轉移entry數組最前面的batchcount個空閒對象,因為這些對象在數組中存在的時間相對較長,不太可能仍然駐留在CPU高速緩存中
相關閱讀:
Linux Slab分配器(一)--概述 http://www.linuxidc.com/Linux/2012-06/62965.htm
Linux Slab分配器(二)--初始化 http://www.linuxidc.com/Linux/2012-06/62966.htm
Linux Slab分配器(三)--創建緩存 http://www.linuxidc.com/Linux/2012-06/63109.htm
Linux Slab分配器(四)--分配對象 http://www.linuxidc.com/Linux/2012-06/63138.htm
Linux Slab分配器(六)--創建slab和銷毀slab http://www.linuxidc.com/Linux/2012-06/63229.htm
釋放對象通過函數kmem_cache_free()來完成,下圖給出了主要的工作流程
我們以__cache_free函數作為入口進行分析
- static inline void __cache_free(struct kmem_cache *cachep, void *objp)
- {
- struct array_cache *ac = cpu_cache_get(cachep);
-
- check_irq_off();
- kmemleak_free_recursive(objp, cachep->flags);
- objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
-
- kmemcheck_slab_free(cachep, objp, obj_size(cachep));
-
- /*
- * Skip calling cache_free_alien() when the platform is not numa.
- * This will avoid cache misses that happen while accessing slabp (which
- * is per page memory reference) to get nodeid. Instead use a global
- * variable to skip the call, which is mostly likely to be present in
- * the cache.
- */
- if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
- return;
-
- /*如果本地高速緩存中的空閒對象小於空閒對象上限,則直接用entry中的元素記錄對象的地址*/
- if (likely(ac->avail < ac->limit)) {
- STATS_INC_FREEHIT(cachep);
- ac->entry[ac->avail++] = objp;
- return;
- } else {/*否則將本地高速緩存中的空閒對象批量轉移到slab中*/
- STATS_INC_FREEMISS(cachep);
- cache_flusharray(cachep, ac);
- ac->entry[ac->avail++] = objp;
- }
- }