Skip to content

Commit

Permalink
Add MstatDump to runtimelab (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
agocke authored Apr 25, 2023
1 parent d3fc408 commit 7e0c982
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 57 deletions.
48 changes: 0 additions & 48 deletions Experiment.sln

This file was deleted.

30 changes: 30 additions & 0 deletions MstatDump.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EE66DA4C-CFF2-4F61-A044-97AD65C24E96}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MstatDump", "MstatDump", "{B47BCDBA-5EF2-4FEE-AEB5-6C776599236F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MstatDump", "src\MstatDump\src\MstatDump.csproj", "{194F3857-D533-4B38-B758-3F9418DC77C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{194F3857-D533-4B38-B758-3F9418DC77C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{194F3857-D533-4B38-B758-3F9418DC77C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{194F3857-D533-4B38-B758-3F9418DC77C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{194F3857-D533-4B38-B758-3F9418DC77C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B47BCDBA-5EF2-4FEE-AEB5-6C776599236F} = {EE66DA4C-CFF2-4F61-A044-97AD65C24E96}
{194F3857-D533-4B38-B758-3F9418DC77C3} = {B47BCDBA-5EF2-4FEE-AEB5-6C776599236F}
EndGlobalSection
EndGlobal
9 changes: 0 additions & 9 deletions src/Experiment/src/MyClass.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.4" />
</ItemGroup>

</Project>
133 changes: 133 additions & 0 deletions src/MstatDump/src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Rocks;

namespace MstatDump;

internal class Program
{
static void Main(string[] args)
{
var asm = AssemblyDefinition.ReadAssembly(args[1]);
var globalType = (TypeDefinition)asm.MainModule.LookupToken(0x02000001);

var types = globalType.Methods.First(x => x.Name == "Types");
var typeStats = GetTypes(types).ToList();
var typeSize = typeStats.Sum(x => x.Size);
var typesByModules = typeStats.GroupBy(x => x.Type.Scope).Select(x => new { x.Key.Name, Sum = x.Sum(x => x.Size) }).ToList();
Console.WriteLine($"// ********** Types Total Size {typeSize:n0}");
foreach (var m in typesByModules.OrderByDescending(x => x.Sum))
{
Console.WriteLine($"{m.Name,-40} {m.Sum,7:n0}");
}
Console.WriteLine($"// **********");

Console.WriteLine();

var methods = globalType.Methods.First(x => x.Name == "Methods");
var methodStats = GetMethods(methods).ToList();
var methodSize = methodStats.Sum(x => x.Size + x.GcInfoSize + x.EhInfoSize);
var methodsByModules = methodStats.GroupBy(x => x.Method.DeclaringType.Scope).Select(x => new { x.Key.Name, Sum = x.Sum(x => x.Size + x.GcInfoSize + x.EhInfoSize) }).ToList();
Console.WriteLine($"// ********** Methods Total Size {methodSize:n0}");
foreach (var m in methodsByModules.OrderByDescending(x => x.Sum))
{
Console.WriteLine($"{m.Name,-40} {m.Sum,7:n0}");
}
Console.WriteLine($"// **********");

Console.WriteLine();

string FindNamespace(TypeReference type)
{
var current = type;
while (true)
{
if (!String.IsNullOrEmpty(current.Namespace))
{
return current.Namespace;
}

if (current.DeclaringType == null)
{
return current.Name;
}

current = current.DeclaringType;
}
}

var methodsByNamespace = methodStats.Select(x => new TypeStats { Type = x.Method.DeclaringType, Size = x.Size + x.GcInfoSize + x.EhInfoSize }).Concat(typeStats).GroupBy(x => FindNamespace(x.Type)).Select(x => new { x.Key, Sum = x.Sum(x => x.Size) }).ToList();
Console.WriteLine($"// ********** Size By Namespace");
foreach (var m in methodsByNamespace.OrderByDescending(x => x.Sum))
{
Console.WriteLine($"{m.Key,-40} {m.Sum,7:n0}");
}
Console.WriteLine($"// **********");

Console.WriteLine();

var blobs = globalType.Methods.First(x => x.Name == "Blobs");
var blobStats = GetBlobs(blobs).ToList();
var blobSize = blobStats.Sum(x => x.Size);
Console.WriteLine($"// ********** Blobs Total Size {blobSize:n0}");
foreach (var m in blobStats.OrderByDescending(x => x.Size))
{
Console.WriteLine($"{m.Name,-40} {m.Size,7:n0}");
}
Console.WriteLine($"// **********");
}

public static IEnumerable<TypeStats> GetTypes(MethodDefinition types)
{
types.Body.SimplifyMacros();
var il = types.Body.Instructions;
for (int i = 0; i + 2 < il.Count; i += 2)
{
var type = (TypeReference)il[i + 0].Operand;
var size = (int)il[i + 1].Operand;
yield return new TypeStats
{
Type = type,
Size = size
};
}
}

public static IEnumerable<MethodStats> GetMethods(MethodDefinition methods)
{
methods.Body.SimplifyMacros();
var il = methods.Body.Instructions;
for (int i = 0; i + 4 < il.Count; i += 4)
{
var method = (MethodReference)il[i + 0].Operand;
var size = (int)il[i + 1].Operand;
var gcInfoSize = (int)il[i + 2].Operand;
var ehInfoSize = (int)il[i + 3].Operand;
yield return new MethodStats
{
Method = method,
Size = size,
GcInfoSize = gcInfoSize,
EhInfoSize = ehInfoSize
};
}
}

public static IEnumerable<BlobStats> GetBlobs(MethodDefinition blobs)
{
blobs.Body.SimplifyMacros();
var il = blobs.Body.Instructions;
for (int i = 0; i + 2 < il.Count; i += 2)
{
var name = (string)il[i + 0].Operand;
var size = (int)il[i + 1].Operand;
yield return new BlobStats
{
Name = name,
Size = size
};
}
}
}
24 changes: 24 additions & 0 deletions src/MstatDump/src/StatDefinitions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

using Mono.Cecil;

namespace MstatDump;

public class TypeStats
{
public TypeReference Type { get; set; }
public int Size { get; set; }
}

public class MethodStats
{
public MethodReference Method { get; set; }
public int Size { get; set; }
public int GcInfoSize { get; set; }
public int EhInfoSize { get; set; }
}

public class BlobStats
{
public string Name { get; set; }
public int Size { get; set; }
}
File renamed without changes.
File renamed without changes.

0 comments on commit 7e0c982

Please sign in to comment.