Line data Source code
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 50 : std::unique_ptr<TypeProfile> TypeProfile::Collect(Isolate* isolate) {
16 50 : 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 50 : isolate->factory()->feedback_vectors_for_profiling_tools());
24 :
25 50 : Script::Iterator scripts(isolate);
26 :
27 650 : for (Script script = scripts.Next(); !script.is_null();
28 : script = scripts.Next()) {
29 275 : if (!script->IsUserJavaScript()) {
30 150 : continue;
31 : }
32 :
33 : Handle<Script> script_handle(script, isolate);
34 :
35 : TypeProfileScript type_profile_script(script_handle);
36 : std::vector<TypeProfileEntry>* entries = &type_profile_script.entries;
37 :
38 : // TODO(franzih): Sort the vectors by script first instead of iterating
39 : // the list multiple times.
40 2680 : for (int i = 0; i < list->Length(); i++) {
41 2430 : FeedbackVector vector = FeedbackVector::cast(list->Get(i));
42 1215 : SharedFunctionInfo info = vector->shared_function_info();
43 : DCHECK(info->IsSubjectToDebugging());
44 :
45 : // Match vectors with script.
46 2430 : if (script != info->script()) {
47 1080 : continue;
48 : }
49 825 : if (!info->HasFeedbackMetadata() ||
50 1070 : info->feedback_metadata()->is_empty() ||
51 520 : !info->feedback_metadata()->HasTypeProfileSlot()) {
52 : continue;
53 : }
54 135 : FeedbackSlot slot = vector->GetTypeProfileSlot();
55 : FeedbackNexus nexus(vector, slot);
56 135 : Handle<String> name(info->DebugName(), isolate);
57 135 : std::vector<int> source_positions = nexus.GetSourcePositions();
58 455 : for (int position : source_positions) {
59 : DCHECK_GE(position, 0);
60 : entries->emplace_back(position, nexus.GetTypesForSourcePositions(
61 370 : static_cast<uint32_t>(position)));
62 : }
63 :
64 : // Releases type profile data collected so far.
65 135 : nexus.ResetTypeProfile();
66 : }
67 125 : if (!entries->empty()) {
68 40 : result->emplace_back(type_profile_script);
69 : }
70 : }
71 50 : return result;
72 : }
73 :
74 125 : void TypeProfile::SelectMode(Isolate* isolate, debug::TypeProfile::Mode mode) {
75 : HandleScope handle_scope(isolate);
76 :
77 125 : if (mode == debug::TypeProfile::Mode::kNone) {
78 60 : if (!isolate->factory()
79 : ->feedback_vectors_for_profiling_tools()
80 120 : ->IsUndefined(isolate)) {
81 : // Release type profile data collected so far.
82 :
83 : // Feedback vectors are already listed to prevent losing them to GC.
84 : DCHECK(isolate->factory()
85 : ->feedback_vectors_for_profiling_tools()
86 : ->IsArrayList());
87 : Handle<ArrayList> list = Handle<ArrayList>::cast(
88 50 : isolate->factory()->feedback_vectors_for_profiling_tools());
89 :
90 740 : for (int i = 0; i < list->Length(); i++) {
91 640 : FeedbackVector vector = FeedbackVector::cast(list->Get(i));
92 320 : SharedFunctionInfo info = vector->shared_function_info();
93 : DCHECK(info->IsSubjectToDebugging());
94 320 : if (info->feedback_metadata()->HasTypeProfileSlot()) {
95 130 : FeedbackSlot slot = vector->GetTypeProfileSlot();
96 : FeedbackNexus nexus(vector, slot);
97 130 : nexus.ResetTypeProfile();
98 : }
99 : }
100 :
101 : // Delete the feedback vectors from the list if they're not used by code
102 : // coverage.
103 50 : if (isolate->is_best_effort_code_coverage()) {
104 : isolate->SetFeedbackVectorsForProfilingTools(
105 50 : ReadOnlyRoots(isolate).undefined_value());
106 : }
107 : }
108 : } else {
109 : DCHECK_EQ(debug::TypeProfile::Mode::kCollect, mode);
110 65 : isolate->MaybeInitializeVectorListFromHeap();
111 : }
112 : isolate->set_type_profile_mode(mode);
113 125 : }
114 :
115 : } // namespace internal
116 183867 : } // namespace v8
|