// strawberry-scan-bench: dev tool (build-only, not installed). Runs the REAL scan path // (DetectionPipeline::scan_on_demand: budgeted read -> sha256 -> IOC -> YARA) over a list of files // with a real rule set and the balanced resource governor, so a Tracy capture of it (build with // -Denable_profiling=true, run under scripts/profile_capture.sh) breaks a scan into stages without // needing root or the daemon. Prints one line per file plus a total. // // strawberry-scan-bench --rules DIR [--profile balanced] [--repeat N] FILE... // (a FILE argument of "@list.txt" reads one path per line) // strawberry-scan-bench --rules DIR --engine FILE... # engine A/B: every file scanned with each // variant (full, fast, loaded, loaded+fast) straight through YaraScanner; prints per-variant // time and every match difference against "full" (the parity check for a variant). #include "strawberry/config.hpp" #include "strawberry/detection_pipeline.hpp" #include "strawberry/resource_governor.hpp" #include "strawberry/profiling.hpp" #include "strawberry/yara_scanner.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace { int usage() { std::cerr << "usage: strawberry-scan-bench --rules DIR [--profile NAME] [--repeat N] FILE... (@list.txt ok)\n"; return 2; } } // namespace int main(int argc, char** argv) { std::string rules; std::string profile = "balanced"; int repeat = 1; bool engine_ab = false; bool partitions_ab = false; std::vector files; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; if (a == "--rules" && i + 1 < argc) { rules = argv[++i]; } else if (a == "--partitions") { partitions_ab = true; } else if (a == "--engine") { engine_ab = true; } else if (a == "--profile" && i + 1 < argc) { profile = argv[++i]; } else if (a == "--repeat" && i + 1 < argc) { char* end = nullptr; repeat = static_cast(std::strtol(argv[++i], &end, 10)); if (*end != '\0' || repeat < 1) return usage(); } else if (!a.empty() && a[0] == '@') { std::ifstream list(a.substr(1)); for (std::string line; std::getline(list, line);) { if (!line.empty()) files.push_back(line); } } else if (!a.empty() && a[0] != '-') { files.push_back(a); } else { return usage(); } } if (rules.empty() || files.empty()) return usage(); const auto named = strawberry::scan_profile_named(profile); if (!named) { std::cerr << "unknown profile " << profile << "\n"; return 2; } if (partitions_ab) { // Partition A/B on the real pack: full rule set vs partitioned, same files, alternating order; // every match-set difference is printed (it must be zero) and per-mode time is reported. strawberry::YaraScanner off, on; off.set_rule_partitions(false); on.set_rule_partitions(true); const std::vector src{{rules, ""}}; const auto rss_now = [] { std::ifstream statm("/proc/self/statm"); long pages = 0, rss = 0; statm >> pages >> rss; return rss * (::sysconf(_SC_PAGESIZE) / 1024) / 1024; }; const long rss0 = rss_now(); if (!off.load_rules(src)) return 1; const long rss_full = rss_now(); if (!on.load_rules(src)) return 1; for (int i = 0; i < 1200 && on.partition_status().state == "building"; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(100)); const auto st = on.partition_status(); std::cout << "partitions: " << st.state << " " << st.reason << ", " << st.subsets << " subsets\n"; if (std::getenv("SB_BENCH_RSS")) { // RSS with both scanners loaded (full + full-with-subsets): the subsets' cost is the delta // against twice a single full set, measured separately with SB_BENCH_RSS=single. std::ifstream statm("/proc/self/statm"); long pages = 0, rss = 0; statm >> pages >> rss; const long rss_both = rss * (::sysconf(_SC_PAGESIZE) / 1024) / 1024; std::cout << "rss_mib base " << rss0 << " | +full set " << (rss_full - rss0) << " | +partitioned scanner (full + subsets) " << (rss_both - rss_full) << " => subsets cost " << (rss_both - rss_full) - (rss_full - rss0) << "\n"; } if (st.state != "ready") return 1; if (std::getenv("SB_BENCH_KEPT")) { static const char* cls[] = {"nonpe", "pe"}; static const char* bk[] = {"<1M", "1-4M", "4-16M", ">=16M"}; for (std::size_t i = 0; i < 8; ++i) std::cout << "kept " << cls[i / 4] << " " << bk[i % 4] << ": " << st.kept[i] << " / " << st.total_rules << "\n"; } double t_off = 0, t_on = 0; uint64_t bytes = 0; int nfiles = 0, diffs = 0, matched = 0; for (const auto& path : files) { std::ifstream in(path, std::ios::binary); std::vector data((std::istreambuf_iterator(in)), std::istreambuf_iterator()); if (data.empty()) continue; ++nfiles; bytes += data.size(); std::vector a, b; const auto timed = [&](strawberry::YaraScanner& sc, std::vector& m) { const auto t0 = std::chrono::steady_clock::now(); (void) sc.scan_memory(data.data(), data.size(), m, 120); return std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); }; if (nfiles % 2) { t_off += timed(off, a); t_on += timed(on, b); } else { t_on += timed(on, b); t_off += timed(off, a); } auto names = [](const std::vector& m) { std::vector n; for (const auto& x : m) n.push_back(x.rule_name); std::sort(n.begin(), n.end()); return n; }; if (!a.empty()) ++matched; if (names(a) != names(b)) { ++diffs; std::cout << "DIFF " << path << ": full=" << a.size() << " partitioned=" << b.size() << "\n"; } } const double mib = bytes / 1048576.0; std::cout << nfiles << " files, " << mib << " MiB, " << matched << " with matches, " << diffs << " match-set differences\n"; std::cout << "full: " << t_off << " s (" << (t_off > 0 ? mib / t_off : 0) << " MiB/s)\n"; std::cout << "partitioned: " << t_on << " s (" << (t_on > 0 ? mib / t_on : 0) << " MiB/s), speedup " << (t_on > 0 ? t_off / t_on : 0) << "x\n"; return diffs == 0 ? 0 : 3; } if (engine_ab) { strawberry::YaraScanner scanner; if (!scanner.load_rules(std::vector{{rules, ""}})) { std::cerr << "cannot load rules " << rules << "\n"; return 1; } struct Variant { const char* name; bool loaded; bool fast; double secs = 0; uint64_t bytes = 0; int diffs = 0, matched = 0; }; std::vector variants{{"full", false, false}, {"fast", false, true}, {"loaded", true, false}, {"loaded+fast", true, true}}; int nfiles = 0; for (const auto& path : files) { std::ifstream in(path, std::ios::binary); std::vector data((std::istreambuf_iterator(in)), std::istreambuf_iterator()); if (data.empty()) continue; ++nfiles; const auto ranges = strawberry::elf_load_ranges(data.data(), data.size()); std::vector baseline; for (auto& v : variants) { const std::vector use = v.loaded ? ranges : std::vector{}; uint64_t scanned = 0; for (const auto& r : use) scanned += r.size; if (use.empty()) scanned = data.size(); strawberry::YaraScanner::EngineOptions eo; eo.fast_mode = v.fast; std::vector m; const auto t0 = std::chrono::steady_clock::now(); { STRAWBERRY_ZONE("bench.engine_scan"); (void) scanner.scan_memory_ranges(data.data(), data.size(), use, eo, m, 60); } v.secs += std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); v.bytes += scanned; std::vector names; for (const auto& x : m) names.push_back(x.namespace_name + ":" + x.rule_name); std::sort(names.begin(), names.end()); if (!names.empty()) ++v.matched; if (&v == &variants[0]) { baseline = names; } else if (names != baseline) { ++v.diffs; std::cout << "DIFF " << v.name << " " << path << ": full=" << baseline.size() << " " << v.name << "=" << names.size() << "\n"; } } } std::cout << nfiles << " files\n"; for (const auto& v : variants) { std::cout << v.name << ": " << v.secs << " s, " << v.bytes / 1048576.0 << " MiB scanned, " << (v.secs > 0 ? v.bytes / 1048576.0 / v.secs : 0) << " MiB/s, speedup " << (v.secs > 0 ? variants[0].secs / v.secs : 0) << "x, " << v.matched << " files matched, " << v.diffs << " match-set differences vs full\n"; } return 0; } const auto work = std::filesystem::temp_directory_path() / ("sb-scan-bench-" + std::to_string(::getpid())); std::filesystem::create_directories(work); strawberry::GovernorOptions options; options.profile = *named; strawberry::ResourceGovernor governor(options); governor.tick(); strawberry::Config config; config.db_path = (work / "bench.db").string(); config.yara_rules_path = rules; config.rules_cache_dir = (work / "rules-cache").string(); strawberry::DetectionPipeline pipeline; pipeline.set_governor(&governor); const auto t_init = std::chrono::steady_clock::now(); if (!pipeline.initialize(config)) { std::cerr << "pipeline initialize failed (rules: " << rules << ")\n"; return 1; } std::cout << "rules loaded in " << std::chrono::duration_cast(std::chrono::steady_clock::now() - t_init).count() << " ms\n"; uint64_t total_bytes = 0; int64_t total_us = 0; int scanned = 0, failed = 0, matched = 0; for (int r = 0; r < repeat; ++r) { for (const auto& path : files) { const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOCTTY); if (fd < 0) { ++failed; continue; } const auto t0 = std::chrono::steady_clock::now(); const auto res = pipeline.scan_on_demand(fd, config.process_scan_max_bytes); const auto us = std::chrono::duration_cast(std::chrono::steady_clock::now() - t0).count(); ::close(fd); if (!res.ok && !res.error.empty()) { ++failed; std::cout << "FAIL " << path << ": " << res.error << "\n"; continue; } ++scanned; total_bytes += res.size; total_us += us; if (!res.matches.empty()) ++matched; std::cout << us / 1000 << " ms " << res.size / 1048576.0 << " MiB " << res.matches.size() << " match(es) " << path << "\n"; } } pipeline.shutdown(); std::error_code ec; std::filesystem::remove_all(work, ec); const double secs = static_cast(total_us) / 1e6; std::cout << "total: " << scanned << " scanned, " << failed << " failed, " << matched << " with matches, " << total_bytes / 1048576.0 << " MiB in " << secs << " s (" << (secs > 0 ? total_bytes / 1048576.0 / secs : 0) << " MiB/s)\n"; return failed > 0 && scanned == 0 ? 1 : 0; }