V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
debug-type-profile.cc
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/debug/debug-type-profile.h"
6 
7 #include "src/feedback-vector.h"
8 #include "src/isolate.h"
9 #include "src/objects-inl.h"
10 #include "src/objects.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 std::unique_ptr<TypeProfile> TypeProfile::Collect(Isolate* isolate) {
16  std::unique_ptr<TypeProfile> result(new TypeProfile());
17 
18  // Feedback vectors are already listed to prevent losing them to GC.
19  DCHECK(isolate->factory()
20  ->feedback_vectors_for_profiling_tools()
21  ->IsArrayList());
22  Handle<ArrayList> list = Handle<ArrayList>::cast(
23  isolate->factory()->feedback_vectors_for_profiling_tools());
24 
25  Script::Iterator scripts(isolate);
26 
27  while (Script* script = scripts.Next()) {
28  if (!script->IsUserJavaScript()) {
29  continue;
30  }
31 
32  Handle<Script> script_handle(script, isolate);
33 
34  TypeProfileScript type_profile_script(script_handle);
35  std::vector<TypeProfileEntry>* entries = &type_profile_script.entries;
36 
37  // TODO(franzih): Sort the vectors by script first instead of iterating
38  // the list multiple times.
39  for (int i = 0; i < list->Length(); i++) {
40  FeedbackVector* vector = FeedbackVector::cast(list->Get(i));
41  SharedFunctionInfo* info = vector->shared_function_info();
42  DCHECK(info->IsSubjectToDebugging());
43 
44  // Match vectors with script.
45  if (script != info->script()) {
46  continue;
47  }
48  if (info->feedback_metadata()->is_empty() ||
49  !info->feedback_metadata()->HasTypeProfileSlot()) {
50  continue;
51  }
52  FeedbackSlot slot = vector->GetTypeProfileSlot();
53  FeedbackNexus nexus(vector, slot);
54  Handle<String> name(info->DebugName(), isolate);
55  std::vector<int> source_positions = nexus.GetSourcePositions();
56  for (int position : source_positions) {
57  DCHECK_GE(position, 0);
58  entries->emplace_back(position, nexus.GetTypesForSourcePositions(
59  static_cast<uint32_t>(position)));
60  }
61 
62  // Releases type profile data collected so far.
63  nexus.ResetTypeProfile();
64  }
65  if (!entries->empty()) {
66  result->emplace_back(type_profile_script);
67  }
68  }
69  return result;
70 }
71 
72 void TypeProfile::SelectMode(Isolate* isolate, debug::TypeProfile::Mode mode) {
73  HandleScope handle_scope(isolate);
74 
75  if (mode == debug::TypeProfile::Mode::kNone) {
76  if (!isolate->factory()
77  ->feedback_vectors_for_profiling_tools()
78  ->IsUndefined(isolate)) {
79  // Release type profile data collected so far.
80 
81  // Feedback vectors are already listed to prevent losing them to GC.
82  DCHECK(isolate->factory()
83  ->feedback_vectors_for_profiling_tools()
84  ->IsArrayList());
85  Handle<ArrayList> list = Handle<ArrayList>::cast(
86  isolate->factory()->feedback_vectors_for_profiling_tools());
87 
88  for (int i = 0; i < list->Length(); i++) {
89  FeedbackVector* vector = FeedbackVector::cast(list->Get(i));
90  SharedFunctionInfo* info = vector->shared_function_info();
91  DCHECK(info->IsSubjectToDebugging());
92  if (info->feedback_metadata()->HasTypeProfileSlot()) {
93  FeedbackSlot slot = vector->GetTypeProfileSlot();
94  FeedbackNexus nexus(vector, slot);
95  nexus.ResetTypeProfile();
96  }
97  }
98 
99  // Delete the feedback vectors from the list if they're not used by code
100  // coverage.
101  if (isolate->is_best_effort_code_coverage()) {
102  isolate->SetFeedbackVectorsForProfilingTools(
103  ReadOnlyRoots(isolate).undefined_value());
104  }
105  }
106  } else {
107  DCHECK_EQ(debug::TypeProfile::Mode::kCollect, mode);
108  isolate->MaybeInitializeVectorListFromHeap();
109  }
110  isolate->set_type_profile_mode(mode);
111 }
112 
113 } // namespace internal
114 } // namespace v8
Definition: libplatform.h:13