Flutter Engine
The Flutter Engine
precompiler_tracer.h
Go to the documentation of this file.
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
16namespace dart {
17
18// Forward declarations.
19class 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.
26class 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 uword 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.ptr() == b.ptr();
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 Class::Cast(obj).Hash();
103 } else if (obj.IsField()) {
104 return Field::Cast(obj).Hash();
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 buffer_.VPrintf(format, va);
123 va_end(va);
124 }
125
126 CString NameForTrace(const Function& f);
127
128 void WriteEntityTable();
129 void WriteStringTable();
130
131 Zone* zone_;
132 Precompiler* precompiler_;
133 TextBuffer buffer_;
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_
static uint32_t hash(const SkShaderBase::GradientInfo &v)
static StringPtr New(Thread *thread, const char *cstr)
Definition: symbols.h:723
static Thread * Current()
Definition: thread.h:362
static bool b
struct MyStruct a[10]
uint32_t uint32_t * format
Dart_NativeFunction function
Definition: fuchsia.cc:51
size_t length
ImplicitString Name
Definition: DMSrcSink.h:38
va_start(args, format)
va_end(args)
Definition: dart_vm.cc:33
uintptr_t uword
Definition: globals.h:501
static uint32_t Hash(uint32_t key)
Definition: hashmap_test.cc:65
#define Pd
Definition: globals.h:408
#define PRINTF_ATTRIBUTE(string_index, first_to_check)
Definition: globals.h:697