summaryrefslogtreecommitdiff
blob: aefc46bd36044ec4a43c3bdda944f3252367ed9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
https://bugreports.qt.io/browse/QTBUG-130767
https://bugs.kde.org/show_bug.cgi?id=494804
https://codereview.qt-project.org/c/qt/qtdeclarative/+/604180
(+required https://code.qt.io/cgit/qt/qtdeclarative.git/commit/?id=104b0d6e88)
--- a/src/qml/jsruntime/qv4qobjectwrapper_p.h
+++ b/src/qml/jsruntime/qv4qobjectwrapper_p.h
@@ -224,2 +224,15 @@
 
+// We generally musn't pass ReturnedValue as arguments to other functions.
+// In this case, we do it solely for marking purposes so it's fine.
+inline void markIfPastMarkWeakValues(ExecutionEngine *engine, ReturnedValue rv)
+{
+    const auto gcState = engine->memoryManager->gcStateMachine->state;
+    if (gcState != GCStateMachine::Invalid && gcState >= GCState::MarkWeakValues) {
+        QV4::WriteBarrier::markCustom(engine, [rv](QV4::MarkStack *ms) {
+            auto *m = StaticValue::fromReturnedValue(rv).m();
+            m->mark(ms);
+        });
+    }
+}
+
 inline ReturnedValue QObjectWrapper::wrap(ExecutionEngine *engine, QObject *object)
@@ -235,3 +248,5 @@
 
-    return wrap_slowPath(engine, object);
+    const auto rv = wrap_slowPath(engine, object);
+    markIfPastMarkWeakValues(engine, rv);
+    return rv;
 }
@@ -244,3 +259,5 @@
 
-    return wrapConst_slowPath(engine, object);
+    const auto rv = wrapConst_slowPath(engine, object);
+    markIfPastMarkWeakValues(engine, rv);
+    return rv;
 }
--- a/src/qml/memory/qv4mm.cpp
+++ b/src/qml/memory/qv4mm.cpp
@@ -62,2 +62,4 @@
 Q_DECLARE_LOGGING_CATEGORY(lcGcAllocatorStats)
+Q_LOGGING_CATEGORY(lcGcStateTransitions, "qt.qml.gc.stateTransitions")
+Q_DECLARE_LOGGING_CATEGORY(lcGcStateTransitions)
 
@@ -682,3 +684,3 @@
     that->mm->engine->isGCOngoing = true;
-    return MarkGlobalObject;
+    return GCState::MarkGlobalObject;
 }
@@ -688,3 +690,3 @@
     that->mm->engine->markObjects(that->mm->m_markStack.get());
-    return MarkJSStack;
+    return GCState::MarkJSStack;
 }
@@ -694,3 +696,3 @@
     that->mm->collectFromJSStack(that->mm->markStack());
-    return InitMarkPersistentValues;
+    return GCState::InitMarkPersistentValues;
 }
@@ -700,5 +702,5 @@
     if (!that->mm->m_persistentValues)
-        return InitMarkWeakValues; // no persistent values to mark
+        return GCState::InitMarkWeakValues; // no persistent values to mark
     stateData = GCIteratorStorage { that->mm->m_persistentValues->begin() };
-    return MarkPersistentValues;
+    return GCState::MarkPersistentValues;
 }
@@ -719,3 +721,3 @@
     if (wasDrainNecessary(markStack, that->deadline) && that->deadline.hasExpired())
-        return MarkPersistentValues;
+        return GCState::MarkPersistentValues;
     PersistentValueStorage::Iterator& it = get<GCIteratorStorage>(stateData).it;
@@ -724,3 +726,3 @@
         if (!it.p)
-            return InitMarkWeakValues;
+            return GCState::InitMarkWeakValues;
         if (Managed *m = (*it).as<Managed>())
@@ -729,3 +731,3 @@
     }
-    return MarkPersistentValues;
+    return GCState::MarkPersistentValues;
 }
@@ -735,3 +737,3 @@
     stateData = GCIteratorStorage { that->mm->m_weakValues->begin() };
-    return MarkWeakValues;
+    return GCState::MarkWeakValues;
 }
@@ -742,3 +744,3 @@
     if (wasDrainNecessary(markStack, that->deadline) && that->deadline.hasExpired())
-        return MarkWeakValues;
+        return GCState::MarkWeakValues;
     PersistentValueStorage::Iterator& it = get<GCIteratorStorage>(stateData).it;
@@ -747,3 +749,3 @@
         if (!it.p)
-            return MarkDrain;
+            return GCState::MarkDrain;
         QObjectWrapper *qobjectWrapper = (*it).as<QObjectWrapper>();
@@ -768,3 +770,3 @@
     }
-    return MarkWeakValues;
+    return GCState::MarkWeakValues;
 }
@@ -775,3 +777,3 @@
         that->mm->markStack()->drain();
-        return MarkReady;
+        return GCState::MarkReady;
     }
@@ -779,4 +781,4 @@
     return drainState == MarkStack::DrainState::Complete
-            ? MarkReady
-            : MarkDrain;
+            ? GCState::MarkReady
+            : GCState::MarkDrain;
 }
@@ -786,3 +788,3 @@
     //Possibility to do some clean up, stat printing, etc...
-    return InitCallDestroyObjects;
+    return GCState::InitCallDestroyObjects;
 }
@@ -803,5 +805,5 @@
     if (!that->mm->m_weakValues)
-        return FreeWeakMaps; // no need to call destroy objects
+        return GCState::FreeWeakMaps; // no need to call destroy objects
     stateData = GCIteratorStorage { that->mm->m_weakValues->begin() };
-    return CallDestroyObjects;
+    return GCState::CallDestroyObjects;
 }
@@ -818,3 +820,3 @@
         if (!it.p)
-            return FreeWeakMaps;
+            return GCState::FreeWeakMaps;
         Managed *m = (*it).managed();
@@ -828,3 +830,3 @@
     }
-    return CallDestroyObjects;
+    return GCState::CallDestroyObjects;
 }
@@ -845,3 +847,3 @@
     freeWeakMaps(that->mm);
-    return FreeWeakSets;
+    return GCState::FreeWeakSets;
 }
@@ -863,3 +865,3 @@
     freeWeakSets(that->mm);
-    return HandleQObjectWrappers;
+    return GCState::HandleQObjectWrappers;
 }
@@ -869,3 +871,3 @@
     that->mm->cleanupDeletedQObjectWrappersInSweep();
-    return DoSweep;
+    return GCState::DoSweep;
 }
@@ -893,3 +895,3 @@
 
-    return Invalid;
+    return GCState::Invalid;
 }
@@ -1493,4 +1495,8 @@
             }
+            qCDebug(lcGcStateTransitions) << "Preparing to execute the"
+                                          << QMetaEnum::fromType<GCState>().key(state) << "state";
             GCStateInfo& stateInfo = stateInfoMap[int(state)];
             state = stateInfo.execute(this, stateData);
+            qCDebug(lcGcStateTransitions) << "Transitioning to the"
+                                          << QMetaEnum::fromType<GCState>().key(state) << "state";
             if (stateInfo.breakAfter)
@@ -1507,4 +1513,8 @@
         while (state != GCState::Invalid) {
+            qCDebug(lcGcStateTransitions) << "Preparing to execute the"
+                                          << QMetaEnum::fromType<GCState>().key(state) << "state";
             GCStateInfo& stateInfo = stateInfoMap[int(state)];
             state = stateInfo.execute(this, stateData);
+            qCDebug(lcGcStateTransitions) << "Transitioning to the"
+                                          << QMetaEnum::fromType<GCState>().key(state) << "state";
         }
@@ -1516 +1526,3 @@
 QT_END_NAMESPACE
+
+#include "moc_qv4mm_p.cpp"
--- a/src/qml/memory/qv4mm_p.h
+++ b/src/qml/memory/qv4mm_p.h
@@ -30,22 +30,2 @@
 
-enum GCState {
-    MarkStart = 0,
-    MarkGlobalObject,
-    MarkJSStack,
-    InitMarkPersistentValues,
-    MarkPersistentValues,
-    InitMarkWeakValues,
-    MarkWeakValues,
-    MarkDrain,
-    MarkReady,
-    InitCallDestroyObjects,
-    CallDestroyObjects,
-    FreeWeakMaps,
-    FreeWeakSets,
-    HandleQObjectWrappers,
-    DoSweep,
-    Invalid,
-    Count,
-};
-
 struct GCData { virtual ~GCData(){};};
@@ -55,11 +35,39 @@
 };
-struct GCStateMachine;
-
-struct GCStateInfo {
-    using ExtraData = std::variant<std::monostate, GCIteratorStorage>;
-    GCState (*execute)(GCStateMachine *, ExtraData &) = nullptr;  // Function to execute for this state, returns true if ready to transition
-    bool breakAfter{false};
-};
 
 struct GCStateMachine {
+    Q_GADGET_EXPORT(Q_QML_EXPORT)
+
+public:
+    enum GCState {
+        MarkStart = 0,
+        MarkGlobalObject,
+        MarkJSStack,
+        InitMarkPersistentValues,
+        MarkPersistentValues,
+        InitMarkWeakValues,
+        MarkWeakValues,
+        MarkDrain,
+        MarkReady,
+        InitCallDestroyObjects,
+        CallDestroyObjects,
+        FreeWeakMaps,
+        FreeWeakSets,
+        HandleQObjectWrappers,
+        DoSweep,
+        Invalid,
+        Count,
+    };
+    Q_ENUM(GCState)
+
+    struct StepTiming {
+        qint64 rolling_sum = 0;
+        qint64 count = 0;
+    };
+
+    struct GCStateInfo {
+        using ExtraData = std::variant<std::monostate, GCIteratorStorage>;
+        GCState (*execute)(GCStateMachine *, ExtraData &) = nullptr;  // Function to execute for this state, returns true if ready to transition
+        bool breakAfter{false};
+    };
+
     using ExtraData = GCStateInfo::ExtraData;
@@ -96,2 +104,4 @@
 
+using GCState = GCStateMachine::GCState;
+using GCStateInfo = GCStateMachine::GCStateInfo;