Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder: delay free data structure to reduce image build time #1298

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions rafs/src/builder/core/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

use anyhow::{bail, Result};
use nydus_utils::{root_tracer, timing_tracer};
use nydus_utils::{lazy_drop, root_tracer, timing_tracer};

use super::node::{ChunkSource, Node, NodeChunk, NodeInfo};
use super::overlay::{Overlay, WhiteoutType};
Expand Down Expand Up @@ -180,8 +180,10 @@ impl Tree {
// Handle the root node.
upper.lock_node().overlay = Overlay::UpperModification;
self.node = upper.node.clone();
self.merge_children(ctx, &upper)?;
lazy_drop(upper);

self.merge_children(ctx, &upper)
Ok(())
}

fn merge_children(&mut self, ctx: &BuildContext, upper: &Tree) -> Result<()> {
Expand Down
4 changes: 3 additions & 1 deletion rafs/src/builder/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs;
use std::fs::DirEntry;

use anyhow::{anyhow, Context, Result};
use nydus_utils::{event_tracer, root_tracer, timing_tracer};
use nydus_utils::{event_tracer, lazy_drop, root_tracer, timing_tracer};

use super::core::blob::Blob;
use super::core::context::{
Expand Down Expand Up @@ -190,6 +190,8 @@ impl Builder for DirectoryBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
4 changes: 3 additions & 1 deletion rafs/src/builder/stargz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use nydus_storage::{RAFS_MAX_CHUNKS_PER_BLOB, RAFS_MAX_CHUNK_SIZE};
use nydus_utils::compact::makedev;
use nydus_utils::compress::{self, compute_compressed_gzip_size};
use nydus_utils::digest::{self, DigestData, RafsDigest};
use nydus_utils::{root_tracer, timing_tracer, try_round_up_4k, ByteSize};
use nydus_utils::{lazy_drop, root_tracer, timing_tracer, try_round_up_4k, ByteSize};
use serde::{Deserialize, Serialize};

use super::core::blob::Blob;
Expand Down Expand Up @@ -897,6 +897,8 @@ impl Builder for StargzBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
Expand Down
4 changes: 3 additions & 1 deletion rafs/src/builder/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use nydus_utils::compact::makedev;
use nydus_utils::compress::zlib_random::{ZranReader, ZRAN_READER_BUF_SIZE};
use nydus_utils::compress::ZlibDecoder;
use nydus_utils::digest::RafsDigest;
use nydus_utils::{div_round_up, root_tracer, timing_tracer, BufReaderInfo, ByteSize};
use nydus_utils::{div_round_up, lazy_drop, root_tracer, timing_tracer, BufReaderInfo, ByteSize};

use super::core::blob::Blob;
use super::core::context::{
Expand Down Expand Up @@ -585,6 +585,8 @@ impl Builder for TarballBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/bin/nydus-image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ use nydus_storage::factory::BlobFactory;
use nydus_storage::meta::{format_blob_features, BatchContextGenerator};
use nydus_storage::{RAFS_DEFAULT_CHUNK_SIZE, RAFS_MAX_CHUNK_SIZE};
use nydus_utils::trace::{EventTracerClass, TimingTracerClass, TraceClass};
use nydus_utils::{compress, digest, event_tracer, register_tracer, root_tracer, timing_tracer};
use nydus_utils::{
compress, digest, event_tracer, lazy_drop, register_tracer, root_tracer, timing_tracer,
};
use serde::{Deserialize, Serialize};

use crate::unpack::{OCIUnpacker, Unpacker};
Expand Down Expand Up @@ -1026,6 +1028,8 @@ impl Command {
"total_build"
)?;

lazy_drop(build_ctx);

// Some operations like listing xattr pairs of certain namespace need the process
// to be privileged. Therefore, trace what euid and egid are.
event_tracer!("euid", "{}", geteuid());
Expand Down
15 changes: 15 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ impl Delayer {
}
}

struct LazyDrop<T> {
v: T,
}

unsafe impl<T> Send for LazyDrop<T> {}

/// Lazy drop of object.
pub fn lazy_drop<T: 'static>(v: T) {
let v = LazyDrop { v };
std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(600));
let _ = v.v;
});
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down