Skip to main content

bge_m3_embedding_server/embedder/trt_cache/
fsync.rs

1// Copyright (c) 2026 J. Patrick Fulton
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Post-compile cache durability via directory fsync.
16
17use std::path::Path;
18
19#[cfg(target_os = "linux")]
20pub(crate) fn fsync_cache_dir(dir: &Path) {
21    use std::fs::File;
22    let read_dir = match std::fs::read_dir(dir) {
23        Ok(r) => r,
24        Err(e) => {
25            tracing::warn!(
26                cache_path = %dir.display(),
27                error = %e,
28                "trt cache: could not enumerate directory for fsync; \
29                 engine plan files may not be durable on the next OOM"
30            );
31            return;
32        }
33    };
34
35    let mut synced = 0usize;
36    for entry in read_dir.flatten() {
37        let Ok(file_type) = entry.file_type() else {
38            continue;
39        };
40        if !file_type.is_file() {
41            continue;
42        }
43        let path = entry.path();
44        match File::open(&path).and_then(|f| f.sync_all()) {
45            Ok(()) => synced += 1,
46            Err(e) => {
47                tracing::warn!(
48                    file = %path.display(),
49                    error = %e,
50                    "trt cache: fsync(file) failed; engine plan may not be durable"
51                );
52            }
53        }
54    }
55
56    match File::open(dir).and_then(|d| d.sync_all()) {
57        Ok(()) => {
58            tracing::debug!(
59                cache_path = %dir.display(),
60                files_synced = synced,
61                "trt cache: directory fsynced"
62            );
63        }
64        Err(e) => {
65            tracing::warn!(
66                cache_path = %dir.display(),
67                error = %e,
68                "trt cache: fsync(directory) failed; name → inode mapping \
69                 may not be durable on the next OOM"
70            );
71        }
72    }
73}
74
75/// Non-Linux no-op stub so callers do not need to gate every call site.
76#[cfg(not(target_os = "linux"))]
77pub(crate) fn fsync_cache_dir(_dir: &Path) {}