歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux Slab分配器(四)--分配對象

Linux Slab分配器(四)--分配對象

日期:2017/3/1 10:19:26   编辑:Linux編程

從一個緩存中分配對象總是遵循下面的原則:

1.本地高速緩存中是否有空閒對象,如果有的話則從其中獲取對象,這時分配的對象是最“熱”的;

2.如果本地高速緩存中沒有對象,則從kmem_list3中的slab鏈表中尋找空閒對象並填充到本地高速緩存再分配;

3.如果所有的slab中都沒有空閒對象了,那麼就要創建新的slab,再分配 。

函數kmem_cache_alloc用於從特定的緩存獲取對象,kmalloc用於從普通緩存中獲取對象,它們的執行流程如下圖所示

實質性的工作是從____cache_alloc()開始的,因此從這個函數作為入口來分析

  1. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2. {
  3. void *objp;
  4. struct array_cache *ac;
  5. check_irq_off();
  6. /*獲取緩存的本地高速緩存的描述符array_cache*/
  7. ac = cpu_cache_get(cachep);
  8. /*如果本地高速緩存中還有空閒對象可以分配則從本地高速緩存中分配*/
  9. if (likely(ac->avail)) {
  10. STATS_INC_ALLOCHIT(cachep);
  11. ac->touched = 1;
  12. /*先將avail的值減1,這樣avail對應的空閒對象是最熱的,即最近釋放出來的,
  13. 更有可能駐留在CPU高速緩存中*/
  14. objp = ac->entry[--ac->avail];
  15. } else {/*否則需要填充本地高速緩存*/
  16. STATS_INC_ALLOCMISS(cachep);
  17. objp = cache_alloc_refill(cachep, flags);
  18. }
  19. /*
  20. * To avoid a false negative, if an object that is in one of the
  21. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  22. * treat the array pointers as a reference to the object.
  23. */
  24. kmemleak_erase(&ac->entry[ac->avail]);
  25. return objp;
  26. }
  1. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2. {
  3. int batchcount;
  4. struct kmem_list3 *l3;
  5. struct array_cache *ac;
  6. int node;
  7. retry:
  8. check_irq_off();
  9. node = numa_node_id();
  10. ac = cpu_cache_get(cachep);
  11. batchcount = ac->batchcount; /*獲取批量轉移的數目*/
  12. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  13. /*
  14. * If there was little recent activity on this cache, then
  15. * perform only a partial refill. Otherwise we could generate
  16. * refill bouncing.
  17. */
  18. batchcount = BATCHREFILL_LIMIT;
  19. }
  20. /*獲取kmem_list3*/
  21. l3 = cachep->nodelists[node];
  22. BUG_ON(ac->avail > 0 || !l3);
  23. spin_lock(&l3->list_lock);
  24. /* See if we can refill from the shared array */
  25. /*如果有共享本地高速緩存,則從共享本地高速緩存填充*/
  26. if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
  27. goto alloc_done;
  28. while (batchcount > 0) {
  29. struct list_head *entry;
  30. struct slab *slabp;
  31. /* Get slab alloc is to come from. */
  32. /*掃描slab鏈表,先從partial鏈表開始,如果整個partial鏈表都無法找到batchcount個空閒對象,
  33. 再掃描free鏈表*/
  34. entry = l3->slabs_partial.next;
  35. /*entry回到表頭說明partial鏈表已經掃描完畢,開始掃描free鏈表*/
  36. if (entry == &l3->slabs_partial) {
  37. l3->free_touched = 1;
  38. entry = l3->slabs_free.next;
  39. if (entry == &l3->slabs_free)
  40. goto must_grow;
  41. }
  42. /*由鏈表項得到slab描述符*/
  43. slabp = list_entry(entry, struct slab, list);
  44. check_slabp(cachep, slabp);
  45. check_spinlock_acquired(cachep);
  46. /*
  47. * The slab was either on partial or free list so
  48. * there must be at least one object available for
  49. * allocation.
  50. */
  51. BUG_ON(slabp->inuse >= cachep->num);
  52. /*如果slabp中還存在空閒對象並且還需要繼續填充對象到本地高速緩存*/
  53. while (slabp->inuse < cachep->num && batchcount--) {
  54. STATS_INC_ALLOCED(cachep);
  55. STATS_INC_ACTIVE(cachep);
  56. STATS_SET_HIGH(cachep);
  57. /*填充的本質就是用ac後面的void*數組元素指向一個空閒對象*/
  58. ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
  59. node);
  60. }
  61. check_slabp(cachep, slabp);
  62. /* move slabp to correct slabp list: */
  63. /*由於從slab中分配出去了對象,因此有可能需要將slab移到其他鏈表中去*/
  64. list_del(&slabp->list);
  65. /*free等於BUFCTL_END表示空閒對象已耗盡,將slab插入full鏈表*/
  66. if (slabp->free == BUFCTL_END)
  67. list_add(&slabp->list, &l3->slabs_full);
  68. else/*否則肯定是插入partial鏈表*/
  69. list_add(&slabp->list, &l3->slabs_partial);
  70. }
  71. must_grow:
  72. l3->free_objects -= ac->avail;/*刷新kmem_list3中的空閒對象*/
  73. alloc_done:
  74. spin_unlock(&l3->list_lock);
  75. /*avail為0表示kmem_list3中的slab全部處於full狀態或者沒有slab,則要為緩存分配slab*/
  76. if (unlikely(!ac->avail)) {
  77. int x;
  78. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  79. /* cache_grow can reenable interrupts, then ac could change. */
  80. ac = cpu_cache_get(cachep);
  81. if (!x && ac->avail == 0) /* no objects in sight? abort */
  82. return NULL;
  83. if (!ac->avail) /* objects refilled by interrupt? */
  84. goto retry;
  85. }
  86. ac->touched = 1;
  87. /*返回最後一個末端的對象*/
  88. return ac->entry[--ac->avail];
  89. }

對於所有slab都空閒對象的情況,需要調用cache_grow()來增加cache的容量,這個函數在後面分析slab的分配時再做介紹。

相關閱讀:

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/63167.htm

Copyright © Linux教程網 All Rights Reserved