-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathclass_table.cc
423 lines (372 loc) · 13.1 KB
/
class_table.cc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/class_table.h"
#include <memory>
#include "platform/atomic.h"
#include "vm/flags.h"
#include "vm/growable_array.h"
#include "vm/heap/heap.h"
#include "vm/object.h"
#include "vm/object_graph.h"
#include "vm/raw_object.h"
#include "vm/visitor.h"
namespace dart {
DEFINE_FLAG(bool, print_class_table, false, "Print initial class table.");
ClassTable::ClassTable(ClassTableAllocator* allocator)
: allocator_(allocator),
classes_(allocator),
top_level_classes_(allocator) {
if (Dart::vm_isolate() == nullptr) {
classes_.SetNumCidsAndCapacity(kNumPredefinedCids, kInitialCapacity);
} else {
// Duplicate the class table from the VM isolate.
ClassTable* vm_class_table = Dart::vm_isolate_group()->class_table();
classes_.SetNumCidsAndCapacity(kNumPredefinedCids,
vm_class_table->classes_.capacity());
const auto copy_info_for_cid = [&](intptr_t cid) {
classes_.At<kClassIndex>(cid) = vm_class_table->At(cid);
classes_.At<kSizeIndex>(cid) = vm_class_table->SizeAt(cid);
};
// The following cids don't have a corresponding class object in Dart code.
// We therefore need to initialize them eagerly.
COMPILE_ASSERT(kFirstInternalOnlyCid == kObjectCid + 1);
for (intptr_t i = kObjectCid; i <= kLastInternalOnlyCid; i++) {
copy_info_for_cid(i);
}
copy_info_for_cid(kTypeArgumentsCid);
copy_info_for_cid(kFreeListElement);
copy_info_for_cid(kForwardingCorpse);
copy_info_for_cid(kDynamicCid);
copy_info_for_cid(kVoidCid);
}
UpdateCachedAllocationTracingStateTablePointer();
}
ClassTable::~ClassTable() {
#if !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
for (intptr_t i = 1; i < classes_.num_cids(); i++) {
const char* name = UserVisibleNameFor(i);
if (name != nullptr) {
free(const_cast<char*>(name));
}
}
#endif // !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
}
void ClassTable::Register(const Class& cls) {
ASSERT(Thread::Current()->IsDartMutatorThread());
ASSERT(cls.id() == kIllegalCid || cls.id() < kNumPredefinedCids);
bool did_grow = false;
const classid_t cid =
cls.id() != kIllegalCid ? cls.id() : classes_.AddRow(&did_grow);
ASSERT(!IsTopLevelCid(cid));
const intptr_t instance_size =
cls.is_abstract() ? 0 : Class::host_instance_size(cls.ptr());
cls.set_id(cid);
classes_.At<kClassIndex>(cid) = cls.ptr();
classes_.At<kSizeIndex>(cid) = static_cast<int32_t>(instance_size);
#if !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
classes_.At<kClassNameIndex>(cid) = nullptr;
#endif // !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
if (did_grow) {
IsolateGroup::Current()->set_cached_class_table_table(
classes_.GetColumn<kClassIndex>());
UpdateCachedAllocationTracingStateTablePointer();
} else {
// GCC warns that TSAN doesn't understand thread fences.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wtsan"
#endif
std::atomic_thread_fence(std::memory_order_release);
}
}
void ClassTable::RegisterTopLevel(const Class& cls) {
ASSERT(Thread::Current()->IsDartMutatorThread());
ASSERT(cls.id() == kIllegalCid);
bool did_grow = false;
const intptr_t index = top_level_classes_.AddRow(&did_grow);
cls.set_id(ClassTable::CidFromTopLevelIndex(index));
top_level_classes_.At<kClassIndex>(index) = cls.ptr();
}
void ClassTable::AllocateIndex(intptr_t index) {
bool did_grow = false;
if (IsTopLevelCid(index)) {
top_level_classes_.AllocateIndex(IndexFromTopLevelCid(index), &did_grow);
return;
}
classes_.AllocateIndex(index, &did_grow);
if (did_grow) {
IsolateGroup::Current()->set_cached_class_table_table(table());
UpdateCachedAllocationTracingStateTablePointer();
}
}
void ClassTable::UnregisterTopLevel(intptr_t cid) {
ASSERT(IsTopLevelCid(cid));
const intptr_t tlc_index = IndexFromTopLevelCid(cid);
top_level_classes_.At<kClassIndex>(tlc_index) = nullptr;
}
void ClassTable::Remap(intptr_t* old_to_new_cid) {
ASSERT(Thread::Current()->OwnsSafepoint());
classes_.Remap(old_to_new_cid);
}
void ClassTable::VisitObjectPointers(ObjectPointerVisitor* visitor) {
ASSERT(visitor != nullptr);
visitor->set_gc_root_type("class table");
const auto visit = [&](ClassPtr* table, intptr_t num_cids) {
if (num_cids == 0) {
return;
}
ObjectPtr* from = reinterpret_cast<ObjectPtr*>(&table[0]);
ObjectPtr* to = reinterpret_cast<ObjectPtr*>(&table[num_cids - 1]);
visitor->VisitPointers(from, to);
};
visit(classes_.GetColumn<kClassIndex>(), classes_.num_cids());
visit(top_level_classes_.GetColumn<kClassIndex>(),
top_level_classes_.num_cids());
visitor->clear_gc_root_type();
}
void ClassTable::CopySizesFromClassObjects() {
ASSERT(kIllegalCid == 0);
for (intptr_t i = 1; i < classes_.num_cids(); i++) {
UpdateClassSize(i, classes_.At<kClassIndex>(i));
}
}
void ClassTable::SetAt(intptr_t cid, ClassPtr raw_cls) {
if (IsTopLevelCid(cid)) {
top_level_classes_.At<kClassIndex>(IndexFromTopLevelCid(cid)) = raw_cls;
return;
}
// This is called by snapshot reader and class finalizer.
UpdateClassSize(cid, raw_cls);
classes_.At<kClassIndex>(cid) = raw_cls;
}
void ClassTable::UpdateClassSize(intptr_t cid, ClassPtr raw_cls) {
ASSERT(IsolateGroup::Current()->program_lock()->IsCurrentThreadWriter());
ASSERT(!IsTopLevelCid(cid)); // "top-level" classes don't get instantiated
const intptr_t size =
raw_cls == nullptr ? 0 : Class::host_instance_size(raw_cls);
classes_.At<kSizeIndex>(cid) = static_cast<int32_t>(size);
}
void ClassTable::Validate() {
Class& cls = Class::Handle();
for (intptr_t cid = kNumPredefinedCids; cid < classes_.num_cids(); cid++) {
// Some of the class table entries maybe nullptr as we create some
// top level classes but do not add them to the list of anonymous
// classes in a library if there are no top level fields or functions.
// Since there are no references to these top level classes they are
// not written into a full snapshot and will not be recreated when
// we read back the full snapshot. These class slots end up with nullptr
// entries.
if (HasValidClassAt(cid)) {
cls = At(cid);
ASSERT(cls.IsClass());
#if defined(DART_PRECOMPILER)
// Precompiler can drop classes and set their id() to kIllegalCid.
// It still leaves them in the class table so dropped program
// structure could still be accessed while writing debug info.
ASSERT((cls.id() == cid) || (cls.id() == kIllegalCid));
#else
ASSERT(cls.id() == cid);
#endif // defined(DART_PRECOMPILER)
}
}
}
void ClassTable::Print() {
Class& cls = Class::Handle();
String& name = String::Handle();
for (intptr_t i = 1; i < classes_.num_cids(); i++) {
if (!HasValidClassAt(i)) {
continue;
}
cls = At(i);
if (cls.ptr() != nullptr) {
name = cls.Name();
OS::PrintErr("%" Pd ": %s\n", i, name.ToCString());
}
}
}
#if defined(DART_PRECOMPILER)
void ClassTable::PrintObjectLayout(const char* filename) {
Class& cls = Class::Handle();
Array& fields = Array::Handle();
Field& field = Field::Handle();
JSONWriter js;
js.OpenArray();
for (intptr_t i = ClassId::kObjectCid; i < classes_.num_cids(); i++) {
if (!HasValidClassAt(i)) {
continue;
}
cls = At(i);
ASSERT(!cls.IsNull());
ASSERT(cls.id() != kIllegalCid);
ASSERT(cls.is_finalized()); // Precompiler already finalized all classes.
ASSERT(!cls.IsTopLevel());
js.OpenObject();
js.PrintProperty("class", cls.UserVisibleNameCString());
js.PrintProperty("size", cls.target_instance_size());
js.OpenArray("fields");
fields = cls.fields();
if (!fields.IsNull()) {
for (intptr_t i = 0, n = fields.Length(); i < n; ++i) {
field ^= fields.At(i);
js.OpenObject();
js.PrintProperty("field", field.UserVisibleNameCString());
if (field.is_static()) {
js.PrintPropertyBool("static", true);
} else {
js.PrintProperty("offset", field.TargetOffset());
}
js.CloseObject();
}
}
js.CloseArray();
js.CloseObject();
}
js.CloseArray();
auto file_open = Dart::file_open_callback();
auto file_write = Dart::file_write_callback();
auto file_close = Dart::file_close_callback();
if ((file_open == nullptr) || (file_write == nullptr) ||
(file_close == nullptr)) {
OS::PrintErr("warning: Could not access file callbacks.");
return;
}
void* file = file_open(filename, /*write=*/true);
if (file == nullptr) {
OS::PrintErr("warning: Failed to write object layout: %s\n", filename);
return;
}
char* output = nullptr;
intptr_t output_length = 0;
js.Steal(&output, &output_length);
file_write(output, output_length, file);
free(output);
file_close(file);
}
#endif // defined(DART_PRECOMPILER)
#if !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
void ClassTable::PopulateUserVisibleNames() {
Class& cls = Class::Handle();
for (intptr_t i = 0; i < classes_.num_cids(); ++i) {
if (HasValidClassAt(i) && UserVisibleNameFor(i) == nullptr) {
cls = At(i);
cls.SetUserVisibleNameInClassTable();
}
}
}
#endif // !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
#if !defined(PRODUCT)
void ClassTable::PrintToJSONObject(JSONObject* object) {
Class& cls = Class::Handle();
object->AddProperty("type", "ClassList");
{
JSONArray members(object, "classes");
for (intptr_t i = ClassId::kObjectCid; i < classes_.num_cids(); i++) {
if (HasValidClassAt(i)) {
cls = At(i);
members.AddValue(cls);
}
}
}
}
void ClassTable::AllocationProfilePrintJSON(JSONStream* stream, bool internal) {
Isolate* isolate = Isolate::Current();
ASSERT(isolate != nullptr);
auto isolate_group = isolate->group();
Heap* heap = isolate_group->heap();
ASSERT(heap != nullptr);
JSONObject obj(stream);
obj.AddProperty("type", "AllocationProfile");
if (isolate_group->last_allocationprofile_accumulator_reset_timestamp() !=
0) {
obj.AddPropertyF(
"dateLastAccumulatorReset", "%" Pd64 "",
isolate_group->last_allocationprofile_accumulator_reset_timestamp());
}
if (isolate_group->last_allocationprofile_gc_timestamp() != 0) {
obj.AddPropertyF("dateLastServiceGC", "%" Pd64 "",
isolate_group->last_allocationprofile_gc_timestamp());
}
if (internal) {
JSONObject heaps(&obj, "_heaps");
{
heap->PrintToJSONObject(Heap::kNew, &heaps);
}
{
heap->PrintToJSONObject(Heap::kOld, &heaps);
}
}
{
JSONObject memory(&obj, "memoryUsage");
{
heap->PrintMemoryUsageJSON(&memory);
}
}
Thread* thread = Thread::Current();
CountObjectsVisitor visitor(thread, NumCids());
{
HeapIterationScope iter(thread);
iter.IterateObjects(&visitor);
isolate->group()->VisitWeakPersistentHandles(&visitor);
}
{
JSONArray arr(&obj, "members");
Class& cls = Class::Handle();
for (intptr_t i = 3; i < classes_.num_cids(); i++) {
if (!HasValidClassAt(i)) continue;
cls = At(i);
if (cls.IsNull()) continue;
JSONObject obj(&arr);
obj.AddProperty("type", "ClassHeapStats");
obj.AddProperty("class", cls);
intptr_t count = visitor.new_count_[i] + visitor.old_count_[i];
intptr_t size = visitor.new_size_[i] + visitor.old_size_[i];
obj.AddProperty64("instancesAccumulated", count);
obj.AddProperty64("accumulatedSize", size);
obj.AddProperty64("instancesCurrent", count);
obj.AddProperty64("bytesCurrent", size);
if (internal) {
{
JSONArray new_stats(&obj, "_new");
new_stats.AddValue(visitor.new_count_[i]);
new_stats.AddValue(visitor.new_size_[i]);
new_stats.AddValue(visitor.new_external_size_[i]);
}
{
JSONArray old_stats(&obj, "_old");
old_stats.AddValue(visitor.old_count_[i]);
old_stats.AddValue(visitor.old_size_[i]);
old_stats.AddValue(visitor.old_external_size_[i]);
}
}
}
}
}
#endif // !PRODUCT
ClassTableAllocator::ClassTableAllocator()
: pending_freed_(new MallocGrowableArray<std::pair<void*, Deleter>>()) {}
ClassTableAllocator::~ClassTableAllocator() {
FreePending();
delete pending_freed_;
}
void ClassTableAllocator::Free(ClassTable* ptr) {
if (ptr != nullptr) {
pending_freed_->Add(std::make_pair(
ptr, [](void* ptr) { delete static_cast<ClassTable*>(ptr); }));
}
}
void ClassTableAllocator::Free(void* ptr) {
if (ptr != nullptr) {
pending_freed_->Add(std::make_pair(ptr, nullptr));
}
}
void ClassTableAllocator::FreePending() {
while (!pending_freed_->is_empty()) {
auto [ptr, deleter] = pending_freed_->RemoveLast();
if (deleter == nullptr) {
free(ptr);
} else {
deleter(ptr);
}
}
}
} // namespace dart