Bump corefx
[mono-project.git] / mono / sgen / sgen-gray.h
blobb2f69d0586d2dd296a42c0d573fbc4c78a2d677e
1 /*
2 * sgen-gray.h: Gray queue management.
4 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
5 * Copyright (C) 2012 Xamarin Inc
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8 */
9 #ifndef __MONO_SGEN_GRAY_H__
10 #define __MONO_SGEN_GRAY_H__
12 #include "mono/sgen/sgen-protocol.h"
15 * This gray queue has to be as optimized as possible, because it is in the core of
16 * the mark/copy phase of the garbage collector. The memory access has then to be as
17 * cache friendly as possible. That's why we use a cursor based implementation.
19 * This simply consist in maintaining a pointer to the current element in the
20 * queue. In addition to using this cursor, we use a simple linked list of arrays,
21 * called sections, so that we have the cache friendliness of arrays without having
22 * the cost of memory reallocation of a dynaic array, not the cost of memory
23 * indirection of a linked list.
25 * This implementation also allows the dequeuing of a whole section at a time. This is
26 * for example used in the parallel GC because it would be too costly to take one element
27 * at a time. This imply the main constraint that, because we don't carry the cursor
28 * with the section, we still have to store the index of the last element. This is done
29 * through the 'size' field on the section, which default value is it's maximum value
30 * SGEN_GRAY_QUEUE_SECTION_SIZE. This field is updated in multiple cases :
31 * - section allocation : default value
32 * - object push : default value if we fill the current queue first
33 * - section dequeue : position of the cursor in the dequeued section
34 * - section enqueue : position of the cursor in the previously first section in the queue
36 * The previous implementation was an index based access where we would store the index
37 * of the last element in the section. This was less efficient because we would have
38 * to make 1 memory access for the index value, 1 for the base address of the objects
39 * array and another 1 for the actual value in the array.
42 /* SGEN_GRAY_QUEUE_HEADER_SIZE is number of machine words */
43 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
44 #define SGEN_GRAY_QUEUE_HEADER_SIZE 5
45 #else
46 #define SGEN_GRAY_QUEUE_HEADER_SIZE 3
47 #endif
49 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - SGEN_GRAY_QUEUE_HEADER_SIZE)
51 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
52 typedef enum {
53 GRAY_QUEUE_SECTION_STATE_FLOATING,
54 GRAY_QUEUE_SECTION_STATE_ENQUEUED,
55 GRAY_QUEUE_SECTION_STATE_FREE_LIST,
56 GRAY_QUEUE_SECTION_STATE_FREED
57 } GrayQueueSectionState;
58 #endif
60 typedef struct _GrayQueueEntry GrayQueueEntry;
61 struct _GrayQueueEntry {
62 GCObject *obj;
63 SgenDescriptor desc;
66 #define SGEN_GRAY_QUEUE_ENTRY(obj,desc) { (obj), (desc) }
68 #define GRAY_OBJECT_ENQUEUE_SERIAL(queue, obj, desc) (GRAY_OBJECT_ENQUEUE (queue, obj, desc, FALSE))
69 #define GRAY_OBJECT_ENQUEUE_PARALLEL(queue, obj, desc) (GRAY_OBJECT_ENQUEUE (queue, obj, desc, TRUE))
70 #define GRAY_OBJECT_DEQUEUE_SERIAL(queue, obj, desc) (GRAY_OBJECT_DEQUEUE (queue, obj, desc, FALSE))
71 #define GRAY_OBJECT_DEQUEUE_PARALLEL(queue, obj, desc) (GRAY_OBJECT_DEQUEUE (queue, obj, desc, TRUE))
74 * This is a stack now instead of a queue, so the most recently added items are removed
75 * first, improving cache locality, and keeping the stack size manageable.
77 typedef struct _GrayQueueSection GrayQueueSection;
78 struct _GrayQueueSection {
79 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
81 * The dummy is here so that the state doesn't get overwritten
82 * by the internal allocator once the section is freed.
84 int dummy;
85 GrayQueueSectionState state;
86 #endif
87 int size;
88 GrayQueueSection *next, *prev;
89 GrayQueueEntry entries [SGEN_GRAY_QUEUE_SECTION_SIZE];
92 typedef struct _SgenGrayQueue SgenGrayQueue;
94 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
95 typedef void (*GrayQueueEnqueueCheckFunc) (GCObject*);
97 struct _SgenGrayQueue {
98 GrayQueueEntry *cursor;
99 GrayQueueSection *first, *last;
100 GrayQueueSection *free_list;
101 mono_mutex_t steal_mutex;
102 gint32 num_sections;
103 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
104 GrayQueueEnqueueCheckFunc enqueue_check_func;
105 #endif
108 typedef struct _SgenSectionGrayQueue SgenSectionGrayQueue;
110 struct _SgenSectionGrayQueue {
111 GrayQueueSection *first;
112 gboolean locked;
113 mono_mutex_t lock;
114 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
115 GrayQueueEnqueueCheckFunc enqueue_check_func;
116 #endif
119 #define GRAY_LAST_CURSOR_POSITION(s) ((s)->entries + SGEN_GRAY_QUEUE_SECTION_SIZE - 1)
120 #define GRAY_FIRST_CURSOR_POSITION(s) ((s)->entries)
122 #ifdef HEAVY_STATISTICS
123 extern guint64 stat_gray_queue_section_alloc;
124 extern guint64 stat_gray_queue_section_free;
125 extern guint64 stat_gray_queue_enqueue_fast_path;
126 extern guint64 stat_gray_queue_dequeue_fast_path;
127 extern guint64 stat_gray_queue_enqueue_slow_path;
128 extern guint64 stat_gray_queue_dequeue_slow_path;
129 #endif
131 void sgen_init_gray_queues (void);
133 void sgen_gray_object_enqueue (SgenGrayQueue *queue, GCObject *obj, SgenDescriptor desc, gboolean is_parallel);
134 GrayQueueEntry sgen_gray_object_dequeue (SgenGrayQueue *queue, gboolean is_parallel);
135 GrayQueueSection* sgen_gray_object_dequeue_section (SgenGrayQueue *queue);
136 GrayQueueSection* sgen_gray_object_steal_section (SgenGrayQueue *queue);
137 void sgen_gray_object_spread (SgenGrayQueue *queue, int num_sections);
138 void sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section, gboolean is_parallel);
139 void sgen_gray_object_queue_trim_free_list (SgenGrayQueue *queue);
140 void sgen_gray_object_queue_init (SgenGrayQueue *queue, GrayQueueEnqueueCheckFunc enqueue_check_func, gboolean reuse_free_list);
141 void sgen_gray_object_queue_dispose (SgenGrayQueue *queue);
142 void sgen_gray_object_queue_deinit (SgenGrayQueue *queue);
143 void sgen_gray_object_alloc_queue_section (SgenGrayQueue *queue, gboolean is_parallel);
144 void sgen_gray_object_free_queue_section (GrayQueueSection *section);
146 void sgen_section_gray_queue_init (SgenSectionGrayQueue *queue, gboolean locked,
147 GrayQueueEnqueueCheckFunc enqueue_check_func);
148 gboolean sgen_section_gray_queue_is_empty (SgenSectionGrayQueue *queue);
149 GrayQueueSection* sgen_section_gray_queue_dequeue (SgenSectionGrayQueue *queue);
150 void sgen_section_gray_queue_enqueue (SgenSectionGrayQueue *queue, GrayQueueSection *section);
152 gboolean sgen_gray_object_fill_prefetch (SgenGrayQueue *queue);
154 static inline gboolean
155 sgen_gray_object_queue_is_empty (SgenGrayQueue *queue)
157 return queue->first == NULL;
160 static inline MONO_ALWAYS_INLINE void
161 GRAY_OBJECT_ENQUEUE (SgenGrayQueue *queue, GCObject *obj, SgenDescriptor desc, gboolean is_parallel)
163 #if SGEN_MAX_DEBUG_LEVEL >= 9
164 sgen_gray_object_enqueue (queue, obj, desc, is_parallel);
165 #else
166 if (G_UNLIKELY (!queue->first || queue->cursor == GRAY_LAST_CURSOR_POSITION (queue->first))) {
167 sgen_gray_object_enqueue (queue, obj, desc, is_parallel);
168 } else {
169 GrayQueueEntry entry = SGEN_GRAY_QUEUE_ENTRY (obj, desc);
171 HEAVY_STAT (stat_gray_queue_enqueue_fast_path ++);
173 *++queue->cursor = entry;
174 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
175 binary_protocol_gray_enqueue (queue, queue->cursor, obj);
176 #endif
178 #endif
181 static inline MONO_ALWAYS_INLINE void
182 GRAY_OBJECT_DEQUEUE (SgenGrayQueue *queue, GCObject** obj, SgenDescriptor *desc, gboolean is_parallel)
184 GrayQueueEntry entry;
185 #if SGEN_MAX_DEBUG_LEVEL >= 9
186 entry = sgen_gray_object_dequeue (queue, is_parallel);
187 *obj = entry.obj;
188 *desc = entry.desc;
189 #else
190 if (!queue->first) {
191 HEAVY_STAT (stat_gray_queue_dequeue_fast_path ++);
193 *obj = NULL;
194 } else if (G_UNLIKELY (queue->cursor == GRAY_FIRST_CURSOR_POSITION (queue->first))) {
195 entry = sgen_gray_object_dequeue (queue, is_parallel);
196 *obj = entry.obj;
197 *desc = entry.desc;
198 } else {
199 HEAVY_STAT (stat_gray_queue_dequeue_fast_path ++);
201 entry = *queue->cursor--;
202 *obj = entry.obj;
203 *desc = entry.desc;
204 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
205 binary_protocol_gray_dequeue (queue, queue->cursor + 1, *obj);
206 #endif
208 #endif
211 #endif