perf: reduce excessive openat syscalls during scan run #127

Open
opened 2026-06-04 18:26:05 +02:00 by michael · 1 comment
Owner

Goal

Reduce the number of file system open calls during a scan run.
Currently fragjan performs ~26,000 openat syscalls for 26 files and 774 checks.
At 100 runs per day this amounts to ~2.6 million unnecessary file system operations.

Root Causes

Two independent issues in core/runner/init.lua:

1. Repeated directory walk per rule
mod_collect.collect_files is called once per rule. Each call performs a full
recursive file system walk of the scan target. With ~70 rules the same directory
tree is walked ~70 times.

2. require inside rule loop
pcall(require, rule.check.primitive) is called inside the rule loop.
Although Lua caches loaded modules in package.loaded, the lookup itself
may still trigger file system probes on each call when the module is not
yet cached (first run) or due to package.path traversal overhead.

Acceptance Criteria

  • collect_files is called once per unique (pattern, file_path, excludes, includes) combination and results are cached for the duration of the run
  • Primitive modules are loaded once before the rule loop and reused
  • openat syscall count reduces significantly (target: proportional to actual file count, not rule count)
  • No change to output or result format
  • fragjan passes its own rule checks after the change

Technical Notes

Cache key for file collection: pattern .. "|" .. (file_path or "") .. "|" .. table.concat(excludes, ",") .. "|" .. table.concat(includes, ",")

Primitive cache: simple table keyed by primitive name, populated before the rule loop in core/runner/init.lua.

Measured baseline: 26,408 openat calls, 2.74s wall clock, 5.5 MB RSS for 26 files / 774 checks.

## Goal Reduce the number of file system `open` calls during a scan run. Currently fragjan performs ~26,000 `openat` syscalls for 26 files and 774 checks. At 100 runs per day this amounts to ~2.6 million unnecessary file system operations. ## Root Causes Two independent issues in `core/runner/init.lua`: **1. Repeated directory walk per rule** `mod_collect.collect_files` is called once per rule. Each call performs a full recursive file system walk of the scan target. With ~70 rules the same directory tree is walked ~70 times. **2. `require` inside rule loop** `pcall(require, rule.check.primitive)` is called inside the rule loop. Although Lua caches loaded modules in `package.loaded`, the lookup itself may still trigger file system probes on each call when the module is not yet cached (first run) or due to package.path traversal overhead. ## Acceptance Criteria - [ ] `collect_files` is called once per unique `(pattern, file_path, excludes, includes)` combination and results are cached for the duration of the run - [ ] Primitive modules are loaded once before the rule loop and reused - [ ] `openat` syscall count reduces significantly (target: proportional to actual file count, not rule count) - [ ] No change to output or result format - [ ] fragjan passes its own rule checks after the change ## Technical Notes Cache key for file collection: `pattern .. "|" .. (file_path or "") .. "|" .. table.concat(excludes, ",") .. "|" .. table.concat(includes, ",")` Primitive cache: simple table keyed by primitive name, populated before the rule loop in `core/runner/init.lua`. Measured baseline: 26,408 `openat` calls, 2.74s wall clock, 5.5 MB RSS for 26 files / 774 checks.
Author
Owner

Full strace breakdown

Complete syscall profile reveals the real cost distribution:

 40.71%   85,129 newfstatat   -- fs.exists() / is_dir() per file lookup
 25.10%   50,158 getdents64   -- directory walk (repeated per rule)
 15.10%   26,438 openat
  9.33%   26,158 close
  6.98%   26,107 fstat

newfstatat and getdents64 together account for 66% of syscall time.
The collect_files cache fix will reduce all three categories, not just openat --
since each directory walk also triggers stat and getdents calls.

Ratio getdents64 / openat ~= 2x confirms the repeated walk per rule.

Approach

Walk the directory tree once at the start of the run, build an in-memory file set,
then all rule checks work against that set instead of hitting the filesystem repeatedly.
This eliminates redundant getdents64 and newfstatat calls entirely for the per-rule phase.

## Full strace breakdown Complete syscall profile reveals the real cost distribution: ``` 40.71% 85,129 newfstatat -- fs.exists() / is_dir() per file lookup 25.10% 50,158 getdents64 -- directory walk (repeated per rule) 15.10% 26,438 openat 9.33% 26,158 close 6.98% 26,107 fstat ``` newfstatat and getdents64 together account for 66% of syscall time. The collect_files cache fix will reduce all three categories, not just openat -- since each directory walk also triggers stat and getdents calls. Ratio getdents64 / openat ~= 2x confirms the repeated walk per rule. ## Approach Walk the directory tree once at the start of the run, build an in-memory file set, then all rule checks work against that set instead of hitting the filesystem repeatedly. This eliminates redundant getdents64 and newfstatat calls entirely for the per-rule phase.
michael added this to the Basic Version milestone 2026-06-04 19:34:17 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
DAW/fragjan#127
No description provided.