# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1734017966 0
#      Thu Dec 12 15:39:26 2024 +0000
# Node ID 390de01879ca0ba7d4e96d4c1fa22199dd545e71
# Parent  567b718ba537985141a51c8c23f3c0ffdee6148e
# EXP-Topic drop-fs-caches
simple-command: add support for dropping fs caches

diff --git a/python-libs/poulpe/benchmarks.py b/python-libs/poulpe/benchmarks.py
--- a/python-libs/poulpe/benchmarks.py
+++ b/python-libs/poulpe/benchmarks.py
@@ -187,6 +187,10 @@
                 self._data["simple-command"]["acceptable-return-codes"] = (
                     new_return_codes
                 )
+            dfsc = variants[selected].get("drop-fs-caches")
+            if dfsc is not None:
+                self._data["simple-command"]["drop-fs-caches"] = dfsc
+
 
     @property
     def all_dimensions(self):
@@ -212,9 +216,15 @@
         cmd.apply_data_env(data_env)
 
         prepare = self.get_var("simple-command.prepare-run")
+        drop_fs_caches = self.get_var("simple-command.drop-fs-caches")
 
         r = self._time_command(
-            bin_env_path, data_env_path, cmd, prepare, debug=debug
+            bin_env_path,
+            data_env_path,
+            cmd,
+            prepare=prepare,
+            drop_fs_caches=drop_fs_caches,
+            debug=debug,
         )
         res = {}
         # we should store more
@@ -227,7 +237,13 @@
         return res
 
     def _time_command(
-        self, bin_env_path, data_env_path, cmd, prepare=None, debug=False
+        self,
+        bin_env_path,
+        data_env_path,
+        cmd,
+        prepare=None,
+        drop_fs_caches=False,
+        debug=False,
     ):
         bin_env_path = os.path.abspath(bin_env_path)
         data_env_path = os.path.abspath(data_env_path)
@@ -257,6 +273,12 @@
                 # If there are non-zero status codes, we will check them in
                 # post-processing.
                 time_cmd.append("--ignore-failure")
+            if drop_fs_caches:
+                drop_invocation = "sync && echo 3 > /proc/sys/vm/drop_caches"
+                if prepare is None:
+                    prepare = drop_invocation
+                else:
+                    prepare = f"{prepare};{drop_invocation}"
 
             if debug:
                 time_cmd.append("--runs")
diff --git a/suites/hg/benchmarks/lib/simple-command.pkl b/suites/hg/benchmarks/lib/simple-command.pkl
--- a/suites/hg/benchmarks/lib/simple-command.pkl
+++ b/suites/hg/benchmarks/lib/simple-command.pkl
@@ -22,6 +22,10 @@
 /// Ignore command failure when running benchmark
 accept_failure: Boolean = false
 
+/// Whether to drop filesystem caches before each run.
+/// This is done at the benchmark level, for all variants
+drop_fs_caches: Boolean = false
+
 ///////////////////////////////////////////////////////////////////////////////
 /// rendering as toml /////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
@@ -31,6 +35,9 @@
         when (prepare_run != null) {
             ["prepare-run"] = prepare_run
         }
+        when (drop_fs_caches) {
+            ["drop-fs-caches"] = drop_fs_caches
+        }
         when (accept_failure) {
             ["accept-failure"] = accept_failure
         }
@@ -103,6 +110,9 @@
     /// return code that should be considered a success
     acceptable_return_codes: Listing<Int>?
 
+    /// Whether to drop filesystem caches before each run.
+    drop_fs_caches: Boolean?
+
     /// additional constraint on when this variant can be used
     constraints: Constraints?
 
@@ -123,6 +133,9 @@
         when (acceptable_return_codes != null) {
             ["acceptable-return-codes"] = acceptable_return_codes
         }
+        when (drop_fs_caches != null) {
+            ["drop-fs-caches"] = drop_fs_caches
+        }
         when (constraints != null) {
             ["constraints"] = constraints.as_v0
         }
diff --git a/suites/hg/benchmarks/lib/variants.pkl b/suites/hg/benchmarks/lib/variants.pkl
--- a/suites/hg/benchmarks/lib/variants.pkl
+++ b/suites/hg/benchmarks/lib/variants.pkl
@@ -371,5 +371,12 @@
             }
         }
     }
+    ["fs-caches"] {
+        default_key = "keep"
+        cases {
+            ["keep"] {drop_fs_caches = false}
+            ["drop"] {drop_fs_caches = true}
+        }
+    }
 }
 
diff --git a/suites/hg/benchmarks/status.pbd b/suites/hg/benchmarks/status.pbd
--- a/suites/hg/benchmarks/status.pbd
+++ b/suites/hg/benchmarks/status.pbd
@@ -105,3 +105,10 @@
 
 [simple-command.variants.dimensions.range.from-1000th-tiprev]
 extend-command = " --rev -1000"
+
+[simple-command.variants.dimensions.fs-caches.keep]
+default = true
+drop-fs-caches = false
+
+[simple-command.variants.dimensions.fs-caches.drop]
+drop-fs-caches = true
diff --git a/suites/hg/benchmarks/status.pkl b/suites/hg/benchmarks/status.pkl
--- a/suites/hg/benchmarks/status.pkl
+++ b/suites/hg/benchmarks/status.pkl
@@ -55,6 +55,6 @@
             ["from-1000th-tiprev"] {extend_command = " --rev -1000"}
         }
     }
-
+    ["fs-caches"] = v.simple_command["fs-caches"]
 }