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 40 : std::unique_ptr<TypeProfile> TypeProfile::Collect(Isolate* isolate) {
16 40 : 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 40 : Script::Iterator scripts(isolate);
26 :
27 520 : for (Script script = scripts.Next(); !script.is_null();
28 : script = scripts.Next()) {
29 220 : if (!script->IsUserJavaScript()) {
30 120 : 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 1072 : for (int i = 0; i < list->Length(); i++) {
41 972 : FeedbackVector vector = FeedbackVector::cast(list->Get(i));
42 972 : SharedFunctionInfo info = vector->shared_function_info();
43 : DCHECK(info->IsSubjectToDebugging());
44 :
45 : // Match vectors with script.
46 1944 : if (script != info->script()) {
47 864 : continue;
48 : }
49 660 : if (!info->HasFeedbackMetadata() ||
50 416 : info->feedback_metadata()->is_empty() ||
51 416 : !info->feedback_metadata()->HasTypeProfileSlot()) {
52 : continue;
53 : }
54 108 : FeedbackSlot slot = vector->GetTypeProfileSlot();
55 : FeedbackNexus nexus(vector, slot);
56 108 : Handle<String> name(info->DebugName(), isolate);
57 108 : std::vector<int> source_positions = nexus.GetSourcePositions();
58 256 : for (int position : source_positions) {
59 : DCHECK_GE(position, 0);
60 296 : entries->emplace_back(position, nexus.GetTypesForSourcePositions(
61 148 : static_cast<uint32_t>(position)));
62 : }
63 :
64 : // Releases type profile data collected so far.
65 108 : nexus.ResetTypeProfile();
66 : }
67 100 : if (!entries->empty()) {
68 32 : result->emplace_back(type_profile_script);
69 : }
70 : }
71 40 : return result;
72 : }
73 :
74 100 : void TypeProfile::SelectMode(Isolate* isolate, debug::TypeProfileMode mode) {
75 : HandleScope handle_scope(isolate);
76 :
77 100 : if (mode == debug::TypeProfileMode::kNone) {
78 48 : if (!isolate->factory()
79 : ->feedback_vectors_for_profiling_tools()
80 : ->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 : isolate->factory()->feedback_vectors_for_profiling_tools());
89 :
90 552 : for (int i = 0; i < list->Length(); i++) {
91 256 : FeedbackVector vector = FeedbackVector::cast(list->Get(i));
92 : SharedFunctionInfo info = vector->shared_function_info();
93 : DCHECK(info->IsSubjectToDebugging());
94 256 : if (info->feedback_metadata()->HasTypeProfileSlot()) {
95 104 : FeedbackSlot slot = vector->GetTypeProfileSlot();
96 : FeedbackNexus nexus(vector, slot);
97 104 : nexus.ResetTypeProfile();
98 : }
99 : }
100 :
101 : // Delete the feedback vectors from the list if they're not used by code
102 : // coverage.
103 40 : if (isolate->is_best_effort_code_coverage()) {
104 : isolate->SetFeedbackVectorsForProfilingTools(
105 40 : ReadOnlyRoots(isolate).undefined_value());
106 : }
107 : }
108 : } else {
109 : DCHECK_EQ(debug::TypeProfileMode::kCollect, mode);
110 52 : isolate->MaybeInitializeVectorListFromHeap();
111 : }
112 : isolate->set_type_profile_mode(mode);
113 100 : }
114 :
115 : } // namespace internal
116 122004 : } // namespace v8
|