Datasets:
Download scripts/t2b-bench/memeq.py from DrumsRL/drums-benchmarks: direct link, hf CLI and curl.
- Browser
- Download file 7.07 kB
-
https://huggingface.co/datasets/DrumsRL/drums-benchmarks/resolve/main/scripts/t2b-bench/memeq.py
- Command line
-
hf download hf://datasets/DrumsRL/drums-benchmarks/scripts/t2b-bench/memeq.py
-
curl -L -o memeq.py https://huggingface.co/datasets/DrumsRL/drums-benchmarks/resolve/main/scripts/t2b-bench/memeq.py
7.07 kB
| """memeq.py: is an incremental CRIU dump (pages in a parent chain) the same memory as a full dump of the same stopped tree? | |
| sudo python3 memeq.py SLOTNUM BASE_DIR OUT_DIR | |
| Freezes the env's cgroup (cgroup.freeze, CRIU --freeze-cgroup), dumps it in full (F, no --track-mem: soft-dirty untouched), then | |
| incrementally on top of BASE_DIR (I, --track-mem --prev-images-dir), thaws it, and compares, for every | |
| task, the set of present pages and the sha256 of each page in F against I with its parent chain resolved. Prints one | |
| JSON line; exit 0 only when every page matches. The env's incremental base is consumed (use a throwaway env).""" | |
| import hashlib, json, os, signal, struct, subprocess, sys, time | |
| sys.path.insert(0, "/opt/drums/src/drums-lab/benchmark") | |
| from sharedos_rootfs import launch as L, criu_root as cr | |
| PE_PARENT, PE_LAZY, PE_PRESENT = 1, 2, 4 | |
| PAGE = 4096 | |
| def varint(b, i): | |
| r = s = 0 | |
| while True: | |
| c = b[i]; i += 1 | |
| r |= (c & 0x7f) << s; s += 7 | |
| if c < 0x80: | |
| return r, i | |
| def pb(b): | |
| out, i = {}, 0 | |
| while i < len(b): | |
| k, i = varint(b, i) | |
| f, t = k >> 3, k & 7 | |
| if t == 0: | |
| v, i = varint(b, i) | |
| elif t == 2: | |
| n, i = varint(b, i); v = b[i:i + n]; i += n | |
| elif t == 5: | |
| v = struct.unpack_from("<I", b, i)[0]; i += 4 | |
| elif t == 1: | |
| v = struct.unpack_from("<Q", b, i)[0]; i += 8 | |
| else: | |
| raise ValueError(f"wire type {t}") | |
| out.setdefault(f, v) | |
| return out | |
| def entries(path): | |
| b = open(path, "rb").read() | |
| i = 8 # two magics | |
| msgs = [] | |
| while i < len(b): | |
| n = struct.unpack_from("<I", b, i)[0]; i += 4 | |
| msgs.append(pb(b[i:i + n])); i += n | |
| return msgs | |
| def pages(imgdir, tid, memo): | |
| """{vaddr: sha256} of every present page of task tid, parent chain resolved.""" | |
| key = (imgdir, tid) | |
| if key in memo: | |
| return memo[key] | |
| pm = os.path.join(imgdir, f"pagemap-{tid}.img") | |
| if not os.path.exists(pm): | |
| memo[key] = None | |
| return None | |
| msgs = entries(pm) | |
| pid = msgs[0][1] | |
| data = open(os.path.join(imgdir, f"pages-{pid}.img"), "rb").read() | |
| off, out, parent = 0, {}, None | |
| for e in msgs[1:]: | |
| va, nr = e[1], e.get(5, e[2]) | |
| fl = e.get(4, PE_PRESENT) | |
| if e.get(3): | |
| fl |= PE_PARENT | |
| if 6 in e: | |
| raise SystemExit("compressed blocks: not supported by this checker") | |
| if fl & PE_PRESENT and fl & 8 and off % PAGE: | |
| off += PAGE - off % PAGE # PE_PAYLOAD_ALIGNED | |
| for k in range(nr): | |
| a = va + k * PAGE | |
| if fl & PE_PARENT: | |
| if parent is None: | |
| parent = pages(os.path.join(imgdir, "parent"), tid, memo) | |
| if parent is None: | |
| raise SystemExit(f"task {tid}: in-parent pages but no parent pagemap") | |
| pv = parent.get(a) | |
| out[a] = (pv[0], pv[1], pv[2], pv[3] + 1) if pv else ("MISSING-IN-PARENT", None, 0, 1) | |
| elif fl & PE_PRESENT: | |
| out[a] = (hashlib.sha256(data[off:off + PAGE]).hexdigest(), os.path.join(imgdir, f"pages-{pid}.img"), off, 0); off += PAGE | |
| memo[key] = out | |
| return out | |
| def main(): | |
| slot, base, outd = int(sys.argv[1]), sys.argv[2], sys.argv[3] | |
| up = sys.argv[4] if len(sys.argv) > 4 else None | |
| st = json.load(open(f"/var/lib/drums/state/slot{slot}/slot.json")) | |
| init = st["init_pid"] | |
| mt = open(f"/proc/{init}/mountinfo").read() | |
| criu = json.load(open("/etc/drums/drumsd.json"))["criu"] | |
| cg = open(f"/proc/{init}/cgroup").read().split("::", 1)[1].strip() | |
| pids = [int(x) for x in open(f"/sys/fs/cgroup{cg}/cgroup.procs").read().split()] | |
| os.makedirs(outd, exist_ok=True) | |
| F, I = os.path.join(outd, "full"), os.path.join(outd, "incr") | |
| for d in (F, I): | |
| os.makedirs(d) | |
| def argv(d, extra): | |
| a = list(L.dump_argv(init, d, d + ".log", leave_running=True, file_locks=True, externals=cr.dump_externals(mt))) | |
| a[0] = criu | |
| return a + extra | |
| rel = os.path.relpath(base, I) | |
| t0 = time.time() | |
| fz = f"/sys/fs/cgroup{cg}" | |
| open(f"{fz}/cgroup.freeze", "w").write("1") | |
| for _ in range(200): | |
| if "frozen 1" in open(f"{fz}/cgroup.events").read(): | |
| break | |
| time.sleep(0.01) | |
| try: | |
| rf = subprocess.run(argv(F, ["--freeze-cgroup", fz]), capture_output=True, text=True) | |
| ri = subprocess.run(argv(I, ["--freeze-cgroup", fz, "--track-mem", "--prev-images-dir", rel, "--parent-template-only"] + (["--prev-images-uptime", up] if up else [])), capture_output=True, text=True) | |
| finally: | |
| open(f"{fz}/cgroup.freeze", "w").write("0") | |
| rep = {"slot": slot, "base": base, "pids": len(pids), "full_rc": rf.returncode, "incr_rc": ri.returncode, "stopped_s": round(time.time() - t0, 3)} | |
| if rf.returncode or ri.returncode: | |
| rep["err"] = (rf.stderr + ri.stderr)[-600:] | |
| rep["incr_log"] = [l for l in open(I + ".log", errors="replace") if "Error" in l][-6:] if os.path.exists(I + ".log") else None | |
| rep["full_log"] = [l for l in open(F + ".log", errors="replace") if "Error" in l][-6:] if os.path.exists(F + ".log") else None | |
| print(json.dumps(rep)); sys.exit(2) | |
| memo = {} | |
| tids = sorted(n[8:-4] for n in os.listdir(F) if n.startswith("pagemap-") and n.endswith(".img")) | |
| bad, npages, nparent, nown = [], 0, 0, 0 | |
| def page_bytes(v): | |
| if not v or not v[1]: | |
| return None | |
| with open(v[1], "rb") as fh: | |
| fh.seek(v[2]); return fh.read(PAGE) | |
| diffs_from_parent, diffs_own = 0, 0 | |
| for t in tids: | |
| f, i = pages(F, t, memo), pages(I, t, memo) | |
| npages += len(f) | |
| i = i or {} | |
| diff = sorted(a for a in set(f) | set(i) if (f.get(a) or (None,))[0] != (i.get(a) or (None,))[0]) | |
| if diff: | |
| det = [] | |
| for a in diff[:20]: | |
| fb, ib = page_bytes(f.get(a)), page_bytes(i.get(a)) | |
| nb = sum(1 for x, y in zip(fb, ib) if x != y) if fb and ib else None | |
| src = "absent" if a not in i else ("parent" if i[a][3] else "own") | |
| det.append({"va": hex(a), "in_full": a in f, "incr_source": src, "bytes_differ": nb}) | |
| fp = sum(1 for a in diff if a in i and i[a][3]) | |
| diffs_from_parent += fp; diffs_own += len(diff) - fp | |
| bad.append({"task": t, "pages_full": len(f), "pages_incr": len(i), "differ": len(diff), "differ_from_parent": fp, "detail": det}) | |
| isz = sum(os.path.getsize(os.path.join(I, n)) for n in os.listdir(I) if n.startswith("pages-")) | |
| fsz = sum(os.path.getsize(os.path.join(F, n)) for n in os.listdir(F) if n.startswith("pages-")) | |
| rep.update({"diff_pages_from_parent": diffs_from_parent, "diff_pages_own": diffs_own, "tasks": len(tids), "pages": npages, "full_pages_bytes": fsz, "incr_pages_bytes": isz, "mismatched_tasks": bad, "equal": not bad}) | |
| print(json.dumps(rep)) | |
| sys.exit(0 if not diffs_from_parent else 1) | |
| if __name__ == "__main__": | |
| main() | |