[vm, observatory] Respect requested tag order even when --profile-vm=false.

Blame time waiting for service or kernel isolate startup against LoadWait instead of VM.

In the table profile view, include VM tags. Because of the flattening nature of this view, it is less intrusive than in the tree view.

Bug: https://github.com/dart-lang/sdk/issues/35394
Change-Id: If13c57daa96cb69c5e5a009dfd973b491b694d04
Reviewed-on: https://dart-review.googlesource.com/c/87380
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
diff --git a/runtime/observatory/lib/src/elements/cpu_profile_table.dart b/runtime/observatory/lib/src/elements/cpu_profile_table.dart
index a78e2a4..b1a9160 100644
--- a/runtime/observatory/lib/src/elements/cpu_profile_table.dart
+++ b/runtime/observatory/lib/src/elements/cpu_profile_table.dart
@@ -411,7 +411,7 @@
 
   Future _request({bool clear: false, bool forceFetch: false}) async {
     _progress = null;
-    _progressStream = _profiles.get(isolate, M.SampleProfileTag.none,
+    _progressStream = _profiles.get(isolate, M.SampleProfileTag.vmOnly,
         clear: clear, forceFetch: forceFetch);
     _r.dirty();
     _progress = (await _progressStream.first).progress;
diff --git a/runtime/vm/kernel_isolate.cc b/runtime/vm/kernel_isolate.cc
index 09c9575..d629c76 100644
--- a/runtime/vm/kernel_isolate.cc
+++ b/runtime/vm/kernel_isolate.cc
@@ -295,6 +295,7 @@
 }
 
 Dart_Port KernelIsolate::WaitForKernelPort() {
+  VMTagScope tagScope(Thread::Current(), VMTag::kLoadWaitTagId);
   MonitorLocker ml(monitor_);
   while (initializing_ && (kernel_port_ == ILLEGAL_PORT)) {
     ml.Wait();
diff --git a/runtime/vm/profiler_service.cc b/runtime/vm/profiler_service.cc
index 27eb4fe..4251002 100644
--- a/runtime/vm/profiler_service.cc
+++ b/runtime/vm/profiler_service.cc
@@ -1922,36 +1922,26 @@
                                   uword user_tag,
                                   ProfileCodeTrieNode* current,
                                   ProcessedSample* sample) {
-    if (FLAG_profile_vm) {
-      // None.
-      if (tag_order() == Profile::kNoTags) {
-        return current;
-      }
-      // User first.
-      if ((tag_order() == Profile::kUserVM) ||
-          (tag_order() == Profile::kUser)) {
-        current = AppendUserTag(user_tag, current, sample);
-        // Only user.
-        if (tag_order() == Profile::kUser) {
-          return current;
-        }
-        return AppendVMTags(vm_tag, current, sample);
-      }
-      // VM first.
-      ASSERT((tag_order() == Profile::kVMUser) ||
-             (tag_order() == Profile::kVM));
-      current = AppendVMTags(vm_tag, current, sample);
-      // Only VM.
-      if (tag_order() == Profile::kVM) {
-        return current;
-      }
-      return AppendUserTag(user_tag, current, sample);
-    }
-
+    // None.
     if (tag_order() == Profile::kNoTags) {
       return current;
     }
-
+    // User first.
+    if ((tag_order() == Profile::kUserVM) || (tag_order() == Profile::kUser)) {
+      current = AppendUserTag(user_tag, current, sample);
+      // Only user.
+      if (tag_order() == Profile::kUser) {
+        return current;
+      }
+      return AppendVMTags(vm_tag, current, sample);
+    }
+    // VM first.
+    ASSERT((tag_order() == Profile::kVMUser) || (tag_order() == Profile::kVM));
+    current = AppendVMTags(vm_tag, current, sample);
+    // Only VM.
+    if (tag_order() == Profile::kVM) {
+      return current;
+    }
     return AppendUserTag(user_tag, current, sample);
   }
 
@@ -2081,36 +2071,26 @@
                                       uword user_tag,
                                       ProfileFunctionTrieNode* current,
                                       ProcessedSample* sample) {
-    if (FLAG_profile_vm) {
-      // None.
-      if (tag_order() == Profile::kNoTags) {
-        return current;
-      }
-      // User first.
-      if ((tag_order() == Profile::kUserVM) ||
-          (tag_order() == Profile::kUser)) {
-        current = AppendUserTag(user_tag, current, sample);
-        // Only user.
-        if (tag_order() == Profile::kUser) {
-          return current;
-        }
-        return AppendVMTags(vm_tag, current, sample);
-      }
-      // VM first.
-      ASSERT((tag_order() == Profile::kVMUser) ||
-             (tag_order() == Profile::kVM));
-      current = AppendVMTags(vm_tag, current, sample);
-      // Only VM.
-      if (tag_order() == Profile::kVM) {
-        return current;
-      }
-      return AppendUserTag(user_tag, current, sample);
-    }
-
+    // None.
     if (tag_order() == Profile::kNoTags) {
       return current;
     }
-
+    // User first.
+    if ((tag_order() == Profile::kUserVM) || (tag_order() == Profile::kUser)) {
+      current = AppendUserTag(user_tag, current, sample);
+      // Only user.
+      if (tag_order() == Profile::kUser) {
+        return current;
+      }
+      return AppendVMTags(vm_tag, current, sample);
+    }
+    // VM first.
+    ASSERT((tag_order() == Profile::kVMUser) || (tag_order() == Profile::kVM));
+    current = AppendVMTags(vm_tag, current, sample);
+    // Only VM.
+    if (tag_order() == Profile::kVM) {
+      return current;
+    }
     return AppendUserTag(user_tag, current, sample);
   }
 
diff --git a/runtime/vm/service_isolate.cc b/runtime/vm/service_isolate.cc
index 8eddb65..8d13ec6 100644
--- a/runtime/vm/service_isolate.cc
+++ b/runtime/vm/service_isolate.cc
@@ -143,6 +143,7 @@
 }
 
 Dart_Port ServiceIsolate::WaitForLoadPort() {
+  VMTagScope tagScope(Thread::Current(), VMTag::kLoadWaitTagId);
   MonitorLocker ml(monitor_);
   while (initializing_ && (load_port_ == ILLEGAL_PORT)) {
     ml.Wait();