《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
20 KiB

12 years ago
12 years ago
12 years ago
  1. #include <defs.h>
  2. #include <list.h>
  3. #include <memlayout.h>
  4. #include <assert.h>
  5. #include <kmalloc.h>
  6. #include <sync.h>
  7. #include <pmm.h>
  8. #include <stdio.h>
  9. #include <rb_tree.h>
  10. /* The slab allocator used in ucore is based on an algorithm first introduced by
  11. Jeff Bonwick for the SunOS operating system. The paper can be download from
  12. http://citeseer.ist.psu.edu/bonwick94slab.html
  13. An implementation of the Slab Allocator as described in outline in;
  14. UNIX Internals: The New Frontiers by Uresh Vahalia
  15. Pub: Prentice Hall ISBN 0-13-101908-2
  16. Within a kernel, a considerable amount of memory is allocated for a finite set
  17. of objects such as file descriptors and other common structures. Jeff found that
  18. the amount of time required to initialize a regular object in the kernel exceeded
  19. the amount of time required to allocate and deallocate it. His conclusion was
  20. that instead of freeing the memory back to a global pool, he would have the memory
  21. remain initialized for its intended purpose.
  22. In our simple slab implementation, the the high-level organization of the slab
  23. structures is simplied. At the highest level is an array slab_cache[SLAB_CACHE_NUM],
  24. and each array element is a slab_cache which has slab chains. Each slab_cache has
  25. two list, one list chains the full allocated slab, and another list chains the notfull
  26. allocated(maybe empty) slab. And each slab has fixed number(2^n) of pages. In each
  27. slab, there are a lot of objects (such as ) with same fixed size(32B ~ 128KB).
  28. +----------------------------------+
  29. | slab_cache[0] for 0~32B obj |
  30. +----------------------------------+
  31. | slab_cache[1] for 33B~64B obj |-->lists for slabs
  32. +----------------------------------+ |
  33. | slab_cache[2] for 65B~128B obj | |
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
  35. +----------------------------------+ |
  36. | slab_cache[12]for 64KB~128KB obj | |
  37. +----------------------------------+ |
  38. |
  39. slabs_full/slabs_not +---------------------+
  40. -<-----------<----------<-+
  41. | | |
  42. slab1 slab2 slab3...
  43. |
  44. |-------|-------|
  45. pages1 pages2 pages3...
  46. |
  47. |
  48. |
  49. slab_t+n*bufctl_t+obj1-obj2-obj3...objn (the size of obj is small)
  50. |
  51. OR
  52. |
  53. obj1-obj2-obj3...objn WITH slab_t+n*bufctl_t in another slab (the size of obj is BIG)
  54. The important functions are:
  55. kmem_cache_grow(kmem_cache_t *cachep)
  56. kmem_slab_destroy(kmem_cache_t *cachep, slab_t *slabp)
  57. kmalloc(size_t size): used by outside functions need dynamicly get memory
  58. kfree(void *objp): used by outside functions need dynamicly release memory
  59. */
  60. #define BUFCTL_END 0xFFFFFFFFL // the signature of the last bufctl
  61. #define SLAB_LIMIT 0xFFFFFFFEL // the max value of obj number
  62. typedef size_t kmem_bufctl_t; //the index of obj in slab
  63. typedef struct slab_s {
  64. list_entry_t slab_link; // the list entry linked to kmem_cache list
  65. void *s_mem; // the kernel virtual address of the first obj in slab
  66. size_t inuse; // the number of allocated objs
  67. size_t offset; // the first obj's offset value in slab
  68. kmem_bufctl_t free; // the first free obj's index in slab
  69. } slab_t;
  70. // get the slab address according to the link element (see list.h)
  71. #define le2slab(le, member) \
  72. to_struct((le), slab_t, member)
  73. typedef struct kmem_cache_s kmem_cache_t;
  74. struct kmem_cache_s {
  75. list_entry_t slabs_full; // list for fully allocated slabs
  76. list_entry_t slabs_notfull; // list for not-fully allocated slabs
  77. size_t objsize; // the fixed size of obj
  78. size_t num; // number of objs per slab
  79. size_t offset; // this first obj's offset in slab
  80. bool off_slab; // the control part of slab in slab or not.
  81. /* order of pages per slab (2^n) */
  82. size_t page_order;
  83. kmem_cache_t *slab_cachep;
  84. };
  85. #define MIN_SIZE_ORDER 5 // 32
  86. #define MAX_SIZE_ORDER 17 // 128k
  87. #define SLAB_CACHE_NUM (MAX_SIZE_ORDER - MIN_SIZE_ORDER + 1)
  88. static kmem_cache_t slab_cache[SLAB_CACHE_NUM];
  89. static void init_kmem_cache(kmem_cache_t *cachep, size_t objsize, size_t align);
  90. static void check_slab(void);
  91. //slab_init - call init_kmem_cache function to reset the slab_cache array
  92. static void
  93. slab_init(void) {
  94. size_t i;
  95. //the align bit for obj in slab. 2^n could be better for performance
  96. size_t align = 16;
  97. for (i = 0; i < SLAB_CACHE_NUM; i ++) {
  98. init_kmem_cache(slab_cache + i, 1 << (i + MIN_SIZE_ORDER), align);
  99. }
  100. check_slab();
  101. }
  102. inline void
  103. kmalloc_init(void) {
  104. slab_init();
  105. cprintf("kmalloc_init() succeeded!\n");
  106. }
  107. //slab_allocated - summary the total size of allocated objs
  108. static size_t
  109. slab_allocated(void) {
  110. size_t total = 0;
  111. int i;
  112. bool intr_flag;
  113. local_intr_save(intr_flag);
  114. {
  115. for (i = 0; i < SLAB_CACHE_NUM; i ++) {
  116. kmem_cache_t *cachep = slab_cache + i;
  117. list_entry_t *list, *le;
  118. list = le = &(cachep->slabs_full);
  119. while ((le = list_next(le)) != list) {
  120. total += cachep->num * cachep->objsize;
  121. }
  122. list = le = &(cachep->slabs_notfull);
  123. while ((le = list_next(le)) != list) {
  124. slab_t *slabp = le2slab(le, slab_link);
  125. total += slabp->inuse * cachep->objsize;
  126. }
  127. }
  128. }
  129. local_intr_restore(intr_flag);
  130. return total;
  131. }
  132. // slab_mgmt_size - get the size of slab control area (slab_t+num*kmem_bufctl_t)
  133. static size_t
  134. slab_mgmt_size(size_t num, size_t align) {
  135. return ROUNDUP(sizeof(slab_t) + num * sizeof(kmem_bufctl_t), align);
  136. }
  137. // cacahe_estimate - estimate the number of objs in a slab
  138. static void
  139. cache_estimate(size_t order, size_t objsize, size_t align, bool off_slab, size_t *remainder, size_t *num) {
  140. size_t nr_objs, mgmt_size;
  141. size_t slab_size = (PGSIZE << order);
  142. if (off_slab) {
  143. mgmt_size = 0;
  144. nr_objs = slab_size / objsize;
  145. if (nr_objs > SLAB_LIMIT) {
  146. nr_objs = SLAB_LIMIT;
  147. }
  148. }
  149. else {
  150. nr_objs = (slab_size - sizeof(slab_t)) / (objsize + sizeof(kmem_bufctl_t));
  151. while (slab_mgmt_size(nr_objs, align) + nr_objs * objsize > slab_size) {
  152. nr_objs --;
  153. }
  154. if (nr_objs > SLAB_LIMIT) {
  155. nr_objs = SLAB_LIMIT;
  156. }
  157. mgmt_size = slab_mgmt_size(nr_objs, align);
  158. }
  159. *num = nr_objs;
  160. *remainder = slab_size - nr_objs * objsize - mgmt_size;
  161. }
  162. // calculate_slab_order - estimate the size(4K~4M) of slab
  163. // paramemters:
  164. // cachep: the slab_cache
  165. // objsize: the size of obj
  166. // align: align bit for objs
  167. // off_slab: the control part of slab in slab or not
  168. // left_over: the size of can not be used area in slab
  169. static void
  170. calculate_slab_order(kmem_cache_t *cachep, size_t objsize, size_t align, bool off_slab, size_t *left_over) {
  171. size_t order;
  172. for (order = 0; order <= KMALLOC_MAX_ORDER; order ++) {
  173. size_t num, remainder;
  174. cache_estimate(order, objsize, align, off_slab, &remainder, &num);
  175. if (num != 0) {
  176. if (off_slab) {
  177. size_t off_slab_limit = objsize - sizeof(slab_t);
  178. off_slab_limit /= sizeof(kmem_bufctl_t);
  179. if (num > off_slab_limit) {
  180. panic("off_slab: objsize = %d, num = %d.", objsize, num);
  181. }
  182. }
  183. if (remainder * 8 <= (PGSIZE << order)) {
  184. cachep->num = num;
  185. cachep->page_order = order;
  186. if (left_over != NULL) {
  187. *left_over = remainder;
  188. }
  189. return ;
  190. }
  191. }
  192. }
  193. panic("calculate_slab_over: failed.");
  194. }
  195. // getorder - find order, should satisfy n <= minest 2^order
  196. static inline size_t
  197. getorder(size_t n) {
  198. size_t order = MIN_SIZE_ORDER, order_size = (1 << order);
  199. for (; order <= MAX_SIZE_ORDER; order ++, order_size <<= 1) {
  200. if (n <= order_size) {
  201. return order;
  202. }
  203. }
  204. panic("getorder failed. %d\n", n);
  205. }
  206. // init_kmem_cache - initial a slab_cache cachep according to the obj with the size = objsize
  207. static void
  208. init_kmem_cache(kmem_cache_t *cachep, size_t objsize, size_t align) {
  209. list_init(&(cachep->slabs_full));
  210. list_init(&(cachep->slabs_notfull));
  211. objsize = ROUNDUP(objsize, align);
  212. cachep->objsize = objsize;
  213. cachep->off_slab = (objsize >= (PGSIZE >> 3));
  214. size_t left_over;
  215. calculate_slab_order(cachep, objsize, align, cachep->off_slab, &left_over);
  216. assert(cachep->num > 0);
  217. size_t mgmt_size = slab_mgmt_size(cachep->num, align);
  218. if (cachep->off_slab && left_over >= mgmt_size) {
  219. cachep->off_slab = 0;
  220. }
  221. if (cachep->off_slab) {
  222. cachep->offset = 0;
  223. cachep->slab_cachep = slab_cache + (getorder(mgmt_size) - MIN_SIZE_ORDER);
  224. }
  225. else {
  226. cachep->offset = mgmt_size;
  227. }
  228. }
  229. static void *kmem_cache_alloc(kmem_cache_t *cachep);
  230. #define slab_bufctl(slabp) \
  231. ((kmem_bufctl_t*)(((slab_t *)(slabp)) + 1))
  232. // kmem_cache_slabmgmt - get the address of a slab according to page
  233. // - and initialize the slab according to cachep
  234. static slab_t *
  235. kmem_cache_slabmgmt(kmem_cache_t *cachep, struct Page *page) {
  236. void *objp = page2kva(page);
  237. slab_t *slabp;
  238. if (cachep->off_slab) {
  239. if ((slabp = kmem_cache_alloc(cachep->slab_cachep)) == NULL) {
  240. return NULL;
  241. }
  242. }
  243. else {
  244. slabp = page2kva(page);
  245. }
  246. slabp->inuse = 0;
  247. slabp->offset = cachep->offset;
  248. slabp->s_mem = objp + cachep->offset;
  249. return slabp;
  250. }
  251. #define SET_PAGE_CACHE(page, cachep) \
  252. do { \
  253. struct Page *__page = (struct Page *)(page); \
  254. kmem_cache_t **__cachepp = (kmem_cache_t **)&(__page->page_link.next); \
  255. *__cachepp = (kmem_cache_t *)(cachep); \
  256. } while (0)
  257. #define SET_PAGE_SLAB(page, slabp) \
  258. do { \
  259. struct Page *__page = (struct Page *)(page); \
  260. slab_t **__cachepp = (slab_t **)&(__page->page_link.prev); \
  261. *__cachepp = (slab_t *)(slabp); \
  262. } while (0)
  263. // kmem_cache_grow - allocate a new slab by calling alloc_pages
  264. // - set control area in the new slab
  265. static bool
  266. kmem_cache_grow(kmem_cache_t *cachep) {
  267. struct Page *page = alloc_pages(1 << cachep->page_order);
  268. if (page == NULL) {
  269. goto failed;
  270. }
  271. slab_t *slabp;
  272. if ((slabp = kmem_cache_slabmgmt(cachep, page)) == NULL) {
  273. goto oops;
  274. }
  275. size_t order_size = (1 << cachep->page_order);
  276. do {
  277. //setup this page in the free list (see memlayout.h: struct page)???
  278. SET_PAGE_CACHE(page, cachep);
  279. SET_PAGE_SLAB(page, slabp);
  280. //this page is used for slab
  281. SetPageSlab(page);
  282. page ++;
  283. } while (-- order_size);
  284. int i;
  285. for (i = 0; i < cachep->num; i ++) {
  286. slab_bufctl(slabp)[i] = i + 1;
  287. }
  288. slab_bufctl(slabp)[cachep->num - 1] = BUFCTL_END;
  289. slabp->free = 0;
  290. bool intr_flag;
  291. local_intr_save(intr_flag);
  292. {
  293. list_add(&(cachep->slabs_notfull), &(slabp->slab_link));
  294. }
  295. local_intr_restore(intr_flag);
  296. return 1;
  297. oops:
  298. free_pages(page, 1 << cachep->page_order);
  299. failed:
  300. return 0;
  301. }
  302. // kmem_cache_alloc_one - allocate a obj in a slab
  303. static void *
  304. kmem_cache_alloc_one(kmem_cache_t *cachep, slab_t *slabp) {
  305. slabp->inuse ++;
  306. void *objp = slabp->s_mem + slabp->free * cachep->objsize;
  307. slabp->free = slab_bufctl(slabp)[slabp->free];
  308. if (slabp->free == BUFCTL_END) {
  309. list_del(&(slabp->slab_link));
  310. list_add(&(cachep->slabs_full), &(slabp->slab_link));
  311. }
  312. return objp;
  313. }
  314. // kmem_cache_alloc - call kmem_cache_alloc_one function to allocate a obj
  315. // - if no free obj, try to allocate a slab
  316. static void *
  317. kmem_cache_alloc(kmem_cache_t *cachep) {
  318. void *objp;
  319. bool intr_flag;
  320. try_again:
  321. local_intr_save(intr_flag);
  322. if (list_empty(&(cachep->slabs_notfull))) {
  323. goto alloc_new_slab;
  324. }
  325. slab_t *slabp = le2slab(list_next(&(cachep->slabs_notfull)), slab_link);
  326. objp = kmem_cache_alloc_one(cachep, slabp);
  327. local_intr_restore(intr_flag);
  328. return objp;
  329. alloc_new_slab:
  330. local_intr_restore(intr_flag);
  331. if (kmem_cache_grow(cachep)) {
  332. goto try_again;
  333. }
  334. return NULL;
  335. }
  336. // kmalloc - simple interface used by outside functions
  337. // - to allocate a free memory using kmem_cache_alloc function
  338. void *
  339. kmalloc(size_t size) {
  340. assert(size > 0);
  341. size_t order = getorder(size);
  342. if (order > MAX_SIZE_ORDER) {
  343. return NULL;
  344. }
  345. return kmem_cache_alloc(slab_cache + (order - MIN_SIZE_ORDER));
  346. }
  347. static void kmem_cache_free(kmem_cache_t *cachep, void *obj);
  348. // kmem_slab_destroy - call free_pages & kmem_cache_free to free a slab
  349. static void
  350. kmem_slab_destroy(kmem_cache_t *cachep, slab_t *slabp) {
  351. struct Page *page = kva2page(slabp->s_mem - slabp->offset);
  352. struct Page *p = page;
  353. size_t order_size = (1 << cachep->page_order);
  354. do {
  355. assert(PageSlab(p));
  356. ClearPageSlab(p);
  357. p ++;
  358. } while (-- order_size);
  359. free_pages(page, 1 << cachep->page_order);
  360. if (cachep->off_slab) {
  361. kmem_cache_free(cachep->slab_cachep, slabp);
  362. }
  363. }
  364. // kmem_cache_free_one - free an obj in a slab
  365. // - if slab->inuse==0, then free the slab
  366. static void
  367. kmem_cache_free_one(kmem_cache_t *cachep, slab_t *slabp, void *objp) {
  368. //should not use divide operator ???
  369. size_t objnr = (objp - slabp->s_mem) / cachep->objsize;
  370. slab_bufctl(slabp)[objnr] = slabp->free;
  371. slabp->free = objnr;
  372. slabp->inuse --;
  373. if (slabp->inuse == 0) {
  374. list_del(&(slabp->slab_link));
  375. kmem_slab_destroy(cachep, slabp);
  376. }
  377. else if (slabp->inuse == cachep->num -1 ) {
  378. list_del(&(slabp->slab_link));
  379. list_add(&(cachep->slabs_notfull), &(slabp->slab_link));
  380. }
  381. }
  382. #define GET_PAGE_CACHE(page) \
  383. (kmem_cache_t *)((page)->page_link.next)
  384. #define GET_PAGE_SLAB(page) \
  385. (slab_t *)((page)->page_link.prev)
  386. // kmem_cache_free - call kmem_cache_free_one function to free an obj
  387. static void
  388. kmem_cache_free(kmem_cache_t *cachep, void *objp) {
  389. bool intr_flag;
  390. struct Page *page = kva2page(objp);
  391. if (!PageSlab(page)) {
  392. panic("not a slab page %08x\n", objp);
  393. }
  394. local_intr_save(intr_flag);
  395. {
  396. kmem_cache_free_one(cachep, GET_PAGE_SLAB(page), objp);
  397. }
  398. local_intr_restore(intr_flag);
  399. }
  400. // kfree - simple interface used by ooutside functions to free an obj
  401. void
  402. kfree(void *objp) {
  403. kmem_cache_free(GET_PAGE_CACHE(kva2page(objp)), objp);
  404. }
  405. static inline void
  406. check_slab_empty(void) {
  407. int i;
  408. for (i = 0; i < SLAB_CACHE_NUM; i ++) {
  409. kmem_cache_t *cachep = slab_cache + i;
  410. assert(list_empty(&(cachep->slabs_full)));
  411. assert(list_empty(&(cachep->slabs_notfull)));
  412. }
  413. }
  414. void
  415. check_slab(void) {
  416. int i;
  417. void *v0, *v1;
  418. size_t nr_free_pages_store = nr_free_pages();
  419. size_t kernel_allocated_store = slab_allocated();
  420. /* slab must be empty now */
  421. check_slab_empty();
  422. assert(slab_allocated() == 0);
  423. kmem_cache_t *cachep0, *cachep1;
  424. cachep0 = slab_cache;
  425. assert(cachep0->objsize == 32 && cachep0->num > 1 && !cachep0->off_slab);
  426. assert((v0 = kmalloc(16)) != NULL);
  427. slab_t *slabp0, *slabp1;
  428. assert(!list_empty(&(cachep0->slabs_notfull)));
  429. slabp0 = le2slab(list_next(&(cachep0->slabs_notfull)), slab_link);
  430. assert(slabp0->inuse == 1 && list_next(&(slabp0->slab_link)) == &(cachep0->slabs_notfull));
  431. struct Page *p0, *p1;
  432. size_t order_size;
  433. p0 = kva2page(slabp0->s_mem - slabp0->offset), p1 = p0;
  434. order_size = (1 << cachep0->page_order);
  435. for (i = 0; i < cachep0->page_order; i ++, p1 ++) {
  436. assert(PageSlab(p1));
  437. assert(GET_PAGE_CACHE(p1) == cachep0 && GET_PAGE_SLAB(p1) == slabp0);
  438. }
  439. assert(v0 == slabp0->s_mem);
  440. assert((v1 = kmalloc(16)) != NULL && v1 == v0 + 32);
  441. kfree(v0);
  442. assert(slabp0->free == 0);
  443. kfree(v1);
  444. assert(list_empty(&(cachep0->slabs_notfull)));
  445. for (i = 0; i < cachep0->page_order; i ++, p0 ++) {
  446. assert(!PageSlab(p0));
  447. }
  448. v0 = kmalloc(16);
  449. assert(!list_empty(&(cachep0->slabs_notfull)));
  450. slabp0 = le2slab(list_next(&(cachep0->slabs_notfull)), slab_link);
  451. for (i = 0; i < cachep0->num - 1; i ++) {
  452. kmalloc(16);
  453. }
  454. assert(slabp0->inuse == cachep0->num);
  455. assert(list_next(&(cachep0->slabs_full)) == &(slabp0->slab_link));
  456. assert(list_empty(&(cachep0->slabs_notfull)));
  457. v1 = kmalloc(16);
  458. assert(!list_empty(&(cachep0->slabs_notfull)));
  459. slabp1 = le2slab(list_next(&(cachep0->slabs_notfull)), slab_link);
  460. kfree(v0);
  461. assert(list_empty(&(cachep0->slabs_full)));
  462. assert(list_next(&(slabp0->slab_link)) == &(slabp1->slab_link)
  463. || list_next(&(slabp1->slab_link)) == &(slabp0->slab_link));
  464. kfree(v1);
  465. assert(!list_empty(&(cachep0->slabs_notfull)));
  466. assert(list_next(&(cachep0->slabs_notfull)) == &(slabp0->slab_link));
  467. assert(list_next(&(slabp0->slab_link)) == &(cachep0->slabs_notfull));
  468. v1 = kmalloc(16);
  469. assert(v1 == v0);
  470. assert(list_next(&(cachep0->slabs_full)) == &(slabp0->slab_link));
  471. assert(list_empty(&(cachep0->slabs_notfull)));
  472. for (i = 0; i < cachep0->num; i ++) {
  473. kfree(v1 + i * cachep0->objsize);
  474. }
  475. assert(list_empty(&(cachep0->slabs_full)));
  476. assert(list_empty(&(cachep0->slabs_notfull)));
  477. cachep0 = slab_cache;
  478. bool has_off_slab = 0;
  479. for (i = 0; i < SLAB_CACHE_NUM; i ++, cachep0 ++) {
  480. if (cachep0->off_slab) {
  481. has_off_slab = 1;
  482. cachep1 = cachep0->slab_cachep;
  483. if (!cachep1->off_slab) {
  484. break;
  485. }
  486. }
  487. }
  488. if (!has_off_slab) {
  489. goto check_pass;
  490. }
  491. assert(cachep0->off_slab && !cachep1->off_slab);
  492. assert(cachep1 < cachep0);
  493. assert(list_empty(&(cachep0->slabs_full)));
  494. assert(list_empty(&(cachep0->slabs_notfull)));
  495. assert(list_empty(&(cachep1->slabs_full)));
  496. assert(list_empty(&(cachep1->slabs_notfull)));
  497. v0 = kmalloc(cachep0->objsize);
  498. p0 = kva2page(v0);
  499. assert(page2kva(p0) == v0);
  500. if (cachep0->num == 1) {
  501. assert(!list_empty(&(cachep0->slabs_full)));
  502. slabp0 = le2slab(list_next(&(cachep0->slabs_full)), slab_link);
  503. }
  504. else {
  505. assert(!list_empty(&(cachep0->slabs_notfull)));
  506. slabp0 = le2slab(list_next(&(cachep0->slabs_notfull)), slab_link);
  507. }
  508. assert(slabp0 != NULL);
  509. if (cachep1->num == 1) {
  510. assert(!list_empty(&(cachep1->slabs_full)));
  511. slabp1 = le2slab(list_next(&(cachep1->slabs_full)), slab_link);
  512. }
  513. else {
  514. assert(!list_empty(&(cachep1->slabs_notfull)));
  515. slabp1 = le2slab(list_next(&(cachep1->slabs_notfull)), slab_link);
  516. }
  517. assert(slabp1 != NULL);
  518. order_size = (1 << cachep0->page_order);
  519. for (i = 0; i < order_size; i ++, p0 ++) {
  520. assert(PageSlab(p0));
  521. assert(GET_PAGE_CACHE(p0) == cachep0 && GET_PAGE_SLAB(p0) == slabp0);
  522. }
  523. kfree(v0);
  524. check_pass:
  525. check_rb_tree();
  526. check_slab_empty();
  527. assert(slab_allocated() == 0);
  528. assert(nr_free_pages_store == nr_free_pages());
  529. assert(kernel_allocated_store == slab_allocated());
  530. cprintf("check_slab() succeeded!\n");
  531. }