PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mono/metadata/sgen-split-nursery.c

https://bitbucket.org/danipen/mono
C | 553 lines | 330 code | 85 blank | 138 comment | 41 complexity | 9053238b049b5e489c6ec034d115f8a8 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. /*
  2. * sgen-splliy-nursery.c: 3-space based nursery collector.
  3. *
  4. * Author:
  5. * Rodrigo Kumpera Kumpera <kumpera@gmail.com>
  6. *
  7. * Copyright 2001-2003 Ximian, Inc
  8. * Copyright 2003-2010 Novell, Inc.
  9. * Copyright 2011-2012 Xamarin Inc (http://www.xamarin.com)
  10. * Copyright (C) 2012 Xamarin Inc
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Library General Public
  14. * License 2.0 as published by the Free Software Foundation;
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Library General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public
  22. * License 2.0 along with this library; if not, write to the Free
  23. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include "config.h"
  26. #ifdef HAVE_SGEN_GC
  27. #include "metadata/profiler-private.h"
  28. #include "metadata/sgen-gc.h"
  29. #include "metadata/sgen-protocol.h"
  30. #include "utils/mono-memory-model.h"
  31. /*
  32. The nursery is logically divided into 3 spaces: Allocator space and two Survivor spaces.
  33. Objects are born (allocated by the mutator) in the Allocator Space.
  34. The Survivor spaces are divided in a copying collector style From and To spaces.
  35. The hole of each space switch on each collection.
  36. On each collection we process objects from the nursery this way:
  37. Objects from the Allocator Space are evacuated into the To Space.
  38. Objects from the Survivor From Space are evacuated into the old generation.
  39. The nursery is physically divided in two parts, set by the promotion barrier.
  40. The Allocator Space takes the botton part of the nursery.
  41. The Survivor spaces are intermingled in the top part of the nursery. It's done
  42. this way since the required size for the To Space depends on the survivor rate
  43. of objects from the Allocator Space.
  44. During a collection when the object scan function see a nursery object it must
  45. determine if the object needs to be evacuated or left in place. Originally, this
  46. check was done by checking if a forwarding pointer is installed, but now an object
  47. can be in the To Space, it won't have a forwarding pointer and it must be left in place.
  48. In order to solve that we classify nursery memory been either in the From Space or in
  49. the To Space. Since the Allocator Space has the same behavior as the Survivor From Space
  50. they are unified for this purpoise - a bit confusing at first.
  51. This from/to classification is done on a larger granule than object to make the check efficient
  52. and, due to that, we must make sure that all fragemnts used to allocate memory from the To Space
  53. are naturally aligned in both ends to that granule to avoid wronly classifying a From Space object.
  54. TODO:
  55. -The promotion barrier is statically defined to 50% of the nursery, it should be dinamically adjusted based
  56. on survival rates;
  57. -We apply the same promotion policy to all objects, finalizable ones should age longer in the nursery;
  58. -We apply the same promotion policy to all stages of a collection, maybe we should promote more aggressively
  59. objects from non-stack roots, specially those found in the remembered set;
  60. -Fix our major collection trigger to happen before we do a minor GC and collect the nursery only once.
  61. -Make the serial fragment allocator fast path inlineable
  62. -Make aging threshold be based on survival rates and survivor occupancy;
  63. -Change promotion barrier to be size and not address based;
  64. -Pre allocate memory for young ages to make sure that on overflow only the older suffer;
  65. -Get rid of par_alloc_buffer_refill_mutex so to the parallel collection of the nursery doesn't suck;
  66. */
  67. /*FIXME Move this to a separate header. */
  68. #define _toi(ptr) ((size_t)ptr)
  69. #define make_ptr_mask(bits) ((1 << bits) - 1)
  70. #define align_down(ptr, bits) ((void*)(_toi(ptr) & ~make_ptr_mask (bits)))
  71. #define align_up(ptr, bits) ((void*) ((_toi(ptr) + make_ptr_mask (bits)) & ~make_ptr_mask (bits)))
  72. /*
  73. Even though the effective max age is 255, aging that much doesn't make sense.
  74. It might even make sense to use nimbles for age recording.
  75. */
  76. #define MAX_AGE 15
  77. /*
  78. * Each age has its allocation buffer. Whenever an object is to be
  79. * aged we try to fit it into its new age's allocation buffer. If
  80. * that is not possible we get new space from the fragment allocator
  81. * and set the allocation buffer to that space (minus the space
  82. * required for the object).
  83. */
  84. typedef struct {
  85. char *next;
  86. char *end;
  87. } AgeAllocationBuffer;
  88. /* Limits the ammount of memory the mutator can have. */
  89. static char *promotion_barrier;
  90. /*
  91. Promotion age and alloc ratio are the two nursery knobs to control
  92. how much effort we want to spend on young objects.
  93. Allocation ratio should be the inverse of the expected survivor rate.
  94. The more objects surviver, the smaller the alloc ratio much be so we can
  95. age all objects.
  96. Promote age depends on how much effort we want to spend aging objects before
  97. we promote them to the old generation. If addional ages don't somewhat improve
  98. mortality, it's better avoid as they increase the cost of minor collections.
  99. */
  100. /*
  101. If we're evacuating an object with this age or more, promote it.
  102. Age is the number of surviving collections of an object.
  103. */
  104. static int promote_age = 2;
  105. /*
  106. Initial ratio of allocation and survivor spaces.
  107. This should be read as the fraction of the whole nursery dedicated
  108. for the allocator space.
  109. */
  110. static float alloc_ratio = 60.f/100.f;
  111. static char *region_age;
  112. static int region_age_size;
  113. static AgeAllocationBuffer age_alloc_buffers [MAX_AGE];
  114. /* The collector allocs from here. */
  115. static SgenFragmentAllocator collector_allocator;
  116. static LOCK_DECLARE (par_alloc_buffer_refill_mutex);
  117. static inline int
  118. get_object_age (char *object)
  119. {
  120. int idx = (object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
  121. return region_age [idx];
  122. }
  123. static inline void
  124. set_object_age (char *object, int age)
  125. {
  126. int idx = (object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
  127. region_age [idx] = age;
  128. }
  129. static void
  130. set_age_in_range (char *start, char *end, int age)
  131. {
  132. char *region_start;
  133. int region_idx, length;
  134. region_idx = (start - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
  135. region_start = &region_age [region_idx];
  136. length = (end - start) >> SGEN_TO_SPACE_GRANULE_BITS;
  137. memset (region_start, age, length);
  138. }
  139. static inline void
  140. mark_bit (char *space_bitmap, char *pos)
  141. {
  142. int idx = (pos - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
  143. int byte = idx / 8;
  144. int bit = idx & 0x7;
  145. g_assert (byte < sgen_space_bitmap_size);
  146. space_bitmap [byte] |= 1 << bit;
  147. }
  148. static void
  149. mark_bits_in_range (char *space_bitmap, char *start, char *end)
  150. {
  151. start = align_down (start, SGEN_TO_SPACE_GRANULE_BITS);
  152. end = align_up (end, SGEN_TO_SPACE_GRANULE_BITS);
  153. for (;start < end; start += SGEN_TO_SPACE_GRANULE_IN_BYTES)
  154. mark_bit (space_bitmap, start);
  155. }
  156. /*
  157. * This splits the fragments at the point of the promotion barrier.
  158. * Two allocator are actually involved here: The mutator allocator and
  159. * the collector allocator. This function is called with the
  160. * collector, but it's a copy of the mutator allocator and contains
  161. * all the fragments in the nursery. The fragments below the
  162. * promotion barrier are left with the mutator allocator and the ones
  163. * above are put into the collector allocator.
  164. */
  165. static void
  166. fragment_list_split (SgenFragmentAllocator *allocator)
  167. {
  168. SgenFragment *prev = NULL, *list = allocator->region_head;
  169. while (list) {
  170. if (list->fragment_end > promotion_barrier) {
  171. if (list->fragment_start < promotion_barrier) {
  172. SgenFragment *res = sgen_fragment_allocator_alloc ();
  173. res->fragment_start = promotion_barrier;
  174. res->fragment_next = promotion_barrier;
  175. res->fragment_end = list->fragment_end;
  176. res->next = list->next;
  177. res->next_in_order = list->next_in_order;
  178. g_assert (res->fragment_end > res->fragment_start);
  179. list->fragment_end = promotion_barrier;
  180. list->next = list->next_in_order = NULL;
  181. set_age_in_range (list->fragment_start, list->fragment_end, 0);
  182. allocator->region_head = allocator->alloc_head = res;
  183. return;
  184. } else {
  185. if (prev)
  186. prev->next = prev->next_in_order = NULL;
  187. allocator->region_head = allocator->alloc_head = list;
  188. return;
  189. }
  190. }
  191. set_age_in_range (list->fragment_start, list->fragment_end, 0);
  192. prev = list;
  193. list = list->next;
  194. }
  195. allocator->region_head = allocator->alloc_head = NULL;
  196. }
  197. /******************************************Minor Collector API ************************************************/
  198. #define AGE_ALLOC_BUFFER_MIN_SIZE SGEN_TO_SPACE_GRANULE_IN_BYTES
  199. #define AGE_ALLOC_BUFFER_DESIRED_SIZE (SGEN_TO_SPACE_GRANULE_IN_BYTES * 8)
  200. static char*
  201. alloc_for_promotion_slow_path (int age, size_t objsize)
  202. {
  203. char *p;
  204. size_t allocated_size;
  205. size_t aligned_objsize = (size_t)align_up (objsize, SGEN_TO_SPACE_GRANULE_BITS);
  206. p = sgen_fragment_allocator_serial_range_alloc (
  207. &collector_allocator,
  208. MAX (aligned_objsize, AGE_ALLOC_BUFFER_DESIRED_SIZE),
  209. MAX (aligned_objsize, AGE_ALLOC_BUFFER_MIN_SIZE),
  210. &allocated_size);
  211. if (p) {
  212. set_age_in_range (p, p + allocated_size, age);
  213. sgen_clear_range (age_alloc_buffers [age].next, age_alloc_buffers [age].end);
  214. age_alloc_buffers [age].next = p + objsize;
  215. age_alloc_buffers [age].end = p + allocated_size;
  216. }
  217. return p;
  218. }
  219. static inline char*
  220. alloc_for_promotion (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references)
  221. {
  222. char *p = NULL;
  223. int age;
  224. age = get_object_age (obj);
  225. if (age >= promote_age)
  226. return major_collector.alloc_object (vtable, objsize, has_references);
  227. /* Promote! */
  228. ++age;
  229. p = age_alloc_buffers [age].next;
  230. if (G_LIKELY (p + objsize <= age_alloc_buffers [age].end)) {
  231. age_alloc_buffers [age].next += objsize;
  232. } else {
  233. p = alloc_for_promotion_slow_path (age, objsize);
  234. if (!p)
  235. return major_collector.alloc_object (vtable, objsize, has_references);
  236. }
  237. *(MonoVTable**)p = vtable;
  238. return p;
  239. }
  240. static char*
  241. par_alloc_for_promotion_slow_path (int age, size_t objsize)
  242. {
  243. char *p;
  244. size_t allocated_size;
  245. size_t aligned_objsize = (size_t)align_up (objsize, SGEN_TO_SPACE_GRANULE_BITS);
  246. mono_mutex_lock (&par_alloc_buffer_refill_mutex);
  247. restart:
  248. p = age_alloc_buffers [age].next;
  249. if (G_LIKELY (p + objsize <= age_alloc_buffers [age].end)) {
  250. if (SGEN_CAS_PTR ((void*)&age_alloc_buffers [age].next, p + objsize, p) != p)
  251. goto restart;
  252. } else {
  253. /* Reclaim remaining space - if we OOMd the nursery nothing to see here. */
  254. char *end = age_alloc_buffers [age].end;
  255. if (end) {
  256. do {
  257. p = age_alloc_buffers [age].next;
  258. } while (SGEN_CAS_PTR ((void*)&age_alloc_buffers [age].next, end, p) != p);
  259. sgen_clear_range (p, end);
  260. }
  261. /* By setting end to NULL we make sure no other thread can advance while we're updating.*/
  262. age_alloc_buffers [age].end = NULL;
  263. STORE_STORE_FENCE;
  264. p = sgen_fragment_allocator_par_range_alloc (
  265. &collector_allocator,
  266. MAX (aligned_objsize, AGE_ALLOC_BUFFER_DESIRED_SIZE),
  267. MAX (aligned_objsize, AGE_ALLOC_BUFFER_MIN_SIZE),
  268. &allocated_size);
  269. if (p) {
  270. set_age_in_range (p, p + allocated_size, age);
  271. age_alloc_buffers [age].next = p + objsize;
  272. STORE_STORE_FENCE; /* Next must arrive before the new value for next. */
  273. age_alloc_buffers [age].end = p + allocated_size;
  274. }
  275. }
  276. mono_mutex_unlock (&par_alloc_buffer_refill_mutex);
  277. return p;
  278. }
  279. static inline char*
  280. par_alloc_for_promotion (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references)
  281. {
  282. char *p;
  283. int age;
  284. age = get_object_age (obj);
  285. if (age >= promote_age)
  286. return major_collector.par_alloc_object (vtable, objsize, has_references);
  287. restart:
  288. p = age_alloc_buffers [age].next;
  289. LOAD_LOAD_FENCE; /* The read of ->next must happen before ->end */
  290. if (G_LIKELY (p + objsize <= age_alloc_buffers [age].end)) {
  291. if (SGEN_CAS_PTR ((void*)&age_alloc_buffers [age].next, p + objsize, p) != p)
  292. goto restart;
  293. } else {
  294. p = par_alloc_for_promotion_slow_path (age, objsize);
  295. /* Have we failed to promote to the nursery, lets just evacuate it to old gen. */
  296. if (!p)
  297. return major_collector.par_alloc_object (vtable, objsize, has_references);
  298. }
  299. *(MonoVTable**)p = vtable;
  300. return p;
  301. }
  302. static char*
  303. minor_alloc_for_promotion (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references)
  304. {
  305. /*
  306. We only need to check for a non-nursery object if we're doing a major collection.
  307. */
  308. if (!sgen_ptr_in_nursery (obj))
  309. return major_collector.alloc_object (vtable, objsize, has_references);
  310. return alloc_for_promotion (vtable, obj, objsize, has_references);
  311. }
  312. static char*
  313. minor_par_alloc_for_promotion (MonoVTable *vtable, char *obj, size_t objsize, gboolean has_references)
  314. {
  315. /*
  316. We only need to check for a non-nursery object if we're doing a major collection.
  317. */
  318. if (!sgen_ptr_in_nursery (obj))
  319. return major_collector.par_alloc_object (vtable, objsize, has_references);
  320. return par_alloc_for_promotion (vtable, obj, objsize, has_references);
  321. }
  322. static SgenFragment*
  323. build_fragments_get_exclude_head (void)
  324. {
  325. int i;
  326. for (i = 0; i < MAX_AGE; ++i) {
  327. /*If we OOM'd on the last collection ->end might be null while ->next not.*/
  328. if (age_alloc_buffers [i].end)
  329. sgen_clear_range (age_alloc_buffers [i].next, age_alloc_buffers [i].end);
  330. }
  331. return collector_allocator.region_head;
  332. }
  333. static void
  334. build_fragments_release_exclude_head (void)
  335. {
  336. sgen_fragment_allocator_release (&collector_allocator);
  337. }
  338. static void
  339. build_fragments_finish (SgenFragmentAllocator *allocator)
  340. {
  341. /* We split the fragment list based on the promotion barrier. */
  342. collector_allocator = *allocator;
  343. fragment_list_split (&collector_allocator);
  344. }
  345. static void
  346. prepare_to_space (char *to_space_bitmap, int space_bitmap_size)
  347. {
  348. SgenFragment **previous, *frag;
  349. memset (to_space_bitmap, 0, space_bitmap_size);
  350. memset (age_alloc_buffers, 0, sizeof (age_alloc_buffers));
  351. previous = &collector_allocator.alloc_head;
  352. for (frag = *previous; frag; frag = *previous) {
  353. char *start = align_up (frag->fragment_next, SGEN_TO_SPACE_GRANULE_BITS);
  354. char *end = align_down (frag->fragment_end, SGEN_TO_SPACE_GRANULE_BITS);
  355. /* Fragment is too small to be usable. */
  356. if ((end - start) < SGEN_MAX_NURSERY_WASTE) {
  357. sgen_clear_range (frag->fragment_next, frag->fragment_end);
  358. frag->fragment_next = frag->fragment_end = frag->fragment_start;
  359. *previous = frag->next;
  360. continue;
  361. }
  362. /*
  363. We need to insert 3 phony objects so the fragments build step can correctly
  364. walk the nursery.
  365. */
  366. /* Clean the fragment range. */
  367. sgen_clear_range (start, end);
  368. /* We need a phony object in between the original fragment start and the effective one. */
  369. if (start != frag->fragment_next)
  370. sgen_clear_range (frag->fragment_next, start);
  371. /* We need an phony object in between the new fragment end and the original fragment end. */
  372. if (end != frag->fragment_end)
  373. sgen_clear_range (end, frag->fragment_end);
  374. frag->fragment_start = frag->fragment_next = start;
  375. frag->fragment_end = end;
  376. mark_bits_in_range (to_space_bitmap, start, end);
  377. previous = &frag->next;
  378. }
  379. }
  380. static void
  381. clear_fragments (void)
  382. {
  383. sgen_clear_allocator_fragments (&collector_allocator);
  384. }
  385. static void
  386. init_nursery (SgenFragmentAllocator *allocator, char *start, char *end)
  387. {
  388. int alloc_quote = (int)((end - start) * alloc_ratio);
  389. promotion_barrier = align_down (start + alloc_quote, 3);
  390. sgen_fragment_allocator_add (allocator, start, promotion_barrier);
  391. sgen_fragment_allocator_add (&collector_allocator, promotion_barrier, end);
  392. region_age_size = (end - start) >> SGEN_TO_SPACE_GRANULE_BITS;
  393. region_age = g_malloc0 (region_age_size);
  394. }
  395. static gboolean
  396. handle_gc_param (const char *opt)
  397. {
  398. if (g_str_has_prefix (opt, "alloc-ratio=")) {
  399. const char *arg = strchr (opt, '=') + 1;
  400. int percentage = atoi (arg);
  401. if (percentage < 1 || percentage > 100) {
  402. fprintf (stderr, "alloc-ratio must be an integer in the range 1-100.\n");
  403. exit (1);
  404. }
  405. alloc_ratio = (float)percentage / 100.0f;
  406. return TRUE;
  407. }
  408. if (g_str_has_prefix (opt, "promotion-age=")) {
  409. const char *arg = strchr (opt, '=') + 1;
  410. promote_age = atoi (arg);
  411. if (promote_age < 1 || promote_age >= MAX_AGE) {
  412. fprintf (stderr, "promotion-age must be an integer in the range 1-%d.\n", MAX_AGE - 1);
  413. exit (1);
  414. }
  415. return TRUE;
  416. }
  417. return FALSE;
  418. }
  419. static void
  420. print_gc_param_usage (void)
  421. {
  422. fprintf (stderr,
  423. ""
  424. " alloc-ratio=P (where P is a percentage, an integer in 1-100)\n"
  425. " promotion-age=P (where P is a number, an integer in 1-%d)\n",
  426. MAX_AGE - 1
  427. );
  428. }
  429. /******************************************Copy/Scan functins ************************************************/
  430. #define SGEN_SPLIT_NURSERY
  431. #define SERIAL_COPY_OBJECT split_nursery_serial_copy_object
  432. #define PARALLEL_COPY_OBJECT split_nursery_parallel_copy_object
  433. #define SERIAL_COPY_OBJECT_FROM_OBJ split_nursery_serial_copy_object_from_obj
  434. #include "sgen-minor-copy-object.h"
  435. #include "sgen-minor-scan-object.h"
  436. void
  437. sgen_split_nursery_init (SgenMinorCollector *collector)
  438. {
  439. collector->is_split = TRUE;
  440. collector->alloc_for_promotion = minor_alloc_for_promotion;
  441. collector->par_alloc_for_promotion = minor_par_alloc_for_promotion;
  442. collector->prepare_to_space = prepare_to_space;
  443. collector->clear_fragments = clear_fragments;
  444. collector->build_fragments_get_exclude_head = build_fragments_get_exclude_head;
  445. collector->build_fragments_release_exclude_head = build_fragments_release_exclude_head;
  446. collector->build_fragments_finish = build_fragments_finish;
  447. collector->init_nursery = init_nursery;
  448. collector->handle_gc_param = handle_gc_param;
  449. collector->print_gc_param_usage = print_gc_param_usage;
  450. FILL_MINOR_COLLECTOR_COPY_OBJECT (collector);
  451. FILL_MINOR_COLLECTOR_SCAN_OBJECT (collector);
  452. LOCK_INIT (par_alloc_buffer_refill_mutex);
  453. }
  454. #endif