Skip to content

Commit 8d97964

Browse files
bkonyiCommit Queue
authored andcommitted
[ VM ] Move class name from heap profiler report callback to allocation callback
Moving the class name of the allocated object to the allocation callback gives more flexibility to the embedder, which may decide to track all sampled allocations, not just those that are still alive. TEST=Updated DartAPI_HeapSampling_* Change-Id: Iaae290e15b67ca3e47d72e1896fa6e32b2d4b081 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/297761 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent 96a346f commit 8d97964

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

runtime/include/dart_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,12 +1282,12 @@ DART_EXPORT void Dart_KillIsolate(Dart_Isolate isolate);
12821282
DART_EXPORT void Dart_NotifyIdle(int64_t deadline);
12831283

12841284
typedef void (*Dart_HeapSamplingReportCallback)(void* context,
1285-
const char* cls_name,
12861285
void* data);
12871286

12881287
typedef void* (*Dart_HeapSamplingCreateCallback)(
12891288
Dart_Isolate isolate,
12901289
Dart_IsolateGroup isolate_group,
1290+
const char* cls_name,
12911291
intptr_t allocation_size);
12921292
typedef void (*Dart_HeapSamplingDeleteCallback)(void* data);
12931293

runtime/vm/dart_api_impl_test.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10507,30 +10507,29 @@ TEST_CASE(DartAPI_UserTags) {
1050710507
#endif // !PRODUCT
1050810508

1050910509
#if !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
10510+
10511+
static void* last_allocation_context = nullptr;
10512+
static const char* last_allocation_cls = nullptr;
10513+
static intptr_t heap_samples = 0;
10514+
static const char* expected_allocation_cls = nullptr;
10515+
static bool found_allocation = false;
10516+
1051010517
static void* HeapSamplingCreate(Dart_Isolate isolate,
1051110518
Dart_IsolateGroup isolate_group,
10519+
const char* cls_name,
1051210520
intptr_t heap_size) {
10513-
return strdup("test data");
10521+
last_allocation_cls = cls_name;
10522+
return strdup(cls_name);
1051410523
}
1051510524

1051610525
static void HeapSamplingDelete(void* data) {
1051710526
free(data);
1051810527
}
1051910528

10520-
static void* last_allocation_context = nullptr;
10521-
static const char* last_allocation_cls = nullptr;
10522-
static void* last_allocation_data = nullptr;
10523-
static intptr_t heap_samples = 0;
10524-
static const char* expected_allocation_cls = nullptr;
10525-
static bool found_allocation = false;
10526-
1052710529
void HeapSamplingReport(void* context,
10528-
const char* cls_name,
1052910530
void* data) {
1053010531
last_allocation_context = context;
10531-
last_allocation_cls = cls_name;
10532-
last_allocation_data = data;
10533-
if (strcmp(cls_name, expected_allocation_cls) == 0) {
10532+
if (strcmp(reinterpret_cast<char*>(data), expected_allocation_cls) == 0) {
1053410533
found_allocation = true;
1053510534
}
1053610535
heap_samples++;
@@ -10542,7 +10541,6 @@ void ResetHeapSamplingState(const char* expected_cls = nullptr) {
1054210541
found_allocation = false;
1054310542
last_allocation_context = nullptr;
1054410543
last_allocation_cls = nullptr;
10545-
last_allocation_data = nullptr;
1054610544
}
1054710545

1054810546
// Threads won't pick up heap sampling profiler state changes until they
@@ -10596,7 +10594,6 @@ TEST_CASE(DartAPI_HeapSampling_UserDefinedClass) {
1059610594
EXPECT(heap_samples < 100000);
1059710595
EXPECT(last_allocation_context == context);
1059810596
EXPECT(found_allocation);
10599-
EXPECT_STREQ(reinterpret_cast<char*>(last_allocation_data), "test data");
1060010597
Dart_EnterIsolate(isolate);
1060110598
}
1060210599

runtime/vm/heap/sampler.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,15 @@ void HeapProfileSampler::HandleNewTLAB(intptr_t old_tlab_remaining_space,
197197
}
198198
}
199199

200-
void* HeapProfileSampler::InvokeCallbackForLastSample() {
200+
void* HeapProfileSampler::InvokeCallbackForLastSample(intptr_t cid) {
201201
ASSERT(enabled_);
202202
ASSERT(create_callback_ != nullptr);
203203
ReadRwLocker locker(thread_, lock_);
204+
ClassTable* table = IsolateGroup::Current()->class_table();
204205
void* result = create_callback_(
205206
reinterpret_cast<Dart_Isolate>(thread_->isolate()),
206207
reinterpret_cast<Dart_IsolateGroup>(thread_->isolate_group()),
207-
last_sample_size_);
208+
table->UserVisibleNameFor(cid), last_sample_size_);
208209
last_sample_size_ = kUninitialized;
209210
return result;
210211
}

runtime/vm/heap/sampler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class HeapProfileSampler {
118118
// allocations.
119119
void HandleNewTLAB(intptr_t old_tlab_remaining_space, bool is_first_tlab);
120120

121-
void* InvokeCallbackForLastSample();
121+
void* InvokeCallbackForLastSample(intptr_t cid);
122122

123123
bool HasOutstandingSample() const {
124124
return last_sample_size_ != kUninitialized;

runtime/vm/heap/weak_table.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,11 @@ void WeakTable::Forward(ObjectPointerVisitor* visitor) {
144144
void WeakTable::ReportSurvivingAllocations(
145145
Dart_HeapSamplingReportCallback callback,
146146
void* context) {
147-
ClassTable* table = IsolateGroup::Current()->class_table();
148147
MutexLocker ml(&mutex_);
149148
for (intptr_t i = 0; i < size_; i++) {
150149
if (IsValidEntryAtExclusive(i)) {
151-
ObjectPtr obj = static_cast<ObjectPtr>(data_[ObjectIndex(i)]);
152150
void* data = reinterpret_cast<void*>(data_[ValueIndex(i)]);
153-
callback(context, table->UserVisibleNameFor(obj->GetClassId()), data);
151+
callback(context, data);
154152
}
155153
}
156154
}

runtime/vm/object.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2833,7 +2833,7 @@ ObjectPtr Object::Allocate(intptr_t cls_id,
28332833
HeapProfileSampler& heap_sampler = thread->heap_sampler();
28342834
if (heap_sampler.HasOutstandingSample()) {
28352835
thread->IncrementNoCallbackScopeDepth();
2836-
void* data = heap_sampler.InvokeCallbackForLastSample();
2836+
void* data = heap_sampler.InvokeCallbackForLastSample(cls_id);
28372837
heap->SetHeapSamplingData(raw_obj, data);
28382838
thread->DecrementNoCallbackScopeDepth();
28392839
}

0 commit comments

Comments
 (0)