MCPcopy Create free account
hub / github.com/Snapchat/Valdi / onANR

Method onANR

valdi/src/valdi/runtime/JavaScript/JavaScriptANRDetector.cpp:131–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129}
130
131void JavaScriptANRDetector::cancelNextTick() {
132 if (_nextTickTask != 0) {
133 _dispatchQueue->cancel(_nextTickTask);
134 _nextTickTask = 0;
135 }
136}
137
138void JavaScriptANRDetector::setTickInterval(std::chrono::steady_clock::duration tickInterval) {
139 std::lock_guard<Mutex> lock(_mutex);
140 _tickInterval = tickInterval;
141}
142
143void JavaScriptANRDetector::onNextTick(DispatchFunction nextTickCallback) {
144 std::lock_guard<Mutex> lock(_mutex);
145 _nextTickCallbacks.emplace_back(std::move(nextTickCallback));
146}
147
148void JavaScriptANRDetector::scheduleNextTick() {
149 if (_nextTickTask != 0 || !_started || !_isInForeground) {
150 return;
151 }
152 _nextTickTask = _dispatchQueue->asyncAfter(
153 [self = Valdi::weakRef(this)]() {
154 auto strongSelf = self.lock();
155 if (strongSelf != nullptr) {
156 strongSelf->checkForANRs();
157 }
158 },
159 _tickInterval);
160}
161
162// Must be called with _mutex held, for an entry whose syn is still waiting at a tick.
163void JavaScriptANRDetector::startSampling(Entry& entry, std::chrono::steady_clock::time_point now) {
164 if (!_samplingConfig.enabled || entry.sampler != nullptr) {
165 return;
166 }
167 entry.sampler = std::make_unique<JavaScriptANRSampler>(_samplingConfig.maxSamples, _samplingConfig.interval);
168 entry.sampler->sample(*entry.taskScheduler, now);
169}
170
171void JavaScriptANRDetector::sampleWaitingEntries() {
172 std::lock_guard<Mutex> lock(_mutex);
173 _nextSampleTask = 0;
174 auto now = std::chrono::steady_clock::now();
175 for (const auto& entry : _entries) {
176 if (isSampling(*entry)) {
177 entry->sampler->sample(*entry->taskScheduler, now);
178 }
179 }
180 scheduleNextSample();
181}
182
183// Must be called with _mutex held.
184void JavaScriptANRDetector::scheduleNextSample() {
185 if (_nextSampleTask != 0 || !_started || !_isInForeground ||
186 std::none_of(_entries.begin(), _entries.end(), [](const auto& entry) { return isSampling(*entry); })) {
187 return;
188 }

Callers

nothing calls this directly

Calls 14

hasRunningStacktraceFunction · 0.85
JavaScriptANRClass · 0.85
getStatusMethod · 0.80
emitANRMethod · 0.80
withAttributionMethod · 0.80
loadMethod · 0.65
toStringMethod · 0.65
getStackTraceMethod · 0.65
captureStackTracesMethod · 0.45
emptyMethod · 0.45
getContextMethod · 0.45
getPathMethod · 0.45

Tested by

no test coverage detected