forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_profiler_controller.dart
More file actions
704 lines (619 loc) · 23.7 KB
/
cpu_profiler_controller.dart
File metadata and controls
704 lines (619 loc) · 23.7 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Copyright 2019 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'dart:async';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:vm_service/vm_service.dart' hide Error;
import '../../service/vm_flags.dart' as vm_flags;
import '../../shared/analytics/analytics.dart' as ga;
import '../../shared/analytics/constants.dart' as gac;
import '../../shared/analytics/metrics.dart';
import '../../shared/globals.dart';
import '../../shared/primitives/utils.dart';
import '../../shared/ui/filter.dart';
import '../../shared/ui/search.dart';
import 'common.dart';
import 'cpu_profile_model.dart';
import 'cpu_profile_service.dart';
import 'cpu_profile_transformer.dart';
import 'panes/method_table/method_table_controller.dart';
enum CpuProfilerViewType {
function,
code;
@override
String toString() {
return this == function ? 'Function' : 'Code';
}
}
enum CpuProfilerTagType { user, vm }
enum CpuProfilerBusyStatus {
fetching,
processing,
none;
String get display => name.toSentenceCase();
}
class CpuProfilerController extends DisposableController
with
SearchControllerMixin<CpuStackFrame>,
FilterControllerMixin<CpuStackFrame>,
AutoDisposeControllerMixin {
CpuProfilerController() {
super.init();
}
/// Tag to represent when no user tag filters are applied.
///
/// The word 'none' is not a magic word - just a user-friendly name to convey
/// the message that no filters are applied.
static const userTagNone = 'none';
/// Special tags that represent profiles broken down by user or VM tags.
static const groupByUserTag = '#group-by-user-tag';
static const groupByVmTag = '#group-by-vm-tag';
/// User tag that marks app start up for Flutter apps.
///
/// This needs to match the tag set and unset at these locations,
/// respectively:
///
/// Flutter Engine - https://github.com/flutter/engine/blob/f52653ba4568b4a40e8624d47773d156740ea9f7/runtime/dart_isolate.cc#L369
/// Flutter Framework - https://github.com/flutter/flutter/blob/33f4107cd1029c3847f119e23719f6147dbc39db/packages/flutter/lib/src/widgets/binding.dart#L866
static const appStartUpUserTag = 'AppStartUp';
/// Data for the initial value and reset value of [_dataNotifier].
///
/// When this data is the value of [_dataNotifier], the CPU profiler is in a
/// base state where recording instructions should be shown.
static CpuProfileData baseStateCpuProfileData = CpuProfileData.empty();
/// Data used to represent and empty profile when attempting to load the
/// 'AppStartUp' profile.
///
/// When this data is the value of [_dataNotifier], the CPU profiler will show
/// an empty message specific to the app start up use case.
static CpuProfileData emptyAppStartUpProfile = CpuProfileData.empty();
/// The store of cached CPU profiles for the currently selected isolate.
///
/// The stored profiles are not guaranteed to be processed.
CpuProfileStore get cpuProfileStore =>
_cpuProfileStoreByIsolateId.putIfAbsent(
serviceConnection
.serviceManager
.isolateManager
.selectedIsolate
.value
?.id ??
'',
() => CpuProfileStore(),
);
/// Store of cached CPU profiles for each isolate.
final _cpuProfileStoreByIsolateId = <String, CpuProfileStore>{};
late final methodTableController = MethodTableController(
dataNotifier: dataNotifier,
);
/// Notifies that new cpu profile data is available.
ValueListenable<CpuProfileData?> get dataNotifier => _dataNotifier;
final _dataNotifier = ValueNotifier<CpuProfileData?>(baseStateCpuProfileData);
/// The current busy state of the cpu profiler.
ValueListenable<CpuProfilerBusyStatus> get profilerBusyStatus =>
_profilerBusyStatus;
final _profilerBusyStatus = ValueNotifier<CpuProfilerBusyStatus>(
CpuProfilerBusyStatus.none,
);
/// Notifies that a cpu stack frame was selected.
ValueListenable<CpuStackFrame?> get selectedCpuStackFrameNotifier =>
_selectedCpuStackFrameNotifier;
final _selectedCpuStackFrameNotifier = ValueNotifier<CpuStackFrame?>(null);
/// Notifies that a user tag filter is set on the CPU profile flame chart.
ValueListenable<String> get userTagFilter => _userTagFilter;
final _userTagFilter = ValueNotifier<String>(userTagNone);
Iterable<String> get userTags =>
cpuProfileStore
.lookupProfile(label: userTagNone)
?.functionProfile
.userTags ??
const <String>[];
ValueListenable<CpuProfilerViewType> get viewType => _viewType;
final _viewType = ValueNotifier<CpuProfilerViewType>(
CpuProfilerViewType.function,
);
static const _nativeFilterId = 'native';
static const _coreDartFilterId = 'core-dart';
static const _coreFlutterFilterId = 'core-flutter';
static const _uriQueryFilterId = 'cpu-profiler-uri-filter';
/// The toggle filters available for the CPU profiler.
@override
SettingFilters<CpuStackFrame> createSettingFilters() => [
ToggleFilter<CpuStackFrame>(
id: _nativeFilterId,
name: 'Hide Native code',
includeCallback: (stackFrame) => !stackFrame.isNative,
defaultValue: true,
),
ToggleFilter<CpuStackFrame>(
id: _coreDartFilterId,
name: 'Hide core Dart libraries',
includeCallback: (stackFrame) => !stackFrame.isDartCore,
defaultValue: false,
),
if (serviceConnection.serviceManager.connectedApp?.isFlutterAppNow ?? true)
ToggleFilter<CpuStackFrame>(
id: _coreFlutterFilterId,
name: 'Hide core Flutter libraries',
includeCallback: (stackFrame) => !stackFrame.isFlutterCore,
defaultValue: false,
),
];
@override
Map<String, QueryFilterArgument<CpuStackFrame>> createQueryFilterArgs() => {
_uriQueryFilterId: QueryFilterArgument<CpuStackFrame>(
keys: ['uri', 'u'],
exampleUsages: [
'uri:my_dart_package/some_lib.dart',
'-u:some_lib_to_hide',
],
dataValueProvider: (stackFrame) => stackFrame.packageUri,
substringMatch: true,
),
};
@override
ValueNotifier<String>? get filterTagNotifier =>
preferences.cpuProfiler.filterTag;
int selectedProfilerTabIndex = 0;
ProfilerTab selectedProfilerTab = ProfilerTab.bottomUp;
void changeSelectedProfilerTab(int index, ProfilerTab tab) {
selectedProfilerTabIndex = index;
// The method table has a different search field than the rest of the
// profiler tabs. This is because the method table shows data of type
// [MethodTableGraphNode] and the other profiler tabs show data of type
// [CpuStackFrame].
//
// In order to ensure a consistent search experience across profiler tabs,
// update the appropriate controller's search value to be consistent when
// switching to or from the method table tab.
final oldTabWasMethodTable = selectedProfilerTab == ProfilerTab.methodTable;
final newTabIsMethodTable = tab == ProfilerTab.methodTable;
selectedProfilerTab = tab;
if (oldTabWasMethodTable != newTabIsMethodTable) {
if (newTabIsMethodTable) {
methodTableController.search = search;
} else {
search = methodTableController.search;
}
}
}
final transformer = CpuProfileTransformer();
/// Notifies that the vm profiler flag has changed.
ValueNotifier<Flag>? get profilerFlagNotifier =>
serviceConnection.vmFlagManager.flag(vm_flags.profiler);
ValueNotifier<Flag>? get profilePeriodFlag =>
serviceConnection.vmFlagManager.flag(vm_flags.profilePeriod);
/// Whether the profiler is enabled.
///
/// Clients interested in the current value of [profilerFlagNotifier] should
/// use this getter. Otherwise, clients subscribing to change notifications,
/// should listen to [profilerFlagNotifier].
bool get profilerEnabled =>
offlineDataController.showingOfflineData.value
? true
: profilerFlagNotifier?.value.valueAsString == 'true';
Future<Response> enableCpuProfiler() {
return serviceConnection.serviceManager.service!.enableCpuProfiler();
}
Future<void> pullAndProcessProfile({
required int startMicros,
required int extentMicros,
required String processId,
}) async {
if (!profilerEnabled) return;
assert(_dataNotifier.value != null);
assert(_profilerBusyStatus.value == CpuProfilerBusyStatus.none);
var cpuProfiles = CpuProfilePair(
functionProfile: baseStateCpuProfileData,
codeProfile: null,
);
_dataNotifier.value = null;
Future<void> pullAndProcessHelper() async {
// TODO(kenz): add a cancel button to the processing UI in case pulling a
// large payload from the vm service takes a long time.
_profilerBusyStatus.value = CpuProfilerBusyStatus.fetching;
cpuProfiles = await serviceConnection.serviceManager.service!
.getCpuProfile(startMicros: startMicros, extentMicros: extentMicros);
await processAndSetData(
cpuProfiles,
processId: processId,
storeAsUserTagNone: true,
shouldApplyFilters: true,
shouldRefreshSearchMatches: true,
);
}
try {
// Pull and process cpu profile data [pullAndProcessHelper] and time the
// operation for analytics.
await ga.timeAsync(
gac.cpuProfiler,
gac.CpuProfilerEvents.cpuProfileProcessingTime.name,
asyncOperation: pullAndProcessHelper,
screenMetricsProvider:
() => ProfilerScreenMetrics(
cpuSampleCount: cpuProfiles.profileMetaData.sampleCount,
cpuStackDepth: cpuProfiles.profileMetaData.stackDepth,
),
);
} on ProcessCancelledException catch (_) {
// Do nothing for instances of [ProcessCancelledException].
}
}
Future<CpuProfilePair> _processDataHelper(
CpuProfilePair cpuProfiles, {
required String processId,
required bool storeAsUserTagNone,
required bool shouldApplyFilters,
}) async {
assert(_profilerBusyStatus.value == CpuProfilerBusyStatus.processing);
if (storeAsUserTagNone) {
cpuProfileStore.storeProfile(cpuProfiles, label: userTagNone);
cpuProfileStore.storeProfile(
cpuProfiles,
time: cpuProfiles.profileMetaData.time,
);
}
if (shouldApplyFilters) {
cpuProfiles = _filterData(cpuProfiles);
}
await cpuProfiles.process(transformer: transformer, processId: processId);
if (storeAsUserTagNone && shouldApplyFilters) {
cpuProfileStore.storeProfile(
cpuProfiles,
label: _wrapWithFilterTag(userTagNone),
);
}
return cpuProfiles;
}
/// Processes [cpuProfiles] and sets the data for the controller.
///
/// If `storeAsUserTagNone` is true, the processed data will be stored as the
/// original data, where no user tag filter has been applied.
Future<void> processAndSetData(
CpuProfilePair cpuProfiles, {
required String processId,
required bool storeAsUserTagNone,
required bool shouldApplyFilters,
required bool shouldRefreshSearchMatches,
}) async {
_dataNotifier.value = null;
_profilerBusyStatus.value = CpuProfilerBusyStatus.processing;
final type = viewType.value;
CpuProfileData cpuProfileData = cpuProfiles.getActive(type);
try {
cpuProfileData = await _processDataHelper(
cpuProfiles,
processId: processId,
storeAsUserTagNone: storeAsUserTagNone,
shouldApplyFilters: shouldApplyFilters,
).then((p) => p.getActive(type));
_dataNotifier.value = cpuProfileData;
if (shouldRefreshSearchMatches) {
refreshSearchMatches();
}
_profilerBusyStatus.value = CpuProfilerBusyStatus.none;
} on AssertionError catch (_) {
_dataNotifier.value = cpuProfileData;
_profilerBusyStatus.value = CpuProfilerBusyStatus.none;
// Rethrow after setting notifiers so that cpu profile data is included
// in the timeline export.
rethrow;
}
}
@override
Iterable<CpuStackFrame> get currentDataToSearchThrough =>
_dataNotifier.value!.stackFrames.values;
Future<void> loadAllSamples() async {
reset();
await pullAndProcessProfile(
startMicros: 0,
// Using [maxJsInt] as [extentMicros] for the getCpuProfile requests will
// give us all cpu samples we have available.
extentMicros: maxJsInt,
processId: 'Load all samples',
);
}
/// Looks up a profile from the [cpuProfileStore] and processes the data if it
/// has not already been processed.
///
/// [createIfAbsent] - an optional callback to create a [CpuProfilePair] that
/// will be stored for [label] if one does not already exist.
/// [shouldApplyFilters] - whether the active filter should be applied to the
/// looked up [CpuProfilePair].
Future<CpuProfilePair?> lookupProfileAndProcess({
required String label,
CpuProfilePair Function()? createIfAbsent,
bool shouldApplyFilters = false,
}) async {
var storedProfile = cpuProfileStore.lookupProfile(label: label);
if (storedProfile == null) {
if (createIfAbsent != null) {
final newProfile = createIfAbsent();
cpuProfileStore.storeProfile(newProfile, label: label);
storedProfile = newProfile;
} else {
return null;
}
}
if (shouldApplyFilters && isFilterActive) {
final storedFilteredProfile = cpuProfileStore.lookupProfile(
label: _wrapWithFilterTag(label),
);
storedProfile = storedFilteredProfile ?? _filterData(storedProfile);
}
if (!storedProfile.processed) {
await storedProfile.process(
transformer: transformer,
processId: 'lookupProfileAndProcess $label',
);
// Overwrite the profile store with the processed profile so that we do not
// process this profile again.
cpuProfileStore.storeProfile(
storedProfile,
label: shouldApplyFilters ? _wrapWithFilterTag(label) : label,
);
}
return storedProfile;
}
String _wrapWithFilterTag(String label, {String? filterTag}) {
filterTag ??= activeFilterTag();
return '$label${filterTag.isNotEmpty ? '-$filterTag' : ''}';
}
Future<void> loadAppStartUpProfile() async {
Future<void> loadAppStartUpProfileHelper() async {
// Look up the stored app start up profiles before calling [reset]. This
// will save us the work of processing the startup profiles again if we have
// already processed them once.
var appStartUpProfile = await lookupProfileAndProcess(
label: appStartUpUserTag,
);
final appStartUpProfileWithFilters = await lookupProfileAndProcess(
label: _wrapWithFilterTag(appStartUpUserTag),
);
reset();
_dataNotifier.value = null;
if (appStartUpProfileWithFilters != null) {
_dataNotifier.value = appStartUpProfileWithFilters.getActive(
viewType.value,
);
refreshSearchMatches();
return;
}
if (appStartUpProfile == null) {
final cpuProfile = await serviceConnection.serviceManager.service!
.getCpuProfile(
startMicros: 0,
// Using [maxJsInt] as [extentMicros] for the getCpuProfile requests will
// give us all cpu samples we have available.
extentMicros: maxJsInt,
);
appStartUpProfile = CpuProfilePair.fromUserTag(
cpuProfile,
appStartUpUserTag,
);
cpuProfileStore.storeProfile(appStartUpProfile, label: userTagNone);
cpuProfileStore.storeProfile(
appStartUpProfile,
label: appStartUpUserTag,
);
}
if (appStartUpProfile.isEmpty) {
_dataNotifier.value = emptyAppStartUpProfile;
return;
}
_userTagFilter.value = appStartUpUserTag;
if (isFilterActive) {
final filteredAppStartUpProfile = _filterData(appStartUpProfile);
await processAndSetData(
filteredAppStartUpProfile,
processId: 'filter app start up profile',
storeAsUserTagNone: false,
shouldApplyFilters: false,
shouldRefreshSearchMatches: true,
);
cpuProfileStore.storeProfile(
filteredAppStartUpProfile,
label: _wrapWithFilterTag(appStartUpUserTag),
);
return;
}
if (!appStartUpProfile.processed) {
await appStartUpProfile.process(
transformer: transformer,
processId: 'appStartUpProfile',
);
}
_dataNotifier.value = appStartUpProfile.getActive(viewType.value);
}
_profilerBusyStatus.value = CpuProfilerBusyStatus.processing;
await loadAppStartUpProfileHelper();
_profilerBusyStatus.value = CpuProfilerBusyStatus.none;
}
// TODO(kenz): filter the data before calling this method, or pass in
// unprocessed data to avoid processing the data twice.
void loadProcessedData(
CpuProfilePair data, {
required bool storeAsUserTagNone,
}) {
assert(data.processed);
_dataNotifier.value = data.getActive(viewType.value);
if (storeAsUserTagNone) {
cpuProfileStore.storeProfile(data, label: userTagNone);
}
}
Future<void> loadDataWithTag(String tag) async {
_userTagFilter.value = tag;
_dataNotifier.value = null;
_profilerBusyStatus.value = CpuProfilerBusyStatus.processing;
try {
_dataNotifier.value = await processDataForTag(
tag,
).then((e) => e.getActive(viewType.value));
} catch (e, stackTrace) {
// In the event of an error, reset the data to the original CPU profile.
final filteredOriginalData = await lookupProfileAndProcess(
label: _wrapWithFilterTag(userTagNone),
);
_dataNotifier.value = filteredOriginalData!.getActive(viewType.value);
Error.throwWithStackTrace(
Exception('Error loading data with tag "$tag": ${e.toString()}'),
stackTrace,
);
} finally {
_profilerBusyStatus.value = CpuProfilerBusyStatus.none;
}
}
Future<CpuProfilePair> processDataForTag(String tag) async {
final profileLabel = _wrapWithFilterTag(tag);
final filteredDataForTag = await lookupProfileAndProcess(
label: profileLabel,
);
if (filteredDataForTag != null) return filteredDataForTag;
final data = await lookupProfileAndProcess(
label: tag,
createIfAbsent: () {
final fullData = cpuProfileStore.lookupProfile(label: userTagNone)!;
final tagType =
tag == groupByUserTag
? CpuProfilerTagType.user
: CpuProfilerTagType.vm;
final data =
tag == groupByUserTag || tag == groupByVmTag
? CpuProfilePair.withTagRoots(fullData, tagType)
: CpuProfilePair.fromUserTag(fullData, tag);
cpuProfileStore.storeProfile(data, label: tag);
return data;
},
shouldApplyFilters: true,
);
return data!;
}
void updateViewForType(CpuProfilerViewType type) {
_viewType.value = type;
_dataNotifier.value = cpuProfileStore
.lookupProfile(label: _wrapWithFilterTag(_userTagFilter.value))
?.getActive(type);
}
void selectCpuStackFrame(CpuStackFrame? stackFrame) {
if (stackFrame == dataNotifier.value!.selectedStackFrame) return;
dataNotifier.value!.selectedStackFrame = stackFrame;
_selectedCpuStackFrameNotifier.value = stackFrame;
}
Future<void> clear() async {
reset();
await serviceConnection.serviceManager.service!.clearSamples();
}
void reset({CpuProfileData? data}) {
_selectedCpuStackFrameNotifier.value = null;
_dataNotifier.value = data ?? baseStateCpuProfileData;
_profilerBusyStatus.value = CpuProfilerBusyStatus.none;
_userTagFilter.value = userTagNone;
transformer.reset();
cpuProfileStore.clear();
methodTableController.reset(shouldResetSearch: true);
resetSearch();
}
@override
void dispose() {
// [methodTableController] needs to be disposed before [_dataNotifier] since
// it is late initialized with a reference to [_dataNotifier].
methodTableController.dispose();
_dataNotifier.dispose();
_profilerBusyStatus.dispose();
_selectedCpuStackFrameNotifier.dispose();
_userTagFilter.dispose();
_viewType.dispose();
super.dispose();
}
/// Tracks the identifier for the attempt to filter the data.
///
/// We use this to prevent multiple filter calls from processing data at the
/// same time.
int _filterIdentifier = 0;
@override
void filterData(Filter<CpuStackFrame> filter) {
super.filterData(filter);
final dataForCurrentTag = cpuProfileStore.lookupProfile(
label: _userTagFilter.value,
);
if (dataForCurrentTag == null) {
// We have nothing to filter from, so bail out early.
return;
}
CpuProfilePair? filteredData;
if (filter.isEmpty) {
// If the filter is empty, no need to filter. Just look up the current
// unfiltered profile.
filteredData = dataForCurrentTag;
} else {
// Lookup the cpu profile from the cached [cpuProfileStore] if present.
final dataLabel = _wrapWithFilterTag(_userTagFilter.value);
filteredData = cpuProfileStore.lookupProfile(label: dataLabel);
if (filteredData == null) {
// TODO(https://github.com/flutter/devtools/issues/5203): optimize
// filtering by filtering from already filtered data when possible. The
// below code is intentionally left in comments for reference.
// CpuProfilePair? filterFrom;
// if (!filter.queryFilter.isEmpty) {
// // Lookup the cpu profile without the query filter, and filter from
// // that profile to optimize performance.
// final filterTagWithoutQuery = activeFilterTag()
// .split(FilterControllerMixin.filterTagSeparator)
// .first;
// final label = _wrapWithFilterTag(
// _userTagFilter.value,
// filterTag: filterTagWithoutQuery,
// );
// filterFrom = cpuProfileStore.lookupProfile(
// label: label,
// );
// }
final filterFrom = dataForCurrentTag;
filteredData = _filterData(filterFrom, filter: filter);
cpuProfileStore.storeProfile(filteredData, label: dataLabel);
}
}
unawaited(
processAndSetData(
filteredData,
processId: 'filter $_filterIdentifier',
storeAsUserTagNone: false,
// Filters are already applied before this call to [processAndSetData].
shouldApplyFilters: false,
shouldRefreshSearchMatches: true,
),
);
_filterIdentifier++;
}
CpuProfilePair _filterData(
CpuProfilePair originalData, {
Filter<CpuStackFrame>? filter,
}) {
filter ??= activeFilter.value;
bool filterCallback(CpuStackFrame stackFrame) {
final filteredOutBySettingFilters = filter!.settingFilters.any(
(settingFilter) => !settingFilter.includeData(stackFrame),
);
if (filteredOutBySettingFilters) return false;
final queryFilter = filter.queryFilter;
if (!queryFilter.isEmpty) {
final filteredOutByQueryFilterArgument = queryFilter.filterArguments
.any((argument) => !argument.matchesValue(stackFrame));
if (filteredOutByQueryFilterArgument) return false;
if (queryFilter.substringExpressions.isNotEmpty) {
for (final substring in queryFilter.substringExpressions) {
bool matches(String? stringToMatch) {
return stringToMatch?.caseInsensitiveContains(substring) ?? false;
}
if (matches(stackFrame.name)) return true;
if (matches(stackFrame.packageUri)) return true;
}
return false;
}
}
return true;
}
return CpuProfilePair.filterFrom(originalData, filterCallback);
}
}