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.
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
46 #define SGEN_GRAY_QUEUE_HEADER_SIZE 3
49 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - SGEN_GRAY_QUEUE_HEADER_SIZE)
51 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
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;
60 typedef struct _GrayQueueEntry GrayQueueEntry;
61 struct _GrayQueueEntry {
66 #define SGEN_GRAY_QUEUE_ENTRY(obj,desc) { (obj), (desc) }
69 * This is a stack now instead of a queue, so the most recently added items are removed
70 * first, improving cache locality, and keeping the stack size manageable.
72 typedef struct _GrayQueueSection GrayQueueSection;
73 struct _GrayQueueSection {
74 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
76 * The dummy is here so that the state doesn't get overwritten
77 * by the internal allocator once the section is freed.
80 GrayQueueSectionState state;
83 GrayQueueSection *next, *prev;
84 GrayQueueEntry entries [SGEN_GRAY_QUEUE_SECTION_SIZE];
87 typedef struct _SgenGrayQueue SgenGrayQueue;
89 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
90 typedef void (*GrayQueueEnqueueCheckFunc) (GCObject*);
92 struct _SgenGrayQueue {
93 GrayQueueEntry *cursor;
94 GrayQueueSection *first, *last;
95 GrayQueueSection *free_list;
96 mono_mutex_t steal_mutex;
98 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
99 GrayQueueEnqueueCheckFunc enqueue_check_func;
103 typedef struct _SgenSectionGrayQueue SgenSectionGrayQueue;
105 struct _SgenSectionGrayQueue {
106 GrayQueueSection *first;
109 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
110 GrayQueueEnqueueCheckFunc enqueue_check_func;
114 #define GRAY_LAST_CURSOR_POSITION(s) ((s)->entries + SGEN_GRAY_QUEUE_SECTION_SIZE - 1)
115 #define GRAY_FIRST_CURSOR_POSITION(s) ((s)->entries)
117 #ifdef HEAVY_STATISTICS
118 extern guint64 stat_gray_queue_section_alloc;
119 extern guint64 stat_gray_queue_section_free;
120 extern guint64 stat_gray_queue_enqueue_fast_path;
121 extern guint64 stat_gray_queue_dequeue_fast_path;
122 extern guint64 stat_gray_queue_enqueue_slow_path;
123 extern guint64 stat_gray_queue_dequeue_slow_path;
126 void sgen_init_gray_queues (void);
128 void sgen_gray_object_enqueue (SgenGrayQueue *queue, GCObject *obj, SgenDescriptor desc);
129 GrayQueueEntry sgen_gray_object_dequeue (SgenGrayQueue *queue);
130 GrayQueueSection* sgen_gray_object_dequeue_section (SgenGrayQueue *queue);
131 GrayQueueSection* sgen_gray_object_steal_section (SgenGrayQueue *queue);
132 void sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section);
133 void sgen_gray_object_queue_trim_free_list (SgenGrayQueue *queue);
134 void sgen_gray_object_queue_init (SgenGrayQueue *queue, GrayQueueEnqueueCheckFunc enqueue_check_func, gboolean reuse_free_list);
135 void sgen_gray_object_queue_dispose (SgenGrayQueue *queue);
136 void sgen_gray_object_queue_deinit (SgenGrayQueue *queue);
137 void sgen_gray_object_alloc_queue_section (SgenGrayQueue *queue);
138 void sgen_gray_object_free_queue_section (GrayQueueSection *section);
140 void sgen_section_gray_queue_init (SgenSectionGrayQueue *queue, gboolean locked,
141 GrayQueueEnqueueCheckFunc enqueue_check_func);
142 gboolean sgen_section_gray_queue_is_empty (SgenSectionGrayQueue *queue);
143 GrayQueueSection* sgen_section_gray_queue_dequeue (SgenSectionGrayQueue *queue);
144 void sgen_section_gray_queue_enqueue (SgenSectionGrayQueue *queue, GrayQueueSection *section);
146 gboolean sgen_gray_object_fill_prefetch (SgenGrayQueue *queue);
148 static inline gboolean
149 sgen_gray_object_queue_is_empty (SgenGrayQueue *queue)
151 return queue->first == NULL;
154 static inline MONO_ALWAYS_INLINE void
155 GRAY_OBJECT_ENQUEUE (SgenGrayQueue *queue, GCObject *obj, SgenDescriptor desc)
157 #if SGEN_MAX_DEBUG_LEVEL >= 9
158 sgen_gray_object_enqueue (queue, obj, desc);
160 if (G_UNLIKELY (!queue->first || queue->cursor == GRAY_LAST_CURSOR_POSITION (queue->first))) {
161 sgen_gray_object_enqueue (queue, obj, desc);
163 GrayQueueEntry entry = SGEN_GRAY_QUEUE_ENTRY (obj, desc);
165 HEAVY_STAT (stat_gray_queue_enqueue_fast_path ++);
167 *++queue->cursor = entry;
168 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
169 binary_protocol_gray_enqueue (queue, queue->cursor, obj);
175 static inline MONO_ALWAYS_INLINE void
176 GRAY_OBJECT_DEQUEUE (SgenGrayQueue *queue, GCObject** obj, SgenDescriptor *desc)
178 GrayQueueEntry entry;
179 #if SGEN_MAX_DEBUG_LEVEL >= 9
180 entry = sgen_gray_object_dequeue (queue);
185 HEAVY_STAT (stat_gray_queue_dequeue_fast_path ++);
188 } else if (G_UNLIKELY (queue->cursor == GRAY_FIRST_CURSOR_POSITION (queue->first))) {
189 entry = sgen_gray_object_dequeue (queue);
193 HEAVY_STAT (stat_gray_queue_dequeue_fast_path ++);
195 entry = *queue->cursor--;
198 #ifdef SGEN_HEAVY_BINARY_PROTOCOL
199 binary_protocol_gray_dequeue (queue, queue->cursor + 1, *obj);