Skip to content

Commit

Permalink
Update leak_tracker to use LeakTesting. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Oct 23, 2023
1 parent 8c73955 commit fb32653
Show file tree
Hide file tree
Showing 26 changed files with 298 additions and 238 deletions.
4 changes: 4 additions & 0 deletions pkgs/leak_tracker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.0.10

* Use `IgnoredLeaks`.

# 9.0.9

* Define `IgnoredLeaks`.
Expand Down
11 changes: 7 additions & 4 deletions pkgs/leak_tracker/lib/src/leak_tracking/_baseliner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import '_primitives/_print_bytes.dart';
import '_primitives/model.dart';

class Baseliner {
Baseliner(this.baselining)
: rss = ValueSampler.start(initialValue: _currentRss());
Baseliner._(this.baselining)
: assert(baselining.mode != BaseliningMode.none),
rss = ValueSampler.start(initialValue: _currentRss());

final MemoryBaselining baselining;
final ValueSampler rss;
Expand All @@ -21,8 +22,10 @@ class Baseliner {
MemoryBaselining? baselining,
) {
oldBaseliner?._finish();
if (baselining == null) return null;
return Baseliner(baselining);
if (baselining == null || baselining.mode == BaseliningMode.none) {
return null;
}
return Baseliner._(baselining);
}

void takeSample() {
Expand Down
31 changes: 6 additions & 25 deletions pkgs/leak_tracker/lib/src/leak_tracking/_leak_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,15 @@ import '_primitives/model.dart';

/// Decides which leaks to report based on allow lists of the phase.
class LeakFilter {
LeakFilter(this.switches);

final Map<PhaseSettings, _PhaseLeakFilter> _phases = {};

final Switches switches;

bool shouldReport(LeakType leakType, ObjectRecord record) {
if (_isLeakTypeDisabled(leakType)) return false;

final filter = _phases.putIfAbsent(
record.phase,
() => _PhaseLeakFilter(record.phase),
);
return filter.shouldReport(leakType, record);
}

bool _isLeakTypeDisabled(LeakType leakType) {
switch (leakType) {
case LeakType.notDisposed:
return switches.disableNotDisposed;
case LeakType.notGCed:
case LeakType.gcedLate:
return switches.disableNotGCed;
}
}
}

class _PhaseLeakFilter {
Expand All @@ -49,31 +33,28 @@ class _PhaseLeakFilter {
return _shouldReport(
leakType,
record,
phase.allowAllNotDisposed,
phase.notDisposedAllowList,
phase.ignoredLeaks.notDisposed,
);
case LeakType.notGCed:
case LeakType.gcedLate:
return _shouldReport(
leakType,
record,
phase.allowAllNotGCed,
phase.notGCedAllowList,
phase.ignoredLeaks.notGCed,
);
}
}

bool _shouldReport(
LeakType leakType,
ObjectRecord record,
bool allAllowed,
Map<String, int?> allowList,
IgnoredLeaksSet ignoredLeaks,
) {
assert(record.phase == phase);
if (allAllowed) return false;
if (ignoredLeaks.ignoreAll) return false;
final objectType = record.type.toString();
if (!allowList.containsKey(objectType)) return true;
final allowedCount = allowList[objectType];
if (!ignoredLeaks.byClass.containsKey(objectType)) return true;
final allowedCount = ignoredLeaks.byClass[objectType];
if (allowedCount == null) return false;

final actualCount = _count.update(
Expand Down
1 change: 0 additions & 1 deletion pkgs/leak_tracker/lib/src/leak_tracking/_leak_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LeakTracker {
disposalTime: config.disposalTime,
numberOfGcCycles: config.numberOfGcCycles,
maxRequestsForRetainingPath: config.maxRequestsForRetainingPath,
switches: config.switches,
);

leakReporter = LeakReporter(
Expand Down
13 changes: 4 additions & 9 deletions pkgs/leak_tracker/lib/src/leak_tracking/_object_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ObjectTracker implements LeakProvider {
required this.disposalTime,
required this.numberOfGcCycles,
required this.maxRequestsForRetainingPath,
required this.switches,
FinalizerBuilder? finalizerBuilder,
GcCounter? gcCounter,
}) {
Expand All @@ -46,24 +45,22 @@ class ObjectTracker implements LeakProvider {

final _objects = ObjectRecords();

late final LeakFilter _leakFilter = LeakFilter(switches);
late final _leakFilter = LeakFilter();

bool disposed = false;

final int numberOfGcCycles;

final int? maxRequestsForRetainingPath;

final Switches switches;

void startTracking(
Object object, {
required Map<String, dynamic>? context,
required String trackedClass,
required PhaseSettings phase,
}) {
throwIfDisposed();
if (phase.isLeakTrackingPaused || switches.isObjectTrackingDisabled) return;
if (phase.ignoreLeaks) return;

final record =
_objects.notGCed.putIfAbsent(object, context, phase, trackedClass);
Expand Down Expand Up @@ -121,11 +118,10 @@ class ObjectTracker implements LeakProvider {
required Map<String, dynamic>? context,
}) {
throwIfDisposed();
if (switches.isObjectTrackingDisabled) return;

final record = _objects.notGCed.record(object);
// If object is not registered, this may mean that it was created when leak tracking was off.
if (record == null || record.phase.isLeakTrackingPaused) return;
if (record == null || record.phase.ignoreLeaks) return;

record.mergeContext(context);

Expand All @@ -143,11 +139,10 @@ class ObjectTracker implements LeakProvider {

void addContext(Object object, {required Map<String, dynamic>? context}) {
throwIfDisposed();
if (switches.isObjectTrackingDisabled) return;

final record = _objects.notGCed.record(object);
// If object is not registered, this may mean that it was created when leak tracking was off.
if (record == null || record.phase.isLeakTrackingPaused) return;
if (record == null || record.phase.ignoreLeaks) return;

record.mergeContext(context);
}
Expand Down
Loading

0 comments on commit fb32653

Please sign in to comment.