-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
229 lines (180 loc) · 5.95 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Mono.Options;
using Mono.Profiler.Aot;
using static System.Console;
namespace aotprofiletool {
class MainClass {
static readonly string Name = "aotprofile-tool";
static bool Methods;
static bool Modules;
static bool Summary;
static bool Types;
static bool Verbose;
static Regex FilterMethod;
static Regex FilterModule;
static Regex FilterType;
static string Output;
static string ProcessArguments (string [] args)
{
var help = false;
var options = new OptionSet {
$"Usage: {Name}.exe OPTIONS* <aotprofile-file>",
"",
"Processes AOTPROFILE files created by Mono's AOT Profiler",
"",
"Copyright 2019 Microsoft Corporation",
"",
"Options:",
{ "h|help|?",
"Show this message and exit",
v => help = v != null },
{ "a|all",
"Show modules, types and methods in the profile",
v => Modules = Types = Methods = true },
{ "d|modules",
"Show modules in the profile",
v => Modules = true },
{ "filter-method=",
"Filter by method with regex VALUE",
v => FilterMethod = new Regex (v) },
{ "filter-module=",
"Filter by module with regex VALUE",
v => FilterModule = new Regex (v) },
{ "filter-type=",
"Filter by type with regex VALUE",
v => FilterType = new Regex (v) },
{ "m|methods",
"Show methods in the profile",
v => Methods = true },
{ "o|output=",
"Write profile to OUTPUT file",
v => Output = v },
{ "s|summary",
"Show summary of the profile",
v => Summary = true },
{ "t|types",
"Show types in the profile",
v => Types = true },
{ "v|verbose",
"Output information about progress during the run of the tool",
v => Verbose = true },
"",
"If no other option than -v is used then --all is used by default"
};
var remaining = options.Parse (args);
if (help || args.Length < 1) {
options.WriteOptionDescriptions (Out);
Environment.Exit (0);
}
if (remaining.Count != 1) {
Error ("Please specify one <aotprofile-file> to process.");
Environment.Exit (2);
}
return remaining [0];
}
public static void Main (string [] args)
{
var path = ProcessArguments (args);
if (!File.Exists (path)) {
Error ($"'{path}' doesn't exist.");
Environment.Exit (3);
}
if (args.Length == 1) {
Modules = Types = Methods = true;
}
var reader = new ProfileReader ();
ProfileData pd;
using (var stream = new FileStream (path, FileMode.Open)) {
if (Verbose)
ColorWriteLine ($"Reading '{path}'...", ConsoleColor.Yellow);
pd = reader.Read (stream);
}
List<MethodRecord> methods = pd.Methods;
ICollection<TypeRecord> types = pd.Types;
ICollection<ModuleRecord> modules = pd.Modules;
if (FilterMethod != null || FilterType != null || FilterModule != null) {
methods = new List<MethodRecord> ();
types = new HashSet<TypeRecord> ();
modules = new HashSet<ModuleRecord> ();
foreach (var method in pd.Methods) {
var type = method.Type;
var module = type.Module;
if (FilterModule != null) {
var match = FilterModule.Match (module.ToString ());
if (!match.Success)
continue;
}
if (FilterType != null) {
var match = FilterType.Match (method.Type.ToString ());
if (!match.Success)
continue;
}
if (FilterMethod != null) {
var match = FilterMethod.Match (method.ToString ());
if (!match.Success)
continue;
}
methods.Add (method);
types.Add (type);
modules.Add (module);
}
}
if (FilterMethod == null && FilterType != null) {
foreach (var type in pd.Types) {
if (types.Contains (type))
continue;
var match = FilterType.Match (type.ToString ());
if (!match.Success)
continue;
types.Add (type);
}
}
if (Modules) {
ColorWriteLine ($"Modules:", ConsoleColor.Green);
foreach (var module in modules)
WriteLine ($"\t{module.Mvid} {module}");
}
if (Types) {
ColorWriteLine ($"Types:", ConsoleColor.Green);
foreach (var type in types)
WriteLine ($"\t{type}");
}
if (Methods) {
ColorWriteLine ($"Methods:", ConsoleColor.Green);
foreach (var method in methods)
WriteLine ($"\t{method}");
}
if (Summary) {
ColorWriteLine ($"Summary:", ConsoleColor.Green);
WriteLine ($"\tModules: {modules.Count.ToString ("N0"),10}{(modules.Count != pd.Modules.Count ? $" (of {pd.Modules.Count})" : "" )}");
WriteLine ($"\tTypes: {types.Count.ToString ("N0"),10}{(types.Count != pd.Types.Count ? $" (of {pd.Types.Count})" : "")}");
WriteLine ($"\tMethods: {methods.Count.ToString ("N0"),10}{(methods.Count != pd.Methods.Count ? $" (of {pd.Methods.Count})" : "")}");
}
if (!string.IsNullOrEmpty (Output)) {
if (Verbose)
ColorWriteLine ($"Going to write the profile to '{Output}'", ConsoleColor.Yellow);
var updatedPD = new ProfileData (new List<ModuleRecord>(modules), new List<TypeRecord> (types), methods);
using (var stream = new FileStream (Output, FileMode.Create)) {
var writer = new ProfileWriter (stream, updatedPD);
writer.Write ();
}
}
}
static void ColorMessage (string message, ConsoleColor color, TextWriter writer, bool writeLine = true)
{
ForegroundColor = color;
if (writeLine)
writer.WriteLine (message);
else
writer.Write (message);
ResetColor ();
}
public static void ColorWriteLine (string message, ConsoleColor color) => ColorMessage (message, color, Out);
public static void ColorWrite (string message, ConsoleColor color) => ColorMessage (message, color, Out, false);
public static void Error (string message) => ColorMessage ($"Error: {Name}: {message}", ConsoleColor.Red, Console.Error);
public static void Warning (string message) => ColorMessage ($"Warning: {Name}: {message}", ConsoleColor.Yellow, Console.Error);
}
}