1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_VM_COMPILER_AOT_PRECOMPILER_TRACER_H_ |
6 | #define RUNTIME_VM_COMPILER_AOT_PRECOMPILER_TRACER_H_ |
7 | |
8 | #if defined(DART_PRECOMPILED_RUNTIME) |
9 | #error "AOT runtime should not use compiler sources (including header files)" |
10 | #endif // defined(DART_PRECOMPILED_RUNTIME) |
11 | |
12 | #include "vm/allocation.h" |
13 | #include "vm/hash_table.h" |
14 | #include "vm/symbols.h" |
15 | |
16 | namespace dart { |
17 | |
18 | // Forward declarations. |
19 | class Precompiler; |
20 | |
21 | #if defined(DART_PRECOMPILER) |
22 | // Tracer which produces machine readable precompiler tracer, which captures |
23 | // information about all compiled functions and dependencies between them. |
24 | // See pkg/vm_snapshot_analysis/README.md for the definition of the |
25 | // format. |
26 | class PrecompilerTracer : public ZoneAllocated { |
27 | public: |
28 | static PrecompilerTracer* StartTracingIfRequested(Precompiler* precompiler); |
29 | |
30 | void Finalize(); |
31 | |
32 | void WriteEntityRef(const Object& field) { |
33 | Write("%" Pd "," , InternEntity(field)); |
34 | } |
35 | |
36 | void WriteFieldRef(const Field& field) { WriteEntityRef(field); } |
37 | |
38 | void WriteFunctionRef(const Function& function) { WriteEntityRef(function); } |
39 | |
40 | void WriteSelectorRef(const String& selector) { |
41 | Write("\"S\",%" Pd "," , InternString(selector)); |
42 | } |
43 | |
44 | void WriteTableSelectorRef(intptr_t id) { Write("\"T\",%" Pd "," , id); } |
45 | |
46 | void WriteClassInstantiationRef(const Class& cls) { WriteEntityRef(cls); } |
47 | |
48 | void WriteCompileFunctionEvent(const Function& function) { |
49 | Write("\"C\"," ); |
50 | WriteEntityRef(function); |
51 | } |
52 | |
53 | private: |
54 | struct CString { |
55 | const char* str; |
56 | const intptr_t length; |
57 | intptr_t hash; |
58 | }; |
59 | |
60 | struct StringTableTraits { |
61 | static bool ReportStats() { return false; } |
62 | static const char* Name() { return "StringTableTraits" ; } |
63 | |
64 | static bool IsMatch(const Object& a, const Object& b) { |
65 | return String::Cast(a).Equals(String::Cast(b)); |
66 | } |
67 | |
68 | static bool IsMatch(const CString& cstr, const Object& other) { |
69 | const String& other_str = String::Cast(other); |
70 | if (other_str.Hash() != cstr.hash) { |
71 | return false; |
72 | } |
73 | |
74 | if (other_str.Length() != cstr.length) { |
75 | return false; |
76 | } |
77 | |
78 | return other_str.Equals(cstr.str); |
79 | } |
80 | |
81 | static uword Hash(const CString& cstr) { return cstr.hash; } |
82 | |
83 | static uword Hash(const Object& obj) { return String::Cast(obj).Hash(); } |
84 | |
85 | static ObjectPtr NewKey(const CString& cstr) { |
86 | return Symbols::New(Thread::Current(), cstr.str); |
87 | } |
88 | }; |
89 | |
90 | struct EntityTableTraits { |
91 | static bool ReportStats() { return false; } |
92 | static const char* Name() { return "EntityTableTraits" ; } |
93 | |
94 | static bool IsMatch(const Object& a, const Object& b) { |
95 | return a.raw() == b.raw(); |
96 | } |
97 | |
98 | static uword Hash(const Object& obj) { |
99 | if (obj.IsFunction()) { |
100 | return Function::Cast(obj).Hash(); |
101 | } else if (obj.IsClass()) { |
102 | return String::HashRawSymbol(Class::Cast(obj).Name()); |
103 | } else if (obj.IsField()) { |
104 | return String::HashRawSymbol(Field::Cast(obj).name()); |
105 | } |
106 | return obj.GetClassId(); |
107 | } |
108 | }; |
109 | |
110 | using StringTable = UnorderedHashMap<StringTableTraits>; |
111 | using EntityTable = UnorderedHashMap<EntityTableTraits>; |
112 | |
113 | PrecompilerTracer(Precompiler* precompiler, void* stream); |
114 | |
115 | intptr_t InternString(const CString& cstr); |
116 | intptr_t InternString(const String& str); |
117 | intptr_t InternEntity(const Object& obj); |
118 | |
119 | void Write(const char* format, ...) PRINTF_ATTRIBUTE(2, 3) { |
120 | va_list va; |
121 | va_start(va, format); |
122 | const char* line = OS::VSCreate(zone_, format, va); |
123 | Dart::file_write_callback()(line, strlen(line), stream_); |
124 | va_end(va); |
125 | } |
126 | |
127 | CString NameForTrace(const Function& f); |
128 | |
129 | void WriteEntityTable(); |
130 | void WriteStringTable(); |
131 | |
132 | Zone* zone_; |
133 | Precompiler* precompiler_; |
134 | void* stream_; |
135 | StringTable strings_; |
136 | EntityTable entities_; |
137 | |
138 | Object& object_; |
139 | Class& cls_; |
140 | }; |
141 | #endif // defined(DART_PRECOMPILER) |
142 | |
143 | } // namespace dart |
144 | |
145 | #endif // RUNTIME_VM_COMPILER_AOT_PRECOMPILER_TRACER_H_ |
146 | |