summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLudovic Henry <luhenry@microsoft.com>2019-05-13 13:15:05 -0700
committerVladimir Sadov <vsadov@microsoft.com>2019-05-13 13:15:04 -0700
commit934d73fea95b4c479b67fee0ff53caea4a325ee5 (patch)
tree35d0d77780d31625887625ef74f614327a9c3f00 /tests
parent24cdacaeb1e42a92051b32a6c4051b7a12ff2770 (diff)
downloadcoreclr-934d73fea95b4c479b67fee0ff53caea4a325ee5.tar.gz
coreclr-934d73fea95b4c479b67fee0ff53caea4a325ee5.tar.bz2
coreclr-934d73fea95b4c479b67fee0ff53caea4a325ee5.zip
Implement GC.GetTotalAllocatedBytes (#23852)
* keep what's allocated so far on each heap * Implement GC.GetTotalAllocatedBytes It is based on https://github.com/dotnet/corefx/issues/34631 and https://github.com/dotnet/corefx/issues/30644 * Fixing races related to dead_threads_non_alloc_bytes * separated per-heap SOH and LOH counters. Different locks imply that we need different counters. * allow/ignore torn 64bit reads on 32bit in imprecise mode. * PR feedback * simplified the test a little to avoid OOM on ARM
Diffstat (limited to 'tests')
-rw-r--r--tests/src/GC/API/GC/GetTotalAllocatedBytes.cs167
-rw-r--r--tests/src/GC/API/GC/GetTotalAllocatedBytes.csproj39
2 files changed, 206 insertions, 0 deletions
diff --git a/tests/src/GC/API/GC/GetTotalAllocatedBytes.cs b/tests/src/GC/API/GC/GetTotalAllocatedBytes.cs
new file mode 100644
index 0000000000..e35bb41ca0
--- /dev/null
+++ b/tests/src/GC/API/GC/GetTotalAllocatedBytes.cs
@@ -0,0 +1,167 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+// Tests GC.Collect()
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Reflection;
+using System.Threading;
+using System.Threading.Tasks;
+
+public class Test
+{
+ static Random Rand = new Random();
+ static volatile object s_stash; // static volatile variable to keep the jit from eliding allocations or anything.
+
+ delegate long GetTotalAllocatedBytesDelegate(bool precise);
+ static GetTotalAllocatedBytesDelegate GetTotalAllocatedBytes = Get_GetTotalAllocatedBytesDelegate();
+
+ private static GetTotalAllocatedBytesDelegate Get_GetTotalAllocatedBytesDelegate()
+ {
+ const string name = "GetTotalAllocatedBytes";
+ var typeInfo = typeof(GC).GetTypeInfo();
+ var method = typeInfo.GetMethod(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
+ GetTotalAllocatedBytesDelegate del = (GetTotalAllocatedBytesDelegate)method.CreateDelegate(typeof(GetTotalAllocatedBytesDelegate));
+ // Prime the delegate to ensure its been called some.
+ del(true);
+ del(false);
+
+ return del;
+ }
+
+ private static long CallGetTotalAllocatedBytes(long previous, out long differenceBetweenPreciseAndImprecise)
+ {
+ long precise = GetTotalAllocatedBytes(true);
+ long imprecise = GetTotalAllocatedBytes(false);
+
+ if (precise <= 0)
+ {
+ throw new Exception($"Bytes allocated is not positive, this is unlikely. precise = {precise}");
+ }
+
+ if (imprecise < precise)
+ {
+ throw new Exception($"Imprecise total bytes allocated less than precise, imprecise is required to be a conservative estimate (that estimates high). imprecise = {imprecise}, precise = {precise}");
+ }
+
+ if (previous > precise)
+ {
+ throw new Exception($"Expected more memory to be allocated. previous = {previous}, precise = {precise}, difference = {previous - precise}");
+ }
+
+ differenceBetweenPreciseAndImprecise = imprecise - precise;
+ return precise;
+ }
+
+ private static long CallGetTotalAllocatedBytes(long previous)
+ {
+ long differenceBetweenPreciseAndImprecise;
+ previous = CallGetTotalAllocatedBytes(previous, out differenceBetweenPreciseAndImprecise);
+ s_stash = new byte[differenceBetweenPreciseAndImprecise];
+ previous = CallGetTotalAllocatedBytes(previous, out differenceBetweenPreciseAndImprecise);
+ return previous;
+ }
+
+ public static void TestSingleThreaded()
+ {
+ long previous = 0;
+ for (int i = 0; i < 1000; ++i)
+ {
+ s_stash = new byte[1234];
+ previous = CallGetTotalAllocatedBytes(previous);
+ }
+ }
+
+ public static void TestSingleThreadedLOH()
+ {
+ long previous = 0;
+ for (int i = 0; i < 1000; ++i)
+ {
+ s_stash = new byte[123456];
+ previous = CallGetTotalAllocatedBytes(previous);
+ }
+ }
+
+ public static void TestAnotherThread()
+ {
+ bool running = true;
+ Task tsk = null;
+
+ try
+ {
+ object lck = new object();
+
+ tsk = Task.Run(() => {
+ while (running)
+ {
+ Thread thd = new Thread(() => {
+ lock (lck)
+ {
+ s_stash = new byte[1234];
+ }
+ });
+
+ thd.Start();
+ thd.Join();
+ }
+ });
+
+ long previous = 0;
+ for (int i = 0; i < 1000; ++i)
+ {
+ lock (lck)
+ {
+ previous = CallGetTotalAllocatedBytes(previous);
+ }
+
+ Thread.Sleep(1);
+ }
+ }
+ finally
+ {
+ running = false;
+ tsk?.Wait(1000);
+ }
+ }
+
+ public static void TestLohSohConcurrently()
+ {
+ List<Thread> threads = new List<Thread>();
+ ManualResetEventSlim me = new ManualResetEventSlim();
+ int threadNum = Environment.ProcessorCount + Environment.ProcessorCount / 2;
+ for (int i = 0; i < threadNum; i++)
+ {
+ Thread thr = new Thread(() =>
+ {
+ me.Wait();
+ long previous = 0;
+ for (int i = 0; i < 2; ++i)
+ {
+ s_stash = new byte[123456];
+ previous = CallGetTotalAllocatedBytes(previous);
+ s_stash = new byte[1234];
+ previous = CallGetTotalAllocatedBytes(previous);
+ }
+ });
+
+ thr.Start();
+ threads.Add(thr);
+ }
+
+ me.Set();
+
+ foreach (var thr in threads)
+ thr.Join();
+ }
+
+ public static int Main()
+ {
+ TestSingleThreaded();
+ TestSingleThreadedLOH();
+ TestAnotherThread();
+ TestLohSohConcurrently();
+ return 100;
+ }
+}
diff --git a/tests/src/GC/API/GC/GetTotalAllocatedBytes.csproj b/tests/src/GC/API/GC/GetTotalAllocatedBytes.csproj
new file mode 100644
index 0000000000..dd349ba306
--- /dev/null
+++ b/tests/src/GC/API/GC/GetTotalAllocatedBytes.csproj
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <AssemblyName>$(MSBuildProjectName)</AssemblyName>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+ <HeapVerifyIncompatible Condition="'$(Platform)' == 'arm'">true</HeapVerifyIncompatible>
+
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <PropertyGroup>
+ <!-- Set to 'Full' if the Debug? column is marked in the spreadsheet. Leave blank otherwise. -->
+ <DebugType>PdbOnly</DebugType>
+ <NoLogo>True</NoLogo>
+ <DefineConstants>$(DefineConstants);DESKTOP</DefineConstants>
+ <GCStressIncompatible>true</GCStressIncompatible>
+ </PropertyGroup>
+ <ItemGroup>
+ <Compile Include="GetTotalAllocatedBytes.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+ <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
+</Project>