[snapshot]6-CRIU逻辑

对照仓库 ai-dynamo/snapshot 的 agent 实现整理。总览见 [snapshot]0-snapshot介绍,restore 落点见 [snapshot]7-placeholder与恢复流程。

本文只讲 NVIDIA 这条产品路径:CPU 侧用 CRIU,GPU 侧用 CUDA Driver 的 process checkpoint API(agent 通过 cuda-checkpoint-helper 调用;CRIU 上游另有一份 cuda_plugin)。不讨论其它厂商。

先记三句话:

  1. Snapshot 不是「对容器直接 criu dump」。它先把 CUDA 冻到可 dump 的形态,再让 CRIU 拍 CPU / 内存 / 文件描述符。
  2. Agent 不 dlopen 任何 CRIU 插件。插件是 CRIU 进程按 libdir 自己加载的。Go 只写配置、环境变量和 RPC。
  3. 上游 cuda_plugin 和 Snapshot 自研的 snapshot_inet_remap.so 是两回事。chart 故意把 libDir 指到 Snapshot 自己的目录,避免 CRIU 全局插件目录把 CUDA 插件加载进来和 helper 抢活。

1. CRIU 在 Snapshot 里扮演什么

CRIU(Checkpoint/Restore In Userspace)把一个 Linux 进程树冻住,写成一组 image 文件,之后在另一个命名空间里再种回去。它管的是:

  • 地址空间(vma、匿名页、文件映射)
  • 寄存器、信号、rlimit
  • 打开的文件 / pipe / socket(受选项约束)
  • 部分 cgroup、mount、tty

它 不管 CUDA context、显存、UMD 在 GPU 上的对象。那是 NVIDIA 驱动从 CUDA 11.x / 驱动 550+ 起提供的 cuCheckpointProcess*。不先做 GPU checkpoint,CRIU 会在 /proc/{pid}/maps 里撞上 CUDA 预留的巨大空洞或 device 映射,报 Unsupported mapping,dump 失败。

所以 NVIDIA Snapshot 的 capture 是两段式:

源容器进程(已 cudaMalloc / 已加载权重)
        │
        │  ① cuda-checkpoint-helper --action lock
        │  ② cuda-checkpoint-helper --action checkpoint
        │     GPU 状态进 host;映射变成 CRIU 能拍的形态
        ▼
        │  ③ go-criu Dump(RPC → criu swrk)
        │     CPU / 内存 / fd / 部分 mount
        ▼
        │  ④ overlay 可写层 diff(best-effort,失败不否决 dump)
        ▼
     artifact(CRIU images + manifest.yaml + 可选 CUDA job file)

Restore 顺序反过来,但 GPU 仍在 CRIU 之后:

placeholder 容器(sleep infinity,ns 已存在)
        │
        │  agent 把 /snapshot-binaries bind 进该 mount ns
        │  nsrestore 进同一 pid/mnt/net ns
        ▼
        │  ① criu restore(兄弟进程,rstSibling)
        │  ② cuda-checkpoint-helper --action restore / unlock
        ▼
     业务进程从冻结点继续跑

代码入口:

阶段 文件
dump 编排 agent/internal/executor/checkpoint.go → captureCheckpoint
CUDA lock/checkpoint agent/internal/cuda/cuda.go → CheckpointProcessTree
CRIU dump RPC agent/internal/criu/dump.go
restore 编排 agent/internal/executor/nsrestore.go
CRIU restore RPC agent/internal/criu/restore.go
CUDA restore/unlock agent/internal/cuda/cuda.go → RestoreAndUnlockProcessTree

2. Agent 怎么把 CRIU 拉起来(go-criu / swrk)

Agent 不 exec criu dump ... 一整串 CLI。它用 go-criu 的 swrk 模式:

  1. Go 进程 fork/exec criu swrk(service worker),两边用 protobuf RPC 说话。
  2. dump/restore 的选项打成 CriuOpts。
  3. image 目录、work 目录以 FD 传给 CRIU(ImagesDirFd / WorkDirFd),而不是只传路径。Go 的 os.Open 默认 O_CLOEXEC,必须 Fcntl F_SETFD 清掉,否则 CRIU 子进程拿不到这个 FD(agent/internal/criu/util.go 的 openPathForCRIU)。

dump 时 CRIU 二进制默认是 agent 容器里的 /usr/local/sbin/criu(Helm config.criu.binaryPath)。

restore 时 不能 用 manifest 里记下的这条路径:那是 dump 节点上 agent 文件系统的路径,placeholder 里没有。nsrestore 已经跑在 placeholder 的 mount ns 里,CRIU 必须来自注入包:

/tmp/snapshot-binaries/criu

restore.go 的 bundledCRIUPath 写死走 bundle。

rstSibling: true 也是为 swrk 准备的:恢复出来的进程树是 criu/nsrestore 的兄弟,而不是把 placeholder 的 PID 1(sleep)替换掉。sleep 继续占着容器;业务是同 pid ns 里新长出来的那棵树。


3. 编进镜像的到底有什么

agent/Dockerfile 的 criu-builder 做三件事(NVIDIA 原版):

  1. git fetch checkpoint-restore/criu 的 criu-dev,make install-criu install-lib install-cuda_plugin
    → criu 本体 + 上游 CUDA 插件(装到 CRIU 自己的插件目录)。
  2. 用仓库 agent/plugins/inet-remap/snapshot_inet_remap.c 编
    → /usr/local/lib/snapshot/criu-plugins/snapshot_inet_remap.so。
  3. 再 clone NVIDIA cuda-checkpoint,把官方 CLI 拷进 agent 和 /snapshot-binaries。

最终 agent 镜像里和 CRIU 相关的路径:

路径 用途
/usr/local/sbin/criu dump 用的二进制(agent 自己的 mount ns)
/usr/local/lib/snapshot/criu-plugins/snapshot_inet_remap.so Snapshot 专用插件目录
/snapshot-binaries/criu restore 注入包里的 criu
/snapshot-binaries/criu-plugins/ restore 时的 libdir
/usr/local/sbin/cuda-checkpoint NVIDIA 官方 CLI(给人用、给 CRIU cuda_plugin exec、给 --launch-job 包装)
/usr/local/bin/cuda-checkpoint-helper agent dump/restore 实际调用 的小程序

Helm 注释写得很直白:config.criu.libDir 不要指到 CRIU 全局插件目录,否则会把上游 cuda_plugin 加载进来。默认:

config.criu.libDir: "/usr/local/lib/snapshot/criu-plugins"

这个目录里 只有 snapshot_inet_remap.so。上游 CUDA 插件即便装进了镜像,只要 dump/restore 的 criu.conf 不指向它,就不会参与 Snapshot 主路径。


4. Dump:CRIU 看见什么、拍什么

4.1 调用前:CUDA 必须已经是 checkpointed

captureCheckpoint 里写死顺序:有 CUDAHostPIDs 就先 CheckpointProcessTree(每个 PID lock 再 checkpoint),失败则整次 capture 失败,不会继续 CRIU。

helper 调的是 Driver API:

  • cuCheckpointProcessLock
  • cuCheckpointProcessCheckpoint
  • 以及筛 PID 用的 cuCheckpointProcessGetRestoreThreadId(--get-restore-tid)

--get-state 打印小写 running / locked / checkpointed。Agent 认这套小写。官方 CLI cuda-checkpoint --get-state 给人手工查,dump 热路径不解析它。

多卡时源进程应在 cuda-checkpoint --launch-job 下启动,才会有 CUDA_CHECKPOINT_JOB_FILE。helper 把这个路径 setenv 进去;checkpoint 成功后再把 job file 拷进 artifact。单卡可以没有这份文件。

4.2 BuildDumpOptions 里的关键开关

agent/internal/criu/dump.go:

选项 Snapshot 怎么设 含义
Pid 容器进程的 host PID CRIU 跑在 agent 里(hostPID),对着宿主机 pid 号 dump
Root 该 PID 的 rootfs 容器根
LeaveRunning false dump 成功后源进程结束。CUDA 锁过的源也不会再 resume。operator 把源死掉当成 capture 成功的预期结果
External net[{inode}]:extNetNs 不 dump 网络命名空间,restore 时接到新 Pod 的 netns
ExtMnt / SkipMnt 由 BuildMountPolicy 算 哪些 mount 当外部挂载、哪些跳过(proc/sys/dev、maskedPaths 等)
FreezeCgroup 容器在宿主机上的 cgroup 路径 冻整组,避免 dump 窗口里还在跑
GhostLimit 默认 512Mi 已删除但仍打开的文件,小于此阈值打进 image 当 ghost file
TcpEstablished true 保留已建立的 TCP(配合后面 inet-remap)
ShellJob true 容器里常见 session leader
FileLocks / ExtUnixSk / LinkRemap / ExtMasters true 文件锁、外部 unix socket、删除仍打开的文件、外部 bind master

LeaveRunning=false 解释了为什么 dump 成功后源 Pod 会 CONTAINER_EXITED、readiness 失败:不是 bug。

网络命名空间 external 的后果:restore 时进程落进 新 Pod 的 netns 和新 IP。旧 IP 上的 TCP 四元组对不上,所以才需要 inet-remap 插件(§6)。

4.3 RPC 传不了的选项:criu.conf

libdir、allow-uprobes、skip-in-flight 不能走 protobuf,写到 artifact 里的 criu.conf,CriuOpts.ConfigFile 指过去。dump 时 libdir 就是 agent 上的 /usr/local/lib/snapshot/criu-plugins。

4.4 失败时看什么

go-criu 经常把 RPC 失败收成一句 Unable to create the binfmt_misc file system: Operation not permitted。不要拿这句当根因。 agent 会把 dump.log 尾部打进日志字段 dump_log_tail。真正的失败行通常是:

  • Unsupported mapping found ...(CUDA 没冻住)
  • Can't open file ...(open file / ConfigMap 时间戳路径 / emptyDir 上的日志)
  • mount 相关 bind 对不上

Helm 还给工作负载配了 seccomp block-iouring.json:CRIU 不能 checkpoint io_uring,必须在源/恢复 Pod 上挡住。


5. Restore:在 placeholder 的命名空间里跑 CRIU

5.1 谁在 exec criu

不是 agent 主进程在 host 上 criu restore。流程是:

  1. Agent 发现带 nvidia.com/restore-from 的本机 Pod,等到容器 Running、有 IP。
  2. nsmount 把工具 bundle 和 checkpoint 目录 bind 进 placeholder 的 mount namespace。
  3. 在该 ns 里跑 nsrestore(bundle 里的静态 Go 二进制)。
  4. nsrestore 调 ExecuteRestore → go-criu → bundle 里的 criu。

因此 restore 时的「当前根」是 placeholder 的 rootfs(业务镜像 + 惰性入口),CriuOpts.Root 是 "/"。

5.2 继承进来的东西

  • netns:打开 /proc/1/ns/net(placeholder 的 net),AddInheritFd("extNetNs", ...) 对上 dump 时的 extNetNs 标签。
  • stdio pipe:manifest 里记下源容器 stdin/out/err 的 pipe:[inode]。restore 打开 placeholder 的 /proc/1/fd/0..2 作为 inherit-fd,这样恢复进程的日志仍进当前容器的 stdout(kubelet 能 kubectl logs)。
  • cgroup:manageCgroupsMode: soft,并把目标写成当前容器的 cgroup 路径。
  • NetworkLock = SKIP:dump 时 netns 已 external,placeholder 是全新 netns,没有 dump 时的 iptables DROP。restore 再 lock 网络没有意义,还会去 fork 一份 bundle 里没有的 iptables-restore。

5.3 libdir 必须改写

artifact 里的 CRIU 配置记录的是 dump 节点上的 agent 路径,placeholder 中不存在该路径。restore 时会复制配置,并把插件目录改为 bundle 内的 CRIU plugins 目录。

这样 restore 时加载的仍是 inet-remap,不是 CRIU 全局 CUDA 插件。

5.4 CUDA restore 为什么在 CRIU 之后

CRIU 恢复完成后,业务进程已经存在,但 GPU 侧仍处于 checkpointed 状态。nsrestore 会在 CRIU 运行前打开 helper 二进制并保留文件描述符。即使恢复 mount namespace 后 bundle 路径消失,已打开的文件描述符仍可使用。

然后:

  1. 读恢复后的 /proc 进程表;
  2. 把 manifest 里记下的 namespace 内 PID 映射成现在的 observed PID;
  3. RestoreAndUnlockProcessTree:helper --action restore(可带 --device-map 做 GPU UUID 对)再 --action unlock。

同卡同 UUID 时 device-map 为空串,走默认 restore。跨卡才填 GPU-old=GPU-new。

cleanup(关掉 image FD 等)放在 CUDA unlock 之后,缩短「进程已经跑但 GPU 还锁着」的窗口。


6. 两套插件,别混

6.1 Snapshot 自己的:snapshot_inet_remap.so

源码:agent/plugins/inet-remap/snapshot_inet_remap.c。

只挂 CRIU hook CR_PLUGIN_HOOK__UPDATE_INETSK。作用:restore 套接字时,把 dump 下来的 IPv4 地址(及 v4-mapped v6)按一张表改掉。

Agent 侧 ConfigureInetRemap(inet_remap.go):

  • 需要 tcpEstablished=true;
  • 源 Pod IP ≠ 目标 Pod IP(都是 IPv4);
  • 必须已经配置 libDir 或 CRIU_LIBS_DIR,否则插件加载不了;
  • 写临时文件 snapshot-inet-remap-*.map,内容一行 旧IP 新IP;
  • setenv SNAPSHOT_CRIU_INET_REMAP_FILE={该文件}。

插件 init 只在 RESTORE stage 读这个环境变量。没有文件或 IP 没变就当没这插件(返回 -ENOTSUP 让 CRIU 用原地址)。

这解决的是:进程记得自己绑在 10.244.0.85:8000,新 Pod 却是 10.244.0.86。 与 GPU 无关。

6.2 CRIU 上游的:cuda_plugin

源码在 CRIU 树 plugins/cuda/cuda_plugin.c(install-cuda_plugin)。Snapshot 编进去了,主路径故意不加载。

它做什么(若被加载):

Hook 行为
init 若存在 /proc/driver/nvidia/gpus/,且 PATH 里的 cuda-checkpoint 支持 --action,才启用
PAUSE_DEVICES cuda-checkpoint --get-restore-tid / --get-state;若还是 running,则 --action lock
CHECKPOINT_DEVICES 短暂放开 restore 线程,--action checkpoint,再打断
RESUME_DEVICES_LATE restore 时 --action restore + unlock,把进程恢复成 running
fini dump 失败或 leave-running 时把锁过的 PID resume 回去

全部通过 execvp("cuda-checkpoint", ...),state 字符串按 小写 running/locked/checkpointed 解析。

NVIDIA Snapshot 为什么还要 helper,而不靠这份插件?

  • dump 必须在 CRIU 收集 mappings 之前 完成 CUDA checkpoint。Agent 把 lock+checkpoint 放在 ExecuteDump 前面,顺序确定、错误归到 CUDA checkpoint failed,而不是埋进 CRIU log。
  • helper 链 libcuda,能传 --device-map、--job-file;官方 CLI 给 --launch-job 和插件用。
  • 若插件也加载,它看到已经是 checkpointed 会 skip——双重路径时插件是保险。Snapshot 用专用 libDir 等于关掉这层保险,GPU 完全由 helper 负责。

有人在宿主机上不用 Snapshot、直接 criu dump 一个 CUDA 进程时,才会走到这份上游插件(前提是 PATH 有 cuda-checkpoint、节点有 NVIDIA procfs)。


7. Artifact 里和 CRIU 有关的文件

一次成功 capture,容器目录下大致是:

{checkpoints}/artifacts/{content-uid}/containers/{name}/
  criu.conf          # dump 时 libdir 等
  dump.log
  manifest.yaml      # 含 criuDump 选项、extMnt、源 PodIP、CUDA PID/UUID
  pages-*.img, pstree.img, mm-*.img, fdinfo-*.img, ...
  cuda-checkpoint-job   # 仅 --launch-job 多卡
  rootfs-diff.tar / deleted-files.json   # overlay,不是 CRIU 本体

restore 工作目录(Helm 默认 /var/criu-work)另有 restore.log、criu-restore.conf、inet-remap 的 map 文件。这些不进 PVC 上也行:restoreScratchDir 优先用 workDir。


8. 一张图串起来

                    dump(源节点 agent,hostPID)
  ┌─────────────────────────────────────────────────────────┐
  │ helper lock/checkpoint ──► CUDA 进入 checkpointed         │
  │ criu.conf libdir = /usr/local/lib/snapshot/criu-plugins   │
  │ go-criu Dump ──► /usr/local/sbin/criu swrk                │
  │   加载 snapshot_inet_remap.so(dump stage 几乎空转)        │
  │   不加载上游 cuda_plugin(libdir 里没有它)                  │
  │ LeaveRunning=false ──► 源进程退出                          │
  └─────────────────────────────────────────────────────────┘
                              │
                              ▼ artifact
                    restore(目标节点 agent → nsrestore)
  ┌─────────────────────────────────────────────────────────┐
  │ bundle: criu + criu-plugins + helper 挂进 placeholder     │
  │ SNAPSHOT_CRIU_INET_REMAP_FILE = 旧IP 新IP                 │
  │ criu-restore.conf libdir = /tmp/snapshot-binaries/criu-plugins
  │ go-criu Restore ──► bundle/criu                           │
  │   inet-remap 改套接字地址                                  │
  │   rstSibling:业务是 sleep 的兄弟进程                      │
  │ inherit: 新 netns + placeholder stdio pipe                 │
  │ helper restore/unlock(经 dump 前打开的 fd)               │
  └─────────────────────────────────────────────────────────┘

9. 和「自己敲 criu 命令」的差别

裸 CRIU + 上游 cuda_plugin NVIDIA Snapshot
谁冻 GPU CRIU hook 里 exec cuda-checkpoint dump 前 Go exec helper
进程在哪 任意,通常宿主机 pid ns 容器进程,agent hostPID dump;restore 进 placeholder ns
网络 常连 netns 一起 dump netns external,新 Pod 新 IP + inet-remap
插件目录 CRIU 默认 plugins 仅 Snapshot criu-plugins
dump 后源进程 可选 leave-running 必须结束
GPU 跨卡 CLI --device-map(若插件走到 restore) helper --device-map,由 BuildDeviceMap 生成

Snapshot 把 CRIU 当成 CPU 侧引擎,把 CUDA checkpoint 当成 必须先完成的前置条件,再用自研插件补 Pod IP 变化。这就是 NVIDIA 在这个项目里「怎么玩 CRIU」。