少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
 * \file
 * Object allocation routines + managed allocators
 *
 * Author:
 *     Paolo Molaro (lupus@ximian.com)
 *  Rodrigo Kumpera (kumpera@gmail.com)
 *
 * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
 * Copyright 2011 Xamarin, Inc.
 * Copyright (C) 2012 Xamarin Inc
 *
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
 
/*
 * ######################################################################
 * ########  Object allocation
 * ######################################################################
 * This section of code deals with allocating memory for objects.
 * There are several ways:
 * *) allocate large objects
 * *) allocate normal objects
 * *) fast lock-free allocation
 * *) allocation of pinned objects
 */
 
#include "config.h"
#ifdef HAVE_SGEN_GC
 
#include <string.h>
 
#include "mono/sgen/sgen-gc.h"
#include "mono/sgen/sgen-protocol.h"
#include "mono/sgen/sgen-memory-governor.h"
#include "mono/sgen/sgen-client.h"
#include "mono/utils/mono-memory-model.h"
 
#define ALIGN_UP        SGEN_ALIGN_UP
#define ALLOC_ALIGN        SGEN_ALLOC_ALIGN
#define MAX_SMALL_OBJ_SIZE    SGEN_MAX_SMALL_OBJ_SIZE
 
#ifdef HEAVY_STATISTICS
static guint64 stat_objects_alloced = 0;
static guint64 stat_bytes_alloced = 0;
static guint64 stat_bytes_alloced_los = 0;
 
#endif
 
/*
 * Allocation is done from a Thread Local Allocation Buffer (TLAB). TLABs are allocated
 * from nursery fragments.
 * tlab_next is the pointer to the space inside the TLAB where the next object will 
 * be allocated.
 * tlab_temp_end is the pointer to the end of the temporary space reserved for
 * the allocation: it allows us to set the scan starts at reasonable intervals.
 * tlab_real_end points to the end of the TLAB.
 */
 
#define TLAB_START    (__thread_info__->tlab_start)
#define TLAB_NEXT    (__thread_info__->tlab_next)
#define TLAB_TEMP_END    (__thread_info__->tlab_temp_end)
#define TLAB_REAL_END    (__thread_info__->tlab_real_end)
 
static GCObject*
alloc_degraded (GCVTable vtable, size_t size, gboolean for_mature)
{
    GCObject *p;
 
    if (!for_mature) {
        sgen_client_degraded_allocation ();
        SGEN_ATOMIC_ADD_P (degraded_mode, size);
        sgen_ensure_free_space (size, GENERATION_OLD);
    } else {
        if (sgen_need_major_collection (size))
            sgen_perform_collection (size, GENERATION_OLD, "mature allocation failure", !for_mature, TRUE);
    }
 
 
    p = major_collector.alloc_degraded (vtable, size);
 
    if (!for_mature)
        binary_protocol_alloc_degraded (p, vtable, size, sgen_client_get_provenance ());
 
    return p;
}
 
static void
zero_tlab_if_necessary (void *p, size_t size)
{
    if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION || nursery_clear_policy == CLEAR_AT_TLAB_CREATION_DEBUG) {
        memset (p, 0, size);
    } else {
        /*
         * This function is called for all allocations in
         * TLABs.  TLABs originate from fragments, which are
         * initialized to be faux arrays.  The remainder of
         * the fragments are zeroed out at initialization for
         * CLEAR_AT_GC, so here we just need to make sure that
         * the array header is zeroed.  Since we don't know
         * whether we're called for the start of a fragment or
         * for somewhere in between, we zero in any case, just
         * to make sure.
         */
        sgen_client_zero_array_fill_header (p, size);
    }
}
 
/*
 * Provide a variant that takes just the vtable for small fixed-size objects.
 * The aligned size is already computed and stored in vt->gc_descr.
 * Note: every SGEN_SCAN_START_SIZE or so we are given the chance to do some special
 * processing. We can keep track of where objects start, for example,
 * so when we scan the thread stacks for pinned objects, we can start
 * a search for the pinned object in SGEN_SCAN_START_SIZE chunks.
 */
GCObject*
sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
{
    /* FIXME: handle OOM */
    void **p;
    char *new_next;
    size_t real_size = size;
    TLAB_ACCESS_INIT;
    
    CANARIFY_SIZE(size);
 
    HEAVY_STAT (++stat_objects_alloced);
    if (real_size <= SGEN_MAX_SMALL_OBJ_SIZE)
        HEAVY_STAT (stat_bytes_alloced += size);
    else
        HEAVY_STAT (stat_bytes_alloced_los += size);
 
    size = ALIGN_UP (size);
 
    SGEN_ASSERT (6, sgen_vtable_get_descriptor (vtable), "VTable without descriptor");
 
    if (G_UNLIKELY (has_per_allocation_action)) {
        static int alloc_count;
        int current_alloc = mono_atomic_inc_i32 (&alloc_count);
 
        if (collect_before_allocs) {
            if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
                sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered", TRUE, TRUE);
                if (!degraded_mode && sgen_can_alloc_size (size) && real_size <= SGEN_MAX_SMALL_OBJ_SIZE) {
                    // FIXME:
                    g_assert_not_reached ();
                }
            }
        } else if (verify_before_allocs) {
            if ((current_alloc % verify_before_allocs) == 0)
                sgen_check_whole_heap_stw ();
        }
    }
 
    /*
     * We must already have the lock here instead of after the
     * fast path because we might be interrupted in the fast path
     * (after confirming that new_next < TLAB_TEMP_END) by the GC,
     * and we'll end up allocating an object in a fragment which
     * no longer belongs to us.
     *
     * The managed allocator does not do this, but it's treated
     * specially by the world-stopping code.
     */
 
    if (real_size > SGEN_MAX_SMALL_OBJ_SIZE) {
        p = (void **)sgen_los_alloc_large_inner (vtable, ALIGN_UP (real_size));
    } else {
        /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
 
        p = (void**)TLAB_NEXT;
        /* FIXME: handle overflow */
        new_next = (char*)p + size;
        TLAB_NEXT = new_next;
 
        if (G_LIKELY (new_next < TLAB_TEMP_END)) {
            /* Fast path */
 
            CANARIFY_ALLOC(p,real_size);
            SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, sgen_client_vtable_get_name (vtable), size);
            binary_protocol_alloc (p , vtable, size, sgen_client_get_provenance ());
            g_assert (*p == NULL);
            mono_atomic_store_seq (p, vtable);
 
            return (GCObject*)p;
        }
 
        /* Slow path */
 
        /* there are two cases: the object is too big or we run out of space in the TLAB */
        /* we also reach here when the thread does its first allocation after a minor 
         * collection, since the tlab_ variables are initialized to NULL.
         * there can be another case (from ORP), if we cooperate with the runtime a bit:
         * objects that need finalizers can have the high bit set in their size
         * so the above check fails and we can readily add the object to the queue.
         * This avoids taking again the GC lock when registering, but this is moot when
         * doing thread-local allocation, so it may not be a good idea.
         */
        if (TLAB_NEXT >= TLAB_REAL_END) {
            int available_in_tlab;
            /* 
             * Run out of space in the TLAB. When this happens, some amount of space
             * remains in the TLAB, but not enough to satisfy the current allocation
             * request. Currently, we retire the TLAB in all cases, later we could
             * keep it if the remaining space is above a treshold, and satisfy the
             * allocation directly from the nursery.
             */
            TLAB_NEXT -= size;
            /* when running in degraded mode, we continue allocing that way
             * for a while, to decrease the number of useless nursery collections.
             */
            if (degraded_mode && degraded_mode < sgen_nursery_size)
                return alloc_degraded (vtable, size, FALSE);
 
            available_in_tlab = (int)(TLAB_REAL_END - TLAB_NEXT);//We'll never have tlabs > 2Gb
            if (size > tlab_size || available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
                /* Allocate directly from the nursery */
                p = (void **)sgen_nursery_alloc (size);
                if (!p) {
                    /*
                     * We couldn't allocate from the nursery, so we try
                     * collecting.  Even after the collection, we might
                     * still not have enough memory to allocate the
                     * object.  The reason will most likely be that we've
                     * run out of memory, but there is the theoretical
                     * possibility that other threads might have consumed
                     * the freed up memory ahead of us.
                     *
                     * What we do in this case is allocate degraded, i.e.,
                     * from the major heap.
                     *
                     * Ideally we'd like to detect the case of other
                     * threads allocating ahead of us and loop (if we
                     * always loop we will loop endlessly in the case of
                     * OOM).
                     */
                    sgen_ensure_free_space (real_size, GENERATION_NURSERY);
                    if (!degraded_mode)
                        p = (void **)sgen_nursery_alloc (size);
                }
                if (!p)
                    return alloc_degraded (vtable, size, TRUE);
 
                zero_tlab_if_necessary (p, size);
            } else {
                size_t alloc_size = 0;
                if (TLAB_START)
                    SGEN_LOG (3, "Retire TLAB: %p-%p [%ld]", TLAB_START, TLAB_REAL_END, (long)(TLAB_REAL_END - TLAB_NEXT - size));
                sgen_nursery_retire_region (p, available_in_tlab);
 
                p = (void **)sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
                if (!p) {
                    /* See comment above in similar case. */
                    sgen_ensure_free_space (tlab_size, GENERATION_NURSERY);
                    if (!degraded_mode)
                        p = (void **)sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
                }
                if (!p)
                    return alloc_degraded (vtable, size, TRUE);
 
                /* Allocate a new TLAB from the current nursery fragment */
                TLAB_START = (char*)p;
                TLAB_NEXT = TLAB_START;
                TLAB_REAL_END = TLAB_START + alloc_size;
                TLAB_TEMP_END = TLAB_START + MIN (SGEN_SCAN_START_SIZE, alloc_size);
 
                zero_tlab_if_necessary (TLAB_START, alloc_size);
 
                /* Allocate from the TLAB */
                p = (void **)TLAB_NEXT;
                TLAB_NEXT += size;
                sgen_set_nursery_scan_start ((char*)p);
            }
        } else {
            /* Reached tlab_temp_end */
 
            /* record the scan start so we can find pinned objects more easily */
            sgen_set_nursery_scan_start ((char*)p);
            /* we just bump tlab_temp_end as well */
            TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
            SGEN_LOG (5, "Expanding local alloc: %p-%p", TLAB_NEXT, TLAB_TEMP_END);
        }
        CANARIFY_ALLOC(p,real_size);
    }
 
    if (G_LIKELY (p)) {
        SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, sgen_client_vtable_get_name (vtable), size);
        binary_protocol_alloc (p, vtable, size, sgen_client_get_provenance ());
        mono_atomic_store_seq (p, vtable);
    }
 
    return (GCObject*)p;
}
 
GCObject*
sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size)
{
    void **p;
    char *new_next;
    size_t real_size = size;
    TLAB_ACCESS_INIT;
 
    CANARIFY_SIZE(size);
 
    size = ALIGN_UP (size);
    SGEN_ASSERT (9, real_size >= SGEN_CLIENT_MINIMUM_OBJECT_SIZE, "Object too small");
 
    SGEN_ASSERT (6, sgen_vtable_get_descriptor (vtable), "VTable without descriptor");
 
    if (real_size > SGEN_MAX_SMALL_OBJ_SIZE)
        return NULL;
 
    if (G_UNLIKELY (size > tlab_size)) {
        /* Allocate directly from the nursery */
        p = (void **)sgen_nursery_alloc (size);
        if (!p)
            return NULL;
        sgen_set_nursery_scan_start ((char*)p);
 
        /*FIXME we should use weak memory ops here. Should help specially on x86. */
        zero_tlab_if_necessary (p, size);
    } else {
        int available_in_tlab;
        char *real_end;
        /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
 
        p = (void**)TLAB_NEXT;
        /* FIXME: handle overflow */
        new_next = (char*)p + size;
 
        real_end = TLAB_REAL_END;
        available_in_tlab = (int)(real_end - (char*)p);//We'll never have tlabs > 2Gb
 
        if (G_LIKELY (new_next < real_end)) {
            TLAB_NEXT = new_next;
 
            /* Second case, we overflowed temp end */
            if (G_UNLIKELY (new_next >= TLAB_TEMP_END)) {
                sgen_set_nursery_scan_start (new_next);
                /* we just bump tlab_temp_end as well */
                TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
                SGEN_LOG (5, "Expanding local alloc: %p-%p", TLAB_NEXT, TLAB_TEMP_END);
            }
        } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
            /* Allocate directly from the nursery */
            p = (void **)sgen_nursery_alloc (size);
            if (!p)
                return NULL;
 
            zero_tlab_if_necessary (p, size);
        } else {
            size_t alloc_size = 0;
 
            sgen_nursery_retire_region (p, available_in_tlab);
            new_next = (char *)sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
            p = (void**)new_next;
            if (!p)
                return NULL;
 
            TLAB_START = (char*)new_next;
            TLAB_NEXT = new_next + size;
            TLAB_REAL_END = new_next + alloc_size;
            TLAB_TEMP_END = new_next + MIN (SGEN_SCAN_START_SIZE, alloc_size);
            sgen_set_nursery_scan_start ((char*)p);
 
            zero_tlab_if_necessary (new_next, alloc_size);
        }
    }
 
    HEAVY_STAT (++stat_objects_alloced);
    HEAVY_STAT (stat_bytes_alloced += size);
 
    CANARIFY_ALLOC(p,real_size);
    SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, sgen_client_vtable_get_name (vtable), size);
    binary_protocol_alloc (p, vtable, size, sgen_client_get_provenance ());
    g_assert (*p == NULL); /* FIXME disable this in non debug builds */
 
    mono_atomic_store_seq (p, vtable);
 
    return (GCObject*)p;
}
 
GCObject*
sgen_alloc_obj (GCVTable vtable, size_t size)
{
    GCObject *res;
    TLAB_ACCESS_INIT;
 
    if (!SGEN_CAN_ALIGN_UP (size))
        return NULL;
 
    if (G_UNLIKELY (has_per_allocation_action)) {
        static int alloc_count;
        int current_alloc = mono_atomic_inc_i32 (&alloc_count);
 
        if (verify_before_allocs) {
            if ((current_alloc % verify_before_allocs) == 0) {
                LOCK_GC;
                sgen_check_whole_heap_stw ();
                UNLOCK_GC;
            }
        }
        if (collect_before_allocs) {
            if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
                LOCK_GC;
                sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered", TRUE, TRUE);
                UNLOCK_GC;
            }
        }
    }
 
    ENTER_CRITICAL_REGION;
    res = sgen_try_alloc_obj_nolock (vtable, size);
    if (res) {
        EXIT_CRITICAL_REGION;
        return res;
    }
    EXIT_CRITICAL_REGION;
 
    LOCK_GC;
    res = sgen_alloc_obj_nolock (vtable, size);
    UNLOCK_GC;
    return res;
}
 
/*
 * To be used for interned strings and possibly MonoThread, reflection handles.
 * We may want to explicitly free these objects.
 */
GCObject*
sgen_alloc_obj_pinned (GCVTable vtable, size_t size)
{
    GCObject *p;
 
    if (!SGEN_CAN_ALIGN_UP (size))
        return NULL;
    size = ALIGN_UP (size);
 
    LOCK_GC;
 
    if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
        /* large objects are always pinned anyway */
        p = (GCObject *)sgen_los_alloc_large_inner (vtable, size);
    } else {
        SGEN_ASSERT (9, sgen_client_vtable_is_inited (vtable), "class %s:%s is not initialized", sgen_client_vtable_get_namespace (vtable), sgen_client_vtable_get_name (vtable));
        p = major_collector.alloc_small_pinned_obj (vtable, size, SGEN_VTABLE_HAS_REFERENCES (vtable));
    }
    if (G_LIKELY (p)) {
        SGEN_LOG (6, "Allocated pinned object %p, vtable: %p (%s), size: %zd", p, vtable, sgen_client_vtable_get_name (vtable), size);
        binary_protocol_alloc_pinned (p, vtable, size, sgen_client_get_provenance ());
    }
    UNLOCK_GC;
    return p;
}
 
GCObject*
sgen_alloc_obj_mature (GCVTable vtable, size_t size)
{
    GCObject *res;
 
    if (!SGEN_CAN_ALIGN_UP (size))
        return NULL;
    size = ALIGN_UP (size);
 
    LOCK_GC;
    res = alloc_degraded (vtable, size, TRUE);
    UNLOCK_GC;
 
    return res;
}
 
/*
 * Clear the thread local TLAB variables for all threads.
 */
void
sgen_clear_tlabs (void)
{
    FOREACH_THREAD (info) {
        /* A new TLAB will be allocated when the thread does its first allocation */
        info->tlab_start = NULL;
        info->tlab_next = NULL;
        info->tlab_temp_end = NULL;
        info->tlab_real_end = NULL;
    } FOREACH_THREAD_END
}
 
void
sgen_init_allocator (void)
{
#ifdef HEAVY_STATISTICS
    mono_counters_register ("# objects allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_objects_alloced);
    mono_counters_register ("bytes allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_bytes_alloced);
    mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_bytes_alloced_los);
#endif
}
 
#endif /*HAVE_SGEN_GC*/