runParallel method
Future<List<ScanDiagnostic> ?>
runParallel(
- int jobs, {
- bool resolve = false,
- ScanParallelConfig config = const ScanParallelConfig(),
Scans with jobs worker isolates (see parallel_scan_engine.dart):
a chunk work queue with a memory/pressure watchdog, per-chunk timeouts
and progress reporting. Output is identical for any worker count.
jobs should come from resolveJobs (scan_jobs.dart). With jobs <= 1
and no explicit engine request this is the plain in-process run /
runResolved. Per-rule profiling and --debug-rule tracing keep
process-local state: callers must use run for those.
Read lastParallelReport afterwards for throttling, slow files and skipped files.
Implementation
Future<List<ScanDiagnostic>?> runParallel(
int jobs, {
bool resolve = false,
ScanParallelConfig config = const ScanParallelConfig(),
}) async {
lastParallelReport = null;
if (jobs <= 1 && !config.forceEngine) {
return resolve ? runResolved() : run();
}
_skippedFileCount = 0;
final plan = _prepare();
if (plan == null) return null;
if (plan.files.isEmpty) return const [];
// Capture the cap in the parent: workers run with it disabled (their
// per-process counters would drop diagnostics that belong in the global
// first N); it is applied once after the ordered merge.
final parentIssueCap = _applyIssueCap();
final workerCount = jobs < 1 ? 1 : jobs;
final limits = PressureLimits(
maxMemoryMb:
config.maxMemoryMb ??
defaultMaxMemoryMb(
jobs: workerCount,
resolved: resolve,
availableMemoryMb: config.availableMemoryMb,
),
reserveMb:
config.reserveMb ??
PressureLimits.defaultReserveMb(config.totalMemoryMb),
);
_err(
'Scanning ${plan.files.length} files with $workerCount '
'worker${workerCount == 1 ? '' : 's'} '
'(memory budget ${limits.maxMemoryMb} MB, keeping ${limits.reserveMb} MB '
'free)...',
);
final spec = _WorkerSpec(
targetPath: targetPath,
tier: tier,
enabledRuleNames: enabledRuleNames,
excludeGlobs: _rawExcludeGlobs,
includeGlobs: _rawIncludeGlobs,
noExclude: noExclude,
excludeLightLane: excludeLightLane,
bypassTierCap: bypassTierCap,
laneIsLight: lane == RuleLane.light,
resolved: resolve,
slowFileMs: config.slowFileMs,
rescueExcluded: !applyExclusionsToFileList,
);
// The parent's own scanner, used only if workers cannot start.
ChunkScanner? inline;
Future<ChunkOutcome> inlineScan(List<String> files) async {
inline ??= await openChunkScanner(
resolved: resolve,
slowFileMs: config.slowFileMs,
rescueExcluded: !applyExclusionsToFileList,
);
final s = inline;
if (s == null) return const ChunkOutcome(diagnostics: []);
return s(files);
}
final result = await runChunkedScan(
files: plan.files,
options: ParallelScanOptions(
jobs: workerCount,
limits: limits,
chunkTimeout: config.chunkTimeout,
slowFileMs: config.slowFileMs,
progress: config.progress,
hasTerminal: config.hasTerminal,
onMessage: _progressOrErr,
chunkSize: config.chunkSize,
),
spec: spec,
workerEntry: _scanWorkerMain,
inlineScan: inlineScan,
readRssMb: _currentRssMb,
readAvailableMb: readAvailableMemoryMb,
);
lastParallelReport = result.report;
_skippedFileCount = result.report.outsideRoots;
_reportParallelSummary(result.report);
// The parent owns sentinel cleanup in parallel mode (workers leave it).
final sentinel = io.File(p.join(p.absolute(targetPath), '.saropa_stop'));
try {
if (sentinel.existsSync()) sentinel.deleteSync();
} on io.IOException {
// Best effort; the scan itself already honored the stop.
}
// A serial run caps non-ERROR diagnostics (Problems-tab safeguard, see
// ProgressTracker). Apply it once over the ordered merge so the result is
// the same for every worker count.
final out = <ScanDiagnostic>[];
var nonError = 0;
var dropped = 0;
for (final d in result.diagnostics) {
if (parentIssueCap > 0 && d.severity.toUpperCase() != 'ERROR') {
nonError++;
if (nonError > parentIssueCap + 1) {
dropped++;
continue;
}
}
out.add(d);
}
_noteIssueCap(parentIssueCap, dropped: dropped);
return out;
}