From f86f41226dd2feb0bdf334665df16f990f07a749 Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Thu, 25 May 2023 10:19:48 +0000 Subject: [PATCH] [vm/timeline] Include callback data into Dart_TimelineRecorderEvent. This simplifies embedders which otherwise need to maintain special mappings to find their isolate specific data structures. TEST=vm/cc/DartAPI_SetTimelineRecorderCallback Change-Id: If819437cad2e1bf3fe5ba50fe67d01e8bd992064 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304962 Reviewed-by: Ryan Macnak Commit-Queue: Slava Egorov --- runtime/include/dart_tools_api.h | 8 +++++++- runtime/vm/timeline.cc | 6 ++++++ runtime/vm/timeline.h | 6 ++++++ runtime/vm/timeline_test.cc | 15 ++++++++++++++- runtime/vm/unit_test.h | 6 ++++-- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/runtime/include/dart_tools_api.h b/runtime/include/dart_tools_api.h index 6136a416734..aab2fa54075 100644 --- a/runtime/include/dart_tools_api.h +++ b/runtime/include/dart_tools_api.h @@ -389,7 +389,7 @@ typedef struct { const char* value; } Dart_TimelineRecorderEvent_Argument; -#define DART_TIMELINE_RECORDER_CURRENT_VERSION (0x00000001) +#define DART_TIMELINE_RECORDER_CURRENT_VERSION (0x00000002) typedef struct { /* Set to DART_TIMELINE_RECORDER_CURRENT_VERSION */ @@ -416,6 +416,12 @@ typedef struct { * isolate group. */ Dart_IsolateGroupId isolate_group; + /* The callback data associated with the isolate if any. */ + void* isolate_data; + + /* The callback data associated with the isolate group if any. */ + void* isolate_group_data; + /* The name / label of the event. */ const char* label; diff --git a/runtime/vm/timeline.cc b/runtime/vm/timeline.cc index fa347f643b2..94da8fc2c9a 100644 --- a/runtime/vm/timeline.cc +++ b/runtime/vm/timeline.cc @@ -679,6 +679,10 @@ void TimelineEvent::Init(EventType event_type, const char* label) { auto isolate_group = thread != nullptr ? thread->isolate_group() : nullptr; isolate_id_ = (isolate != nullptr) ? isolate->main_port() : ILLEGAL_PORT; isolate_group_id_ = (isolate_group != nullptr) ? isolate_group->id() : 0; + isolate_data_ = + (isolate != nullptr) ? isolate->init_callback_data() : nullptr; + isolate_group_data_ = + (isolate_group != nullptr) ? isolate_group->embedder_data() : nullptr; label_ = label; arguments_.Free(); set_event_type(event_type); @@ -1929,6 +1933,8 @@ void TimelineEventEmbedderCallbackRecorder::OnEvent(TimelineEvent* event) { recorder_event.timestamp1_or_async_id = event->timestamp1(); recorder_event.isolate = event->isolate_id(); recorder_event.isolate_group = event->isolate_group_id(); + recorder_event.isolate_data = event->isolate_data(); + recorder_event.isolate_group_data = event->isolate_group_data(); recorder_event.label = event->label(); recorder_event.stream = event->stream()->name(); recorder_event.argument_count = event->GetNumArguments(); diff --git a/runtime/vm/timeline.h b/runtime/vm/timeline.h index 480910f93ce..7bb39f54cce 100644 --- a/runtime/vm/timeline.h +++ b/runtime/vm/timeline.h @@ -474,6 +474,10 @@ class TimelineEvent { uint64_t isolate_group_id() const { return isolate_group_id_; } + void* isolate_data() const { return isolate_data_; } + + void* isolate_group_data() const { return isolate_group_data_; } + const char* label() const { return label_; } // Does this duration end before |micros| ? @@ -597,6 +601,8 @@ class TimelineEvent { ThreadId thread_; Dart_Port isolate_id_; uint64_t isolate_group_id_; + void* isolate_data_; + void* isolate_group_data_; TimelineEvent* next_; friend class TimelineEventRecorder; diff --git a/runtime/vm/timeline_test.cc b/runtime/vm/timeline_test.cc index dc13f1ca8e0..828775c5b66 100644 --- a/runtime/vm/timeline_test.cc +++ b/runtime/vm/timeline_test.cc @@ -448,6 +448,8 @@ static Dart_Port expected_isolate; static Dart_IsolateGroupId expected_isolate_group; static bool saw_begin; static bool saw_end; +static void* expected_isolate_data; +static void* expected_isolate_group_data; static void TestTimelineRecorderCallback(Dart_TimelineRecorderEvent* event) { EXPECT_EQ(DART_TIMELINE_RECORDER_CURRENT_VERSION, event->version); @@ -458,6 +460,8 @@ static void TestTimelineRecorderCallback(Dart_TimelineRecorderEvent* event) { EXPECT_NE(0, event->timestamp0); EXPECT_EQ(expected_isolate, event->isolate); EXPECT_EQ(expected_isolate_group, event->isolate_group); + EXPECT_EQ(expected_isolate_data, event->isolate_data); + EXPECT_EQ(expected_isolate_group_data, event->isolate_group_data); EXPECT_STREQ("Dart", event->stream); EXPECT_EQ(1, event->argument_count); EXPECT_STREQ("Dart Arguments", event->arguments[0].name); @@ -470,6 +474,8 @@ static void TestTimelineRecorderCallback(Dart_TimelineRecorderEvent* event) { EXPECT_NE(0, event->timestamp0); EXPECT_EQ(expected_isolate, event->isolate); EXPECT_EQ(expected_isolate_group, event->isolate_group); + EXPECT_EQ(expected_isolate_data, event->isolate_data); + EXPECT_EQ(expected_isolate_group_data, event->isolate_group_data); EXPECT_STREQ("Dart", event->stream); EXPECT_EQ(1, event->argument_count); EXPECT_STREQ("Dart Arguments", event->arguments[0].name); @@ -498,9 +504,13 @@ UNIT_TEST_CASE(DartAPI_SetTimelineRecorderCallback) { params.cleanup_group = TesterState::group_cleanup_callback; params.start_kernel_isolate = true; + int64_t isolate_data = 0; + EXPECT(Dart_Initialize(¶ms) == nullptr); { - TestIsolateScope scope; + // Note: run_vm_tests will create and attach an instance of + // bin::IsolateGroupData to the newly created isolate group. + TestIsolateScope scope(/*isolate_group_data=*/nullptr, &isolate_data); const char* kScriptChars = "import 'dart:developer';\n" "main() {\n" @@ -514,6 +524,9 @@ UNIT_TEST_CASE(DartAPI_SetTimelineRecorderCallback) { EXPECT_NE(ILLEGAL_PORT, expected_isolate); expected_isolate_group = Dart_CurrentIsolateGroupId(); EXPECT_NE(ILLEGAL_PORT, expected_isolate_group); + expected_isolate_data = &isolate_data; + EXPECT_EQ(expected_isolate_data, Dart_CurrentIsolateData()); + expected_isolate_group_data = Dart_CurrentIsolateGroupData(); saw_begin = false; saw_end = false; diff --git a/runtime/vm/unit_test.h b/runtime/vm/unit_test.h index 562b19880d3..0e78d65ca93 100644 --- a/runtime/vm/unit_test.h +++ b/runtime/vm/unit_test.h @@ -464,8 +464,10 @@ class RawTestCase : TestCaseBase { class TestIsolateScope { public: - TestIsolateScope() { - isolate_ = reinterpret_cast(TestCase::CreateTestIsolate()); + TestIsolateScope(void* isolate_group_data = nullptr, + void* isolate_data = nullptr) { + isolate_ = reinterpret_cast(TestCase::CreateTestIsolate( + /*name=*/nullptr, isolate_group_data, isolate_data)); Dart_EnterScope(); // Create a Dart API scope for unit tests. } ~TestIsolateScope() {