mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 01:41:18 -10:00
Compare commits
38
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4e14ad8a4 | ||
|
|
f2c22c8ac4 | ||
|
|
c256a0fa80 | ||
|
|
2885957720 | ||
|
|
74658ba46b | ||
|
|
c9b277cd3b | ||
|
|
dd7f7d0976 | ||
|
|
1d0fc47618 | ||
|
|
e3d1a191c2 | ||
|
|
94749fe6fb | ||
|
|
3699cf07ae | ||
|
|
a0fe1a09e8 | ||
|
|
274118e6b8 | ||
|
|
39ed1bce7b | ||
|
|
b6f92c45d2 | ||
|
|
066e0029af | ||
|
|
0912bc0f6e | ||
|
|
0cc69fd165 | ||
|
|
7a75b4d9d5 | ||
|
|
7514f01302 | ||
|
|
013eacaf86 | ||
|
|
5fc3062411 | ||
|
|
fd15f967dc | ||
|
|
f919f3d3db | ||
|
|
20e8a6dab6 | ||
|
|
5761c3f874 | ||
|
|
223cf0bd19 | ||
|
|
54f317467e | ||
|
|
f63f78022c | ||
|
|
3f44389aad | ||
|
|
b2b9703239 | ||
|
|
1b56a9eb8b | ||
|
|
70ecd9ea56 | ||
|
|
5115ab0100 | ||
|
|
1df089428c | ||
|
|
766b9f4616 | ||
|
|
bc29c2a6e1 | ||
|
|
adb22ab81d |
No files matched your search
@@ -34,6 +34,60 @@ using NUnit.Framework;
|
||||
|
||||
namespace MonoTests.System {
|
||||
|
||||
public interface SizedObject
|
||||
{
|
||||
long ExpectedSize();
|
||||
}
|
||||
|
||||
public class NoPointer : SizedObject
|
||||
{
|
||||
public long ExpectedSize()
|
||||
{
|
||||
return 2 * IntPtr.Size;
|
||||
}
|
||||
}
|
||||
|
||||
public class TwoPointer : SizedObject
|
||||
{
|
||||
public object field1;
|
||||
public object field2;
|
||||
|
||||
public long ExpectedSize()
|
||||
{
|
||||
return 4*IntPtr.Size;
|
||||
}
|
||||
}
|
||||
|
||||
public class FourPointer : SizedObject
|
||||
{
|
||||
public object field1;
|
||||
public object field2;
|
||||
public object field3;
|
||||
public object field4;
|
||||
|
||||
public long ExpectedSize()
|
||||
{
|
||||
return 6*IntPtr.Size;
|
||||
}
|
||||
}
|
||||
|
||||
public class EightPointer : SizedObject
|
||||
{
|
||||
public object field1;
|
||||
public object field2;
|
||||
public object fiedl3;
|
||||
public object field4;
|
||||
public object field5;
|
||||
public object field6;
|
||||
public object field7;
|
||||
public object field8;
|
||||
|
||||
public long ExpectedSize()
|
||||
{
|
||||
return (long)(10*IntPtr.Size);
|
||||
}
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class GCTest {
|
||||
class MyFinalizeObject
|
||||
@@ -71,5 +125,45 @@ namespace MonoTests.System {
|
||||
|
||||
Assert.IsTrue (t.Wait (5000));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public static void TestGetBytesAllocatedForCurrentThread()
|
||||
{
|
||||
|
||||
Func<SizedObject>[] objectAllocators = {
|
||||
() => new NoPointer(),
|
||||
() => new TwoPointer(),
|
||||
() => new FourPointer(),
|
||||
() => new EightPointer()
|
||||
};
|
||||
|
||||
Random r = new Random();
|
||||
|
||||
// Methods trigger allocation when first run.
|
||||
// So this code warms up allocation before measuring.
|
||||
for (int i = 0; i < objectAllocators.Length; i++)
|
||||
{
|
||||
Console.WriteLine(objectAllocators[i]().ExpectedSize());
|
||||
}
|
||||
|
||||
Console.WriteLine(r.Next(1, 10));
|
||||
|
||||
Assert.AreEqual(1L, 1L);
|
||||
|
||||
// End warmup
|
||||
|
||||
long expectedSize = 0;
|
||||
long bytesBeforeAlloc = GC.GetAllocatedBytesForCurrentThread();
|
||||
|
||||
for (int i = 0; i < 10000000; i++)
|
||||
{
|
||||
expectedSize += objectAllocators[r.Next(0, objectAllocators.Length) ]().ExpectedSize();
|
||||
}
|
||||
|
||||
long bytesAfterAlloc = GC.GetAllocatedBytesForCurrentThread();
|
||||
|
||||
Assert.AreEqual(expectedSize, bytesAfterAlloc - bytesBeforeAlloc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,7 @@ System/EnumTest.cs
|
||||
System/EnvironmentTest.cs
|
||||
System/ExceptionTest.cs
|
||||
System/FloatingPointFormatterTest.cs
|
||||
System.GC/GCtest.cs
|
||||
System.Globalization/CalendarTest.cs
|
||||
System.Globalization/CompareInfoTest.cs
|
||||
System.Globalization/CultureInfoTest.cs
|
||||
|
||||
@@ -311,6 +311,12 @@ mono_gc_max_generation (void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
gint64
|
||||
mono_gc_get_allocated_bytes_for_current_thread (void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* mono_gc_get_generation:
|
||||
* \param object a managed object
|
||||
|
||||
@@ -311,6 +311,8 @@ void* mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data);
|
||||
|
||||
int mono_gc_get_los_limit (void);
|
||||
|
||||
int64_t mono_gc_get_allocated_bytes_for_current_thread (void);
|
||||
|
||||
guint8* mono_gc_get_card_table (int *shift_bits, gpointer *card_mask);
|
||||
guint8* mono_gc_get_target_card_table (int *shift_bits, target_mgreg_t *card_mask);
|
||||
gboolean mono_gc_card_table_nursery_check (void);
|
||||
|
||||
@@ -9174,7 +9174,7 @@ ves_icall_System_GC_GetMaxGeneration (void)
|
||||
gint64
|
||||
ves_icall_System_GC_GetAllocatedBytesForCurrentThread (void)
|
||||
{
|
||||
return 0; // TODO: implement https://github.com/mono/mono/issues/8397
|
||||
return mono_gc_get_allocated_bytes_for_current_thread ();
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -67,6 +67,12 @@ mono_gc_max_generation (void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
gint64
|
||||
mono_gc_get_allocated_bytes_for_current_thread (void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
mono_gc_get_generation (MonoObject *object)
|
||||
{
|
||||
|
||||
@@ -2486,6 +2486,16 @@ mono_gc_get_los_limit (void)
|
||||
return SGEN_MAX_SMALL_OBJ_SIZE;
|
||||
}
|
||||
|
||||
int64_t
|
||||
mono_gc_get_allocated_bytes_for_current_thread (void)
|
||||
{
|
||||
SgenThreadInfo* info;
|
||||
info = mono_thread_info_current ();
|
||||
|
||||
/*There are some more allocated bytes in the current tlab that have not been recorded yet */
|
||||
return info->total_bytes_allocated + info->tlab_next - info->tlab_start;
|
||||
}
|
||||
|
||||
gpointer
|
||||
sgen_client_default_metadata (void)
|
||||
{
|
||||
|
||||
+37
-7
@@ -51,7 +51,7 @@ static guint64 stat_bytes_alloced_los = 0;
|
||||
/*
|
||||
* 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
|
||||
* 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.
|
||||
@@ -63,10 +63,17 @@ static guint64 stat_bytes_alloced_los = 0;
|
||||
#define TLAB_TEMP_END (__thread_info__->tlab_temp_end)
|
||||
#define TLAB_REAL_END (__thread_info__->tlab_real_end)
|
||||
|
||||
static void
|
||||
increment_thread_allocation_counter (size_t byte_size)
|
||||
{
|
||||
mono_thread_info_current ()->total_bytes_allocated += byte_size;
|
||||
}
|
||||
|
||||
static GCObject*
|
||||
alloc_degraded (GCVTable vtable, size_t size, gboolean for_mature)
|
||||
{
|
||||
GCObject *p;
|
||||
increment_thread_allocation_counter (size);
|
||||
|
||||
if (!for_mature) {
|
||||
sgen_client_degraded_allocation ();
|
||||
@@ -124,7 +131,7 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
char *new_next;
|
||||
size_t real_size = size;
|
||||
TLAB_ACCESS_INIT;
|
||||
|
||||
|
||||
CANARIFY_SIZE(size);
|
||||
|
||||
HEAVY_STAT (++stat_objects_alloced);
|
||||
@@ -168,6 +175,9 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
|
||||
if (real_size > SGEN_MAX_SMALL_OBJ_SIZE) {
|
||||
p = (void **)sgen_los_alloc_large_inner (vtable, ALIGN_UP (real_size));
|
||||
if (p) {
|
||||
increment_thread_allocation_counter (size);
|
||||
}
|
||||
} else {
|
||||
/* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
|
||||
|
||||
@@ -191,7 +201,7 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
/* 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
|
||||
/* 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
|
||||
@@ -201,12 +211,13 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
*/
|
||||
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.
|
||||
* request. We keep the TLAB for future allocations if the remaining
|
||||
* space is above a treshold, and satisfy the allocation directly
|
||||
* from the nursery. Otherwise, we attempt to get a new TLAB from the
|
||||
* nursery and allocate into it.
|
||||
*/
|
||||
TLAB_NEXT -= size;
|
||||
/* when running in degraded mode, we continue allocing that way
|
||||
@@ -240,6 +251,10 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
sgen_ensure_free_space (real_size, GENERATION_NURSERY);
|
||||
if (!sgen_degraded_mode)
|
||||
p = (void **)sgen_nursery_alloc (size);
|
||||
|
||||
if (p)
|
||||
increment_thread_allocation_counter (size);
|
||||
|
||||
}
|
||||
if (!p)
|
||||
return alloc_degraded (vtable, size, TRUE);
|
||||
@@ -261,6 +276,8 @@ sgen_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
if (!p)
|
||||
return alloc_degraded (vtable, size, TRUE);
|
||||
|
||||
increment_thread_allocation_counter (TLAB_NEXT - TLAB_START);
|
||||
|
||||
/* Allocate a new TLAB from the current nursery fragment */
|
||||
TLAB_START = (char*)p;
|
||||
TLAB_NEXT = TLAB_START;
|
||||
@@ -315,9 +332,12 @@ sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
|
||||
if (G_UNLIKELY (size > sgen_tlab_size)) {
|
||||
/* Allocate directly from the nursery */
|
||||
|
||||
p = (void **)sgen_nursery_alloc (size);
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
increment_thread_allocation_counter (size);
|
||||
sgen_set_nursery_scan_start ((char*)p);
|
||||
|
||||
/*FIXME we should use weak memory ops here. Should help specially on x86. */
|
||||
@@ -350,6 +370,7 @@ sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
increment_thread_allocation_counter (size);
|
||||
zero_tlab_if_necessary (p, size);
|
||||
} else {
|
||||
size_t alloc_size = 0;
|
||||
@@ -360,6 +381,8 @@ sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size)
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
increment_thread_allocation_counter (TLAB_NEXT - TLAB_START);
|
||||
|
||||
TLAB_START = (char*)new_next;
|
||||
TLAB_NEXT = new_next + size;
|
||||
TLAB_REAL_END = new_next + alloc_size;
|
||||
@@ -435,6 +458,7 @@ sgen_alloc_obj_pinned (GCVTable vtable, size_t size)
|
||||
{
|
||||
GCObject *p;
|
||||
|
||||
|
||||
if (!SGEN_CAN_ALIGN_UP (size))
|
||||
return NULL;
|
||||
size = ALIGN_UP (size);
|
||||
@@ -450,6 +474,7 @@ sgen_alloc_obj_pinned (GCVTable vtable, size_t size)
|
||||
}
|
||||
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);
|
||||
increment_thread_allocation_counter (size);
|
||||
sgen_binary_protocol_alloc_pinned (p, vtable, size, sgen_client_get_provenance ());
|
||||
}
|
||||
UNLOCK_GC;
|
||||
@@ -469,6 +494,10 @@ sgen_alloc_obj_mature (GCVTable vtable, size_t size)
|
||||
res = alloc_degraded (vtable, size, TRUE);
|
||||
UNLOCK_GC;
|
||||
|
||||
if (res) {
|
||||
increment_thread_allocation_counter (size);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -480,6 +509,7 @@ sgen_clear_tlabs (void)
|
||||
{
|
||||
FOREACH_THREAD_ALL (info) {
|
||||
/* A new TLAB will be allocated when the thread does its first allocation */
|
||||
info->total_bytes_allocated += info->tlab_next - info->tlab_start;
|
||||
info->tlab_start = NULL;
|
||||
info->tlab_next = NULL;
|
||||
info->tlab_temp_end = NULL;
|
||||
|
||||
@@ -414,6 +414,9 @@ struct _SgenThreadInfo {
|
||||
char *tlab_next;
|
||||
char *tlab_temp_end;
|
||||
char *tlab_real_end;
|
||||
|
||||
/* Total bytes allocated by this thread in its lifetime so far. */
|
||||
gint64 total_bytes_allocated;
|
||||
};
|
||||
|
||||
gboolean sgen_is_worker_thread (MonoNativeThreadId thread);
|
||||
|
||||
Reference in new issue
Block a user