OXYGENHUB
◆ push-notes oxygen / src / main.rs
1.7 KiB · Rust · last changed mimalloc as the global allocator (refs #41) 1 month ago source blame history raw
1
//! `oxy`: command-line interface for Oxygen.
2
//!
3
//! Command definitions and dispatch live in `cli`; each command group has
4
//! its own module there.
5
6
mod cli;
7
8
#[cfg(feature = "alloc-stats")]
9
#[global_allocator]
10
static ALLOC: oxy_core::alloc_stats::Counting = oxy_core::alloc_stats::Counting;
11
12
// Many small allocations from every thread (paths, entries, atoms): mimalloc
13
// serves them from per-thread pages without the arena locks of glibc's.
14
#[cfg(not(feature = "alloc-stats"))]
15
#[global_allocator]
16
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
17
18
fn main() {
19
    // Behave like other CLI tools when output is piped into `head`: die
20
    // quietly on SIGPIPE instead of panicking on a failed write.
21
    #[cfg(unix)]
22
    unsafe {
23
        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
24
    }
25
    // With the counting allocator (over glibc's): glibc gives each thread
26
    // that allocates an arena of its own and keeps what is freed there: the
27
    // first commit of the Linux tree, read and hashed across threads, held
28
    // 4.3 GB for 2.7 GB in use. Two arenas hold 3.1 GB, no slower.
29
    // MALLOC_ARENA_MAX still has the last word.
30
    #[cfg(all(target_os = "linux", target_env = "gnu", feature = "alloc-stats"))]
31
    if std::env::var_os("MALLOC_ARENA_MAX").is_none() {
32
        unsafe {
33
            libc::mallopt(libc::M_ARENA_MAX, 2);
34
        }
35
    }
36
    let command = oxy_core::timing::phase("command");
37
    let result = cli::run();
38
    drop(command);
39
    oxy_core::timing::memory_summary();
40
    match result {
41
        Ok(code) => std::process::exit(code),
42
        Err(e) => {
43
            cli::ui::init(false);
44
            eprintln!("{} {e:#}", cli::ui::err("error:"));
45
            std::process::exit(1);
46
        }
47
    }
48
}