少年修仙传客户端基础资源
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
500
501
502
503
504
505
506
507
508
/**
 * \file
 * When to schedule collections based on memory usage.
 *
 * Author:
 *     Rodrigo Kumpera (rkumpera@novell.com)
 *
 * Copyright 2001-2003 Ximian, Inc
 * Copyright 2003-2010 Novell, Inc.
 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
 * Copyright (C) 2012 Xamarin Inc
 *
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
 
#include "config.h"
#ifdef HAVE_SGEN_GC
 
#include <stdlib.h>
 
#include "mono/sgen/sgen-gc.h"
#include "mono/sgen/sgen-memory-governor.h"
#include "mono/sgen/sgen-workers.h"
#include "mono/sgen/sgen-client.h"
 
#define MIN_MINOR_COLLECTION_ALLOWANCE    ((mword)(SGEN_DEFAULT_NURSERY_SIZE * default_allowance_nursery_size_ratio))
 
static SgenPointerQueue log_entries = SGEN_POINTER_QUEUE_INIT (INTERNAL_MEM_TEMPORARY);
static mono_mutex_t log_entries_mutex;
 
mword total_promoted_size = 0;
mword total_allocated_major = 0;
static mword total_promoted_size_start;
static mword total_allocated_major_end;
 
/*Heap limits and allocation knobs*/
static mword max_heap_size = ((mword)0)- ((mword)1);
static mword soft_heap_limit = ((mword)0) - ((mword)1);
 
static double default_allowance_nursery_size_ratio = SGEN_DEFAULT_ALLOWANCE_NURSERY_SIZE_RATIO;
static double save_target_ratio = SGEN_DEFAULT_SAVE_TARGET_RATIO;
 
/**/
static mword allocated_heap;
static mword total_alloc = 0;
static mword total_alloc_max = 0;
 
static SGEN_TV_DECLARE(last_minor_start);
static SGEN_TV_DECLARE(last_major_start);
 
/* GC triggers. */
 
static gboolean debug_print_allowance = FALSE;
 
 
/* use this to tune when to do a major/minor collection */
static mword major_collection_trigger_size;
 
static mword major_pre_sweep_heap_size;
static mword major_start_heap_size;
 
static gboolean need_calculate_minor_collection_allowance;
 
/* The size of the LOS after the last major collection, after sweeping. */
static mword last_collection_los_memory_usage = 0;
static mword last_used_slots_size = 0;
 
static mword sgen_memgov_available_free_space (void);
 
 
/* GC trigger heuristics. */
 
static void
sgen_memgov_calculate_minor_collection_allowance (void)
{
    size_t new_major, new_heap_size, allowance_target, allowance;
    size_t decrease;
 
    if (!need_calculate_minor_collection_allowance)
        return;
 
    SGEN_ASSERT (0, major_collector.have_swept (), "Can only calculate allowance if heap is swept");
 
    new_major = major_collector.get_bytes_survived_last_sweep ();
    new_heap_size = new_major + last_collection_los_memory_usage;
 
    /*
     * We allow the heap to grow by one third its current size before we start the next
     * major collection.
     */
    allowance_target = new_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO;
 
    allowance = MAX (allowance_target, MIN_MINOR_COLLECTION_ALLOWANCE);
 
    /*
     * For the concurrent collector, we decrease the allowance relative to the memory
     * growth during the M&S phase, survival rate of the collection and the allowance
     * ratio.
     */
    decrease = (major_pre_sweep_heap_size - major_start_heap_size) * ((float)new_heap_size / major_pre_sweep_heap_size) * (SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO + 1);
    if (decrease > allowance)
        decrease = allowance;
    allowance -= decrease;
 
    if (new_heap_size + allowance > soft_heap_limit) {
        if (new_heap_size > soft_heap_limit)
            allowance = MIN_MINOR_COLLECTION_ALLOWANCE;
        else
            allowance = MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
    }
 
    /* FIXME: Why is this here? */
    if (major_collector.free_swept_blocks)
        major_collector.free_swept_blocks (major_collector.get_num_major_sections () * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO);
 
    major_collection_trigger_size = new_heap_size + allowance;
 
    need_calculate_minor_collection_allowance = FALSE;
 
    if (debug_print_allowance) {
        SGEN_LOG (0, "Surviving sweep: %ld bytes (%ld major, %ld LOS)", (long)new_heap_size, (long)new_major, (long)last_collection_los_memory_usage);
        SGEN_LOG (0, "Allowance: %ld bytes", (long)allowance);
        SGEN_LOG (0, "Trigger size: %ld bytes", (long)major_collection_trigger_size);
    }
}
 
static inline size_t
get_heap_size (void)
{
    return major_collector.get_num_major_sections () * major_collector.section_size + los_memory_usage;
}
 
gboolean
sgen_need_major_collection (mword space_needed)
{
    size_t heap_size;
 
    if (sgen_concurrent_collection_in_progress ()) {
        heap_size = get_heap_size ();
 
        if (heap_size <= major_collection_trigger_size)
            return FALSE; 
 
        /*
         * The more the heap grows, the more we need to decrease the allowance above,
         * in order to have similar trigger sizes as the synchronous collector.
         * If the heap grows so much that we would need to have a negative allowance,
         * we force the finishing of the collection, to avoid increased memory usage.
         */
        if ((heap_size - major_start_heap_size) > major_start_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO)
            return TRUE;
        return FALSE;
    }
 
    /* FIXME: This is a cop-out.  We should have some way of figuring this out. */
    if (!major_collector.have_swept ())
        return FALSE;
 
    if (space_needed > sgen_memgov_available_free_space ())
        return TRUE;
 
    sgen_memgov_calculate_minor_collection_allowance ();
 
    heap_size = get_heap_size ();
 
    return heap_size > major_collection_trigger_size;
}
 
void
sgen_memgov_minor_collection_start (void)
{
    total_promoted_size_start = total_promoted_size;
    SGEN_TV_GETTIME (last_minor_start);
}
 
static void
sgen_add_log_entry (SgenLogEntry *log_entry)
{
    mono_os_mutex_lock (&log_entries_mutex);
    sgen_pointer_queue_add (&log_entries, log_entry);
    mono_os_mutex_unlock (&log_entries_mutex);
}
 
void
sgen_memgov_minor_collection_end (const char *reason, gboolean is_overflow)
{
    if (mono_trace_is_traced (G_LOG_LEVEL_INFO, MONO_TRACE_GC)) {
        SgenLogEntry *log_entry = (SgenLogEntry*)sgen_alloc_internal (INTERNAL_MEM_LOG_ENTRY);
        SGEN_TV_DECLARE (current_time);
        SGEN_TV_GETTIME (current_time);
 
        log_entry->type = SGEN_LOG_NURSERY;
        log_entry->reason = reason;
        log_entry->is_overflow = is_overflow;
        log_entry->time = SGEN_TV_ELAPSED (last_minor_start, current_time);
        log_entry->promoted_size = total_promoted_size - total_promoted_size_start;
        log_entry->major_size = major_collector.get_num_major_sections () * major_collector.section_size;
        log_entry->major_size_in_use = last_used_slots_size + total_allocated_major - total_allocated_major_end;
        log_entry->los_size = los_memory_usage_total;
        log_entry->los_size_in_use = los_memory_usage;
 
        sgen_add_log_entry (log_entry);
    }
}
 
void
sgen_memgov_major_pre_sweep (void)
{
    if (sgen_concurrent_collection_in_progress ()) {
        major_pre_sweep_heap_size = get_heap_size ();
    } else {
        /* We decrease the allowance only in the concurrent case */
        major_pre_sweep_heap_size = major_start_heap_size;
    }
}
 
void
sgen_memgov_major_post_sweep (mword used_slots_size)
{
    if (mono_trace_is_traced (G_LOG_LEVEL_INFO, MONO_TRACE_GC)) {
        SgenLogEntry *log_entry = (SgenLogEntry*)sgen_alloc_internal (INTERNAL_MEM_LOG_ENTRY);
 
        log_entry->type = SGEN_LOG_MAJOR_SWEEP_FINISH;
        log_entry->major_size = major_collector.get_num_major_sections () * major_collector.section_size;
        log_entry->major_size_in_use = used_slots_size + total_allocated_major - total_allocated_major_end;
 
        sgen_add_log_entry (log_entry);
    }
    last_used_slots_size = used_slots_size;
}
 
void
sgen_memgov_major_collection_start (gboolean concurrent, const char *reason)
{
    need_calculate_minor_collection_allowance = TRUE;
    major_start_heap_size = get_heap_size ();
 
    if (debug_print_allowance) {
        SGEN_LOG (0, "Starting collection with heap size %ld bytes", (long)major_start_heap_size);
    }
    if (concurrent && mono_trace_is_traced (G_LOG_LEVEL_INFO, MONO_TRACE_GC)) {
        SgenLogEntry *log_entry = (SgenLogEntry*)sgen_alloc_internal (INTERNAL_MEM_LOG_ENTRY);
 
        log_entry->type = SGEN_LOG_MAJOR_CONC_START;
        log_entry->reason = reason;
 
        sgen_add_log_entry (log_entry);
    }
    SGEN_TV_GETTIME (last_major_start);
}
 
void
sgen_memgov_major_collection_end (gboolean forced, gboolean concurrent, const char *reason, gboolean is_overflow)
{
    if (mono_trace_is_traced (G_LOG_LEVEL_INFO, MONO_TRACE_GC)) {
        SgenLogEntry *log_entry = (SgenLogEntry*)sgen_alloc_internal (INTERNAL_MEM_LOG_ENTRY);
        SGEN_TV_DECLARE (current_time);
        SGEN_TV_GETTIME (current_time);
 
        if (concurrent) {
            log_entry->type = SGEN_LOG_MAJOR_CONC_FINISH;
        } else {
            log_entry->type = SGEN_LOG_MAJOR_SERIAL;
        }
        log_entry->time = SGEN_TV_ELAPSED (last_major_start, current_time);
        log_entry->reason = reason;
        log_entry->is_overflow = is_overflow;
        log_entry->los_size = los_memory_usage_total;
        log_entry->los_size_in_use = los_memory_usage;
 
        sgen_add_log_entry (log_entry);
    }
 
    last_collection_los_memory_usage = los_memory_usage;
    total_allocated_major_end = total_allocated_major;
    if (forced) {
        sgen_get_major_collector ()->finish_sweeping ();
        sgen_memgov_calculate_minor_collection_allowance ();
    }
}
 
void
sgen_memgov_collection_start (int generation)
{
}
 
static void
sgen_output_log_entry (SgenLogEntry *entry, gint64 stw_time, int generation)
{
    char full_timing_buff [1024];
    full_timing_buff [0] = '\0';
 
    if (!entry->is_overflow)
                sprintf (full_timing_buff, "stw %.2fms", stw_time / 10000.0f);
 
    switch (entry->type) {
        case SGEN_LOG_NURSERY:
            mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MINOR%s: (%s) time %.2fms, %s promoted %zdK major size: %zdK in use: %zdK los size: %zdK in use: %zdK",
                entry->is_overflow ? "_OVERFLOW" : "",
                entry->reason ? entry->reason : "",
                entry->time / 10000.0f,
                (generation == GENERATION_NURSERY) ? full_timing_buff : "",
                entry->promoted_size / 1024,
                entry->major_size / 1024,
                entry->major_size_in_use / 1024,
                entry->los_size / 1024,
                entry->los_size_in_use / 1024);
            break;
        case SGEN_LOG_MAJOR_SERIAL:
            mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR%s: (%s) time %.2fms, %s los size: %zdK in use: %zdK",
                entry->is_overflow ? "_OVERFLOW" : "",
                entry->reason ? entry->reason : "",
                (int)entry->time / 10000.0f,
                full_timing_buff,
                entry->los_size / 1024,
                entry->los_size_in_use / 1024);
            break;
        case SGEN_LOG_MAJOR_CONC_START:
            mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR_CONCURRENT_START: (%s)", entry->reason ? entry->reason : "");
            break;
        case SGEN_LOG_MAJOR_CONC_FINISH:
            mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR_CONCURRENT_FINISH: (%s) time %.2fms, %s los size: %zdK in use: %zdK",
                entry->reason ? entry->reason : "",
                entry->time / 10000.0f,
                full_timing_buff,
                entry->los_size / 1024,
                entry->los_size_in_use / 1024);
            break;
        case SGEN_LOG_MAJOR_SWEEP_FINISH:
            mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_GC, "GC_MAJOR_SWEEP: major size: %zdK in use: %zdK",
                entry->major_size / 1024,
                entry->major_size_in_use / 1024);
            break;
        default:
            SGEN_ASSERT (0, FALSE, "Invalid log entry type");
            break;
    }
}
 
void
sgen_memgov_collection_end (int generation, gint64 stw_time)
{
    /*
     * At this moment the world has been restarted which means we can log all pending entries
     * without risking deadlocks.
     */
    if (mono_trace_is_traced (G_LOG_LEVEL_INFO, MONO_TRACE_GC)) {
        size_t i;
        SGEN_ASSERT (0, !sgen_is_world_stopped (), "We can't log if the world is stopped");
        mono_os_mutex_lock (&log_entries_mutex);
        for (i = 0; i < log_entries.next_slot; i++) {
            sgen_output_log_entry (log_entries.data [i], stw_time, generation);
            sgen_free_internal (log_entries.data [i], INTERNAL_MEM_LOG_ENTRY);
        }
        sgen_pointer_queue_clear (&log_entries);
        mono_os_mutex_unlock (&log_entries_mutex);
    }
}
 
/*
Global GC memory tracking.
This tracks the total usage of memory by the GC. This includes
managed and unmanaged memory.
*/
 
static unsigned long
prot_flags_for_activate (int activate)
{
    unsigned long prot_flags = activate? MONO_MMAP_READ|MONO_MMAP_WRITE: MONO_MMAP_NONE;
    return prot_flags | MONO_MMAP_PRIVATE | MONO_MMAP_ANON;
}
 
void
sgen_assert_memory_alloc (void *ptr, size_t requested_size, const char *assert_description)
{
    if (ptr || !assert_description)
        return;
    fprintf (stderr, "Error: Garbage collector could not allocate %zu bytes of memory for %s.\n", requested_size, assert_description);
    exit (1);
}
 
/*
 * Allocate a big chunk of memory from the OS (usually 64KB to several megabytes).
 * This must not require any lock.
 */
void*
sgen_alloc_os_memory (size_t size, SgenAllocFlags flags, const char *assert_description, MonoMemAccountType type)
{
    void *ptr;
 
    g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
 
    ptr = mono_valloc (0, size, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE), type);
    sgen_assert_memory_alloc (ptr, size, assert_description);
    if (ptr) {
        SGEN_ATOMIC_ADD_P (total_alloc, size);
        total_alloc_max = MAX (total_alloc_max, total_alloc);
    }
    return ptr;
}
 
/* size must be a power of 2 */
// FIXME: remove assert_description
void*
sgen_alloc_os_memory_aligned (size_t size, mword alignment, SgenAllocFlags flags, const char *assert_description, MonoMemAccountType type)
{
    void *ptr;
 
    g_assert (!(flags & ~(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE)));
 
    ptr = mono_valloc_aligned (size, alignment, prot_flags_for_activate (flags & SGEN_ALLOC_ACTIVATE), type);
    sgen_assert_memory_alloc (ptr, size, assert_description);
    if (ptr) {
        SGEN_ATOMIC_ADD_P (total_alloc, size);
        total_alloc_max = MAX (total_alloc_max, total_alloc);
    }
    return ptr;
}
 
/*
 * Free the memory returned by sgen_alloc_os_memory (), returning it to the OS.
 */
void
sgen_free_os_memory (void *addr, size_t size, SgenAllocFlags flags, MonoMemAccountType type)
{
    g_assert (!(flags & ~SGEN_ALLOC_HEAP));
 
    mono_vfree (addr, size, type);
    SGEN_ATOMIC_ADD_P (total_alloc, -(gssize)size);
    total_alloc_max = MAX (total_alloc_max, total_alloc);
}
 
size_t
sgen_gc_get_total_heap_allocation (void)
{
    return total_alloc;
}
 
 
/*
Heap Sizing limits.
This limit the max size of the heap. It takes into account
only memory actively in use to hold heap objects and not
for other parts of the GC.
 */
static mword
sgen_memgov_available_free_space (void)
{
    return max_heap_size - MIN (allocated_heap, max_heap_size);
}
 
void
sgen_memgov_release_space (mword size, int space)
{
    SGEN_ATOMIC_ADD_P (allocated_heap, -(gssize)size);
}
 
gboolean
sgen_memgov_try_alloc_space (mword size, int space)
{
    if (sgen_memgov_available_free_space () < size) {
        SGEN_ASSERT (4, !sgen_workers_is_worker_thread (mono_native_thread_id_get ()), "Memory shouldn't run out in worker thread");
        return FALSE;
    }
 
    SGEN_ATOMIC_ADD_P (allocated_heap, size);
    sgen_client_total_allocated_heap_changed (allocated_heap);
    return TRUE;
}
 
void
sgen_memgov_init (size_t max_heap, size_t soft_limit, gboolean debug_allowance, double allowance_ratio, double save_target)
{
    if (soft_limit)
        soft_heap_limit = soft_limit;
 
    debug_print_allowance = debug_allowance;
    major_collection_trigger_size = MIN_MINOR_COLLECTION_ALLOWANCE;
 
    mono_counters_register ("Memgov alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_VARIABLE, (void*)&total_alloc);
    mono_counters_register ("Memgov max alloc", MONO_COUNTER_GC | MONO_COUNTER_WORD | MONO_COUNTER_BYTES | MONO_COUNTER_MONOTONIC, (void*)&total_alloc_max);
 
    mono_os_mutex_init (&log_entries_mutex);
 
    sgen_register_fixed_internal_mem_type (INTERNAL_MEM_LOG_ENTRY, sizeof (SgenLogEntry));
 
    if (max_heap == 0)
        return;
 
    if (max_heap < soft_limit) {
        sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least as large as `soft-heap-limit`.");
        max_heap = soft_limit;
    }
 
    if (max_heap < SGEN_DEFAULT_NURSERY_SIZE * 4) {
        sgen_env_var_error (MONO_GC_PARAMS_NAME, "Setting to minimum.", "`max-heap-size` must be at least 4 times as large as `nursery size`.");
        max_heap = SGEN_DEFAULT_NURSERY_SIZE * 4;
    }
    max_heap_size = max_heap - SGEN_DEFAULT_NURSERY_SIZE;
 
    if (allowance_ratio)
        default_allowance_nursery_size_ratio = allowance_ratio;
 
    if (save_target)
        save_target_ratio = save_target;
}
 
#endif