[interp] run regression with all combinations of available optimizations (#17644)
[mono-project.git] / mcs / class / Mono.Profiler.Log / Mono.Profiler.Aot / ProfileWriter.cs
blobe7be91e19d6a947e3d95c2304f3bbf8c1fbd7999
1 using System.IO;
2 using System.Collections.Generic;
3 using System.Text;
5 namespace Mono.Profiler.Aot {
6 //
7 // Write the contents of a .aotprofile
8 // See mono/profiler/aot.h for a description of the file format
9 //
10 public sealed class ProfileWriter : ProfileBase {
11 Stream stream;
12 ProfileData data;
13 int id;
15 readonly Dictionary<TypeRecord, int> typeIds = new Dictionary<TypeRecord, int> ();
16 readonly Dictionary<ModuleRecord, int> moduleIds = new Dictionary<ModuleRecord, int> ();
18 unsafe void WriteInt32 (int intValue)
20 byte* b = (byte*)&intValue;
22 for (int i = 0; i < 4; i++)
23 stream.WriteByte (b [i]);
26 void WriteString (string str)
28 WriteInt32 (str.Length);
29 var buf = Encoding.UTF8.GetBytes (str);
30 stream.Write (buf, 0, buf.Length);
33 int AddModule (ModuleRecord m)
35 int mId;
36 if (moduleIds.TryGetValue (m, out mId))
37 return mId;
39 mId = id++;
40 moduleIds [m] = mId;
42 WriteRecord (RecordType.IMAGE, mId);
43 WriteString (m.Name);
44 WriteString (m.Mvid);
46 return mId;
49 int AddType (TypeRecord t)
51 int tId;
52 if (typeIds.TryGetValue (t, out tId))
53 return tId;
55 var moduleId = AddModule (t.Module);
57 int instId = -1;
58 if (t.GenericInst != null)
59 instId = AddGenericInstance (t.GenericInst);
61 tId = id++;
62 typeIds [t] = tId;
64 WriteRecord (RecordType.TYPE, tId);
65 stream.WriteByte ((byte)MonoTypeEnum.MONO_TYPE_CLASS);
66 WriteInt32 (moduleId);
67 WriteInt32 (instId);
68 WriteString (t.Name);
70 return tId;
73 int AddGenericInstance (GenericInstRecord gi)
75 // add the types first, before we start writing the GINST record
76 for (int i = 0; i < gi.Types.Length; i++)
77 AddType (gi.Types [i]);
79 var gId = id++;
81 WriteRecord (RecordType.GINST, gId);
82 WriteInt32 (gi.Types.Length);
84 for (int i = 0; i < gi.Types.Length; i++)
85 WriteInt32 (AddType (gi.Types [i]));
87 return gId;
90 void WriteMethod (MethodRecord m)
92 var typeId = AddType (m.Type);
94 int instId = -1;
95 if (m.GenericInst != null)
96 instId = AddGenericInstance (m.GenericInst);
98 WriteRecord (RecordType.METHOD, id++);
99 WriteInt32 (typeId);
100 WriteInt32 (instId);
101 WriteInt32 (m.ParamCount);
102 WriteString (m.Name);
103 WriteString (m.Signature);
106 void WriteRecord (RecordType rt, int value)
108 stream.WriteByte ((byte)rt);
109 WriteInt32 (value);
112 public void WriteAllData (Stream s, ProfileData pd)
114 stream = s;
115 data = pd;
117 var buf = Encoding.UTF8.GetBytes (MAGIC);
118 this.stream.Write (buf, 0, buf.Length);
120 WriteInt32 ((MAJOR_VERSION << 16) | MINOR_VERSION);
122 foreach (var m in data.Methods)
123 WriteMethod (m);
125 // make sure ew have all the types
126 // sometime the profile contain type, which is not referenced from the methods
127 foreach (var t in data.Types)
128 AddType (t);
130 // just to be complete, do not miss any module too
131 foreach (var module in data.Modules)
132 AddModule (module);
134 WriteRecord (RecordType.NONE, 0);