Skip to content

Commit

Permalink
[vm/timeline] Include callback data into Dart_TimelineRecorderEvent.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Commit-Queue: Slava Egorov <[email protected]>
  • Loading branch information
mraleph authored and Commit Queue committed May 25, 2023
1 parent 192d601 commit f86f412
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
8 changes: 7 additions & 1 deletion runtime/include/dart_tools_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions runtime/vm/timeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions runtime/vm/timeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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| ?
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion runtime/vm/timeline_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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(&params) == 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"
Expand All @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions runtime/vm/unit_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,10 @@ class RawTestCase : TestCaseBase {

class TestIsolateScope {
public:
TestIsolateScope() {
isolate_ = reinterpret_cast<Isolate*>(TestCase::CreateTestIsolate());
TestIsolateScope(void* isolate_group_data = nullptr,
void* isolate_data = nullptr) {
isolate_ = reinterpret_cast<Isolate*>(TestCase::CreateTestIsolate(
/*name=*/nullptr, isolate_group_data, isolate_data));
Dart_EnterScope(); // Create a Dart API scope for unit tests.
}
~TestIsolateScope() {
Expand Down

0 comments on commit f86f412

Please sign in to comment.